Пример #1
0
        /// <summary>
        /// Returns a new wave with the given sample data appended.
        /// </summary>
        /// <param name="wav">An existing wave object.</param>
        /// <param name="samples">Existing sample data.</param>
        public static Wav operator +(Wav wav, List <float> samples)
        {
            Wav result = new Wav(wav);

            result.Add(samples);
            return(result);
        }
Пример #2
0
        /// <summary>
        /// Returns a new wave with the sample data from the second appended.
        /// </summary>
        /// <param name="wav1">An existing wave object.</param>
        /// <param name="wav2">An existing wave object.</param>
        public static Wav operator +(Wav wav1, Wav wav2)
        {
            Wav result = new Wav(wav1);

            result.Add(wav2);
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Returns a new wave object with only a range of the data in it.
        /// </summary>
        /// <param name="index">The index where the samples begin.</param>
        /// <param name="count">The amount of samples to get.</param>
        public Wav GetRange(int index, int count)
        {
            Wav newWav = new Wav(
                _leftChannel.GetRange(index, count),
                _rightChannel.GetRange(index, count));

            return(newWav);
        }
Пример #4
0
        /// <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>
        private static Wav Construct(List <byte> samples,
                                     int channels,
                                     int bitDepth,
                                     int sampRate)
        {
            Wav wav = new Wav();

            //Checks argument validity.
            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.");
            }

            //Holds sampleNum throughout construction.
            List <float> sampleData = new List <float>();

            //If the sample alignment is off, this fixes it.
            while (samples.Count % (bitDepth / 8) != 0)
            {
                samples.Add(0);
            }

            //Converts the raw data into sampleNum in groups based on bit depth.
            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); //Promotes to 4 bytes for ease.
                    sampleData.Add((float)BitConverter.ToSingle
                                       (samples.GetRange(i, 2).ToArray(), 0));
                }
            }
            else //bit depth of 32.
            {
                for (int i = 0; i < samples.Count; i += 4)
                {
                    sampleData.Add(BitConverter.ToSingle
                                       (samples.GetRange(i, 4).ToArray(), 0));
                }
            }

            //De-interleaves the data into the wave.
            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 the sample sampleNum is even, adds it to the left.
                        //If the sample sampleNum is odd, adds it to the right.
                        if (i % 2 == 0)
                        {
                            wav._leftChannel.Add(sampleData.Last());
                        }
                        else
                        {
                            wav._rightChannel.Add(sampleData.Last());
                        }
                    }
                }
            }

            return(wav);
        }
Пример #5
0
 /// <summary>
 /// Creates a wav object from the samples of a wav.
 /// </summary>
 /// <param name="wav">An existing wav object.</param>
 public Wav(Wav wav) : this()
 {
     _leftChannel.AddRange(wav._leftChannel);
     _rightChannel.AddRange(wav._rightChannel);
 }
Пример #6
0
 /// <summary>
 /// Copies samples from the wave object to the given index.
 /// </summary>
 /// <param name="wav">An existing wave object.</param>
 /// <param name="index">The position to insert after.</param>
 public void Insert(Wav wav, int index)
 {
     _leftChannel.InsertRange(index, wav._leftChannel);
     _rightChannel.InsertRange(index, wav._rightChannel);
 }
Пример #7
0
 /// <summary>
 /// Copies samples from a wave object to both channels.
 /// </summary>
 /// <param name="wav">An existing wave object.</param>
 public void Add(Wav wav)
 {
     _leftChannel.AddRange(wav._leftChannel);
     _rightChannel.AddRange(wav._rightChannel);
 }