如何在c#中使用Naudio从立体声声道mp3中获取PCM数据
本文关键字:mp3 立体声 声道 获取 数据 PCM Naudio | 更新日期: 2023-09-27 18:13:47
我是Naudio的新手,使用它从Mp3文件中获取PCM数据,这是我的代码,从单声道文件中获取PCM,但不知道如何使用立体声声道文件
代码:Mp3FileReader file = new Mp3FileReader(op.FileName);
int _Bytes = (int)file.Length;
byte[] Buffer = new byte[_Bytes];
file.Read(Buffer, 0, (int)_Bytes);
for (int i = 0; i < Buffer.Length - 2; i += 2)
{
byte[] Sample_Byte = new byte[2];
Sample_Byte[0] = Buffer[i + 1];
Sample_Byte[1] = Buffer[i + 2];
Int16 _ConvertedSample = BitConverter.ToInt16(Sample_Byte, 0);
}
如何从立体声声道Mp3文件中获得PCM ?
在立体声文件中,样本是交错的:一个左通道样本后面跟着一个右通道等。所以在你的循环中,你可以一次通过四个字节来读取样本。
你的代码中也有一些bug。您应该使用Read的返回值,而不是缓冲区的大小,并且您在代码中有一个错误来访问示例。此外,不需要复制到临时缓冲区中。
像这样的东西应该为你工作:
var file = new Mp3FileReader(fileName);
int _Bytes = (int)file.Length;
byte[] Buffer = new byte[_Bytes];
int read = file.Read(Buffer, 0, (int)_Bytes);
for (int i = 0; i < read; i += 4)
{
Int16 leftSample = BitConverter.ToInt16(Buffer, i);
Int16 rightSample = BitConverter.ToInt16(Buffer, i + 2);
}