-1

我有一个程序,当用户说"Start"or"Stop"时,程序会在屏幕上显示一个骨架。我使用与Shape Game相同的代码,它在那里工作得很好,但在我的代码上却不行。我不知道代码的哪一部分不起作用,因为这是我第一次进行语音识别编程。感谢您的帮助(对不起,如果我的代码很乱)

识别语音

public class SpeechRecognizer : IDisposable
{
    private KinectAudioSource kinectAudioSource;
    private struct WhatSaid
    {
        public Verbs Verb;
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.Stop();

            if (this.sre != null)
            {
                // NOTE: The SpeechRecognitionEngine can take a long time to dispose
                // so we will dispose it on a background thread
                ThreadPool.QueueUserWorkItem(
                    delegate(object state)
                    {
                        IDisposable toDispose = state as IDisposable;
                        if (toDispose != null)
                        {
                            toDispose.Dispose();
                        }
                    },
                        this.sre);
                this.sre = null;
            }

            this.isDisposed = true;
        }
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    public EchoCancellationMode EchoCancellationMode
    {
        get
        {
            this.CheckDisposed();

            return this.kinectAudioSource.EchoCancellationMode;
        }

        set
        {
            this.CheckDisposed();

            this.kinectAudioSource.EchoCancellationMode = value;
        }
    }

    public static SpeechRecognizer Create()
    {
        SpeechRecognizer recognizer = null;

        try
        {
            recognizer = new SpeechRecognizer();
        }
        catch (Exception)
        {
            // speech prereq isn't installed. a null recognizer will be handled properly by the app.
        }

        return recognizer;
    }

    private void CheckDisposed()
    {
        if (this.isDisposed)
        {
            throw new ObjectDisposedException("SpeechRecognizer");
        }
    }

    public void Stop()
    {
        this.CheckDisposed();

        if (this.sre != null)
        {
            this.kinectAudioSource.Stop();
            this.sre.RecognizeAsyncCancel();
            this.sre.RecognizeAsyncStop();

            this.sre.SpeechRecognized -= this.SreSpeechRecognized;
            this.sre.SpeechHypothesized -= this.SreSpeechHypothesized;
            this.sre.SpeechRecognitionRejected -= this.SreSpeechRecognitionRejected;
        }
    }

    public void Start(KinectAudioSource kinectSource)
    {
        this.CheckDisposed();

        this.kinectAudioSource = kinectSource;
        this.kinectAudioSource.AutomaticGainControlEnabled = false;
        this.kinectAudioSource.BeamAngleMode = BeamAngleMode.Adaptive;
        var kinectStream = this.kinectAudioSource.Start();
        this.sre.SetInputToAudioStream(
            kinectStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
        this.sre.RecognizeAsync(RecognizeMode.Multiple);
    }

    public enum Verbs
    {
        None = 0,
        Start,
        Stop,
        Resume,
        Pause
    }
    private bool isDisposed;
    private readonly Dictionary<string, WhatSaid> speechCommands = new Dictionary<string, WhatSaid>
    {
        { "Start", new WhatSaid { Verb = Verbs.Start } },
        { "Stop", new WhatSaid { Verb = Verbs.Stop } },
        { "Resume", new WhatSaid { Verb = Verbs.Resume } },
        { "Pause", new WhatSaid { Verb = Verbs.Pause } },
    };
    private SpeechRecognitionEngine sre;

    private static RecognizerInfo GetKinectRecognizer()
    {
        Func<RecognizerInfo, bool> matchingFunc = r =>
        {
            string value;
            r.AdditionalInfo.TryGetValue("Kinect", out value);
            return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
        };
        return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
    }

    private SpeechRecognizer()
    {
        RecognizerInfo ri = GetKinectRecognizer();
        this.sre = new SpeechRecognitionEngine(ri);
        this.LoadGrammar(this.sre);
    }

    private void LoadGrammar(SpeechRecognitionEngine speechRecognitionEngine)
    {
        // Build a simple grammar of shapes, colors, and some simple program control
        var single = new Choices();
        foreach (var phrase in this.speechCommands)
        {
            single.Add(phrase.Key);
        }

        var objectChoices = new Choices();
        objectChoices.Add(single);

        var actionGrammar = new GrammarBuilder();
        actionGrammar.AppendWildcard();
        actionGrammar.Append(objectChoices);

        var allChoices = new Choices();
        allChoices.Add(actionGrammar);
        allChoices.Add(single);

        // This is needed to ensure that it will work on machines with any culture, not just en-us.
        var gb = new GrammarBuilder { Culture = speechRecognitionEngine.RecognizerInfo.Culture };
        gb.Append(allChoices);

        var g = new Grammar(gb);
        speechRecognitionEngine.LoadGrammar(g);
        speechRecognitionEngine.SpeechRecognized += this.SreSpeechRecognized;
        speechRecognitionEngine.SpeechHypothesized += this.SreSpeechHypothesized;
        speechRecognitionEngine.SpeechRecognitionRejected += this.SreSpeechRecognitionRejected;
    }

    private void SreSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
    {
        var said = new SaidSomethingEventArgs { Verb = Verbs.None, Matched = "?" };

        this.SetLabel("Word not Recognized.... Try 'Start', 'Stop', 'Pause' or 'Resume'");

        if (this.SaidSomething != null)
        {
            this.SaidSomething(new object(), said);
        }
    }

    private void SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
    {
        this.SetLabel("I think you said: " + e.Result.Text);
    }

    public event EventHandler<SaidSomethingEventArgs> SaidSomething;

    private void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        this.SetLabel("\rSpeech Recognized: \t" + e.Result.Text);

        if ((this.SaidSomething == null) || (e.Result.Confidence < 0.3))
        {
            return;
        }

        var said = new SaidSomethingEventArgs { Verb = 0, Phrase = e.Result.Text };

        foreach (var phrase in this.speechCommands)
        {
            if (e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Pause))
            {
                //pause = true;
                break;
            }

            else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Resume)))
            {
                //resume = true;
                break;
            }

            else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Start)))
            {
                //start = true;
                break;
            }

            else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Stop)))
            {
                //stop = true;
                break;
            }
        }

        // Look for a match in the order of the lists below, first match wins.
        List<Dictionary<string, WhatSaid>> allDicts = new List<Dictionary<string, WhatSaid>> { this.speechCommands };

        bool found = false;
        for (int i = 0; i < allDicts.Count && !found; ++i)
        {
            foreach (var phrase in allDicts[i])
            {
                if (e.Result.Text.Contains(phrase.Key))
                {
                    found = true;
                    break;
                }
            }
        }

        if (!found)
        {
            return;
        }
    }

    public class SaidSomethingEventArgs : EventArgs
    {
        public Verbs Verb { get; set; }

        public string Phrase { get; set; }

        public string Matched { get; set; }
    }

    public event Action<string> SetLabel = delegate { };
}

在我的代码中

private void RecognizeSaidSomething(object sender, SpeechRecognizer.SpeechRecognizer.SaidSomethingEventArgs e)
    {
        FlyingText.FlyingText.NewFlyingText(this.skeleton.Width / 30, new Point(this.skeleton.Width / 2, this.skeleton.Height / 2), e.Matched);
        switch (e.Verb)
        {
            case SpeechRecognizer.SpeechRecognizer.Verbs.Pause:
                pause = true;
                break;
            case SpeechRecognizer.SpeechRecognizer.Verbs.Resume:
                resume = true;
                break;
            case SpeechRecognizer.SpeechRecognizer.Verbs.Start:
                start = true;
                break;
            case SpeechRecognizer.SpeechRecognizer.Verbs.Stop:
                stop = true;
                break;
        }
    }
4

1 回答 1

1

看起来你从来没有RecognizeSaidSomething()SreSpeechRecognized(). 您创建事件参数:

var said = new SaidSomethingEventArgs { Verb = 0, Phrase = e.Result.Text };

但它似乎没有你用它做任何事情。

下面的foreach循环似乎没有任何用途,您测试该短语然后跳出循环。你没有在我可以看到的那个循环中设置任何变量或调用任何函数。

然后有一个for循环似乎做了类似于foreach循环的事情(只是以不同的方式)。它搜索与已识别短语的匹配项,但对找到的内容不做任何事情。它只是返回。

我想在SreSpeechRecognized()事件处理程序的某个地方你想调用RecognizeSaidSomething()并将它传递给SaidSomethingEventArgs.

于 2012-05-08T14:09:31.630 回答