Exemple #1
0
        public void Start(AudioBuffer buffer, bool loops)
        {
            AL.BindBufferToSource(Source, buffer.BufferID);
            AL.Source(Source, ALSourceb.Looping, loops);

            Start();
        }
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
        public Sound(SoundSource _source)
        {
            this._SourceId = AL.GenSource();
            this._Source   = _source;

            AL.BindBufferToSource(this._SourceId, _source._BufferId);
        }
        private int TakeSourceFor(SoundEffect soundEffect, bool filter = false)
        {
            int result;

            while (!this.freeSources.TryPop(out result))
            {
                this.ExpandSources(32);
            }
            if (filter && ALHelper.Efx.IsInitialized)
            {
                ALHelper.Efx.Filter(this.filterId, EfxFilterf.LowpassGainHF, MathHelper.Clamp(this.lowpassGainHf, 0.0f, 1f));
                ALHelper.Efx.BindFilterToSource(result, this.filterId);
                OpenALSoundController.FilteringLock.EnterWriteLock();
                this.filteredSources.Add(result);
                OpenALSoundController.FilteringLock.ExitWriteLock();
            }
            OpenALSoundController.AllocationsLock.EnterUpgradeableReadLock();
            OpenALSoundController.BufferAllocation bufferAllocation;
            if (!this.allocatedBuffers.TryGetValue(soundEffect, out bufferAllocation))
            {
                bufferAllocation = new OpenALSoundController.BufferAllocation();
                while (!this.freeBuffers.TryPop(out bufferAllocation.BufferId))
                {
                    this.ExpandBuffers(32);
                }
                OpenALSoundController.AllocationsLock.EnterWriteLock();
                this.allocatedBuffers.Add(soundEffect, bufferAllocation);
                OpenALSoundController.AllocationsLock.ExitWriteLock();
                AL.BufferData <byte>(bufferAllocation.BufferId, soundEffect.Format, soundEffect._data, soundEffect.Size, soundEffect.Rate);
            }
            ++bufferAllocation.SourceCount;
            AL.BindBufferToSource(result, bufferAllocation.BufferId);
            OpenALSoundController.AllocationsLock.ExitUpgradeableReadLock();
            return(result);
        }
Exemple #5
0
        public override void Play(AudioBuffer buffer)
        {
            OpenALAudioBuffer alBuffer = (OpenALAudioBuffer)buffer;

            AL.BindBufferToSource(ID, alBuffer.ID);
            AL.SourcePlay(ID);
        }
Exemple #6
0
        public static void RegisterSound(string soundName)
        {
            var file = $"{SharpCraft.Instance.GameFolderDir}/assets/sharpcraft/sounds/{soundName}.ogg";

            if (!File.Exists(file))
            {
                Console.WriteLine($"ERROR: Couldn't find sound '{soundName}'");
                return;
            }

            using (var wr = new VorbisWaveReader(file))
            {
                byte[] data = new byte[wr.Length];
                wr.Read(data, 0, data.Length);

                int buffer = AL.GenBuffer();
                int source = AL.GenSource();

                AL.BufferData(buffer, ALFormat.VorbisExt, data, data.Length, wr.WaveFormat.SampleRate);

                AL.Source(source, ALSourcei.Buffer, buffer);
                AL.BindBufferToSource(source, buffer);

                //Sounds.TryAdd(soundName, new ValueTuple<int, int>(source, buffer));
            }
        }
Exemple #7
0
 internal void PlayBuffer(int buf, float volume)
 {
     actions.Enqueue(() => {
         var src = sources.Dequeue();
         AL.BindBufferToSource(src, buf);
         AL.Source(src, ALSourcef.Gain, volume);
         AL.SourcePlay(src);
         playingSources.Add(src);
     });
 }
Exemple #8
0
 private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
 {
     ShowInfo((uint)TreeView1.SelectedNode.Index);
     TrackBar1.Value = 0;
     Playing         = false;
     if (SoundBuffer != 0)
     {
         AL.BindBufferToSource(SoundSource, 0);
         AL.DeleteBuffer(SoundBuffer);
         SoundBuffer = 0;
     }
 }
Exemple #9
0
                private void Play_(AudioBufferOpenTk buffer, bool looping, float pitch)
                {
                    if (buffer != this.currentBuffer_ &&
                        AL.GetSourceState(this.Id) == ALSourceState.Paused)
                    {
                        AL.SourcePlay(this.Id);
                        return;
                    }

                    AL.SourceRewind(this.Id);
                    AL.BindBufferToSource(this.Id, buffer.Id);
                    this.currentBuffer_ = buffer;

                    // TODO: Pull these out as properties of the source?
                    AL.Source(this.Id, ALSourceb.Looping, looping);
                    AL.Source(this.Id, ALSourcef.Pitch, pitch);

                    AL.SourcePlay(this.Id);
                }
Exemple #10
0
 private void Button3_Click(object sender, EventArgs e)
 {
     /*SoundBuffer.Stop();
      * SoundBuffer.CurrentPosition = 0;
      * Playing = false;
      * int WP = 0;
      * int playPos = 0;
      * SoundBuffer.GetCurrentPosition(out playPos, out WP);
      * TrackBar1.Value = playPos;
      * SharpDX.Multimedia.WaveFormat[] waveFormats = new SharpDX.Multimedia.WaveFormat[] { };
      * var NULL = 0;
      * SoundBuffer.GetFormat(waveFormats, 0, out NULL);
      * Label9.Text = ToTime((uint)(TrackBar1.Value / waveFormats[0].AverageBytesPerSecond));*/
     AL.SourceStop(SoundSource);
     AL.BindBufferToSource(SoundSource, 0);
     AL.DeleteBuffer(SoundBuffer);
     SoundBuffer = 0;
     Label9.Text = "0:00";
 }
Exemple #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="soundSource"></param>
        /// <param name="fileName"></param>
        public void BindBufferToSource(int soundSource, string fileName)
        {
            // find the buffer
            int buffer = -1;

            if (!_audioBuffers.ContainsKey(fileName))
            {
                GenBuffer(fileName);
                if (!_audioBuffers.ContainsKey(fileName))
                {
                    Console.WriteLine("Invalid Audio File: " + fileName);
                    return;
                }

                buffer = _audioBuffers[fileName];
            }
            else
            {
                buffer = _audioBuffers[fileName];
            }

            // bind the buffer to the source
            AL.BindBufferToSource(soundSource, buffer);
        }
Exemple #12
0
        private void LoadBuffer(uint index)
        {
            if (!(Sounds[index].Type == 2))
            {
                System.IO.MemoryStream SoundStream  = new System.IO.MemoryStream();
                System.IO.BinaryWriter StreamWriter = new System.IO.BinaryWriter(SoundStream);
                Twinsanity.ADPCM       ADPCM_Device = new Twinsanity.ADPCM();
                SoundStream.Position = 44;
                if (Sounds[index].Type == 0)
                {
                    System.IO.MemoryStream ADPCM       = new System.IO.MemoryStream();
                    System.IO.MemoryStream PCM         = new System.IO.MemoryStream();
                    System.IO.BinaryWriter ADPCMWriter = new System.IO.BinaryWriter(ADPCM);
                    if (!Sounds[index].External)
                    {
                        MB.Position = Sounds[index].Offset + 48;
                        ADPCMWriter.Write(MBReader.ReadBytes((int)Sounds[index].Size));
                    }
                    else
                    {
                        ADPCMWriter.Write(Sounds[index].Stream.ToArray());
                    }
                    ADPCM.Position = 0;
                    ADPCM_Device.ADPCM2PCM(ADPCM, ref PCM);
                    SoundStream.Position = 0;
                    WriteHeader(ref SoundStream, Sounds[index].SampleRate, 1, (uint)PCM.Length);
                    StreamWriter.Write(PCM.ToArray());
                }
                else
                {
                    System.IO.MemoryStream ADPCM       = new System.IO.MemoryStream();
                    System.IO.MemoryStream ADPCM_R     = new System.IO.MemoryStream();
                    System.IO.MemoryStream ADPCM_L     = new System.IO.MemoryStream();
                    System.IO.MemoryStream PCM         = new System.IO.MemoryStream();
                    System.IO.MemoryStream PCM_R       = new System.IO.MemoryStream();
                    System.IO.MemoryStream PCM_L       = new System.IO.MemoryStream();
                    System.IO.BinaryWriter ADPCMWriter = new System.IO.BinaryWriter(ADPCM);
                    System.IO.BinaryWriter PCMWriter   = new System.IO.BinaryWriter(PCM);
                    if (!Sounds[index].External)
                    {
                        MB.Position = Sounds[index].Offset;
                        ADPCMWriter.Write(MBReader.ReadBytes((int)Sounds[index].Size));
                    }
                    else
                    {
                        ADPCMWriter.Write(Sounds[index].Stream.ToArray());
                    }
                    ADPCM.Position = 0;
                    ADPCM_Device.ADPCM_Demux(ADPCM, ref ADPCM_R, ref ADPCM_L, Interleave);
                    ADPCM_R.Position = 0;
                    PCM_R.Position   = 0;
                    ADPCM_L.Position = 0;
                    PCM_L.Position   = 0;
                    ADPCM_Device.ADPCM2PCM(ADPCM_R, ref PCM_R);
                    ADPCM_Device.ADPCM2PCM(ADPCM_L, ref PCM_L);
                    ADPCM_Device.PCM_Mux(ref PCM, PCM_R, PCM_L);
                    SoundStream.Position = 0;
                    WriteHeader(ref SoundStream, Sounds[index].SampleRate, 2, (uint)PCM.Length);
                    StreamWriter.Write(PCM.ToArray());
                }
                SoundStream.Position = 0;

                AL.BindBufferToSource(SoundSource, SoundBuffer);
                AL.BufferData(SoundBuffer, ALFormat.Stereo16, SoundStream.GetBuffer(), SoundStream.GetBuffer().Length, 44100);


                TrackBar1.Maximum = SoundStream.GetBuffer().Length - 1;
                Label10.Text      = "Loaded: yes";
            }
            else
            {
                Interaction.MsgBox("No data to play");
            }
        }
Exemple #13
0
 public void SetVolume(float volume)
 {
     AL.BindBufferToSource(SourceHash, BufferHash);
     AL.Listener(ALListenerf.Gain, volume);
 }
Exemple #14
0
 public void Parse()
 {
     AL.BindBufferToSource(SourceHash, BufferHash);
     AL.SourcePause(SourceHash);
 }
Exemple #15
0
 public void Play()
 {
     AL.BindBufferToSource(SourceHash, BufferHash);
     AL.SourcePlay(SourceHash);
 }
Exemple #16
0
 public void Stop()
 {
     AL.BindBufferToSource(SourceHash, BufferHash);
     AL.SourceStop(SourceHash);
 }
        public void Dispose()
        {
            lock (mutex)
            {
                if (ALSourceIndex >= 0)
                {
                    AL.SourceStop(Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex));
                    ALError alError = AL.GetError();
                    if (alError != ALError.NoError)
                    {
                        throw new Exception("Failed to stop source: " + AL.GetErrorString(alError));
                    }

                    if (IsStream)
                    {
                        uint alSource = Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex);

                        AL.SourceStop(alSource);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to stop streamed source: " + AL.GetErrorString(alError));
                        }

                        int   buffersToUnqueue = 0;
                        int[] unqueuedBuffers  = null;

                        buffersToUnqueue = 0;
                        AL.GetSource(alSource, ALGetSourcei.BuffersProcessed, out buffersToUnqueue);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to determine processed buffers from streamed source: " + AL.GetErrorString(alError));
                        }

                        unqueuedBuffers = new int[buffersToUnqueue];
                        AL.SourceUnqueueBuffers((int)alSource, buffersToUnqueue, unqueuedBuffers);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to unqueue buffers from streamed source: " + AL.GetErrorString(alError));
                        }

                        AL.BindBufferToSource(alSource, 0);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to reset buffer for streamed source: " + AL.GetErrorString(alError));
                        }

                        for (int i = 0; i < 4; i++)
                        {
                            AL.DeleteBuffer(ref streamBuffers[i]);
                            alError = AL.GetError();
                            if (alError != ALError.NoError)
                            {
                                throw new Exception("Failed to delete streamBuffers[" + i.ToString() + "] (" + streamBuffers[i].ToString() + "): " + AL.GetErrorString(alError));
                            }
                        }

                        reachedEndSample = true;
                    }
                    else
                    {
                        AL.BindBufferToSource(Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex), 0);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to unbind buffer to non-streamed source: " + AL.GetErrorString(alError));
                        }
                    }

                    ALSourceIndex = -1;
                }
            }
        }
        public SoundChannel(Sound sound, float gain, Vector3?position, float near, float far, string category, bool muffle = false)
        {
            Sound = sound;

            IsStream        = sound.Stream;
            FilledByNetwork = sound is VoipSound;
            decayTimer      = 0;
            streamSeekPos   = 0; reachedEndSample = false;
            startedPlaying  = true;

            mutex = new object();

            lock (mutex)
            {
                if (sound.Owner.CountPlayingInstances(sound) < sound.MaxSimultaneousInstances)
                {
                    ALSourceIndex = sound.Owner.AssignFreeSourceToChannel(this);
                }

                if (ALSourceIndex >= 0)
                {
                    if (!IsStream)
                    {
                        AL.BindBufferToSource(sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex), 0);
                        ALError alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to reset source buffer: " + AL.GetErrorString(alError));
                        }

                        if (!AL.IsBuffer(sound.ALBuffer))
                        {
                            throw new Exception(sound.Filename + " has an invalid buffer!");
                        }

                        uint alBuffer = sound.Owner.GetCategoryMuffle(category) || muffle ? sound.ALMuffledBuffer : sound.ALBuffer;
                        AL.BindBufferToSource(sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex), alBuffer);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to bind buffer to source (" + ALSourceIndex.ToString() + ":" + sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex) + "," + sound.ALBuffer.ToString() + "): " + AL.GetErrorString(alError));
                        }

                        AL.SourcePlay(sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex));
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to play source: " + AL.GetErrorString(alError));
                        }
                    }
                    else
                    {
                        AL.BindBufferToSource(sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex), (uint)sound.ALBuffer);
                        ALError alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to reset source buffer: " + AL.GetErrorString(alError));
                        }

                        AL.Source(sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex), ALSourceb.Looping, false);
                        alError = AL.GetError();
                        if (alError != ALError.NoError)
                        {
                            throw new Exception("Failed to set stream looping state: " + AL.GetErrorString(alError));
                        }

                        streamShortBuffer = new short[STREAM_BUFFER_SIZE];

                        streamBuffers = new uint[4];
                        emptyBuffers  = new List <uint>();
                        for (int i = 0; i < 4; i++)
                        {
                            AL.GenBuffer(out streamBuffers[i]);

                            alError = AL.GetError();
                            if (alError != ALError.NoError)
                            {
                                throw new Exception("Failed to generate stream buffers: " + AL.GetErrorString(alError));
                            }

                            if (!AL.IsBuffer(streamBuffers[i]))
                            {
                                throw new Exception("Generated streamBuffer[" + i.ToString() + "] is invalid!");
                            }
                        }

                        Sound.Owner.InitStreamThread();
                    }
                }

                this.Position = position;
                this.Gain     = gain;
                this.Looping  = false;
                this.Near     = near;
                this.Far      = far;
                this.Category = category;
            }
        }
Exemple #19
0
 public void SetLoop(bool loop)
 {
     AL.BindBufferToSource(SourceHash, BufferHash);
     AL.Source(SourceHash, ALSourceb.Looping, loop);
 }
Exemple #20
0
 public int GetState()
 {
     AL.BindBufferToSource(SourceHash, BufferHash);
     AL.GetSource(SourceHash, ALGetSourcei.SourceState, out int state);
     return(state);
 }