为什么二进制读取器不能读取*.wav的内容?

本文关键字:读取 wav 不能读 二进制 不能 为什么 | 更新日期: 2023-09-27 18:16:15

我不明白为什么在阅读了*.wav文件的假定内容后我得到了零。

链接到输出图像:https://i.stack.imgur.com/x4n5h.jpg

using (BinaryReader reader = new BinaryReader(File.Open("test.wav", FileMode.Open))) {
            // Read the wave file header from the buffer. 
            int chunkID = reader.ReadInt32 ();          print (chunkID);
            int fileSize = reader.ReadInt32();          print (fileSize);
            int riffType = reader.ReadInt32();          print (riffType);
            int fmtID = reader.ReadInt32();             print (fmtID);
            int fmtSize = reader.ReadInt32();           print (fmtSize);
            int fmtCode = reader.ReadInt16();           print (fmtCode);
            int channels = reader.ReadInt16();          print (channels);
            int sampleRate = reader.ReadInt32();        print (sampleRate);
            int fmtAvgBPS = reader.ReadInt32();         print (fmtAvgBPS);
            int fmtBlockAlign = reader.ReadInt16();     print (fmtBlockAlign);
            int bitDepth = reader.ReadInt16();          print (bitDepth);
            int dataID = reader.ReadInt32();            print (dataID);
            int dataSize = reader.ReadInt32();          print (dataSize);
            byteArray = reader.ReadBytes(dataSize);
            // After this you have to split that byte array for each channel (Left,Right)
            // Wav supports many channels, so you have to read channel from header
        }

提前感谢您的帮助!

为什么二进制读取器不能读取*.wav的内容?

查看WAV头文件的定义:

http://soundfile.sapp.org/doc/WaveFormat/

你会看到在BitsPerSample (bitDepth)之后有一些可选的值在你到达"data" header &数据大小。

如果不是PCM格式-你也需要阅读这些,或者只是在bitDepth后面搜索"data"。

在我刚刚检查的文件中——第一个值是一个int16,指定后面int16的数量——在这个特殊的例子中是12,所以读取额外的24个字节会把我带到"data"头。

    // Read the wave file header from the buffer. 
    int chunkID = reader.ReadInt32();    // ASCII string = "RIFF"
    print(chunkID);
    int fileSize = reader.ReadInt32();
    print(fileSize);
    int riffType = reader.ReadInt32();   // ASCII string "WAVE"
    print(riffType);
    int fmtID = reader.ReadInt32();      // ASCII string "fmt "
    print(fmtID);
    int fmtSize = reader.ReadInt32();    // 16 = PCM (actually specifies size of data of this section)
    print(fmtSize);
    short fmtCode = reader.ReadInt16();
    print(fmtCode);
    short channels = reader.ReadInt16();
    print(channels);
    int sampleRate = reader.ReadInt32();
    print(sampleRate);
    int fmtAvgBPS = reader.ReadInt32();
    print(fmtAvgBPS);
    short fmtBlockAlign = reader.ReadInt16();
    print(fmtBlockAlign);
    short bitDepth = reader.ReadInt16();
    print(bitDepth);
    short[] extraPars = null;
    if (fmtSize != 16)
    {
      short NoOfEPs = reader.ReadInt16();
      extraPars = new short[NoOfEPs];
      for (int i=0;i<NoOfEPs;i++)
        extraPars[i] = reader.ReadInt16();;
    }
    int dataID = reader.ReadInt32();    // ASCII string = "data"
    print(dataID);
    int dataSize = reader.ReadInt32();
    print(dataSize);
    byte [] byteArray = new byte[dataSize];
    byteArray = reader.ReadBytes(dataSize);