我正在创建一个简单的WinForms语音到文本应用程序。此应用程序使用NAudio API侦听麦克风,并将音频发送到Google Cloud Speech API。当Google Cloud Speech API使用文本进行响应时,我会将该文本附加到TextBox。请参阅以下代码:
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
总是为空的东西外,一切正常。但是,当我将此缓冲区数据保存到文件,然后使用以下代码调用Google Cloud Speech API时:
var response = speech.Recognize(new RecognitionConfig()
{
Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en",
}, RecognitionAudio.FromFile("audio.raw"));
工作正常。