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.