【发布时间】:2016-03-12 22:34:47
【问题描述】:
我正在开发一个语音识别系统来与我的电脑交谈。现在我将计算机音频输出设置为环绕声系统。这给识别系统带来了问题。例如,当我说“测试”以查看它是否在线时,系统会响应“测试完成”。麦克风听到“测试完成”并进入说测试完成的无限循环。我的问题是,有没有办法让程序在说话时停止收听,然后在它说完后再开始收听?我在想也许以某种方式确保它只响应我的声音。我愿意接受任何建议。
我的代码如下。
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;
}
}
}
【问题讨论】:
-
我不熟悉语音命名空间,但查看文档,
SpeechSynthesizer有一个 SpeakCompleted 事件,在发言完成时引发。您可以将麦克风静音,并连接一个监听器,在事件触发时再次取消静音。
标签: c# speech-recognition