AudaciousTunes/Audacious/Wav.cs
/// <summary>
/// (Over)writes wave data and sampleNum to a memory stream, saving with
/// the given settings.
/// </summary>
/// <param name="channels">May be 1 (mono) or 2 (stereo).</param>
/// <param name="bitDepth">May be 8, 16, 24, or 32-bit.</param>
/// <param name="sampRate">
/// Samples per second; usually factors or multiples of 44100.
/// </param>
(Over)writes wave data and sampleNum to a memory stream, saving with
the given settings.
( Over ) writes wave data and sampleNum to a memory stream saving with the given settings .
public MemoryStream Serialize(int channels,
            int bitDepth,
            int sampRate)
        {
            MemoryStream ms = new MemoryStream();
            if (channels < 1 || channels > 2)
            {
                throw new ArgumentException("Only mono and stereo channels " +
                    "are supported.");
            }
            if (bitDepth != 8 && bitDepth != 16 &&
                bitDepth != 24 && bitDepth != 32)
            {
                throw new ArgumentException("Only 8, 16, 24, and 32-bit " +
                    "audio is supported.");
            }
            if (sampRate < 0)
            {
                throw new ArgumentException("The sample rate must be " +
                    "positive.");
            }
            int sampleLength = Length();
            uint dataChunkSize = (uint)sampleLength * ((uint)bitDepth / 8);
            uint headerSize = 36 + dataChunkSize;
            ushort blockAlign = (ushort)(channels * (bitDepth / 8));
            ms.Write(ASCIIEncoding.ASCII.GetBytes("RIFF"), 0, 4);
            ms.Write(BitConverter.GetBytes(headerSize), 0, 4);
            ms.Write(ASCIIEncoding.ASCII.GetBytes("WAVE"), 0, 4);
            ms.Write(ASCIIEncoding.ASCII.GetBytes("fmt "), 0, 4);
            ms.Write(BitConverter.GetBytes(16u), 0, 4);
            ms.Write(BitConverter.GetBytes((ushort)1), 0, 2);
            ms.Write(BitConverter.GetBytes((ushort)channels), 0, 2);
            ms.Write(BitConverter.GetBytes((uint)sampRate), 0, 4);
            ms.Write(BitConverter.GetBytes((uint)sampRate * blockAlign), 0, 4);
            ms.Write(BitConverter.GetBytes(blockAlign), 0, 2);
            ms.Write(BitConverter.GetBytes((ushort)bitDepth), 0, 2);
            ms.Write(ASCIIEncoding.ASCII.GetBytes("data"), 0, 4);
            ms.Write(BitConverter.GetBytes(dataChunkSize), 0, 4);
            for (int i = 0; i < sampleLength; i++)
            {
                if (i >= _leftChannel.Count)
                {
                    _leftChannel.Add(0);
                }
                if (i >= _rightChannel.Count)
                {
                    _rightChannel.Add(0);
                }
                if (bitDepth == 8)
                {
                    ms.Write(BitConverter.GetBytes((byte)_leftChannel[i]), 0, 2);
                    if (channels == 2)
                    {
                        ms.Write(
                            BitConverter.GetBytes((byte)_rightChannel[i]), 0, 2);
                    }
                }
                else if (bitDepth == 16)
                {
                    ms.Write(BitConverter.GetBytes((short)_leftChannel[i]), 0, 2);
                    if (channels == 2)
                    {
                        ms.Write(
                            BitConverter.GetBytes((short)_rightChannel[i]), 0, 2);
                    }
                }
                else if (bitDepth == 24)
                {
                    throw new NotImplementedException();
                }
                else if (bitDepth == 32)
                {
                    ms.Write(BitConverter.GetBytes(_leftChannel[i]), 0, 2);
                    if (channels == 2)
                    {
                        ms.Write(BitConverter.GetBytes(_rightChannel[i]), 0, 2);
                    }
                }
            }
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
public MemoryStream Serialize ( int channels , int bitDepth , int sampRate ) { MemoryStream ms = new MemoryStream ( ) ; if ( channels < 1 || channels > 2 ) { throw new ArgumentException ( " " + " " ) ; } if ( bitDepth != 8 && bitDepth != 16 && bitDepth != 24 && bitDepth != 32 ) { throw new ArgumentException ( " " + " " ) ; } if ( sampRate < 0 ) { throw new ArgumentException ( " " + " " ) ; } int sampleLength = Length ( ) ; uint dataChunkSize = ( uint ) sampleLength * ( ( uint ) bitDepth / 8 ) ; uint headerSize = 36 + dataChunkSize ; ushort blockAlign = ( ushort ) ( channels * ( bitDepth / 8 ) ) ; ms . Write ( ASCIIEncoding . ASCII . GetBytes ( " " ) , 0 , 4 ) ; ms . Write ( BitConverter . GetBytes ( headerSize ) , 0 , 4 ) ; ms . Write ( ASCIIEncoding . ASCII . GetBytes ( " " ) , 0 , 4 ) ; ms . Write ( ASCIIEncoding . ASCII . GetBytes ( " " ) , 0 , 4 ) ; ms . Write ( BitConverter . GetBytes ( 16u ) , 0 , 4 ) ; ms . Write ( BitConverter . GetBytes ( ( ushort ) 1 ) , 0 , 2 ) ; ms . Write ( BitConverter . GetBytes ( ( ushort ) channels ) , 0 , 2 ) ; ms . Write ( BitConverter . GetBytes ( ( uint ) sampRate ) , 0 , 4 ) ; ms . Write ( BitConverter . GetBytes ( ( uint ) sampRate * blockAlign ) , 0 , 4 ) ; ms . Write ( BitConverter . GetBytes ( blockAlign ) , 0 , 2 ) ; ms . Write ( BitConverter . GetBytes ( ( ushort ) bitDepth ) , 0 , 2 ) ; ms . Write ( ASCIIEncoding . ASCII . GetBytes ( " " ) , 0 , 4 ) ; ms . Write ( BitConverter . GetBytes ( dataChunkSize ) , 0 , 4 ) ; for ( int i = 0 ; i < sampleLength ; i ++ ) { if ( i >= _leftChannel . Count ) { _leftChannel . Add ( 0 ) ; } if ( i >= _rightChannel . Count ) { _rightChannel . Add ( 0 ) ; } if ( bitDepth == 8 ) { ms . Write ( BitConverter . GetBytes ( ( byte ) _leftChannel [ i ] ) , 0 , 2 ) ; if ( channels == 2 ) { ms . Write ( BitConverter . GetBytes ( ( byte ) _rightChannel [ i ] ) , 0 , 2 ) ; } } else if ( bitDepth == 16 ) { ms . Write ( BitConverter . GetBytes ( ( short ) _leftChannel [ i ] ) , 0 , 2 ) ; if ( channels == 2 ) { ms . Write ( BitConverter . GetBytes ( ( short ) _rightChannel [ i ] ) , 0 , 2 ) ; } } else if ( bitDepth == 24 ) { throw new NotImplementedException ( ) ; } else if ( bitDepth == 32 ) { ms . Write ( BitConverter . GetBytes ( _leftChannel [ i ] ) , 0 , 2 ) ; if ( channels == 2 ) { ms . Write ( BitConverter . GetBytes ( _rightChannel [ i ] ) , 0 , 2 ) ; } } } ms . Flush ( ) ; ms . Seek ( 0 , SeekOrigin . Begin ) ; return ms ; }
(Over)writes wave data and sampleNum to a memory stream, saving with
the given settings.
( Over ) writes wave data and sampleNum to a memory stream saving with the given settings .
//Creates a new memory stream instance.             //Checks argument validity. //Calculates variable info. //Writes the RIFF header: chunk id, header size, and format. /* Writes the format block with the following info, in order:
             chunk id, header size, audio format (PCM), sampleNum of channels,
             sample rate, byte rate, block alignment, and bits per sample. */ //Writes the data block header and size information. //Pads each channel with silence until they have equal size. //Appends silence if the index is out of bounds. //todo: Change the range of data before it's casted to another //type so that you get the proportionate range (w/o errors). //todo: Save floats as ints and remove the last bit. //Ensures the data has been written and returns the MemoryStream.
channels
int
May be 1 (mono) or 2 (stereo).
May be 1 ( mono ) or 2 ( stereo ) .
bitDepth
int
None
sampRate
int
Samples per second; usually factors or multiples of 44100.
Samples per second ; usually factors or multiples of 44100 .