3
votes

I am working on PCM as a beginner, and I am using Naudio library. So I was wondering that does the storage of PCM data depends on Little Endian and Big Endian or if not the storage depends then does the memory addressing depends on this Big and Little Endian, because a PCM data got Least significant byte and ofcourse most significant byte and as I know that Little Endian and Big Endian got different way of storing those bytes into memory therefore reading the data from disk into memory may produce wrong values until I reverse the array and then convert it into short value.

Just to make you understand what I am talking about, here is a short example:

A very short PCM data, of just 4 Bytes:

Address Value
1000    90
1001    AB
1002    12
1003    CD

This is the storage in a in big endian. and the following is the storage in little Endian

Address Value
1000    CD
1001    12
1002    AB
1003    90

so does this reversing of bytes happen when you read PCM using Naudio? because I check if the BitConverter is Little Endian or not, according to that I reverse my array of bytes, kind of like this:

                    Int16 Left_Sample = BitConverter.ToInt16(Buffer, i);
                    Int16 Right_Sample = BitConverter.ToInt16(Buffer, i + 2);
                    Int16 Final_Sample = (Int16)(0.5f * ((float)Left_Sample + (float)Right_Sample));
                    byte[] Final_Byte = BitConverter.GetBytes(Final_Sample);
                    if (BitConverter.IsLittleEndian){ Array.Reverse(Final_Byte); }

So in summary:

Q: Does the Big and Little Endian affects the byte storage?

Q: Do I need to use if(BitConverter.IsLittleEndian) statement to reverse the array?

1

1 Answers

2
votes

Inside WAV files, PCM samples are stored as little-endian.

MP3 files are a compressed audio format, so it doesn't really make sense to say what endianness it uses, but when NAudio converts to PCM, it will again be little-endian.

AIFF files typically store samples as big-endian, but again, NAudio will handle this for you.

Without seeing more of your code it's hard to say, but I doubt you'll need to do any byte order swapping yourself if you're using NAudio normally.