简体   繁体   English

如何在C#中使用Naudio从立体声通道mp3获取PCM数据

[英]how to get PCM data from stereo channel mp3 using Naudio in C#

I am new to Naudio and using it to get PCM data from Mp3 files, this is my code to take PCM from mono-channel file, but don't know how to do it with stereo channel file 我是Naudio的新手,并用它从Mp3文件中获取PCM数据,这是我的代码,用于从单声道文件中获取PCM,但不知道如何使用立体声声道文件

code: 码:

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

How can I get PCM from stereo channel Mp3 file? 如何从立体声通道Mp3文件获取PCM?

In a stereo file, the samples are interleaved: one left channel sample followed by one right channel etc. So in your loop you could go through four bytes at a time to read out the samples. 在立体声文件中,样本是交错的:一个左声道样本,然后是一个右声道,等等。因此,在循环中,您可以一次遍历四个字节来读出样本。

Also there are some bugs in your code. 您的代码中也有一些错误。 You should use return value of Read, not the size of the buffer, and you have an off by one error in the code to access the samples. 您应该使用Read的返回值,而不是缓冲区的大小,并且在访问样本的代码中有一个错误,一个错误。 Also, no need to copy into a temporary buffer. 同样,无需复制到临时缓冲区。

Something like this should work for you: 这样的事情应该为您工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM