AudaciousTunes/Audacious/Wav.cs
/// <summary>
/// Constructs a wave object from the byte data.
/// </summary>
/// <param name="samples">The raw byte data.</param>
/// <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>
Constructs a wave object from the byte data.
Constructs a wave object from the byte data .
private static Wav Construct(List<byte> samples,
            int channels,
            int bitDepth,
            int sampRate)
        {
            Wav wav = new Wav();
            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.");
            }
            List<float> sampleData = new List<float>();
            while (samples.Count % (bitDepth / 8) != 0)
            {
                samples.Add(0);
            }
            if (bitDepth == 8)
            {
                for (int i = 0; i < samples.Count; i++)
                {
                    sampleData.Add((float)samples[i]);
                }
            }
            else if (bitDepth == 16)
            {
                for (int i = 0; i < samples.Count; i += 2)
                {
                    sampleData.Add((float)BitConverter.ToInt16
                        (samples.GetRange(i, 2).ToArray(), 0));
                }
            }
            else if (bitDepth == 24)
            {
                for (int i = 0; i < samples.Count; i += 3)
                {
                    List<byte> byteArray = samples.GetRange(i, 3);
                    byteArray.Add(0); 
                    sampleData.Add((float)BitConverter.ToSingle
                        (samples.GetRange(i, 2).ToArray(), 0));
                }
            }
            else 
            {
                for (int i = 0; i < samples.Count; i += 4)
                {
                    sampleData.Add(BitConverter.ToSingle
                        (samples.GetRange(i, 4).ToArray(), 0));
                }
            }
            if (channels == 1)
            {
                for (int i = 0; i < sampleData.Count; i++)
                {
                    if (channels == 1)
                    {
                        wav._leftChannel.Add(sampleData[i]);
                        wav._rightChannel.Add(sampleData[i]);
                    }
                    else
                    {
                        if (i % 2 == 0)
                        {
                            wav._leftChannel.Add(sampleData.Last());
                        }
                        else
                        {
                            wav._rightChannel.Add(sampleData.Last());
                        }
                    }
                }
            }
            return wav;
        }
private static Wav Construct ( List < byte > samples , int channels , int bitDepth , int sampRate ) { Wav wav = new Wav ( ) ; 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 ( " " + " " ) ; } List < float > sampleData = new List < float > ( ) ; while ( samples . Count % ( bitDepth / 8 ) != 0 ) { samples . Add ( 0 ) ; } if ( bitDepth == 8 ) { for ( int i = 0 ; i < samples . Count ; i ++ ) { sampleData . Add ( ( float ) samples [ i ] ) ; } } else if ( bitDepth == 16 ) { for ( int i = 0 ; i < samples . Count ; i += 2 ) { sampleData . Add ( ( float ) BitConverter . ToInt16 ( samples . GetRange ( i , 2 ) . ToArray ( ) , 0 ) ) ; } } else if ( bitDepth == 24 ) { for ( int i = 0 ; i < samples . Count ; i += 3 ) { List < byte > byteArray = samples . GetRange ( i , 3 ) ; byteArray . Add ( 0 ) ; sampleData . Add ( ( float ) BitConverter . ToSingle ( samples . GetRange ( i , 2 ) . ToArray ( ) , 0 ) ) ; } } else { for ( int i = 0 ; i < samples . Count ; i += 4 ) { sampleData . Add ( BitConverter . ToSingle ( samples . GetRange ( i , 4 ) . ToArray ( ) , 0 ) ) ; } } if ( channels == 1 ) { for ( int i = 0 ; i < sampleData . Count ; i ++ ) { if ( channels == 1 ) { wav . _leftChannel . Add ( sampleData [ i ] ) ; wav . _rightChannel . Add ( sampleData [ i ] ) ; } else { if ( i % 2 == 0 ) { wav . _leftChannel . Add ( sampleData . Last ( ) ) ; } else { wav . _rightChannel . Add ( sampleData . Last ( ) ) ; } } } } return wav ; }
Constructs a wave object from the byte data.
Constructs a wave object from the byte data .
//Checks argument validity. //Holds sampleNum throughout construction. //If the sample alignment is off, this fixes it. //Converts the raw data into sampleNum in groups based on bit depth. //Promotes to 4 bytes for ease. //bit depth of 32. //De-interleaves the data into the wave. //If the sample sampleNum is even, adds it to the left. //If the sample sampleNum is odd, adds it to the right.
samples
List<byte>
The raw byte data.
The raw byte data .
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 .