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

Convert 32 wav to 16 wav

Dec 22, 2012 at 11:43 AM
Edited Dec 22, 2012 at 11:58 AM

Hello,

I use NAudio to capture the loopback sound from my computer and I receive 32 bit sound according to the WaveFormat. Is there a way I can convert the received bytes from 32 bit to 16 bit? Or maybe to make WasapiLoopbackCapture generate 16 bit sound?

Here is what I tried:

private void btnStart_Click(object sender, EventArgs e)
{
    _waveIn = new WasapiLoopbackCapture();

    _waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(_waveIn_DataAvailable);

    _waveIn.StartRecording();
}

void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
{

    byte[] newArray16Bit = new byte[e.BytesRecorded / 2];
    short two;
    int value;
    for (int i = 0, j = 0; i < e.BytesRecorded; i = i + 4, j = j + 2)
    {
        value = (BitConverter.ToInt32(e.Buffer, i));
        two = (short)(value % short.MaxValue);

        newArray16Bit[j] = (byte)(two & 0xFF);
        newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
    }

    //do something with the newArray16Bit 
}

I receive a sound which is similar with what my computer is actually playing but it sounds like a am listening to a radio station with a very poor signal (the sound is not clean - you can nearly understand what the sound is about).

Does anybody know how can I clean up the sound?

Thanks!

Dec 22, 2012 at 1:41 PM

I just changed the type of the 'two' variable from int to float and calculated it by multiplying the 'value' with short.MaxValue.

So I replace:

two = (short)(value % short.MaxValue);

with:

two = (short)(value * short.MaxValue);
Now the noise is gone but the entire sound is lower than it should be. Any ideas?

 

Thanks!

Coordinator
Dec 22, 2012 at 1:48 PM

yes, it is likely that your 32 bit incoming data is float not int. Multiplying by short.MaxValue is correct. It may just be that your recorded signal level is low.

Dec 28, 2012 at 11:24 PM

It was because I was playing it as a mono sound even if the signal was stereo. After I set my player to play it as a stereo sound everything was perfect! Thanks!