使用实时流进行语音识别

Joh*_*ler 9 c# windows audio speech-recognition cscore

首先,为了阐明我的目标:我使用CSCore库并使用WasapiLoopbackCapture该类捕获背景音频,我打算将其用作System.Speech.Recognition识别引擎的实时输入.该类将数据输出到.WAV文件或Stream.然后我尝试这样做:

    private void startButton_Click(object sender, EventArgs e)
    {
        _recognitionEngine.UnloadAllGrammars();
        _recognitionEngine.LoadGrammar(new DictationGrammar());

        LoadTargetDevice();
        StartStreamCapture(); // Here I am starting the capture to _stream (MemoryStream type)

        _stream.Position = 0; // Without setting this, I get a stream format exception.

        _recognitionEngine.SetInputToWaveStream(_stream);
        _recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
    }
Run Code Online (Sandbox Code Playgroud)

结果是我没有得到异常,但我也没有得到SpeechRecognizedSpeechDetected事件被触发.我怀疑这是因为System.Speech.Recognition程序集不支持实时流.我在网上搜索,有人报告实施自定义Stream类型作为解决方法,但我无法按照帖子上的说明进行操作,这些说明不明确(请参阅Dexter Morgan的回复).

我知道这个问题最好通过使用不同的库或替代方法来解决,但我想知道如何专门做这个临时实现,主要是出于知识目的.

谢谢!

Web*_*ter 5

@Justcarty感谢您的澄清,这里是我的解释为什么OP的代码不能工作以及需要做什么才能使其工作.

在语音识别和综合的C#中,您可能会混淆我们有两个Speech DLL的文档.1.Microsoft
Speech DLL(Microsoft.speech.dll)2.系统语音DLL(System.Speech.Dll)

System.speech dll是Windows操作系统的一部分.这两个库在API几乎相同但不完全相同的意义上是相似的.所以,如果你在网上进行语音搜索的例子,从代码片段你得到你可能不知道他们是否解释到System.SpeechMicrosoft.Speech.

因此,对于向C#应用程序添加语音,您需要使用Microsoft.Speech library,而不是System.Speech library.

下面总结了一些主要差异

|-------------------------|---------------------|
|  Microsoft.Speech.dll    | System.Speech.dll  |
|-------------------------|---------------------|
|Must install separately  |                     |
|                         | Part of the OS      |
|                         |  (Windows Vista+)   |
|-------------------------|---------------------|
|Must construct Grammars  | Uses Grammars or    |
|                           free dictation      |
| ------------------------|--------------------|
Run Code Online (Sandbox Code Playgroud)

有关更多信息阅读以下文章,它解释了正确的实施方式