Пример #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="AiffReader" /> class for the specified <paramref name="stream" />.
        /// </summary>
        /// <param name="stream">The stream to be decoded.</param>
        /// <exception cref="System.ArgumentNullException">stream</exception>
        /// <exception cref="System.ArgumentException">
        ///     Stream is not readable.;stream
        ///     or
        ///     Stream is not seekable.;stream
        /// </exception>
        /// <exception cref="CSCore.Codecs.AIFF.AiffException">
        ///     No COMM Chunk found.
        ///     or
        ///     No SSND Chunk found.
        ///     or
        ///     Format not supported.
        /// </exception>
        public AiffReader(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not readable.", "stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is not seekable.", "stream");
            }

            _stream      = stream;
            _closeStream = true;

            _chunkContainer = new AiffChunkContainer(new BinaryReader(_stream));
            var commonChunk = (CommonChunk)_chunkContainer.Chunks.FirstOrDefault(x => x is CommonChunk);

            if (commonChunk == null)
            {
                throw new AiffException("No COMM Chunk found.");
            }
            _soundDataChunk =
                (SoundDataChunk)_chunkContainer.Chunks.FirstOrDefault(x => x is SoundDataChunk);
            if (_soundDataChunk == null)
            {
                throw new AiffException("No SSND Chunk found.");
            }

            WaveFormat = commonChunk.GetWaveFormat();
            if (WaveFormat.BitsPerSample != 8 &&
                WaveFormat.BitsPerSample != 16 &&
                WaveFormat.BitsPerSample != 24 &&
                WaveFormat.BitsPerSample != 32)
            {
                throw new AiffException("Format not supported.",
                                        new NotSupportedException(
                                            string.Format("BitsPerSample of {0} is not supported. Must be 8, 16, 24 or 32 bits.",
                                                          WaveFormat.BitsPerSample)));
            }

            //Length = commonChunk.NumberOfSampleFrames * WaveFormat.BlockAlign;
            //some encoders won't set the NumberOfSampleFrames field ... better prefer:
            Length = _soundDataChunk.DataSize - 8; //sub the 2 SSND fields

            Position = 0;
        }
Пример #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="AiffReader" /> class for the specified <paramref name="stream" />.
        /// </summary>
        /// <param name="stream">The stream to be decoded.</param>
        /// <exception cref="System.ArgumentNullException">stream</exception>
        /// <exception cref="System.ArgumentException">
        ///     Stream is not readable.;stream
        ///     or
        ///     Stream is not seekable.;stream
        /// </exception>
        /// <exception cref="CSCore.Codecs.AIFF.AiffException">
        ///     No COMM Chunk found.
        ///     or
        ///     No SSND Chunk found.
        ///     or
        ///     Format not supported.
        /// </exception>
        public AiffReader(Stream stream)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (!stream.CanRead)
                throw new ArgumentException("Stream is not readable.", "stream");
            if (!stream.CanSeek)
                throw new ArgumentException("Stream is not seekable.", "stream");

            _stream = stream;

            _chunkContainer = new AiffChunkContainer(new BinaryReader(_stream));
            var commonChunk = (CommonChunk) _chunkContainer.Chunks.FirstOrDefault(x => x is CommonChunk);
            if (commonChunk == null)
                throw new AiffException("No COMM Chunk found.");
            _soundDataChunk =
                (SoundDataChunk) _chunkContainer.Chunks.FirstOrDefault(x => x is SoundDataChunk);
            if (_soundDataChunk == null)
                throw new AiffException("No SSND Chunk found.");

            WaveFormat = commonChunk.GetWaveFormat();
            if (WaveFormat.BitsPerSample != 8 &&
                WaveFormat.BitsPerSample != 16 &&
                WaveFormat.BitsPerSample != 24 &&
                WaveFormat.BitsPerSample != 32)
            {
                throw new AiffException("Format not supported.",
                    new NotSupportedException(
                        string.Format("BitsPerSample of {0} is not supported. Must be 8, 16, 24 or 32 bits.",
                            WaveFormat.BitsPerSample)));
            }

            //Length = commonChunk.NumberOfSampleFrames * WaveFormat.BlockAlign;
            //some encoders won't set the NumberOfSampleFrames field ... better prefer:
            Length = _soundDataChunk.DataSize - 8; //sub the 2 SSND fields

            Position = 0;
        }