语音识别反馈循环

时间:2016-03-12 22:34:47

标签: c# speech-recognition

我正在使用语音识别系统与我的电脑通话。现在我将计算机音频输出设置为环绕声系统。这给识别系统带来了问题。例如,当我说“测试”以查看其是否在线时,系统以“测试完成”响应。麦克风听到“测试完成”并进入无限循环说测试完成。我的问题是,是否有任何方式阻止程序在讲话时听,然后在讲完后再开始听?我想的可能是确保它只响应我的声音。我对任何建议持开放态度。

我的代码如下。

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;
    }
}

}

1 个答案:

答案 0 :(得分:2)

您应该使用RecognizeAsyncCancel()RecognizeAsyncStop()

private void RE_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    RE.recognizer.RecognizeAsyncStop();

MSDN

  

停止异步识别   操作,使用RecognizeAsyncCancel()或RecognizeAsyncStop()   方法。您可以暂停正在运行的SpeechRecognitionEngine实例   更新其配置或使用以下方法之一加载和卸载语法   RequestRecognizerUpdate()方法。 SpeechRecognitionEngine可以   在执行期间执行额外的识别模式(称为仿真)   它接受文本而不是语音作为输入。模拟   识别对于调试语法很有用。这个演讲   识别器引发SpeechDetected,SpeechHypothesized,   SpeechRecognitionRejected,和SpeechRecognized事件一样   识别操作未被模拟。启动模拟   识别,调用EmulateRecognize()或其中一个   EmulateRecognizeAsync()方法并传入文本或单词数组   您要为其执行模拟识别。