早期, 我们用speechrecognitionengine如下来进行语音识别,但是复杂的词条加上环境噪声,其实成功率是不高的。
speechrecognitionengine recognizer = new speechrecognitionengine(new system.globalization.cultureinfo("zh-cn"));//初始化中文引擎
choices conmmonds = new choices();
conmmonds.add(new string[] { "启动", "停止", "充电" });//添加词条
grammarbuilder gbuilder = new grammarbuilder();
gbuilder.append(conmmonds);
grammar grammar = new dictationgrammar(); //new grammar(gbuilder);
grammar.name = "default dictation";
grammar.enabled = true;
recognizer.loadgrammar(grammar);
recognizer.speechrecognized += new eventhandler<speechrecognizedeventargs>(deal_speechrecongized);
recognizer.setinputtodefaultaudiodevice();//设置默认输入设备
recognizer.initialsilencetimeout = timespan.fromseconds(3);
recognizer.babbletimeout = timespan.fromseconds(2);
recognizer.endsilencetimeout = timespan.fromseconds(1);
recognizer.endsilencetimeoutambiguous = timespan.fromseconds(1.5);
recognizer.recognizeasync(recognizemode.multiple);//开始监控设备输入准备识别
speak(conmmonds + "测试语音");
另外一种方式,microsoft的speech sdk来实现语音的实时解读,然后根据接收到的文本内容进行处理,实现与用户的语音交互。
开发语音识别的需要使用注册azure账号或测试api-key:
1. 准备工作
- 安装sdk或库:根据所选服务,下载并安装对应的c# sdk或nuget包。比如,如果您选择microsoft的azure cognitive services中的语音服务,您就需要安装
microsoft.cognitiveservices.speech
nuget包。
2. 实现语音识别
使用c#代码初始化语音识别客户端,监听麦克风输入,转换为文本。
1using microsoft.cognitiveservices.speech;
2using microsoft.cognitiveservices.speech.audio;
3
4// 初始化speechconfig,使用您的api key和区域
5var config = speechconfig.fromsubscription("your-api-key", "your-region");
6config.speechrecognitionlanguage = "zh-cn"; // 设置语言
7
8// 设置音频配置,使用默认麦克风
9using var audioconfig = audioconfig.fromdefaultmicrophoneinput();
10using var recognizer = new speechrecognizer(config, audioconfig);
11
12console.writeline("请说话...");
13
14// 开始连续语音识别
15var result = await recognizer.recognizeonceasync();
16
17if (result.reason == resultreason.recognizedspeech)
18{
19 console.writeline($"识别到的文本: {result.text}");
20 // 在这里处理识别到的文本,比如调用另一个函数进行反馈处理
21}
22else if (result.reason == resultreason.nomatch)
23{
24 console.writeline("没有识别到有效语音输入。");
25}
26else if (result.reason == resultreason.canceled)
27{
28 var cancellation = cancellationdetails.fromresult(result);
29 console.writeline($"识别被取消: {cancellation.reason}");
30}
3. 实现语音反馈
处理完识别到的文本后,可以使用相同的speech sdk将处理结果转换为语音反馈给用户。
1// 使用text-to-speech转换文本为语音
2var speechsynthesizer = new speechsynthesizer(config, null);
3
4var ssml = $"<speak version='1.0' xml:lang='zh-cn'><voice name='zh-cn-xiaoyineural'><prosody rate='medium'>{result.text}</prosody></voice></speak>";
5var resultsynthesis = await speechsynthesizer.synthesizessmltowavefileasync(ssml, "output.wav");
6
7if (resultsynthesis.reason == resultreason.synthesizingaudiocompleted)
8{
9 console.writeline("语音合成完成。");
10 // 可以进一步处理,比如播放输出的音频文件
11}
12else if (resultsynthesis.reason == resultreason.canceled)
13{
14 var cancellation = cancellationdetails.fromresult(resultsynthesis);
15 console.writeline($"语音合成被取消: {cancellation.reason}");
16}
发表评论