Esempio n. 1
0
        /// <summary>
        /// Loads previously generated peaks from disk.
        /// </summary>
        internal WavePeakData LoadPeaks()
        {
            if (_header.BitsPerSample != 16)
            {
                throw new Exception("Peaks file must be 16 bits per sample.");
            }

            if (_header.NumberOfChannels != 1 && _header.NumberOfChannels != 2)
            {
                throw new Exception("Peaks file must have 1 or 2 channels.");
            }

            // load data
            byte[] data = new byte[_header.DataChunkSize];
            _stream.Position = _header.DataStartPosition;
            _stream.Read(data, 0, data.Length);

            // read peak values
            WavePeak[] peaks     = new WavePeak[_header.LengthInSamples];
            int        peakIndex = 0;

            if (_header.NumberOfChannels == 2)
            {
                // max value in left channel, min value in right channel
                int byteIndex = 0;
                while (byteIndex < data.Length)
                {
                    short max = (short)ReadValue16Bit(data, ref byteIndex);
                    short min = (short)ReadValue16Bit(data, ref byteIndex);
                    peaks[peakIndex++] = new WavePeak(max, min);
                }
            }
            else
            {
                // single sample value (for backwards compatibility)
                int byteIndex = 0;
                while (byteIndex < data.Length)
                {
                    short value = (short)ReadValue16Bit(data, ref byteIndex);
                    if (value == short.MinValue)
                    {
                        value = -short.MaxValue;
                    }

                    value = Math.Abs(value);
                    peaks[peakIndex++] = new WavePeak(value, (short)-value);
                }
            }

            return(new WavePeakData(_header.SampleRate, peaks));
        }
        /// <summary>
        /// Loads previously generated peaks from disk.
        /// </summary>
        internal WavePeakData LoadPeaks()
        {
            if (_header.BitsPerSample != 16)
                throw new Exception("Peaks file must be 16 bits per sample.");

            if (_header.NumberOfChannels != 1 && _header.NumberOfChannels != 2)
                throw new Exception("Peaks file must have 1 or 2 channels.");

            // load data
            byte[] data = new byte[_header.DataChunkSize];
            _stream.Position = _header.DataStartPosition;
            _stream.Read(data, 0, data.Length);

            // read peak values
            WavePeak[] peaks = new WavePeak[_header.LengthInSamples];
            int peakIndex = 0;
            if (_header.NumberOfChannels == 2)
            {
                // max value in left channel, min value in right channel
                int byteIndex = 0;
                while (byteIndex < data.Length)
                {
                    short max = (short)ReadValue16Bit(data, ref byteIndex);
                    short min = (short)ReadValue16Bit(data, ref byteIndex);
                    peaks[peakIndex++] = new WavePeak(max, min);
                }
            }
            else if (_header.NumberOfChannels == 1)
            {
                // single sample value (for backwards compatibility)
                int byteIndex = 0;
                while (byteIndex < data.Length)
                {
                    short value = (short)ReadValue16Bit(data, ref byteIndex);
                    if (value == short.MinValue)
                        value = -short.MaxValue;
                    value = Math.Abs(value);
                    peaks[peakIndex++] = new WavePeak(value, (short)-value);
                }
            }

            return new WavePeakData(_header.SampleRate, peaks);
        }