如何解释 MP3 文件中的原始 pcm 数据

How to interpret raw pcm data from an MP3 file

我正在使用 NAudio 从 MP3 文件中检索原始 pcm 数据。然后我需要以设定的时间间隔(基于时间)读取这些数据。

但是,我很难理解如何解释这些数据。例如,如果 MP3 是立体声 vs 单声道 vs 其他任何东西,我应该如何读取数据?我如何检查 MP3 是立体声还是单声道?还有哪些其他因素可以改变我读取数据的方式?

我希望这里有示例代码,但如有任何帮助,我们将不胜感激。

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);

前一个问题的上下文:()

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].

如何检测频道?那怎么读取pcm数据来匹配呢?

您可以从 pcm.WaveFormat 中获取格式,从中您可以找到通道数、通道数、每秒平均字节数等...

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;

从那里您需要将字节解码为整数或浮点数。有很多关于 SO 解决这个问题的答案。