繁体   English   中英

如何在C#中将音频文件读入数组

[英]How do I read an audio file into an array in C#

我试图将WAV文件读取到c#中的缓冲区数组中,但是遇到一些问题。 我正在使用文件流来管理音频文件。 这就是我所拥有的...

FileStream WAVFile = new FileStream(@"test.wav", FileMode.Open);

        //Buffer for the wave file...
        BinaryReader WAVreader = new BinaryReader(WAVFile);

        //Read information from the header.
        chunkID = WAVreader.ReadInt32();
        chunkSize = WAVreader.ReadInt32();
        RiffFormat = WAVreader.ReadInt32();

        ...

        channels = WAVreader.ReadInt16();
        samplerate = WAVreader.ReadInt32();
        byteRate = WAVreader.ReadInt32();
        blockAllign = WAVreader.ReadInt16();
        bitsPerSample = WAVreader.ReadInt16();

        dataID = WAVreader.ReadInt32();
        dataSize = WAVreader.ReadInt32();

上面是从WAV文件头读取数据。 然后我有这个:

musicalData = WAVreader.ReadBytes(dataSize);

...读取实际的样本数据,但是对于60秒的音频来说,这只是26个字节。 这个对吗?

我将如何将byte []数组转换为double []?

自从我接触WAVE文件处理以来已经有10到15年的历史了,但是与最初的印象不同,大多数人都将Wave文件作为简单的固定大小的标头,然后是PCM编码的音频数据,WAVE文件是稍微复杂的RIFF格式文件。

我建议不要重新设计RIFF文件处理和各种情况,而是建议使用互操作并调用处理RIFF文件格式的API。

您可以在此示例中看到有关如何打开和获取数据缓冲区(以及有关什么是缓冲区的元信息)的示例 它是C ++语言,但是它显示了mmioOpenmmioReadmmioDescendmmioAscend API的使用,您需要使用mmioAscend API才能使用适当的音频缓冲区。

这段代码可以解决问题。 它将wave文件转换为规范化的双精度数组(-1到1),但是将其转换为int / short数组应该很简单(删除/32768.0位并添加32768)。 如果发现加载的wav文件是mono,则right []数组将设置为null。

我不能说它是完全防弹的(潜在的一次性错误),但是在创建65536个样本数组并创建从-1到1的波动之后,所有样本似乎都没有“穿过”天花板或地板。

// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte)
{
    // convert two bytes to one short (little endian)
    short s = (secondByte << 8) | firstByte;

    // convert to range from -1 to (just below) 1
    return s / 32768.0;
}

// Returns left and right double arrays. 'right' will be null if sound is mono.
public void openWav(string filename, out double[] left, out double[] right)
{
    byte[] wav = File.ReadAllBytes(filename);

    // Determine if mono or stereo
    int channels = wav[22];     // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels

    // Get past all the other sub chunks to get to the data subchunk:
    int pos = 12;   // First Subchunk ID from 12 to 16

    // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
    while(!(wav[pos]==100 && wav[pos+1]==97 && wav[pos+2]==116 && wav[pos+3]==97))
    {
        pos += 4;
        int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
        pos += 4 + chunkSize;
    }

    pos += 8;

    // Pos is now positioned to start of actual sound data.
    int samples = (wav.Length - pos)/2;     // 2 bytes per sample (16 bit sound mono)

    if (channels == 2)
    {
        samples /= 2;        // 4 bytes per sample (16 bit stereo)
    }

    // Allocate memory (right will be null if only mono sound)
    left = new double[samples];

    if (channels == 2)
    {
        right = new double[samples];
    }
    else
    {
        right = null;
    }

    // Write to double array/s:
    int i=0;
    while (pos < length)
    {
        left[i] = bytesToDouble(wav[pos], wav[pos + 1]);
        pos += 2;

        if (channels == 2)
        {
            right[i] = bytesToDouble(wav[pos], wav[pos + 1]);
            pos += 2;
        }

        i++;
    }
}

如果要使用插件,则假设您的WAV文件包含16位PCM(这是最常见的),则可以使用NAudio将其读出到字节数组中,然后为了方便起见将其复制到16位整数数组中。 如果是立体声,则样本将左右左右交错。

using (WaveFileReader reader = new WaveFileReader("myfile.wav"))
{
    Assert.AreEqual(16, reader.WaveFormat.BitsPerSample, "Only works with 16 bit audio");
    byte[] buffer = new byte[reader.Length];
    int read = reader.Read(buffer, 0, buffer.Length);
    short[] sampleBuffer = new short[read / 2];
    Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read);
}

我个人尽量避免使用第三方库。 但是,如果您希望代码看起来更好,更易于处理,则仍然可以使用该选项。

暂无
暂无

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

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