Reading and writing WAVE file produces unplayable output

0

I want to add echo to wave file, in order to do so I need to open it and then save it first. I've written some methods but output file is not correct.

Testing input file size: 731 014

Output file sieze: 730 898

But the "space on disc" is the same for both files, which is 733 184

Error code or whatever it is displayed when opened: 0xc00d36c4

What can be the issue here? This looks pretty simple and yet it's not working. Here's my header, read and write methods:

 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;
           }
      }
 }
c#
wav

1 Answer

0

I didn't find out what the issue was, but for echo purposes this class will work:

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;
           }
      }
 }
answered on Stack Overflow Nov 1, 2017 by Robert Ostrowski

User contributions licensed under CC BY-SA 3.0