Ejemplo n.º 1
0
        /// <summary>
        /// Initialize the sound buffer with a <see cref="SoundDecoder"/>.
        /// </summary>
        /// <param name="decoder"><see cref="SoundDecoder"/> containing audio information and sample to fill the current buffer.</param>
        private void Initialize(SoundDecoder decoder)
        {
            // Retrieve the decoder
            Decoder = decoder;

            // Retrieve sound parameters
            SampleCount  = decoder.SampleCount;
            ChannelCount = decoder.ChannelCount;
            SampleRate   = decoder.SampleRate;

            // Compute duration
            Duration = TimeSpan.FromSeconds((float)SampleCount / SampleRate / ChannelCount);

            // Fill entire buffer immediately if its a sample mode
            if (Mode == BufferMode.Sample)
            {
                // Create the buffer handle
                Handle = ALChecker.Check(() => AL.GenBuffer());

                // Decode the samples
                var samples = new short[SampleCount];
                if (decoder.Decode(samples, SampleCount) == SampleCount)
                {
                    // Update the internal buffer with the new samples
                    LoadBuffer(samples, ChannelCount, SampleRate);
                }
                else
                {
                    throw new Exception("Failed to initialize Sample Buffer");
                }
            }
        }
Ejemplo n.º 2
0
 internal Music(int handle, Sound buffer)
     : base(handle, buffer)
 {
     decoder = buffer.Decoder;
     Initialize(buffer.ChannelCount, buffer.SampleRate);
 }