Search code examples
c#audiomp3

How to interpret raw pcm data from an MP3 file


I am using NAudio to retrieve the raw pcm data from an MP3 file. Then I need to read this data at set intervals (time based).

However, I'm having a hard time understanding how to interpret this data. For example, if the MP3 was stereo vs mono vs anything else, how should I read the data? How do I even check if the MP3 was stereo or mono? What other factors can change how I need to read the data?

I'm hoping for sample code here, but any help is appreciated.

NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(mp3FilePath));
int someInterval = 88200;//~1 second depending on the file specs
byte[] buffer = new byte[someInterval];
int current = 0;
int ret = 0;
do
{
     ret = pcm.Read(buffer, current, someInterval);

     //do something

     current += someInterval;
} while (ret != -1);

Context from a previous question:(How to relate audio data to time)

If you have a raw audio file (no headers or anything) with a single channel (mono, not stereo) sampled at 44.1kHz 16 bit, then you would read 88,200 bytes per second of data [to read 1 second of audio data].

How do I detect the channels? Then how do I read the pcm data to match it?


Solution

  • You can get the format from pcm.WaveFormat from which you can find channel count, number of channels, average bytes per second, etc...

    NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(mp3FilePath));
    int someInterval = pcm.WaveFormat.Channels * pcm.WaveFormat.SampleRate * pcm.WaveFormat.BitsPerSample/8;
    

    From there you'll need to decode the bytes into ints or floats. There are a lot of answers on SO addressing that question.