0

I am trying to read a wav file as under

class Program
    {
        struct WavHeader
        {
            public int riffID; 
            public int size; 
            public int wavID;  
            public int fmtID; 
            public int fmtSize;
            public int format;
            public int channels; 
            public int sampleRate;
            public int bytePerSec; 
            public int blockSize;
            public int bit; 
            public int dataID; 
            public int dataSize; 
        }
        static void Main(string[] args)
        {
            WavHeader Header = new WavHeader();
            List<short> lDataList = new List<short>();
            List<short> rDataList = new List<short>();

            using (FileStream fs = new FileStream(@"D:\Test.wav", FileMode.Open, FileAccess.Read))
            using (BinaryReader br = new BinaryReader(fs))
            {
                try
                {                    
                    Header.riffID = br.ReadInt32();
                    Header.size = br.ReadInt32();
                    Header.wavID = br.ReadInt32();
                    Header.fmtID = br.ReadInt32();
                    Header.fmtSize = br.ReadInt32();
                    Header.format = br.ReadUInt16();
                    Header.channels = br.ReadUInt16();
                    Header.sampleRate = br.ReadInt32();
                    Header.bytePerSec = br.ReadInt32();
                    Header.blockSize = br.ReadInt16();
                    Header.bit = br.ReadInt16();

                    if (Header.fmtSize == 18)
                    {
                        // Read any extra values
                        int fmtExtraSize = br.ReadInt16();
                        br.ReadBytes(fmtExtraSize);
                    }
                    Header.dataID = br.ReadInt32();
                    Header.dataSize = br.ReadInt32();

                    int bytesForSamp = Header.bit / 8;
                    int samps = Header.dataSize / bytesForSamp;


                    for (int i = 0; i < samps; i++)
                    {
                        lDataList.Add((short)br.ReadUInt16());
                        rDataList.Add((short)br.ReadUInt16());
                    }


                }
                finally
                {
                    if (br != null)
                    {
                        br.Close();
                    }
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }
        }
    }

But getting runtime error at

lDataList.Add((short)br.ReadUInt16());
rDataList.Add((short)br.ReadUInt16());

{"Unable to read beyond the end of the stream."}

I have seen this SO Q/A and tried to fit as per the requirement but that's returns float.

halfer
  • 19,824
  • 17
  • 99
  • 186
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173

1 Answers1

0

Here you correctly calculate the bytes per sample and the number of samples:

int bytesForSamp = Header.bit / 8;
int samps = Header.dataSize / bytesForSamp;

But here you assume, that the file has 2 channels and 16-bit samples:

for (int i = 0; i < samps; i++)
{
    lDataList.Add((short)br.ReadUInt16());
    rDataList.Add((short)br.ReadUInt16());
}

If the file actually has 8-bit samples and/or only 1 channel, then this loop tries to read beyond the input file at some point. You would either have to assert, that the file is really 16-bit 2ch after reading the header, or handle the file correctly, by reading correctly according to the number of bits per sample and channels specified in the header of the wave-file.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • it is having 16 bit but 1 channel..u r right..so how to handle then? – priyanka.sarkar Jan 11 '16 at 13:00
  • As a quick fix, for only _this_ special case, you could do: unsigned short data = br.ReadUInt16(); lDataList.Add(data); rDataList.Add(data); to copy the data to both channels. But you will have to handle all cases (1 to n channels, n bit sample size) correctly in productive code. Or bail out with an error message if the wav-file has unhandled properties – Ctx Jan 11 '16 at 13:11
  • Correct, i just did that. – priyanka.sarkar Jan 11 '16 at 13:14