tags:

views:

19

answers:

1

I am very new to NAudio and I need to convert the buffer of input samples from an input device to an array of doubles that range from -1 to 1.

I create the input device as follows:

WaveIn inputDevice = new WaveIn();

//change the input device to the one i want to receive audio from
inputDevice.DeviceNumber = 1;

//change the wave format to what i want it to be.
inputDevice.WaveFormat = new WaveFormat(24000, 16, 2);

//set up the event handlers
inputDevice.DataAvailable += new EventHandler(inputDevice_DataAvailable); inputDevice.RecordingStopped += new EventHandler(inputDevice_RecordingStopped);

//start the device recording
inputDevice.StartRecording();

Now when the 'inputDevice_DataAvailable' callback gets called I get a buffer of audio data. I need to convert this data to and array of doubles that represent volume levels between -1 and 1. If anyone can help me out that would be great.

A: 

the buffer you get back will contain 16 bit short values. You can use the WaveBuffer class from NAudio which will make it easy to read the sample values out as shorts. Divide by 32768 to get your double/float sample value.

    void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        byte[] buffer = e.Buffer;

        for (int index = 0; index < e.BytesRecorded; index += 2)
        {
            short sample = (short)((buffer[index + 1] << 8) |
                                    buffer[index]);
            float sample32 = sample / 32768f;                
        }
    }
Mark Heath
Thanks that worked a treat.
Rob