如何使用naudio将立体声pcm样本转换为单声道样本

时间:2013-04-29 17:45:52

标签: c# naudio

如何使用naudio将立体声pcm样本转换为单声道样本?

或将立体声mp3文件转换为单声道原始样本!

我试着这样做:

for (int u = 0; u < output.Length; u+=4)
            {
                byte[] Lbuffer = new byte[2];
                byte[] Rbuffer = new byte[2];
                Lbuffer[0] = output[u + 0];
                Lbuffer[1] = output[u + 1];
                Rbuffer[0] = output[u + 2];
                Rbuffer[1] = output[u + 3];

                Int16 leftSample = BitConverter.ToInt16(Lbuffer, 0);
                Int16 rightSample = BitConverter.ToInt16(Rbuffer, 0);

                Int16 mixedMono = (Int16)(0.5f * (float)leftSample + (float)rightSample);
                Byte[] mixedMonoBytes = BitConverter.GetBytes(mixedMono);

                mono[counter] = mixedMonoBytes[0];
                mono[counter+1] = mixedMonoBytes[1];


                //mono[counter] = Convert.ToByte((Convert.ToInt16(buffer[0]) + Convert.ToInt16(buffer[2]))/2);
                //mono[counter+1] = Convert.ToByte((Convert.ToInt16(buffer[0]) + Convert.ToInt16(buffer[2]))/2);
                counter += 2;
            }

但目前无效!结果有噪音! output是一个包含原始样本的数组!

1 个答案:

答案 0 :(得分:1)

正如@ daniel-s所指出的,要将PCM样本从立体声(2声道)转换为单声道(1声道),您可以简单地取两个声道的平均值:对于每个样本,从左声道获取值,添加右通道的值,然后除以2.使用saturation arithmetic以避免溢出。

要将MP3文件转换为原始(PCM)样本,您需要通过文件解析器和MP3比特流解码器运行MP3文件。有很多图书馆和应用程序可以做到这一点;例如,请参阅FFmpeg

[编辑]我忘了提及,更重要的是,NAudio支持通过ACM或DMO编解码器解码MP3文件;有关示例,请参阅NAudio - MP3