读写WAVE文件会产生无法播放的输出

时间:2017-11-01 16:49:24

标签: c# wav

我想将echo添加到wave文件中,为此我需要打开它然后先保存它。我写了一些方法,但输出文件不正确。

测试输入文件大小:731 014

输出文件sieze:730 898

但两个文件的“光盘上的空间”是相同的,即733 184

错误代码或打开时显示的内容:0xc00d36c4

这里有什么问题?这看起来很简单但却无法正常工作。这是我的标题,读写方法:

 class WaveFile
 {
      struct WaveHeader
      {
           public byte[] RiffID;
           public uint fileSize;
           public byte[] format;

           //Wave format chunk 1
           public byte[] fmtID;
           public uint fmtSize;
           public ushort audioFormat;
           public ushort channels;
           public uint sampleRate;
           public uint byteRate;
           public ushort blockAlign; 
           public int bitsPerSample;

           //Wave format chunk 2
           public byte[] dataID;
           public uint dataSize;                            
      }

      uint samples;

      public List<short> L;
      public List<short> R;

      WaveHeader header = new WaveHeader();

      //loading file, preparation for modyfying
      public bool loadWaveFile(string filePath)
      {
           using (FileStream fs = File.Open(filePath, FileMode.Open))
           using (BinaryReader reader = new BinaryReader(fs))
           {
                // chunk 0
                header.RiffID = reader.ReadBytes(4);
                header.fileSize = reader.ReadUInt32();
                header.format = reader.ReadBytes(4);
                // chunk 1
                header.fmtID = reader.ReadBytes(4);
                header.fmtSize = reader.ReadUInt32();
                header.audioFormat = reader.ReadUInt16();
                header.channels = reader.ReadUInt16();
                header.sampleRate = reader.ReadUInt32();
                header.byteRate = reader.ReadUInt32();
                header.blockAlign = reader.ReadUInt16();
                header.bitsPerSample = reader.ReadInt16();
                // chunk 2
                header.dataID = reader.ReadBytes(4);
                header.dataSize = reader.ReadUInt32();

                // DATA is stereo
                L = new List<short>();
                R = new List<short>();

                samples = header.dataSize / header.blockAlign;
                for (int i = 0; i < samples; i++)
                {
                     L.Add((short)reader.ReadUInt16());
                     R.Add((short)reader.ReadUInt16());
                }
                reader.Close();
                fs.Close();
           }
           return true;
      }

      public bool addEcho(int threadsNumber, int echoesNumber, int delay, int attenuation)
      {
           return true;
      }

      public bool saveWaveFile(string savingPath)
      {
           using (FileStream fs = new FileStream(@savingPath + "\\echo.wav", FileMode.Create))
           using (BinaryWriter writer = new BinaryWriter(fs))
           {
                //chunk 0
                writer.Write(header.RiffID);
                writer.Write(header.fileSize);
                writer.Write(header.format);
                //chunk 1
                writer.Write(header.fmtID);
                writer.Write(header.fmtSize);
                writer.Write(header.audioFormat);
                writer.Write(header.channels);
                writer.Write(header.sampleRate);
                writer.Write(header.byteRate);
                writer.Write(header.blockAlign);
                writer.Write(header.bitsPerSample);
                //chunk 2
                writer.Write(header.dataID);
                writer.Write(header.dataSize);

                for (int i = 0; i < samples; i++)
                {
                     writer.Write(L[i]);
                     writer.Write(R[i]);
                }
                writer.Close();
                fs.Close();
                return true;
           }
      }
 }

1 个答案:

答案 0 :(得分:0)

我没有发现问题是什么,但为了回声的目的,这个课程将起作用:

Class WaveFile
 {
      byte[] byteArray;

      public void loadWaveFile(string filePath)
      {
           byteArray = File.ReadAllBytes(filePath);
      }

      public bool addEcho(int threadsNumber, int echoesNumber, int delay, int attenuation)
      {
           return true;
      }

      public bool saveWaveFile(string savingPath)
      {
           using (FileStream fs = new FileStream(@savingPath + "\\echo.wav", FileMode.Create))
           using (BinaryWriter writer = new BinaryWriter(fs))
           {
                writer.Write(byteArray);                   
                writer.Close();
                fs.Close();
                return true;
           }
      }
 }