Ejemplo n.º 1
0
        /// <summary>
        /// Reads the chunk describing the waveform format from the underlying stream.
        /// </summary>
        /// <returns>The waveform format.</returns>
        private WaveformFormat ReadFormatChunk()
        {
            var chunkHeader = ReadChunkHeader(WaveformFileFormat.FormatChunkId);

            if (chunkHeader.PayloadSize != WaveformFileFormat.FormatChunkPayloadSize)
            {
                throw new InvalidDataException("Unexpected format chunk size.");
            }

            var formatTag = _stream.ReadAsInt16();

            if (formatTag != WaveformFileFormat.PcmFormatTag)
            {
                throw new InvalidDataException("Unsupported format.");
            }

            var channelsCount    = _stream.ReadAsInt16();
            var samplesPerSecond = _stream.ReadAsInt32();
            var bytesPerSecond   = _stream.ReadAsInt32();
            var frameSize        = _stream.ReadAsInt16();
            var bitsPerSample    = _stream.ReadAsInt16();

            var sampleSize             = WaveformFormat.GetSampleSize(bitsPerSample);
            var expectedFrameSize      = WaveformFormat.GetFrameSize(channelsCount, sampleSize);
            var expectedBytesPerSecond = WaveformFormat.GetBytesPerSecond(frameSize, samplesPerSecond);

            if (frameSize != expectedFrameSize ||
                bytesPerSecond != expectedBytesPerSecond)
            {
                throw new InvalidDataException("Inconsistent sizes specified.");
            }

            return(new WaveformFormat(channelsCount, samplesPerSecond, bitsPerSample));
        }