The key is that we pass the BufferedWaveProvider into the constructor of the SavingWaveProvider, and then pass that to waveOut.Init.
Public WaveFormat WaveFormat Īnd here’s how you use it to both play and save audio at the same time (note this is very simplified WPF app with two buttons and no checks that you don’t press Start twice in a row etc). Var read = sourceWaveProvider.Read(buffer, offset, count) ĭispose() // auto-dispose in case users forget Public int Read(byte buffer, int offset, int count) Writer = new WaveFileWriter(wavFilePath, sourceWaveProvider.WaveFormat) This.sourceWaveProvider = sourceWaveProvider Public SavingWaveProvider(IWaveProvider sourceWaveProvider, string wavFilePath) Private readonly IWaveProvider sourceWaveProvider
XAUDIO2CREATE CODE
Here’s the code for SavingWaveProvider: class SavingWaveProvider : IWaveProvider, IDisposable But we also make the whole class Disposable, since BufferedWaveProvider is set up to always return the number of bytes we asked for in Read, so it will never reach the end itself. We’ll dispose the WaveFileWriter if we read 0 bytes from the source wave provider, which should normally indicate we have reached the end of playback. In it’s Read method, it will read from it’s source wave provider (the BufferedWaveProvider) in our case, and write to a WAV file before we pass it on.
We’ll call it SavingWaveProvider and it will implement IWaveProvider. Normally, you’d pass the BufferedWaveProvider directly to the IWavePlayer device (such as WaveOut, but to implement save to WAV, we’ll first wrap it in a new signal chain component that we’ll create for this purpose. You fill it with (PCM) audio as it becomes available, and then in its Read method, it returns the audio, or silence if the buffer is empty. This is actually quite easy to achieve, so long as you think in terms of a “signal chain” (which is something I talk about a lot in my audio courses on Pluralsight).īasically, the usual strategy I recommend for playing audio that you receive over the network or from the microphone is to put it into a BufferedWaveProvider. Quite often I get questions from people who would like to play audio that they are receiving over the network, or recording from the microphone, but also want to save that audio to WAV at the same time.