/// <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));
        }
        /// <summary>
        /// Reads the chunk containing the waveform data from the underlying stream and returns
        /// a streaming waveform. The underlying stream remains open until the object returned
        /// here is disposed.
        /// </summary>
        /// <param name="format">The waveform format.</param>
        /// <returns>A streaming waveform that should be disposed after use.</returns>
        private StreamWaveform ReadDataChunk(WaveformFormat format)
        {
            var chunkHeader = ReadChunkHeader(WaveformFileFormat.DataChunkId);
            var frameCount  = chunkHeader.PayloadSize / format.FrameSize;

            var frameStream = new FrameStream(_stream, format, frameCount);

            return(new StreamWaveform(format, frameCount, frameStream));
        }
        /// <summary>
        /// Writes the chunk describing the waveform format to the underlying stream.
        /// </summary>
        /// <param name="waveFormFormat">The waveform format.</param>
        private void WriteFormatChunk(WaveformFormat waveFormFormat)
        {
            var chunkSize   = WaveformFileFormat.FormatChunkPayloadSize;
            var chunkHeader = new WaveformFileFormat.ChunkHeader(WaveformFileFormat.FormatChunkId, chunkSize);

            WriteChunkHeader(chunkHeader);

            _stream.WriteInt16(WaveformFileFormat.PcmFormatTag);
            _stream.WriteInt16(waveFormFormat.ChannelsCount);
            _stream.WriteInt32(waveFormFormat.SamplesPerSecond);
            _stream.WriteInt32(waveFormFormat.BytesPerSecond);
            _stream.WriteInt16(waveFormFormat.FrameSize);
            _stream.WriteInt16(waveFormFormat.BitsPerSample);
        }
Example #4
0
 /// <summary>
 /// Initializes an instance of this class.
 /// </summary>
 /// <param name="format">The format of the waveform.</param>
 public WaveformBase(WaveformFormat format)
 {
     Format = format;
 }
Example #5
0
 /// <summary>
 /// Initializes an instance of this class.
 /// </summary>
 /// <param name="format">The format of the waveform.</param>
 /// <param name="frameCount">The number of frames within the waveform.</param>
 /// <param name="frameStream">The stream providing the waveform's frames.</param>
 public StreamWaveform(WaveformFormat format, int frameCount, IFrameStream frameStream)
     : base(format)
 {
     _frameCount  = frameCount;
     _frameStream = frameStream;
 }
Example #6
0
 /// <summary>
 /// Initializes an instance of this class.
 /// </summary>
 /// <param name="format">The format of the waveform.</param>
 /// <param name="frames">The frames within the waveform.</param>
 public MemoryWaveform(WaveformFormat format, IEnumerable <IWaveformFrame> frames)
     : base(format)
 {
     _frames = frames;
 }
 /// <summary>
 /// Initializes an instance of this class.
 /// </summary>
 /// <param name="stream">The stream the frames are read from. The stream's
 /// current position must be right before the frame bytes.</param>
 /// <param name="format">The format of the waveform.</param>
 /// <param name="frameCount">The number of frames within the waveform.</param>
 public FrameStream(Stream stream, WaveformFormat format, int frameCount)
 {
     _stream     = stream;
     _format     = format;
     _frameCount = frameCount;
 }