forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechRecognition.cs
189 lines (152 loc) · 5.49 KB
/
SpeechRecognition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using NewLife.IO;
using NewLife.Log;
using NewLife.Model;
using NewLife.Reflection;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace NewLife.Windows
{
/// <summary>语音识别</summary>
public class SpeechRecognition : DisposeBase
{
#region 属性
private ISpeech _speech;
private IDictionary<String, Action> _dic;
/// <summary>系统名称。用于引导前缀</summary>
public String Name { get; set; } = "丁丁";
/// <summary>最后一次进入引导前缀的时间。</summary>
private DateTime _Tip;
/// <summary>是否可用</summary>
public Boolean Enable { get { return _speech != null; } }
#endregion
#region 构造
private SpeechRecognition()
{
_dic = new Dictionary<String, Action>();
}
/// <summary>销毁</summary>
/// <param name="disposing"></param>
protected override void OnDispose(Boolean disposing)
{
base.OnDispose(disposing);
_speech.TryDispose();
}
#endregion
#region 静态
/// <summary>当前实例</summary>
public static SpeechRecognition Current { get; } = new SpeechRecognition();
/// <summary>获取已注册的所有键值</summary>
/// <returns></returns>
public String[] GetAllKeys()
{
return _dic.Keys.ToArray();
}
#endregion
#region 方法
Boolean Init()
{
if (_speech != null) return true;
_speech = ObjectContainer.Current.Resolve<ISpeech>();
if (_speech != null) return true;
try
{
Assembly.Load("System.Speech, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
var code = Assembly.GetExecutingAssembly().GetFileResource("MySpeech.cs").ToStr();
var sc = ScriptEngine.Create(code, false);
sc.Compile();
_speech = sc.Type.CreateInstance() as ISpeech;
if (_speech == null) return false;
if (!_speech.Init()) return false;
_speech.SpeechRecognized += _rg_SpeechRecognized;
return true;
}
catch (Exception ex)
{
XTrace.WriteException(ex);
_speech.TryDispose();
return false;
}
}
/// <summary>注册要语音识别的关键字到委托</summary>
/// <param name="text"></param>
/// <param name="callback"></param>
public SpeechRecognition Register(String text, Action callback)
{
var flag = _dic.ContainsKey(text);
if (callback != null)
{
_dic[text] = callback;
if (!flag) Change();
}
else if (flag)
_dic.Remove(text);
return this;
}
void Change()
{
if (_speech == null) return;
lock (this)
{
if (!Init()) return;
var list = new List<String>
{
Name
};
list.AddRange(_dic.Keys);
_speech.SetChoices(list);
}
}
//void _rg_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
void _rg_SpeechRecognized(Object sender, RecognitionEventArgs e)
{
var conf = e.Confidence;
if (conf < 0.5) return;
var txt = e.Text;
// 语音识别前,必须先识别前缀名称,然后几秒内识别关键字
if (_Tip.AddSeconds(3) < DateTime.Now)
{
// 此时只识别前缀
if (txt != Name) return;
XTrace.WriteLine("语音识别:{0} {1}", txt, conf);
// 现在可以开始识别关键字啦
_Tip = DateTime.Now;
}
else
{
XTrace.WriteLine("语音识别:{0} {1}", txt, conf);
Action func = null;
if (_dic.TryGetValue(txt, out func)) func();
}
}
#endregion
}
/// <summary>语音接口</summary>
public interface ISpeech
{
/// <summary>语音识别事件</summary>
event EventHandler<RecognitionEventArgs> SpeechRecognized;
/// <summary>初始化</summary>
/// <returns></returns>
Boolean Init();
/// <summary>设置识别短语</summary>
/// <param name="phrases"></param>
void SetChoices(IEnumerable<String> phrases);
}
/// <summary>识别事件参数</summary>
public class RecognitionEventArgs : EventArgs
{
/// <summary>获取识别器分配的值,此值表示与给定输入匹配的可能性</summary>
public Single Confidence { get; }
/// <summary>获取语音识别器从识别的输入生成的规范化文本</summary>
public String Text { get; }
/// <summary>实例化</summary>
/// <param name="conf"></param>
/// <param name="text"></param>
public RecognitionEventArgs(Single conf, String text)
{
Confidence = conf;
Text = text;
}
}
}