使用 NAudio 将 wav 文件分割成字节数组

问题描述 投票:0回答:1

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

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

c# .net visual-studio winforms naudio
1个回答
1
投票

您可以尝试以下方法将 wav 文件分成两个

byte arrays

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, 0, 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");
}
© www.soinside.com 2019 - 2024. All rights reserved.