Represents a decoder to decode specific audio format.
Наследование: IDisposable
Пример #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Music"/> class.
 /// </summary>
 public Music()
     : base()
 {
     _reader      = null;
     _sampleCount = 0;
     _duration    = TimeSpan.Zero;
 }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Music"/> class
        /// from specified <see cref="Stream"/> containing sound data.
        /// </summary>
        /// <param name="stream">A <see cref="Stream"/> contains sound data to load.</param>
        public Music(Stream stream)
            : this()
        {
            _reader = Decoders.CreateReader(stream);
            if (_reader == null)
            {
                throw new NotSupportedException("The specified sound is not supported.");
            }

            _info = _reader.Open(stream);
            Initialize(_info.ChannelCount, _info.SampleRate);
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SoundDecoder"/> class.
        /// </summary>
        /// <param name="stream"><see cref="Stream"/> containing audio data.</param>
        /// <param name="leaveOpen">Specifies whether the given <see cref="Stream"/> should left open after <see cref="SoundDecoder"/> disposed.</param>
        /// <inheritdoc />
        public SoundDecoder(Stream stream, bool leaveOpen = false)
        {
            // Find a suitable reader for the given audio stream
            reader = SoundProcessorFactory.GetReader(stream, leaveOpen);
            if (reader == null)
            {
                throw new NotSupportedException("Failed to open audio stream. Invalid audio format or not supported.");
            }

            // Retrieve the attributes of the sound
            SampleCount  = reader.SampleInfo.SampleCount;
            ChannelCount = reader.SampleInfo.ChannelCount;
            SampleRate   = reader.SampleInfo.SampleRate;
        }
Пример #4
0
        private void Initialize(SoundReader reader, SampleInfo info)
        {
            // Retrieve the sound parameters
            long sampleCount  = info.SampleCount;
            int  channelCount = info.ChannelCount;
            int  sampleRate   = info.SampleRate;

            // Read the samples from the provided file
            using (reader)
            {
                _samples = new short[sampleCount];
                if (reader.Read(_samples, sampleCount) == sampleCount)
                {
                    // Update the internal buffer with the new samples
                    Update(channelCount, sampleRate);
                }
                else
                {
                    throw new Exception("Failed to decode the sound data.");
                }
            }
        }