【问题标题】:What am I doing wrong when parsing a wav file?解析 wav 文件时我做错了什么?
【发布时间】:2016-12-07 03:13:27
【问题描述】:

我正在尝试解析一个 wav 文件。我不确定 wav 文件中是否可以有多个数据块,但我最初假设只有 1 个,因为我正在阅读的 wav 文件格式描述仅提到有 1 个。

但我注意到,当被解析的 wav 文件大约为 36MB 且采样率为 44100 时,subchunk2size 非常小(如 26)。

所以我尝试解析它,假设有多个块,但在第一个块之后,找不到 subchunk2id。

为了逐块进行,我使用了下面的代码

int chunkSize = System.BitConverter.ToInt32(strm, 40);
int widx = 44; //wav data starts at the 44th byte
//strm is a byte array of the wav file
while(widx < strm.Length)
{
    widx += chunkSize;
    if(widx < 1000)
    {
        //log "data" or "100 97 116 97" for the subchunkid
        //This is only getting printed the 1st time though. All prints after that are garbage
        Debug.Log( strm[widx] + " " + strm[widx+1] + " " + strm[widx+2] + " " + strm[widx+3]);
    }
    if(widx + 8 < strm.Length)
    {
        widx += 4;
        chunkSize = System.BitConverter.ToInt32(strm, widx);
        widx += 4;
    }else
    {
        widx += 8;
    }
}

【问题讨论】:

  • 它非常模糊地提到它没有运行/没有工作。如果有任何或任何与错误相关的详细信息,请提及您的例外情况。
  • 我确实提到了这些事情。在第一个之后,我没有在缓冲区中看到“数据”,而且我的 subchunk2size 太小了。
  • 您能解释一下您希望如何格式化数据吗?从代码来看,它看起来像: header(40):chunksize(4):(data chunksize long):(4 bytes of something?):(new chunk size):(data chunksize long): etc...是正确的?
  • 你能显示你得到 subchunk2size 值的代码吗?
  • 我一直使用这个参考作为我的指南soundfile.sapp.org/doc/WaveFormat

标签: c# wav


【解决方案1】:

一个 .wav 文件有 3 个块: 每个块的大小为 4 字节

第一个块是“RIFF”块。它包括 8 个字节的文件大小(4 个字节)和格式的名称(4 个字节,通常是“WAVE”)。

下一个块是“fmt”-块(块名中的空格很重要)。它包括音频格式(2 字节)、通道数(2 字节)、采样率(4 字节)、字节率(4 字节)、块对齐(2 字节)和每个样本的位数(2 字节) .

第三个也是最后一个块是数据块。这是真实数据和样本的幅度。其中包括 4 Byte 的 datasize,即数据的字节数。

您可以找到关于 .wav 文件 here 属性的进一步说明。

根据这些知识,我已经创建了以下类:

public sealed class WaveFile
{
    //privates
    private int fileSize;
    private string format;
    private int fmtChunkSize;
    private int audioFormat;
    private int numChannels;
    private int sampleRate;
    private int byteRate;
    private int blockAlign;
    private int bitsPerSample;
    private int dataSize;
    private int[][] data;//One array per channel

    //publics
    public int FileSize => fileSize;
    public string Format => format;
    public int FmtChunkSize => fmtChunkSize;
    public int AudioFormat => audioFormat;
    public int NumChannels => numChannels;
    public int SampleRate => sampleRate;
    public int ByteRate => byteRate;
    public int BitsPerSample => bitsPerSample;
    public int DataSize => dataSize;
    public int[][] Data => data;

    public WaveFile(string path)
    {
        FileStream fs = File.OpenRead(path);
        LoadChunk(fs); //read RIFF Chunk
        LoadChunk(fs); //read fmt Chunk
        LoadChunk(fs); //read data Chunk
        fs.Close();
    }

    private void LoadChunk(FileStream fs)
    {
        ASCIIEncoding Encoder = new ASCIIEncoding();
        byte[] bChunkID = new byte[4];

        fs.Read(bChunkID, 0, 4);
        string sChunkID = Encoder.GetString(bChunkID);

        byte[] ChunkSize = new byte[4];

        fs.Read(ChunkSize, 0, 4);

        if (sChunkID.Equals("RIFF"))
        {
            fileSize = BitConverter.ToInt32(ChunkSize, 0);

            byte[] Format = new byte[4];
            fs.Read(Format, 0, 4);
            this.format = Encoder.GetString(Format);
        }

        if (sChunkID.Equals("fmt "))
        {
            fmtChunkSize = BitConverter.ToInt32(ChunkSize, 0);
            byte[] audioFormat = new byte[2];
            fs.Read(audioFormat, 0, 2);
            this.audioFormat = BitConverter.ToInt16(audioFormat, 0);
            byte[] numChannels = new byte[2];
            fs.Read(numChannels, 0, 2);
            this.numChannels = BitConverter.ToInt16(numChannels, 0);
            byte[] sampleRate = new byte[4];
            fs.Read(sampleRate, 0, 4);
            this.sampleRate = BitConverter.ToInt32(sampleRate, 0);
            byte[] byteRate = new byte[4];
            fs.Read(byteRate, 0, 4);
            this.byteRate = BitConverter.ToInt32(byteRate, 0);
            byte[] blockAlign = new byte[2];
            fs.Read(blockAlign, 0, 2);
            this.blockAlign = BitConverter.ToInt16(blockAlign, 0);
            byte[] bitsPerSample = new byte[2];
            fs.Read(bitsPerSample, 0, 2);
            this.bitsPerSample = BitConverter.ToInt16(bitsPerSample, 0);
        }

        if (sChunkID.Equals("data"))
        {
            dataSize = BitConverter.ToInt32(ChunkSize, 0);
            data = new int[this.numChannels][];

            byte[] temp = new byte[dataSize];

            for (int i = 0; i < this.numChannels; i++)
            {
                data[i] = new int[this.dataSize / (numChannels * bitsPerSample / 8)];
            }

            for (int i = 0; i < data[0].Length; i++)
            {
                for (int j = 0; j < numChannels; j++)
                {
                    if (fs.Read(temp, 0, blockAlign / numChannels) > 0)
                    {
                        if (blockAlign / numChannels == 2)
                        { data[j][i] = BitConverter.ToInt32(temp, 0); }
                        else
                        { data[j][i] = BitConverter.ToInt16(temp, 0); }

                    }
                }
            }
        }
    }
}

需要使用指令:

using System;
using System.IO;
using System.Text;

该类逐字节读取所有块并设置属性。你只需要初始化这个类,它就会返回你选择的波形文件的所有属性。

【讨论】:

    【解决方案2】:

    在您添加的参考文献中,我没有看到任何提及每个数据块重复的块大小...

    试试这样的:

    int chunkSize = System.BitConverter.ToInt32(strm, 40);
    int widx = 44; //wav data starts at the 44th byte
    //strm is a byte array of the wav file
    while(widx < strm.Length)
    {
        if(widx < 1000)
        {
            //log "data" or "100 97 116 97" for the subchunkid
            //This is only getting printed the 1st time though. All prints after that are garbage
            Debug.Log( strm[widx] + " " + strm[widx+1] + " " + strm[widx+2] + " " + strm[widx+3]);
        }    
        widx += chunkSize;
    }
    

    【讨论】:

    • 是的,我知道它只提到了 1,但我不确定是否还有其他块。它看起来不像,因为我也刚刚尝试在整个数据数组中搜索另一个“数据”块标识符,但找不到。但我不明白为什么当文件为 36MB 时 subchunk2size 只有 26。它应该比这大得多。我想知道 wav 文件是否有问题,因为第一个块大小也是伪造的(我得到 0xffffffff)。我正在使用 ffmpeg 制作 wav 文件,所以我可能缺少一个选项或其他东西?
    • @Thundercleez 听起来文件可能已损坏。尝试拉出 ChunkSize (bytes 4 - 8) 。它应该等于 36 + SubChunk2Size。
    • 它确实在该文档的底部说,WAV 文件通常使用较旧的格式。不幸的是,旧格式的链接已损坏。我认为您应该查看文件中的数据并确保一切都在您期望的位置。如果不是,最好搜索“数据”字(预计为 36-40),然后从那里开始工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2013-08-10
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多