I have some c# code that has two functions. One converts linear PCM data to MuLaw format. The other function converts it back.
public static Byte[] LinearToMulaw(Byte[] bytes, int bitsPerSample, int channels)
public static Byte[] MuLawToLinear(Byte[] bytes, int bitsPerSample, int channels)
In the following explanation I am always using 16 bits, two channels.
When this code was published the author decided to make the functions convert the sound to mono. I need stereo. If I replace each function with just a return of the bytes that come in, I do indeed have the required stereo. A left and right channel.
Now I know a little bit of what has to be done. Here is the code to convert from Linear PCM to MuLaw:
public static Byte[] LinearToMulaw(Byte[] bytes, int bitsPerSample, int channels)
{
// return bytes;
int blockAlign = channels * bitsPerSample / 8;
Byte[] result = new Byte[bytes.Length / blockAlign];
int resultIndex = 0;
for (int i = 0; i < result.Length ; i++)
{
switch (bitsPerSample)
{
case 8:
switch (channels)
{
//8 Bit 1 Channel
case 1:
result[i] = linear2ulaw(bytes[resultIndex]);
resultIndex += 1;
break;
//8 Bit 2 Channel
case 2:
result[i] = linear2ulaw(bytes[resultIndex]);
resultIndex += 2;
break;
}
break;
case 16:
switch (channels)
{
//16 Bit 1 Channel
case 1:
result[i] = linear2ulaw(BitConverter.ToInt16(bytes, resultIndex));
resultIndex += 2;
break;
//16 Bit 2 Channels
case 2:
result[i] = linear2ulaw(BitConverter.ToInt16(bytes, resultIndex));
resultIndex += 4; // this is 4 to skip the right channel
break;
}
break;
}
}
return result;
}
And here is the code to convert back:
public static Byte[] MuLawToLinear(Byte[] bytes, int bitsPerSample, int channels)
{
// return bytes;
int blockAlign = channels * bitsPerSample / 8;
Byte[] result = new Byte[bytes.Length * blockAlign];
for (int i = 0, counter = 0; i < bytes.Length; i++, counter += blockAlign)
{
int value = MulawToLinear(bytes[i]);
Byte[] values = BitConverter.GetBytes(value);
switch (bitsPerSample)
{
case 8:
switch (channels)
{
//8 Bit 1 Channel
case 1:
result[counter] = values[0];
break;
//8 Bit 2 Channel
case 2:
result[counter] = values[0];
result[counter + 1] = values[0];
break;
}
break;
case 16:
switch (channels)
{
//16 Bit 1 Channel
case 1:
result[counter] = values[0];
result[counter + 1] = values[1];
break;
//16 Bit 2 Channels
case 2:
result[counter] = values[0];
result[counter + 1] = values[1];
result[counter + 2] = values[0]; //tried 3 and 4 here
result[counter + 3] = values[1];
break;
}
break;
}
}
return result;
}
These two functions will take a linear stereo PCM array of bytes and output them as mono.
The input bytes are in the following format: Byte 0, byte 1 are the left channel and byte 2 and 3 are the right channel. Now the author converts the input byte array to mono by incrementing the result index by 4, skipping the right channel. In the MuLaw to Linear, he puts the right channel values into the left.
I have tried to modify this by incrementing the result index by only two and reading the resulting bytes back in but all I get is distorted sound that sounds like it is running at half speed. Can anyone suggest where I am going wrong?