Stereo audio files to be convert to 8 bit mono in .net

Asked By 50 points N/A Posted on -
qa-featured

Hi All,

I need few stereo audio files to be convert to 8 bit mono in .net, How should I do the same, Please help me by getting inputs on this regard. Waiting to hear from experts,

Thank You,

Annarforster

SHARE
Answered By 5 points N/A #198680

Stereo audio files to be convert to 8 bit mono in .net

qa-featured

Hey Mr. Annarfoster,

There are two steps in audio conversion: 1. Decoding and 2. Encoding.

Logically, we will first need to decode the audio to PCM and then encode to required format.

Well, converting stereo to mono is very simple. We know that stereo has 2 samples-left and right. Discarding any one of them will give us mono audio format.

Here is the code you need:(We are discarding the right channel)

private byte[] StereoToMono(byte[] input)

{

    byte[] output = new byte[input.Length / 2]; //to discard input length

    int outputIndex = 0;

    for (int n = 0; n < input.Length; n+=4)

    {

        // copy in the first 16 bit sample

        output[outputIndex++] = input[n];

        output[outputIndex++] = input[n+1];

    }

    return output;

}

 

Method 2:

Alternatively, we can try mixing the two samples together:

(I have used Bitconverter function and every two bytes are converted to Int16)

private byte[] MixStereoToMono(byte[] input)
{
    byte[] output = new byte[input.Length / 2];
    int outputIndex = 0;
    for (int n = 0; n < input.Length; n+=4)
    {
        int leftChannel = BitConverter.ToInt16(input,n);
        int rightChannel = BitConverter.ToInt16(input,n+2);
        int mixed = (leftChannel + rightChannel) / 2;
        byte[] outSample = BitConverter.GetBytes((short)mixed);
        
        // copy in the first 16 bit sample
        output[outputIndex++] = outSample[0];
        output[outputIndex++] = outSample[1];
    }
    return output;
}

Let me know if this helps.

Related Questions