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

24 bit to 16 bit on the fly

May 31, 2013 at 2:18 PM
Hi !

I need help with converting 24 bit input to 16 bit "on the fly" im trying to record audio output, if the format is set to 24 bit depth i have problem with mix it down to one channel (sound is slower) here is the example wchich im using to convert if audio has 32 bit i dont know how to change this code if input has 24 bit
                            byte[] newArray16Bit = new byte[e.BytesRecorded / 2];
                            short two;
                            float value;
                            for (int i = 0, j = 0; i < e.BytesRecorded; i += 4, j += 2)
                            {
                                value = (BitConverter.ToSingle(e.Buffer, i));
                                two = (short)(value * short.MaxValue);

                                newArray16Bit[j] = (byte)(two & 0xFF);
                                newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
                            }
                            byte[] mono = StereoToMono(newArray16Bit);
                            OutFile.Write(mono);
i found sample code to convert but it returns float[] i need byte:
public float[] Convert24BitToFloat(byte[] input)
{
    int inputSamples = input.Length / 3; // 24 bit input
    float[] output = new float[inputSamples];
    int outputIndex = 0;
    var temp = new byte[4];
    for(int n = 0; n < inputSamples; n++)
    {
        // copy 3 bytes in
        Array.Copy(input,n*3,temp,0,3);
        int sample = BitConverter.ToInt32(temp,0);
        output[outputIndex++] = sample / 16777216f;
    }
    return output;
}
Coordinator
Jun 3, 2013 at 3:05 PM
use BitConverter.GetBytes to turn a float into a byte array