为什么不赢得二进制阅读器读取* .wav的内容

时间:2016-11-03 17:07:46

标签: c#

我无法弄清楚为什么我在读取* .wav文件的假定内容后会得到零。

链接到输出图像:http://imgur.com/a/rZWes

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
        }

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请参阅WAV标题的此定义:

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

你会看到在BitsPerSample(bitDepth)之后,在你到达“data”标题之前有可选值。数据大小。

如果它不是PCM格式 - 您也需要阅读这些格式,或者只是在bitDepth之后搜索“数据”。

在我刚检查的文件上 - 第一个值是一个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);