How to save an audio capture in a wav file using ASIO and Naudio?

1.3k views Asked by At

So far, I wrote that :

private AsioOut ASIODriver;
private BufferedWaveProvider buffer;

String[] drivernames = AsioOut.GetDriverNames();
ASIODriver = new AsioOut(drivernames[1]);
buffer = new BufferedWaveProvider(new WaveFormat(sampleRate, 16, 2));
ASIODriver.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(ASIODriver_AudioAvailable);
ASIODriver.InitRecordAndPlayback(buffer, 2, sampleRate);
ASIODriver.Play();

private void ASIODriver_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{ 
    byte[] buf = new byte[e.SamplesPerBuffer * 4];
    for (int i = 0; i < e.InputBuffers.Length; i++)
    {
        Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer * 4);
        Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer * 4);

        if (recorderOn && i == 1)
        {
            recorder.addInputToStream(buf);
        }
    }
}

This part is used to capture and playback the sound from a guitar with ASIO. No problems so far.

public void addInputToStream(byte[] buffer)
{
    byte[] sample = new byte[buffer.Length / 4];
    if (waveWriter == null) waveWriter = new WaveFileWriter(@"C:\Temp\Test0001.wav", new WaveFormat(44100, 16, 1));
    for (int i = 0; i < buffer.Length; i = i + 4)
    {
        sample[i/4] = (byte)Convert.ToSingle(buffer[i] + buffer[i + 1] + buffer[i + 2] + buffer[i + 3]);
    }
    waveWriter.Write(sample, 0, sample.Length);
}

The problem appears in the method addInputToStream, the sound is well put in the .wav file but I have a lot of "audio parasites" in the file. Very irritating. I tried to change the i variable when I call the method, to not used Convert.ToSingle but same result.

I suspect a problem with the WaveFormat of waveWriter but I don't know what I'm missing. Anyone of you have a clue ?

1

There are 1 answers

0
mark gamache On

You need to create a WaveFileWriter and write the samples out. This is pretty simple, but took me a while to get also. (note: I didn't setup the waveprovider, but you have that part already).

 class AsioSample
{
    private NAudio.Wave.AsioOut _audioDevice;
    public WaveFileWriter AsioWaveFileWriter;
    public AsioSample()
    {
        //this is set for one channel
        AsioWaveFileWriter = new WaveFileWriter("sample.wav", new WaveFormat(48000, 1));
        _audioDevice = new NAudio.Wave.AsioOut(2);//or whatever
        _audioDevice.InitRecordAndPlayback(_waveProvider, 1, 48000);
        _audioDevice.AudioAvailable += new EventHandler<NAudio.Wave.AsioAudioAvailableEventArgs>(OnAudioAvailable);
        _audioDevice.Play();
    }

    private unsafe void OnAudioAvailable(object sender, NAudio.Wave.AsioAudioAvailableEventArgs e)
    {
        var floatsamples = new float[e.SamplesPerBuffer * e.InputBuffers.Length];
        e.GetAsInterleavedSamples(floatsamples);
        AsioWaveFileWriter.WriteSamples(floatsamples, 0, floatsamples.Length);
        AsioWaveFileWriter.Flush();
    }

}