Exemple #1
0
        public void Dispose()
        {
            if (this.IsDisposed)
            {
                return;
            }
            this.IsDisposed = true;
            ALSourceState sourceState = AL.GetSourceState(this.alSourceId);

            if (sourceState == ALSourceState.Playing || sourceState == ALSourceState.Paused)
            {
                this.StopPlayback();
            }
            lock (this.prepareMutex)
            {
                if (OggStreamer.HasInstance)
                {
                    OggStreamer.Instance.RemoveStream(this);
                }
                if (sourceState != ALSourceState.Initial)
                {
                    this.Empty(false);
                }
                this.Close();
                this.underlyingStream.Dispose();
            }
            if (OpenALSoundController.Instance != null)
            {
                OpenALSoundController.Instance.ReturnSource(this.alSourceId);
                OpenALSoundController.Instance.ReturnBuffers(this.alBufferIds);
            }
            ALHelper.Check();
        }
Exemple #2
0
        private int TakeSourceFor(SoundEffect soundEffect, bool filter = false)
        {
            if (this.freeSources.Count == 0)
            {
                this.ExpandSources();
            }
            int source = this.freeSources.Pop();

            if (filter && ALHelper.Efx.IsInitialized)
            {
                ALHelper.Efx.Filter(this.filterId, EfxFilterf.LowpassGainHF, MathHelper.Clamp(this.lowpassGainHf, 0.0f, 1f));
                ALHelper.Efx.BindFilterToSource(source, this.filterId);
                lock (this.filteredSources)
                    this.filteredSources.Add(source);
            }
            OpenALSoundController.BufferAllocation bufferAllocation;
            if (!this.allocatedBuffers.TryGetValue(soundEffect, out bufferAllocation))
            {
                if (this.freeBuffers.Count == 0)
                {
                    this.ExpandBuffers();
                }
                this.allocatedBuffers.Add(soundEffect, bufferAllocation = new OpenALSoundController.BufferAllocation()
                {
                    BufferId = this.freeBuffers.Pop()
                });
                AL.BufferData <byte>(bufferAllocation.BufferId, soundEffect.Format, soundEffect._data, soundEffect.Size, soundEffect.Rate);
                ALHelper.Check();
            }
            ++bufferAllocation.SourceCount;
            AL.BindBufferToSource(source, bufferAllocation.BufferId);
            ALHelper.Check();
            return(source);
        }
Exemple #3
0
        private void Empty(bool giveUp = false)
        {
            int numEntries1;

            AL.GetSource(this.alSourceId, ALGetSourcei.BuffersQueued, out numEntries1);
            if (numEntries1 <= 0)
            {
                return;
            }
            AL.SourceUnqueueBuffers(this.alSourceId, numEntries1);
            if (!ALHelper.TryCheck() && !giveUp)
            {
                int numEntries2;
                AL.GetSource(this.alSourceId, ALGetSourcei.BuffersProcessed, out numEntries2);
                int[] bids = new int[numEntries2];
                if (numEntries2 > 0)
                {
                    AL.SourceUnqueueBuffers(this.alSourceId, numEntries2, bids);
                    ALHelper.Check();
                }
                AL.SourceStop(this.alSourceId);
                ALHelper.Check();
                this.Empty(true);
            }
        }
Exemple #4
0
 internal void Precache(bool asynchronous = false)
 {
     if (!asynchronous)
     {
         OggStreamer.Instance.FillBuffer(this, this.alBufferIds[0]);
         AL.SourceQueueBuffer(this.alSourceId, this.alBufferIds[0]);
         ALHelper.Check();
     }
     OggStreamer.Instance.AddStream(this);
 }
Exemple #5
0
 public void Resume()
 {
     if (AL.GetSourceState(this.alSourceId) != ALSourceState.Paused)
     {
         return;
     }
     OggStreamer.Instance.AddStream(this);
     AL.SourcePlay(this.alSourceId);
     ALHelper.Check();
 }
Exemple #6
0
 public void Pause()
 {
     if (AL.GetSourceState(this.alSourceId) != ALSourceState.Playing)
     {
         return;
     }
     OggStreamer.Instance.RemoveStream(this);
     AL.SourcePause(this.alSourceId);
     ALHelper.Check();
 }
 internal bool RefreshState()
 {
     if (this.soundState != SoundState.Playing || AL.GetSourceState(this.sourceId) != ALSourceState.Stopped)
     {
         return(false);
     }
     ALHelper.Check();
     this.soundState = SoundState.Stopped;
     return(true);
 }
Exemple #8
0
 private void ExpandSources()
 {
     this.totalSources += 16;
     Trace.WriteLine("[OpenAL] Expanding sources to " + (object)this.totalSources);
     int[] numArray = AL.GenSources(16);
     ALHelper.Check();
     foreach (int num in numArray)
     {
         this.freeSources.Push(num);
     }
 }
Exemple #9
0
        private void TidySources()
        {
            bool flag = false;

            if (this.freeSources.Count <= 32)
            {
                return;
            }
            AL.DeleteSource(this.freeSources.Pop());
            ALHelper.Check();
            --this.totalSources;
            flag = true;
        }
Exemple #10
0
        private void TidyBuffers()
        {
            bool flag = false;

            if (this.freeBuffers.Count <= 48)
            {
                return;
            }
            AL.DeleteBuffer(this.freeBuffers.Pop());
            ALHelper.Check();
            --this.totalBuffers;
            flag = true;
        }
Exemple #11
0
        public bool FillBuffer(OggStream stream, int bufferId)
        {
            int length;

            lock (this.readMutex)
            {
                length = stream.Reader.ReadSamples(this.readSampleBuffer, 0, this.BufferSize);
                OggStreamer.CastBuffer(this.readSampleBuffer, this.castBuffer, length);
            }
            AL.BufferData <short>(bufferId, stream.Reader.Channels == 1 ? ALFormat.Mono16 : ALFormat.Stereo16, this.castBuffer, length * 2, stream.Reader.SampleRate);
            ALHelper.Check();
            return(length != this.BufferSize);
        }
 public void Stop(bool immediate = false)
 {
     if (this.isDisposed)
     {
         throw new ObjectDisposedException("SoundEffectInstance (" + this.soundEffect.Name + ")");
     }
     if (this.soundState == SoundState.Stopped)
     {
         return;
     }
     AL.SourceStop(this.sourceId);
     ALHelper.Check();
     this.soundState = SoundState.Stopped;
 }
 public void Play()
 {
     if (this.isDisposed)
     {
         throw new ObjectDisposedException("SoundEffectInstance (" + this.soundEffect.Name + ")");
     }
     if (this.soundState == SoundState.Playing)
     {
         return;
     }
     AL.SourcePlay(this.sourceId);
     ALHelper.Check();
     this.soundState = SoundState.Playing;
 }
Exemple #14
0
 public OggStream(Stream stream, int bufferCount = 3)
 {
     ALHelper.Check();
     this.BufferCount = bufferCount;
     this.alBufferIds = OpenALSoundController.Instance.TakeBuffers(this.BufferCount);
     this.alSourceId  = OpenALSoundController.Instance.TakeSource();
     if (ALHelper.XRam.IsInitialized)
     {
         ALHelper.XRam.SetBufferMode(this.BufferCount, ref this.alBufferIds[0], XRamExtension.XRamStorage.Hardware);
         ALHelper.Check();
     }
     this.Volume           = 1f;
     this.underlyingStream = stream;
 }
Exemple #15
0
 private void ExpandBuffers()
 {
     this.totalBuffers += 24;
     Trace.WriteLine("[OpenAL] Expanding buffers to " + (object)this.totalBuffers);
     int[] numArray = AL.GenBuffers(24);
     ALHelper.Check();
     if (ALHelper.XRam.IsInitialized)
     {
         ALHelper.XRam.SetBufferMode(numArray.Length, ref numArray[0], XRamExtension.XRamStorage.Hardware);
         ALHelper.Check();
     }
     foreach (int num in numArray)
     {
         this.freeBuffers.Push(num);
     }
 }
Exemple #16
0
 private void ResetSource(int sourceId)
 {
     AL.Source(sourceId, ALSourceb.Looping, false);
     AL.Source(sourceId, ALSource3f.Position, 0.0f, 0.0f, 0.1f);
     AL.Source(sourceId, ALSourcef.Pitch, 1f);
     AL.Source(sourceId, ALSourcef.Gain, 1f);
     AL.Source(sourceId, ALSourcei.Buffer, 0);
     lock (this.filteredSources)
     {
         if (ALHelper.Efx.IsInitialized && this.filteredSources.Remove(sourceId))
         {
             ALHelper.Efx.BindFilterToSource(sourceId, 0);
         }
     }
     ALHelper.Check();
     this.freeSources.Push(sourceId);
 }
Exemple #17
0
 private OpenALSoundController()
 {
     this.context  = new AudioContext();
     this.filterId = ALHelper.Efx.GenFilter();
     ALHelper.Efx.Filter(this.filterId, EfxFilteri.FilterType, 1);
     ALHelper.Efx.Filter(this.filterId, EfxFilterf.LowpassGain, 1f);
     ALHelper.Efx.Filter(this.filterId, EfxFilterf.LowpassGainHF, 1f);
     ALHelper.Check();
     AL.DistanceModel(ALDistanceModel.InverseDistanceClamped);
     ALHelper.Check();
     this.freeBuffers = new Stack <int>(24);
     this.ExpandBuffers();
     this.allocatedBuffers   = new Dictionary <SoundEffect, OpenALSoundController.BufferAllocation>(24);
     this.staleAllocations   = new List <KeyValuePair <SoundEffect, OpenALSoundController.BufferAllocation> >();
     this.filteredSources    = new HashSet <int>();
     this.activeSoundEffects = new List <SoundEffectInstance>();
     this.freeSources        = new Stack <int>(16);
     this.ExpandSources();
 }
Exemple #18
0
        public void Play()
        {
            switch (AL.GetSourceState(this.alSourceId))
            {
            case ALSourceState.Playing:
                break;

            case ALSourceState.Paused:
                this.Resume();
                break;

            default:
                this.Prepare(false);
                AL.SourcePlay(this.alSourceId);
                ALHelper.Check();
                this.Precaching = false;
                OggStreamer.Instance.AddStream(this);
                break;
            }
        }
Exemple #19
0
 public void RegisterSoundEffect(SoundEffect soundEffect)
 {
     if (this.allocatedBuffers.ContainsKey(soundEffect))
     {
         return;
     }
     if (this.freeBuffers.Count == 0)
     {
         this.ExpandBuffers();
     }
     Trace.WriteLine("[OpenAL] Pre-allocating buffer for " + soundEffect.Name);
     OpenALSoundController.BufferAllocation bufferAllocation;
     this.allocatedBuffers.Add(soundEffect, bufferAllocation = new OpenALSoundController.BufferAllocation()
     {
         BufferId    = this.freeBuffers.Pop(),
         SinceUnused = -1f
     });
     AL.BufferData <byte>(bufferAllocation.BufferId, soundEffect.Format, soundEffect._data, soundEffect.Size, soundEffect.Rate);
     ALHelper.Check();
 }
Exemple #20
0
 private void EnsureBuffersFilled()
 {
     while (!this.cancelled)
     {
         Thread.Sleep((int)(1000.0 / (double)this.UpdateRate));
         if (this.cancelled)
         {
             break;
         }
         this.threadLocalStreams.Clear();
         lock (this.iterationMutex)
             this.threadLocalStreams.AddRange((IEnumerable <OggStream>) this.streams);
         if (this.threadLocalStreams.Count != 0)
         {
             foreach (OggStream stream in this.threadLocalStreams)
             {
                 lock (stream.prepareMutex)
                 {
                     lock (this.iterationMutex)
                     {
                         if (!this.streams.Contains(stream))
                         {
                             continue;
                         }
                     }
                     bool local_1 = false;
                     int  local_2;
                     AL.GetSource(stream.alSourceId, ALGetSourcei.BuffersQueued, out local_2);
                     ALHelper.Check();
                     int local_3;
                     AL.GetSource(stream.alSourceId, ALGetSourcei.BuffersProcessed, out local_3);
                     ALHelper.Check();
                     if (local_3 != 0 || local_2 != stream.BufferCount)
                     {
                         int[] local_4 = local_3 <= 0 ? Enumerable.ToArray <int>(Enumerable.Skip <int>((IEnumerable <int>)stream.alBufferIds, local_2)) : AL.SourceUnqueueBuffers(stream.alSourceId, local_3);
                         for (int local_5 = 0; local_5 < local_4.Length; ++local_5)
                         {
                             local_1 = local_1 | this.FillBuffer(stream, local_4[local_5]);
                             if (local_1)
                             {
                                 if (stream.IsLooped)
                                 {
                                     stream.Reader.DecodedTime = TimeSpan.Zero;
                                 }
                                 else
                                 {
                                     this.streams.Remove(stream);
                                     local_5 = local_4.Length;
                                 }
                             }
                         }
                         AL.SourceQueueBuffers(stream.alSourceId, local_4.Length, local_4);
                         ALHelper.Check();
                         if (local_1 && !stream.IsLooped)
                         {
                             continue;
                         }
                     }
                     else
                     {
                         continue;
                     }
                 }
                 lock (stream.stopMutex)
                 {
                     if (!stream.Precaching)
                     {
                         lock (this.iterationMutex)
                         {
                             if (!this.streams.Contains(stream))
                             {
                                 continue;
                             }
                         }
                         if (AL.GetSourceState(stream.alSourceId) == ALSourceState.Stopped)
                         {
                             Trace.WriteLine("[OpenAL] Buffer underrun on " + stream.Name);
                             AL.SourcePlay(stream.alSourceId);
                             ALHelper.Check();
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #21
0
 private void StopPlayback()
 {
     AL.SourceStop(this.alSourceId);
     ALHelper.Check();
 }