Search code examples
naudio

NAudio - Record audio to memory, make changes to it, then save to disk


I would to do the following sequence:

  1. Record audio using WaveIn into memory.
  2. Get the content from memory as an Array of IEEE 32-bit samples.
  3. Make some modifications to the samples.
  4. Save back to disk.

I know how to record using WaveIn and how to use WaveFileReader and WaveFileWriter. But there are other classes that are needed here which I don't know how to use.


Solution

    1. Write all the audio you receive with WaveIn to a MemoryStream
    2. When you've finished recording, reset the MemoryStream.Position to 0. Then pass that into a RawSourceWaveStream using the WaveFormat of your WaveIn
    3. Use the ToSampleProvider on the RawSourceWaveStream to go to IEEE 32 bit
    4. Read the audio a block at a time into a float[] from your sample provider.
    5. Perform whatever DSP you need
    6. Write that to a WaveFileWriter

    If you implement your DSP as a custom ISampleProvider you can simplify your pipeline even more, and use WaveFileWriter.CreateWaveFile to automatically pull all the data through from the MemoryStream into your WAV file