C#将设备音频录制为流,然后通过语音识别进行处理

时间:2017-07-16 07:48:06

标签: c# speech-recognition naudio

我正在尝试使用NAudio.WasapiLoopbackCapture捕获计算机的音频,然后将其保存到Wavstream,然后将其传输到SpeechRecognitionEngine中。我可以轻松地将环回音频保存到wav / mp3文件中。我可以使用SRE轻松测试简单的语音识别。我可以使用以下方法将它们简单地链接在一起:

using (WasapiCapture capture = new WasapiLoopbackCapture())
{
    audioStream = new MemoryStream();
    //create a wavewriter to write the data to
    //using (var w = new WaveFileWriter("C:\\users\\matth\\documents\\dump.wav", capture.WaveFormat))
    using (var w = new WaveFileWriter(audioStream, capture.WaveFormat))
    using (var engine = new SpeechRecognitionEngine())
    {
        //setup an eventhandler to receive the recorded data
        capture.DataAvailable += (s, e) =>
        {
            //save the recorded audio
            w.Write(e.Buffer, 0, e.BytesRecorded);
        };
        engine.SpeechRecognized += (s, e) =>
        {
            Console.WriteLine(e.Result.Text);
        };
        engine.SpeechRecognitionRejected += (s, e) =>
        {
            Console.WriteLine(e.Result.Text);
        };

        //start recording
        capture.StartRecording();
        audioStream.Position = 0;

        var g = new DictationGrammar();
        engine.LoadGrammar(g);

        engine.SetInputToWaveStream(audioStream);
        engine.RecognizeAsync(RecognizeMode.Multiple);

        Console.ReadKey();

        //stop recording
        capture.StopRecording();
    }
}

0 个答案:

没有答案