This project has moved. For the latest updates, please go here.

Using WasapiLoopbackCapture to do a sound visualizer

Apr 16, 2013 at 5:27 PM
Edited Apr 16, 2013 at 11:18 PM

i'm trying to do a visualizer using Naudio wasapi loop back capture feature, but i have a problem using samples.

NAudio.Wave.WasapiLoopbackCapture wasapi = new NAudio.Wave.WasapiLoopbackCapture();
wasapi.DataAvailable += InputBufferToFileCallback;

Now i use the event InputBufferToFileCallback to get the e.buffer and then extracting from the byte array the samples
        byte[] buffer = e.Buffer;
        int bytesRecorded = e.BytesRecorded;
        int bufferIncrement = (int)(wasapi.WaveFormat.BlockAlign / wasapi.WaveFormat.Channels);
        int bitsPerSample = wasapi.WaveFormat.BitsPerSample;

        for (int index = 0; index < e.BytesRecorded; index += bufferIncrement)
            float sample32left = 0;
            float sample32right = 0;
            sample32right = BitConverter.ToSingle(buffer, index);
            sample32left = BitConverter.ToSingle(buffer, index + 4);
            sampleAggregator.Add(sample32left, sample32right);
My question is if the samples for the right and left channels are right or if is it possible to use the wavechannel32 class to get the left and right samples in relation to the wasapiloopbackCapture.


EDIT: resolved, the problem was the bufferIncrement that is equal to wasapi.WaveFormat.BlockAlign , no division.