使用 NAudio 将 wav 文件拆分为字节数组

问题描述

我想知道如何使用 PCM 数据将 WAV 文件的通道拆分为两个字节数组。

我一直在尝试使用 NAudio 来做到这一点,但我无法做到。

解决方法

您可以尝试以下方法将 wav 文件拆分为两个字节数组。

 using (WaveFileReader pcm = new WaveFileReader(@"E:\\test.wav"))
            {
                int samplesDesired = 5000;
                byte[] buffer = new byte[samplesDesired * 4];
                short[] left = new short[samplesDesired];
                short[] right = new short[samplesDesired];
                int bytesRead = pcm.Read(buffer,10000);
                int index = 0;
                for (int sample = 0; sample < bytesRead / 4; sample++)
                {
                    left[sample] = BitConverter.ToInt16(buffer,index);
                    index += 2;
                    right[sample] = BitConverter.ToInt16(buffer,index);
                    index += 2;
                }
      
                MessageBox.Show("success");
            }