可以正确读取WAV文件的数据

时间:2016-01-10 01:44:03

标签: c# audio

我正在尝试阅读

下的wav file
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();
                    }
                }
            }
        }
    }

但是在

处获得运行时错误
lDataList.Add((short)br.ReadUInt16());
rDataList.Add((short)br.ReadUInt16());

{&#34;无法在流的末尾阅读。&#34;}

我已经看到了这个SO Q/A,并试图按照要求进行调整,但是返回浮动..帮助将不胜感激。

提前致谢

1 个答案:

答案 0 :(得分:0)

在这里,您可以正确计算每个样本的字节数和样本数:

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

但是你假设这个文件有2个通道和16位样本:

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

如果文件实际上有8位样本和/或只有1个通道,则此循环会尝试在某个时刻读取输入文件。 您可能必须断言,在读取标头后文件确实是16位2ch,或者通过根据波形文件标题中指定的每个样本和通道的位数正确读取来正确处理文件。