语音识别反馈回路
Speech Recognition feedback loop
我正在开发一个语音识别系统来与我的电脑对话。现在我将我的计算机音频输出设置为环绕声系统。这给识别系统带来了问题。例如,当我说 "test" 看它是否在线时,系统响应 "test complete"。麦克风听到 "test complete" 并进入无限循环说测试完成。我的问题是,有没有办法让程序在说话时停止收听,然后在说完后再次开始收听?我在想也许可以通过某种方式确保它只响应我的声音。我愿意接受任何建议。
我的代码如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SpeechRecog
{
public partial class Form1 : Form
{
private Choices onoff = new Choices();
private Choices recChoices = new Choices();
SpeechRecognitionEngine RE = new SpeechRecognitionEngine();
SpeechSynthesizer ss = new SpeechSynthesizer();
SpeechRecognitionEngine LRE = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
ss.SelectVoiceByHints(VoiceGender.Male);
}
private void btnEnable_Click(object sender, EventArgs e)
{
RE.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Choices commands = new Choices();
commands.Add(new string[] { "Say Hello", "Test","What's my name", "yahoo", "Thank you", "Hey", "Facebook", "Music", "Lock", "Time"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(commands);
Grammar Grammar = new Grammar(gb);
RE.LoadGrammarAsync(Grammar);
RE.SetInputToDefaultAudioDevice();
RE.SpeechRecognized += RE_SpeechRecognized;
}
private void LRE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text.Equals("Hey")) ;
{
RE.RecognizeAsync(RecognizeMode.Multiple);
} }
public string time()
{ DateTime n = DateTime.Now;
string o = n.GetDateTimeFormats('t')[0];
return o;
}
private void RE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
switch (e.Result.Text)
{
case "Test":
ss.SpeakAsync("Test Complete");
break;
case "Say Hello":
case "Introduce yourself":
ss.SpeakAsync("My name is Friday. I was designed to simplify daily life. How can I assist you today?");
break;
case "What's my name":
ss.SpeakAsync("Arno");
break;
case "yahoo":
System.Diagnostics.Process.Start("http://www.yahoo.com");
break;
case "Facebook":
System.Diagnostics.Process.Start("http://www.Facebook.com");
break;
case "Music":
System.Diagnostics.Process.Start("iTunes.exe");
break;
case "Lock":
System.Diagnostics.Process.Start("Rundll32.exe", "User32.dll,LockWorkStation");
break;
case "Thank You":
break;
case "Time":
ss.Speak(time());
break;
}
}
private void btnDisable_Click(object sender, EventArgs e)
{
RE.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}
您应该使用 RecognizeAsyncCancel()
或 RecognizeAsyncStop()
private void RE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
RE.recognizer.RecognizeAsyncStop();
MSDN:
To stop an asynchronous recognition
operation, use the RecognizeAsyncCancel() or RecognizeAsyncStop()
methods. You can pause a running SpeechRecognitionEngine instance to
update its configuration or to load and unload grammars using one of
the RequestRecognizerUpdate() methods. The SpeechRecognitionEngine can
perform an additional mode of recognition (called emulation) during
which it accepts text, rather than speech, as input. Emulated
recognition can be useful for debugging grammars. The speech
recognizer raises the SpeechDetected, SpeechHypothesized,
SpeechRecognitionRejected, and SpeechRecognized events as if the
recognition operation is not emulated. To initiate emulated
recognition, call one of the EmulateRecognize() or
EmulateRecognizeAsync() methods and pass in text or an array of words
for which you want to perform emulated recognition.
我正在开发一个语音识别系统来与我的电脑对话。现在我将我的计算机音频输出设置为环绕声系统。这给识别系统带来了问题。例如,当我说 "test" 看它是否在线时,系统响应 "test complete"。麦克风听到 "test complete" 并进入无限循环说测试完成。我的问题是,有没有办法让程序在说话时停止收听,然后在说完后再次开始收听?我在想也许可以通过某种方式确保它只响应我的声音。我愿意接受任何建议。
我的代码如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SpeechRecog
{
public partial class Form1 : Form
{
private Choices onoff = new Choices();
private Choices recChoices = new Choices();
SpeechRecognitionEngine RE = new SpeechRecognitionEngine();
SpeechSynthesizer ss = new SpeechSynthesizer();
SpeechRecognitionEngine LRE = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
ss.SelectVoiceByHints(VoiceGender.Male);
}
private void btnEnable_Click(object sender, EventArgs e)
{
RE.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Choices commands = new Choices();
commands.Add(new string[] { "Say Hello", "Test","What's my name", "yahoo", "Thank you", "Hey", "Facebook", "Music", "Lock", "Time"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(commands);
Grammar Grammar = new Grammar(gb);
RE.LoadGrammarAsync(Grammar);
RE.SetInputToDefaultAudioDevice();
RE.SpeechRecognized += RE_SpeechRecognized;
}
private void LRE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text.Equals("Hey")) ;
{
RE.RecognizeAsync(RecognizeMode.Multiple);
} }
public string time()
{ DateTime n = DateTime.Now;
string o = n.GetDateTimeFormats('t')[0];
return o;
}
private void RE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
switch (e.Result.Text)
{
case "Test":
ss.SpeakAsync("Test Complete");
break;
case "Say Hello":
case "Introduce yourself":
ss.SpeakAsync("My name is Friday. I was designed to simplify daily life. How can I assist you today?");
break;
case "What's my name":
ss.SpeakAsync("Arno");
break;
case "yahoo":
System.Diagnostics.Process.Start("http://www.yahoo.com");
break;
case "Facebook":
System.Diagnostics.Process.Start("http://www.Facebook.com");
break;
case "Music":
System.Diagnostics.Process.Start("iTunes.exe");
break;
case "Lock":
System.Diagnostics.Process.Start("Rundll32.exe", "User32.dll,LockWorkStation");
break;
case "Thank You":
break;
case "Time":
ss.Speak(time());
break;
}
}
private void btnDisable_Click(object sender, EventArgs e)
{
RE.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}
您应该使用 RecognizeAsyncCancel()
或 RecognizeAsyncStop()
private void RE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
RE.recognizer.RecognizeAsyncStop();
MSDN:
To stop an asynchronous recognition operation, use the RecognizeAsyncCancel() or RecognizeAsyncStop() methods. You can pause a running SpeechRecognitionEngine instance to update its configuration or to load and unload grammars using one of the RequestRecognizerUpdate() methods. The SpeechRecognitionEngine can perform an additional mode of recognition (called emulation) during which it accepts text, rather than speech, as input. Emulated recognition can be useful for debugging grammars. The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated. To initiate emulated recognition, call one of the EmulateRecognize() or EmulateRecognizeAsync() methods and pass in text or an array of words for which you want to perform emulated recognition.