AudaciousTunes/Audacious/Wav.cs
/// <summary>
/// Overwrites the wave object from the sampleNum stored in a memory
/// stream. Appends data to the original.
/// </summary>
/// <param name="ms">The existing memory stream with wave data.</param>
Overwrites the wave object from the sampleNum stored in a memory
stream. Appends data to the original.
Overwrites the wave object from the sampleNum stored in a memory stream . Appends data to the original .
private void Load(MemoryStream ms)
        {
            BinaryReader br = new BinaryReader(ms, System.Text.Encoding.ASCII);
            string temp_chunkID = System.Text.Encoding.ASCII.GetString
                (br.ReadBytes(4));
            if (temp_chunkID != "RIFF")
            {
                throw new Exception("Wav load error: RIFF header expected. " +
                "Got " + temp_chunkID + " instead.");
            }
            uint temp_chunkSize = br.ReadUInt32();
            if (temp_chunkSize != br.BaseStream.Length - 8)
            {
                throw new Exception("Wav load error: RIFF size does not " +
                "match expected file size.");
            }
            string temp_format = System.Text.Encoding.ASCII.GetString
                (br.ReadBytes(4));
            if (temp_format != "WAVE")
            {
                throw new Exception("Wav load error: Block must be 'WAVE'." +
                " Got " + temp_format + " instead.");
            }
            List<string> temp_chunksName = new List<string>();
            List<uint> temp_chunksSize = new List<uint>();
            List<long> temp_chunksPos = new List<long>();
            ushort temp_audioFormat = 0;
            ushort temp_numChannels = 0;
            ushort temp_bitsPerSample = 0;
            uint temp_sampRate = 0;
            while (br.BaseStream.Length - br.BaseStream.Position > 4)
            {
                temp_chunksName.Add(System.Text.Encoding.ASCII
                    .GetString(br.ReadBytes(4)));
                temp_chunksSize.Add(br.ReadUInt32());
                temp_chunksPos.Add(br.BaseStream.Position);
                if (br.BaseStream.Position + temp_chunksSize.Last() > br.BaseStream.Length)
                {
                    throw new Exception("A chunk size doesn't match.");
                }
                br.BaseStream.Position += temp_chunksSize.Last();
            }
            for (int i = 0; i < temp_chunksName.Count; i++)
            {
                if (temp_chunksName[i] == "fmt ")
                {
                    br.BaseStream.Seek(temp_chunksPos[i], SeekOrigin.Begin);
                    temp_audioFormat = br.ReadUInt16();
                    if (temp_audioFormat != 1)
                    {
                        throw new Exception("The wav file must be " +
                            "uncompressed (linear quantization). Got " +
                        "code " + temp_audioFormat + " instead.");
                    }
                    temp_numChannels = br.ReadUInt16();
                    if (temp_numChannels != 1 && temp_numChannels != 2)
                    {
                        throw new Exception("The wav file must have " +
                            "mono or stereo channels only. Channels: " +
                        temp_numChannels + ".");
                    }
                    temp_sampRate = br.ReadUInt32();
                    if (temp_sampRate < 1)
                    {
                        throw new Exception("Sample rate must be greater " +
                        "than zero. Got " + temp_sampRate + " instead.");
                    }
                    br.ReadUInt32(); 
                    br.ReadUInt16(); 
                    temp_bitsPerSample = br.ReadUInt16();
                    if (temp_bitsPerSample != 8 &&
                        temp_bitsPerSample != 16 &&
                        temp_bitsPerSample != 24 &&
                        temp_bitsPerSample != 32)
                    {
                        throw new Exception("The wav file must have a " +
                        "bit depth of 8, 16, 24, or 32. Bit depth: " +
                        temp_bitsPerSample + ".");
                    }
                }
                else if (i == temp_chunksName.Count)
                {
                    string mssg = "'fmt ' chunk not found. Chunks: ";
                    foreach (string chunk in temp_chunksName)
                    {
                        mssg += chunk;
                    }
                    throw new Exception(mssg);
                }
            }
            for (int i = 0; i < temp_chunksName.Count; i++)
            {
                if (temp_chunksName[i] == "data")
                {
                    br.BaseStream.Seek(temp_chunksPos[i], SeekOrigin.Begin);
                    if (br.BaseStream.Position + temp_chunksSize[i] >
                        br.BaseStream.Length)
                    {
                        throw new Exception("File size invalid: 'data'.");
                    }
                    List<byte> rawData = new List<byte>();
                    rawData.AddRange(br.ReadBytes((int)temp_chunksSize[i]));
                    Add(Construct(rawData,
                        temp_numChannels,
                        temp_bitsPerSample,
                        (int)temp_sampRate));
                }
                else if (i == temp_chunksName.Count - 1)
                {
                    string mssg = "'data' chunk not found. Chunks: ";
                    foreach (string chunk in temp_chunksName)
                    {
                        mssg += chunk;
                    }
                    throw new Exception(mssg);
                }
            }
            br.Dispose();
        }
private void Load ( MemoryStream ms ) { BinaryReader br = new BinaryReader ( ms , System . Text . Encoding . ASCII ) ; string temp_chunkID = System . Text . Encoding . ASCII . GetString ( br . ReadBytes ( 4 ) ) ; if ( temp_chunkID != " " ) { throw new Exception ( " " + " " + temp_chunkID + " " ) ; } uint temp_chunkSize = br . ReadUInt32 ( ) ; if ( temp_chunkSize != br . BaseStream . Length - 8 ) { throw new Exception ( " " + " " ) ; } string temp_format = System . Text . Encoding . ASCII . GetString ( br . ReadBytes ( 4 ) ) ; if ( temp_format != " " ) { throw new Exception ( " " + " " + temp_format + " " ) ; } List < string > temp_chunksName = new List < string > ( ) ; List < uint > temp_chunksSize = new List < uint > ( ) ; List < long > temp_chunksPos = new List < long > ( ) ; ushort temp_audioFormat = 0 ; ushort temp_numChannels = 0 ; ushort temp_bitsPerSample = 0 ; uint temp_sampRate = 0 ; while ( br . BaseStream . Length - br . BaseStream . Position > 4 ) { temp_chunksName . Add ( System . Text . Encoding . ASCII . GetString ( br . ReadBytes ( 4 ) ) ) ; temp_chunksSize . Add ( br . ReadUInt32 ( ) ) ; temp_chunksPos . Add ( br . BaseStream . Position ) ; if ( br . BaseStream . Position + temp_chunksSize . Last ( ) > br . BaseStream . Length ) { throw new Exception ( " " ) ; } br . BaseStream . Position += temp_chunksSize . Last ( ) ; } for ( int i = 0 ; i < temp_chunksName . Count ; i ++ ) { if ( temp_chunksName [ i ] == " " ) { br . BaseStream . Seek ( temp_chunksPos [ i ] , SeekOrigin . Begin ) ; temp_audioFormat = br . ReadUInt16 ( ) ; if ( temp_audioFormat != 1 ) { throw new Exception ( " " + " " + " " + temp_audioFormat + " " ) ; } temp_numChannels = br . ReadUInt16 ( ) ; if ( temp_numChannels != 1 && temp_numChannels != 2 ) { throw new Exception ( " " + " " + temp_numChannels + " " ) ; } temp_sampRate = br . ReadUInt32 ( ) ; if ( temp_sampRate < 1 ) { throw new Exception ( " " + " " + temp_sampRate + " " ) ; } br . ReadUInt32 ( ) ; br . ReadUInt16 ( ) ; temp_bitsPerSample = br . ReadUInt16 ( ) ; if ( temp_bitsPerSample != 8 && temp_bitsPerSample != 16 && temp_bitsPerSample != 24 && temp_bitsPerSample != 32 ) { throw new Exception ( " " + " " + temp_bitsPerSample + " " ) ; } } else if ( i == temp_chunksName . Count ) { string mssg = " " ; foreach ( string chunk in temp_chunksName ) { mssg += chunk ; } throw new Exception ( mssg ) ; } } for ( int i = 0 ; i < temp_chunksName . Count ; i ++ ) { if ( temp_chunksName [ i ] == " " ) { br . BaseStream . Seek ( temp_chunksPos [ i ] , SeekOrigin . Begin ) ; if ( br . BaseStream . Position + temp_chunksSize [ i ] > br . BaseStream . Length ) { throw new Exception ( " " ) ; } List < byte > rawData = new List < byte > ( ) ; rawData . AddRange ( br . ReadBytes ( ( int ) temp_chunksSize [ i ] ) ) ; Add ( Construct ( rawData , temp_numChannels , temp_bitsPerSample , ( int ) temp_sampRate ) ) ; } else if ( i == temp_chunksName . Count - 1 ) { string mssg = " " ; foreach ( string chunk in temp_chunksName ) { mssg += chunk ; } throw new Exception ( mssg ) ; } } br . Dispose ( ) ; }
Overwrites the wave object from the sampleNum stored in a memory
stream.
Overwrites the wave object from the sampleNum stored in a memory stream .
//Loads the data into a stream for a binary reader. //Attempts to read the RIFF header of the first chunk. //Attempts to read the size of the first chunk. //Attempts to read the format from the first chunk. //Attempts to begin organizing chunk locations. //Creates variables to store the names and sizes of all chunks. //Reads to the end of the data to find each block.             //Defines variables here to remain in scope. //Attempts to read the chunk's name. //Attempts to read the chunk's size. //Stores the chunk's position. //Skips the rest of the known chunk. //Operates on the 'fmt ' chunk. //If the chunk exists. //Seeks to the beginning of the chunk after the ID and //Size segments. //Tests for supported audio compression formats. //Tests for supported channel formats. //Tests for possible sample rate. //Reads past Byte Rate //Reads past Block Align //Tests for supported bitPerSample formats. //If the chunk doesn't exist. //Operates on the 'data' chunk. //If the chunk exists. //Reads all of the data into a byte list. //Constructs a wave from the given data. //If the chunk doesn't exist. //Ensures the binary reader is closed.
ms
MemoryStream
The existing memory stream with wave data.
The existing memory stream with wave data .