簡體   English   中英

如何在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的返回值,而不是緩沖區的大小,並且在訪問樣本的代碼中有一個錯誤,一個錯誤。 同樣,無需復制到臨時緩沖區。

這樣的事情應該為您工作:

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