NAudio管道WaveIn或WaveProvider到Stream

时间:2014-04-22 22:14:51

标签: c# naudio

我想使用NAudio的WaveIn / WaveProvider / WavOut功能操作Microhpone(或其他来源)

WaveIn recorder = new WaveIn();
recorder.DataAvailable += RecorderOnDataAvailable;
BufferedWaveProvider buffer = new BufferedWaveProvider(recorder.WaveFormat);

但最后我需要将缓冲区转换为流,因为SpeechRecognitionEngine适用于Stream。

engine.SetInputToAudioStream(stream, format);

如何将WavProvider传输到Stream?

1 个答案:

答案 0 :(得分:3)

好的,我找到了一个有效的解决方案:

1)构建WaveIn

  waveIn = new WaveInEvent();
  waveIn.DeviceNumber = 0;
  waveIn.WaveFormat = new WaveFormat(16000, 2);
  waveIn.DataAvailable += waveIn_DataAvailable;

2)构建PipeStream

  buffer = new PipeStream();
  engine.SetInputToAudioStream(buffer, waveIn.WaveFormat);

3)将WaveIn复制到缓冲区

void waveIn_DataAvailable(object sender, WaveInEventArgs e) {
  buffer.Write(e.Buffer, 0, e.BytesRecorded);
}

它工作正常,无需担心。 PipeStream已经处理了缓冲区大小。