Exemple #1
0
        void Empty()
        {
            int queued;

            AL.GetSource(sourceId, ALGetSourcei.BuffersQueued, out queued);
            if (queued > 0)
            {
                try
                {
                    AL.SourceUnqueueBuffers(sourceId, queued);
                    AudioDevice.CheckALError();
                }
                catch (InvalidOperationException)
                {
                    //work around OpenAL bug
                    int processed;
                    AL.GetSource(sourceId, ALGetSourcei.BuffersProcessed, out processed);
                    var salvaged = new int[processed];
                    if (processed > 0)
                    {
                        AL.SourceUnqueueBuffers(sourceId, processed, salvaged);
                        AudioDevice.CheckALError();
                    }
                    AL.SourceStop(sourceId);
                    AudioDevice.CheckALError();
                    Empty();
                }
            }
        }
Exemple #2
0
 public void Play()
 {
     if (currentState == AudioPlayState.Playing)
     {
         return;
     }
     if (currentState == AudioPlayState.Stopped)
     {
         finished     = false;
         currentState = AudioPlayState.Playing;
         for (int i = 0; i < bufferIds.Length; i++)
         {
             int length = BufferNeeded(this, buffer);
             AL.BufferData(bufferIds [i], bufferFormat, buffer, length, sampleRate);
             AudioDevice.CheckALError();
             AL.SourceQueueBuffer(sourceId, bufferIds [i]);
             AudioDevice.CheckALError();
             AL.SourcePlay(sourceId);
             AudioDevice.CheckALError();
         }
         device.Add(this);
         threadRunning = true;
     }
     currentState = AudioPlayState.Playing;
 }
Exemple #3
0
        internal StreamingAudio(AudioDevice device, ALFormat format, int sampleRate)
        {
            bufferFormat = format;
            while (!device.ready)
            {
                ;
            }
            var sid = AL.GenSource();

            sourceId = (int)sid;
            AudioDevice.CheckALError();
            bufferIds = AL.GenBuffers(4);
            AudioDevice.CheckALError();
            this.device     = device;
            this.sampleRate = sampleRate;
        }
Exemple #4
0
        internal void Update()
        {
            //manage state
            if (currentState == AudioPlayState.Stopped)
            {
                AL.SourceStop(sourceId);
                device.Remove(this);
                threadRunning = false;
                if (!userStopped)
                {
                    if (PlaybackFinished != null)
                    {
                        PlaybackFinished(this, true);
                    }
                }
                userStopped = false;
                return;
            }
            var state = AL.GetSourceState(sourceId);

            AudioDevice.CheckALError();
            if (currentState == AudioPlayState.Paused)
            {
                if (state != ALSourceState.Paused)
                {
                    AL.SourcePause(sourceId);
                }
                return;
            }

            //load buffers
            int processed_count;

            AL.GetSource(sourceId, ALGetSourcei.BuffersProcessed, out processed_count);
            while (processed_count > 0)
            {
                int bid = AL.SourceUnqueueBuffer(sourceId);
                if (bid != 0 && !finished)
                {
                    int length = BufferNeeded(this, buffer);
                    finished = length <= 0;
                    if (!finished)
                    {
                        AL.BufferData(bid, bufferFormat, buffer, length, sampleRate);
                        AL.SourceQueueBuffer(sourceId, bid);
                    }
                }
                --processed_count;
            }
            //check buffer
            if (state == ALSourceState.Stopped && !finished)
            {
                AL.SourcePlay(sourceId);
            }
            //are we finished?
            if (finished && state == ALSourceState.Stopped)
            {
                device.Remove(this);
                currentState  = AudioPlayState.Stopped;
                threadRunning = false;
                Empty();
                if (PlaybackFinished != null)
                {
                    PlaybackFinished(this, false);
                }
            }
        }