开发者

Audio repeater with NAudio

I'm implementing a program that reads an audio stream from an input device and sends it to an output device using NAudio. To do that, I get the data from the input stream using WaveIn and its DataAvailable event. To write the data, I am about to use WaveOut, which also means I need to use some implementation of IWaveProvider. Implementing that for my needs seems quite straightforward using a queue. Except for one thing: what should happen when the queue is empty and the Read() method is called? I didn't find anything about this i开发者_高级运维n the documentation. Options I think I have:

  1. The method should block, until some data are available.
  2. The method immediately returns 0, indicating no data are currently available.
  3. Fill the buffer with zeroes.

I was thinking about using option 1 or 2, but then I found BufferedWaveProvider (and WaveInProvider, that uses BufferedWaveProvider internally) that uses option 3. From that, I inferred that option 3 is the preferred one.

The question is, did I infer that right and should I use option 3? What would happen if I used options 1 or 2? Would that even work?


The Read method is called when the soundcard is in need of more data to play. Blocking is not usually a good option because you might be on a thread from within the driver itself (as with ASIO or WaveOut with function callbacks), or you might be on the GUI thread (as with WaveOut with windows message callbacks).

Returning 0 from the Read method means "this is the end of the audio data", so returning 0 would result in playback stopping.

So option three is the best choice to not block on threads you shouldn't be blocking on and to allow playback to continue.

If the consumer of your Read method was for example a WaveFileWriter, option 1 would be fine, (and option 2 might be depending on how you determine when to stop writing to the WAV file).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜