代码之家  ›  专栏  ›  技术社区  ›  Aishwarya Shiva

使用NAudio录制音频时,向Google Cloud Speech发送字节数据

  •  0
  • Aishwarya Shiva  · 技术社区  · 7 年前

    我正在创建一个简单的WinForms语音到文本应用程序。这个应用程序使用naudioapi监听麦克风,并将音频发送到googlecloudspeechapi。当googlecloudspeechapi用文本响应时,我会将文本附加到文本框中。参见以下代码:

    async private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        await Task.Factory.StartNew(()=> { WriteToTextBox(e.Buffer); }, TaskCreationOptions.LongRunning);
    }
    void WriteToTextBox(byte[] bufferData)
    {
        var speech = SpeechClient.Create();
        var response = speech.Recognize(new RecognitionConfig()
        {
            Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
            SampleRateHertz = 16000,
            LanguageCode = "en",
        }, RecognitionAudio.FromBytes(bufferData));
    
        foreach (var result in response.Results)
        {
            foreach (var alternative in result.Alternatives)
            {
                textBox1.Text = textBox1.Text + " " + alternative.Transcript;
            }
        }
    }
    

    一切都很好除了 response.Results

    var response = speech.Recognize(new RecognitionConfig()
                {
                    Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                    SampleRateHertz = 16000,
                    LanguageCode = "en",
                }, RecognitionAudio.FromFile("audio.raw"));
    

    它工作得很好。

    0 回复  |  直到 7 年前