예제 #1
0
        public bool Read(Stream stream)
        {
            using (BinaryReader binaryReader = new BinaryReader(stream))
            {
                while (stream.Position < stream.Length)
                {
                    string chunkID;
                    long   remaining = stream.Length - stream.Position;
                    var    data      = binaryReader.ReadBytes(4);

                    chunkID = System.Text.Encoding.UTF8.GetString(data);
                    if (data[0] == 0) // Sometimes data segment ends with a parasite null byte
                    {
                        stream.Seek(-3, SeekOrigin.Current);
                        data    = binaryReader.ReadBytes(4);
                        chunkID = System.Text.Encoding.UTF8.GetString(data);
                    }

                    if (stream.Position == stream.Length)
                    {
                        return(true);
                    }

                    int chunkSize = binaryReader.ReadInt32();

                    Debug.WriteLine("Chunk ID: " + chunkID + "; chunk size: " + chunkSize + "; stream remaining size: " + remaining);
                    if (chunkID == "RIFF")
                    {
                        WaveFileFormat.Read(binaryReader);
                    }
                    else if (chunkID == "fmt ")
                    {
                        Format.Read(binaryReader, chunkSize);
                    }
                    else if (chunkID == "data")
                    {
                        if (Format.FormatCode != FormatCode.PCM)
                        {
                            Error = "Format is not PCM";
                            return(false);
                        }
                        ReadDataChunk(binaryReader, chunkSize);
                    }
                    else if (chunkID == "bext")
                    { // Broadcast Extension
                        binaryReader.ReadBytes(chunkSize);
                    }
                    else if (chunkID == "JUNK")
                    { //
                        binaryReader.ReadBytes(chunkSize);
                    }
                    else if (chunkID == "ResU")
                    { //
                        binaryReader.ReadBytes(chunkSize);
                    }
                    else if (chunkID == "LIST")
                    { //
                        binaryReader.ReadBytes(chunkSize);
                    }
                    else if (chunkID == "id3 ")
                    { //
                        binaryReader.ReadBytes(chunkSize);
                    }
                    else
                    { // unknown chunk - skip it...
                        if (chunkSize < 0 || chunkSize > remaining)
                        {
                            Error = "Chunk size error";
                            return(false);
                        }
                        binaryReader.ReadBytes(chunkSize);
                    }
                }
            }

            return(true);
        }
예제 #2
0
 public WaveReader()
 {
     Format         = new FormatChunk();
     WaveFileFormat = new WaveFileFormat();
     Fact           = new FactChunk();
 }