Exemplo n.º 1
0
        private void Refill(int count)
        {
            lock (locker)
            {
                if (source.Position >= source.TotalSamples)
                {
                    if (isLooping)
                    {
                        source.Position = 0;
                    }
                    else
                    {
                        return;
                    }
                }

                var state = AL.GetSourceState(IDs[0]);
                for (int a = 0; a < count; a++)
                {
                    var     sampleRate   = source.SampleRate;
                    var     bufferCount  = Sound.Supports3D ? source.Channels : 1;
                    var     channelCount = Sound.Supports3D ? 1 : source.Channels;
                    float[] samples;
                    samples = source.ReadSamples(RINGBUFFER_SAMPLES);
                    if (samples.Length == 0)
                    {
                        return;
                    }

                    short[] samplesChannel = new short[samples.Length / bufferCount];
                    for (int i = 0; i < bufferCount; i++)
                    {
                        SampleConverter.To16Bit(samples, samplesChannel, i, bufferCount);
                        var buffer = ringbuffers[i, ringBufferIndex];
                        buffer.SetData(AudioFormat.Short16, channelCount, samplesChannel, sampleRate);
                        AL.SourceQueueBuffer(IDs[i], buffer.ID);
                    }
                    ringBufferIndex = (ringBufferIndex + 1) % RINGBUFFER_COUNT;
                }
                var newState = AL.GetSourceState(IDs[0]);
                if (newState != ALSourceState.Playing && state == ALSourceState.Playing)
                {
                    Play();
                }
            }
        }
Exemplo n.º 2
0
        internal Sound(AudioDevice audioDevice, string file, SoundFlags flags) : base(audioDevice)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException("file");
            }
            if (!System.IO.File.Exists(file))
            {
                throw new System.IO.FileNotFoundException("file", file);
            }

            this.File       = file;
            this.Flags      = flags;
            this.Supports3D = flags.HasFlag(SoundFlags.Support3D);
            this.IsStreamed = flags.HasFlag(SoundFlags.Streamed);
            this.AllowRead  = flags.HasFlag(SoundFlags.AllowRead);

            instances = new List <WeakReference <SoundInstance> >();

            if (!IsStreamed)
            {
                using (var source = SampleSourceFactory.FromFile(file))
                {
                    var bufferCount  = Supports3D ? source.Channels : 1;
                    var channelCount = Supports3D ? 1 : source.Channels;
                    var sampleRate   = source.SampleRate;
                    var samples      = source.ReadAll();

                    buffers = new List <AudioBuffer <short> >();
                    short[] samplesChannel = new short[samples.Length / bufferCount];
                    for (int i = 0; i < bufferCount; i++)
                    {
                        SampleConverter.To16Bit(samples, samplesChannel, i, bufferCount);
                        var buffer = new AudioBuffer <short>(audioDevice, !AllowRead);
                        buffer.SetData(AudioFormat.Short16, channelCount, samplesChannel, sampleRate);
                        buffers.Add(buffer);
                    }
                }
            }
        }