jms2505
jms2505

Reputation: 76

Split wav file into byte arrays with NAudio

I would like to know how can I split the channels of a WAV file into two byte arrays with the PCM data.

I've been trying to do this with NAudio, but I can't get it.

Upvotes: 0

Views: 480

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

You can try the following to split wav file into two byte arrays.

using (WaveFileReader pcm = new WaveFileReader(@"E:\\test.wav"))
{
    int samplesDesired = 5000;
    byte[] buffer = new byte[samplesDesired * 4];
    short[] left = new short[samplesDesired];
    short[] right = new short[samplesDesired];
    int bytesRead = pcm.Read(buffer, 0, 10000);
    int index = 0;

    for (int sample = 0; sample < bytesRead / 4; sample++)
    {
        left[sample] = BitConverter.ToInt16(buffer, index);
        index += 2;
        right[sample] = BitConverter.ToInt16(buffer, index);
        index += 2;
    }

    MessageBox.Show("success");
}

Upvotes: 1

Related Questions