如何在 C# 中使用 Naudio 从立体声通道 mp3 获取 PCM 数据
how to get PCM data from stereo channel mp3 using Naudio in C#
我是 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?
在立体声文件中,样本是交错的:一个左声道样本后跟一个右声道样本,依此类推。因此在您的循环中,您可以一次通过四个字节来读出样本。
您的代码中也有一些错误。您应该使用 Read 的 return 值,而不是缓冲区的大小,并且您在访问样本的代码中有一个错误。此外,无需复制到临时缓冲区。
像这样的东西应该适合你:
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);
}
我是 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?
在立体声文件中,样本是交错的:一个左声道样本后跟一个右声道样本,依此类推。因此在您的循环中,您可以一次通过四个字节来读出样本。
您的代码中也有一些错误。您应该使用 Read 的 return 值,而不是缓冲区的大小,并且您在访问样本的代码中有一个错误。此外,无需复制到临时缓冲区。
像这样的东西应该适合你:
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);
}