/// <summary>
 /// Gets the OpenAL sound controller, constructs the sound buffer, and sets up the event delegates for
 /// the reserved and recycled events.
 /// </summary>
 internal void InitializeSound()
 {
     controller = OpenALSoundController.GetInstance;
     soundBuffer = new OALSoundBuffer();
     soundBuffer.Reserved += HandleSoundBufferReserved;
     soundBuffer.Recycled += HandleSoundBufferRecycled;
 }
예제 #2
0
        private void PlatformInitialize(byte[] buffer, int sampleRate, AudioChannels channels)
        {
            Rate = (float)sampleRate;
            Size = (int)buffer.Length;

#if OPENAL && !(MONOMAC || IOS)
            _data  = buffer;
            Format = (channels == AudioChannels.Stereo) ? ALFormat.Stereo16 : ALFormat.Mono16;
#endif

#if MONOMAC || IOS
            //buffer should contain 16-bit PCM wave data
            short bitsPerSample = 16;

            if ((int)channels <= 1)
            {
                Format = bitsPerSample == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
            }
            else
            {
                Format = bitsPerSample == 8 ? ALFormat.Stereo8 : ALFormat.Stereo16;
            }

            _name = "";
            _data = buffer;
#endif
            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(_data, Format, Size, (int)Rate);
        }
        private void PlatformSubmitBuffer(byte[] buffer, int offset, int count)
        {
            // Get a buffer
            OALSoundBuffer oalBuffer = new OALSoundBuffer();

            // Bind the data
            if (offset == 0)
            {
                oalBuffer.BindDataBuffer(buffer, _format, count, _sampleRate);
            }
            else
            {
                // BindDataBuffer does not support offset
                var offsetBuffer = new byte[count];
                Array.Copy(buffer, offset, offsetBuffer, 0, count);
                oalBuffer.BindDataBuffer(offsetBuffer, _format, count, _sampleRate);
            }

            // Queue the buffer
            AL.SourceQueueBuffer(SourceId, oalBuffer.OpenALDataBuffer);
            ALHelper.CheckError();
            _queuedBuffers.Enqueue(oalBuffer);

            // If the source has run out of buffers, restart it
            var sourceState = AL.GetSourceState(SourceId);

            if (_state == SoundState.Playing && sourceState == ALSourceState.Stopped)
            {
                AL.SourcePlay(SourceId);
                ALHelper.CheckError("Failed to resume source playback.");
            }
        }
예제 #4
0
 private void InitializeSound()
 {
     controller            = OpenALSoundController.GetInstance;
     soundBuffer           = new OALSoundBuffer();
     soundBuffer.Reserved += HandleSoundBufferReserved;
     soundBuffer.Recycled += HandleSoundBufferRecycled;
 }
 public void PauseSound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     AL.SourcePause(soundBuffer.SourceId);
 }
예제 #6
0
 private void PlatformDispose(bool disposing)
 {
     if (SoundBuffer != null)
     {
         SoundBuffer.Dispose();
         SoundBuffer = null;
     }
 }
 public void PauseSound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     soundBuffer.Pause();
 }
예제 #8
0
        public void StopSound(OALSoundBuffer soundBuffer)
        {
            AL.SourceStop(soundBuffer.SourceId);

            AL.Source(soundBuffer.SourceId, ALSourcei.Buffer, 0);
            playingSourcesCollection.Remove(soundBuffer);
            RecycleSource(soundBuffer);
        }
 public void ResumeSound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     AL.SourcePlay(soundBuffer.SourceId);
 }
예제 #10
0
 private void InitializeSound()
 {
     controller  = OpenALSoundController.GetInstance;
     soundBuffer = new OALSoundBuffer();
     soundBuffer.BindDataBuffer(soundEffect._data, soundEffect.Format, soundEffect.Size, (int)soundEffect.Rate);
     soundBuffer.Reserved += HandleSoundBufferReserved;
     soundBuffer.Recycled += HandleSoundBufferRecycled;
 }
 public void ResumeSound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     soundBuffer.Resume();
 }
예제 #12
0
 public void Dispose()
 {
     soundBuffer.Reserved -= HandleSoundBufferReserved;
     soundBuffer.Recycled -= HandleSoundBufferRecycled;
     soundBuffer.Dispose();
     soundBuffer = null;
     isDisposed  = true;
 }
예제 #13
0
		private void InitializeSound ()
		{
			controller = OpenALSoundController.GetInstance;
			soundBuffer = new OALSoundBuffer ();
			soundBuffer.BindDataBuffer (soundEffect._data, soundEffect.Format, soundEffect.Size, (int)soundEffect.Rate);
			soundBuffer.Reserved += HandleSoundBufferReserved;
			soundBuffer.Recycled += HandleSoundBufferRecycled;

		}
예제 #14
0
 public void RecycleSource(OALSoundBuffer soundBuffer)
 {
     if (!_bSoundAvailable)
     {
         return;
     }
     inUseSourcesCollection.Remove(soundBuffer);
     availableSourcesCollection.Add(soundBuffer.SourceId);
     soundBuffer.RecycleSoundBuffer();
 }
예제 #15
0
        private void PlatformInitializePcm(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength)
        {
            Rate   = (float)sampleRate;
            Size   = (int)count;
            Format = channels == AudioChannels.Stereo ? ALFormat.Stereo16 : ALFormat.Mono16;

            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(buffer, Format, Size, (int)Rate);
        }
 public void PlaySound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     lock (playingSourcesCollection)
     {
         playingSourcesCollection.Add(soundBuffer);
     }
     AL.SourcePlay(soundBuffer.SourceId);
 }
예제 #17
0
 public void PlaySound(OALSoundBuffer soundBuffer)
 {
     if (!_bSoundAvailable)
     {
         return;
     }
     lock (playingSourcesCollection)
     {
         playingSourcesCollection.Add(soundBuffer);
     }
     AL.SourcePlay(soundBuffer.SourceId);
 }
예제 #18
0
 public void Dispose()
 {
     if (!isDisposed)
     {
         this.Stop(true);
         soundBuffer.Reserved -= HandleSoundBufferReserved;
         soundBuffer.Recycled -= HandleSoundBufferRecycled;
         soundBuffer.Dispose();
         soundBuffer = null;
         isDisposed  = true;
     }
 }
예제 #19
0
        public bool IsState(OALSoundBuffer soundBuffer, int state)
        {
            int sourceState;

            AL.GetSource(soundBuffer.SourceId, ALGetSourcei.SourceState, out sourceState);

            if (state == sourceState)
            {
                return(true);
            }

            return(false);
        }
예제 #20
0
 private void PlatformDispose(bool disposing)
 {
     if (disposing)
     {
         if (soundBuffer != null)
         {
             this.Stop(true);
             soundBuffer.Reserved -= HandleSoundBufferReserved;
             soundBuffer.Recycled -= HandleSoundBufferRecycled;
             soundBuffer.Dispose();
             soundBuffer = null;
         }
     }
 }
        public void StopSound(OALSoundBuffer soundBuffer)
        {
            if (!CheckInitState())
            {
                return;
            }
            AL.SourceStop(soundBuffer.SourceId);

            AL.Source(soundBuffer.SourceId, ALSourcei.Buffer, 0);
            lock (playingSourcesCollection) {
                playingSourcesCollection.Remove(soundBuffer);
            }
            RecycleSource(soundBuffer);
        }
예제 #22
0
        private void PlatformInitializeAdpcm(byte [] buffer, int offset, int count, int sampleRate, AudioChannels channels, int dataFormat, int loopStart, int loopLength)
        {
            Rate = (float)sampleRate;
            Size = (int)count;
            #if DESKTOPGL
            Format = channels == AudioChannels.Stereo ? ALFormat.StereoMSADPCM : ALFormat.MonoMSADPCM;
            #else
            Format = channels == AudioChannels.Stereo ? (ALFormat)0x1303 : (ALFormat)0x1302;
            #endif

            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(buffer, Format, Size, (int)Rate, dataFormat);
        }
예제 #23
0
        public void StopSound(OALSoundBuffer soundBuffer)
        {
            if (!_bSoundAvailable)
            {
                return;
            }
            AL.SourceStop(soundBuffer.SourceId);

            AL.Source(soundBuffer.SourceId, ALSourcei.Buffer, 0);
            lock (playingSourcesCollection) {
                playingSourcesCollection.Remove(soundBuffer);
            }
            RecycleSource(soundBuffer);
        }
예제 #24
0
        private void PlatformInitializeIeeeFloat(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength)
        {
            if (!OpenALSoundController.GetInstance.SupportsIeee)
            {
                // If 32-bit IEEE float is not supported, convert to 16-bit signed PCM
                buffer = AudioLoader.ConvertFloatTo16(buffer, offset, count);
                PlatformInitializePcm(buffer, 0, buffer.Length, 16, sampleRate, channels, loopStart, loopLength);
                return;
            }

            var format = AudioLoader.GetSoundFormat(AudioLoader.FormatIeee, (int)channels, 32);

            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(buffer, format, count, sampleRate);
        }
예제 #25
0
        private void PlatformInitializePcm(byte[] buffer, int offset, int count, int sampleBits, int sampleRate, AudioChannels channels, int loopStart, int loopLength)
        {
            if (sampleBits == 24)
            {
                // Convert 24-bit signed PCM to 16-bit signed PCM
                buffer     = AudioLoader.Convert24To16(buffer, offset, count);
                offset     = 0;
                count      = buffer.Length;
                sampleBits = 16;
            }

            var format = AudioLoader.GetSoundFormat(AudioLoader.FormatPcm, (int)channels, sampleBits);

            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(buffer, format, count, sampleRate);
        }
예제 #26
0
        private void PlatformInitializeIma4(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int blockAlignment, int loopStart, int loopLength)
        {
            if (!OpenALSoundController.GetInstance.SupportsIma4)
            {
                // If IMA/ADPCM is not supported, convert to 16-bit signed PCM
                buffer = AudioLoader.ConvertIma4ToPcm(buffer, offset, count, (int)channels, blockAlignment);
                PlatformInitializePcm(buffer, 0, buffer.Length, 16, sampleRate, channels, loopStart, loopLength);
                return;
            }

            var format          = AudioLoader.GetSoundFormat(AudioLoader.FormatIma4, (int)channels, 0);
            int sampleAlignment = AudioLoader.SampleAlignment(format, blockAlignment);

            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(buffer, format, count, sampleRate, sampleAlignment);
        }
예제 #27
0
        public bool ReserveSource(OALSoundBuffer soundBuffer)
        {
            int sourceNumber;

            if (availableSourcesCollection.Count == 0)
            {
                soundBuffer.SourceId = 0;
                return(false);
            }


            sourceNumber         = availableSourcesCollection.First();
            soundBuffer.SourceId = sourceNumber;
            inUseSourcesCollection.Add(soundBuffer);

            availableSourcesCollection.Remove(sourceNumber);

            //sourceId = sourceNumber;
            return(true);
        }
예제 #28
0
        private void PlatformInitializeAdpcm(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int blockAlignment, int loopStart, int loopLength)
        {
            if (!OpenALSoundController.GetInstance.SupportsAdpcm)
            {
                // If MS-ADPCM is not supported, convert to 16-bit signed PCM
                buffer = AudioLoader.ConvertMsAdpcmToPcm(buffer, offset, count, (int)channels, blockAlignment);
                PlatformInitializePcm(buffer, 0, buffer.Length, 16, sampleRate, channels, loopStart, loopLength);
                return;
            }

            var format          = AudioLoader.GetSoundFormat(AudioLoader.FormatMsAdpcm, (int)channels, 0);
            int sampleAlignment = AudioLoader.SampleAlignment(format, blockAlignment);

            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            // Buffer length must be aligned with the block alignment
            int alignedCount = count - (count % blockAlignment);

            SoundBuffer.BindDataBuffer(buffer, format, alignedCount, sampleRate, sampleAlignment);
        }
예제 #29
0
        private void PlatformInitialize(byte[] buffer, int sampleRate, AudioChannels channels)
        {
			Rate = (float)sampleRate;
            Size = (int)buffer.Length;

#if OPENAL && !(MONOMAC || IOS)

            _data = buffer;
            Format = (channels == AudioChannels.Stereo) ? ALFormat.Stereo16 : ALFormat.Mono16;

#endif

#if MONOMAC || IOS

            //buffer should contain 16-bit PCM wave data
            short bitsPerSample = 16;

            if ((int)channels <= 1)
                Format = bitsPerSample == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
            else
                Format = bitsPerSample == 8 ? ALFormat.Stereo8 : ALFormat.Stereo16;

            _name = "";
            _data = buffer;

#endif
            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(_data, Format, Size, (int)Rate);
        }
 public void ResumeSound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     AL.SourcePlay(soundBuffer.SourceId);
 }
		public void PauseSound (OALSoundBuffer soundBuffer)
		{
            if (!CheckInitState())
            {
                return;
            }
            AL.SourcePause(soundBuffer.SourceId);
		}
예제 #32
0
        private void PlatformLoadAudioStream(Stream s, out TimeSpan duration)
        {
            byte[] buffer;

#if OPENAL && !(MONOMAC || IOS)
            ALFormat format;
            int      size;
            int      freq;

            var stream = s;

            buffer = AudioLoader.Load(stream, out format, out size, out freq);

            Format = format;
            Size   = size;
            Rate   = freq;

            duration = TimeSpan.FromSeconds((float)size / freq);
#endif

#if MONOMAC || IOS
            var audiodata = new byte[s.Length];
            s.Read(audiodata, 0, (int)s.Length);

            using (AudioFileStream afs = new AudioFileStream(AudioFileType.WAVE))
            {
                afs.ParseBytes(audiodata, false);
                Size = (int)afs.DataByteCount;

                buffer = new byte[afs.DataByteCount];
                Array.Copy(audiodata, afs.DataOffset, buffer, 0, afs.DataByteCount);

                AudioStreamBasicDescription asbd = afs.DataFormat;
                int channelsPerFrame             = asbd.ChannelsPerFrame;
                int bitsPerChannel = asbd.BitsPerChannel;

                // There is a random chance that properties asbd.ChannelsPerFrame and asbd.BitsPerChannel are invalid because of a bug in Xamarin.iOS
                // See: https://bugzilla.xamarin.com/show_bug.cgi?id=11074 (Failed to get buffer attributes error when playing sounds)
                if (channelsPerFrame <= 0 || bitsPerChannel <= 0)
                {
                    NSError err;
                    using (NSData nsData = NSData.FromArray(audiodata))
                        using (AVAudioPlayer player = AVAudioPlayer.FromData(nsData, out err))
                        {
                            channelsPerFrame = (int)player.NumberOfChannels;
                            bitsPerChannel   = player.SoundSetting.LinearPcmBitDepth.GetValueOrDefault(16);

                            Rate     = (float)player.SoundSetting.SampleRate;
                            duration = TimeSpan.FromSeconds(player.Duration);
                        }
                }
                else
                {
                    Rate = (float)asbd.SampleRate;
                    double durationSec = (Size / ((bitsPerChannel / 8) * channelsPerFrame)) / asbd.SampleRate;
                    duration = TimeSpan.FromSeconds(durationSec);
                }

                if (channelsPerFrame == 1)
                {
                    Format = (bitsPerChannel == 8) ? ALFormat.Mono8 : ALFormat.Mono16;
                }
                else
                {
                    Format = (bitsPerChannel == 8) ? ALFormat.Stereo8 : ALFormat.Stereo16;
                }
            }
#endif
            // bind buffer
            SoundBuffer = new OALSoundBuffer();
            SoundBuffer.BindDataBuffer(buffer, Format, Size, (int)Rate);
        }
예제 #33
0
		public void PauseSound (OALSoundBuffer soundBuffer)
		{
			AL.SourcePause (soundBuffer.SourceId);
		}
예제 #34
0
		public void PlaySound (OALSoundBuffer soundBuffer)
		{
			playingSourcesCollection.Add (soundBuffer);
			AL.SourcePlay (soundBuffer.SourceId);
		}
예제 #35
0
		public bool ReserveSource (OALSoundBuffer soundBuffer)
		{
			int sourceNumber;
			if (availableSourcesCollection.Count == 0) {

				soundBuffer.SourceId = 0;
				return false;
			}
			

			sourceNumber = availableSourcesCollection.First ();
			soundBuffer.SourceId = sourceNumber;
			inUseSourcesCollection.Add (soundBuffer);

			availableSourcesCollection.Remove (sourceNumber);

			//sourceId = sourceNumber;
			return true;
		}
예제 #36
0
 private void PlatformDispose(bool disposing)
 {
     if (SoundBuffer != null)
     {
         SoundBuffer.Dispose();
         SoundBuffer = null;
     }
 }
예제 #37
0
 public void PlaySound(OALSoundBuffer soundBuffer)
 {
     playingSourcesCollection.Add(soundBuffer);
     AL.SourcePlay(soundBuffer.SourceId);
 }
예제 #38
0
        /// <summary>
        /// Stops the current running sound effect, if relevant, removes its event handlers, and disposes
        /// of the sound buffer.
        /// </summary>
		public void Dispose ()
        {
            if (!isDisposed)
            {
                this.Stop(true);
                soundBuffer.Reserved -= HandleSoundBufferReserved;
                soundBuffer.Recycled -= HandleSoundBufferRecycled;
                soundBuffer.Dispose();
                soundBuffer = null;
                isDisposed = true;
            }
		}
예제 #39
0
 public void ResumeSound(OALSoundBuffer soundBuffer)
 {
     if (!CheckInitState())
     {
         return;
     }
     soundBuffer.Resume();
 }
예제 #40
0
 public void ResumeSound(OALSoundBuffer soundBuffer)
 {
     AL.SourcePlay(soundBuffer.SourceId);
 }
 private void PlatformDispose(bool disposing)
 {
     if (disposing)
     {
         if (soundBuffer != null)
         {
             this.Stop(true);
             soundBuffer.Reserved -= HandleSoundBufferReserved;
             soundBuffer.Recycled -= HandleSoundBufferRecycled;
             soundBuffer.Dispose();
             soundBuffer = null;
         }
     }
 }
예제 #42
0
 public void PauseSound(OALSoundBuffer soundBuffer)
 {
     AL.SourcePause(soundBuffer.SourceId);
 }
예제 #43
0
		public void RecycleSource (OALSoundBuffer soundBuffer)
		{
			inUseSourcesCollection.Remove (soundBuffer);
			availableSourcesCollection.Add (soundBuffer.SourceId);
			soundBuffer.RecycleSoundBuffer();
		}
		public void RecycleSource (OALSoundBuffer soundBuffer)
		{
            if (!CheckInitState())
            {
                return;
            }
            inUseSourcesCollection.Remove(soundBuffer);
			availableSourcesCollection.Add (soundBuffer.SourceId);
			soundBuffer.RecycleSoundBuffer();
		}
예제 #45
0
		public void StopSound (OALSoundBuffer soundBuffer)
		{
			AL.SourceStop (soundBuffer.SourceId);

			AL.Source (soundBuffer.SourceId,ALSourcei.Buffer, 0);
			playingSourcesCollection.Remove (soundBuffer);
			RecycleSource (soundBuffer);
		}
		public void PlaySound (OALSoundBuffer soundBuffer)
        {
            if (!CheckInitState())
            {
                return;
            }
            lock (playingSourcesCollection)
            {
                playingSourcesCollection.Add (soundBuffer);
            }
			AL.SourcePlay (soundBuffer.SourceId);
		}
예제 #47
0
		public bool IsState (OALSoundBuffer soundBuffer, int state)
		{
			int sourceState;

			AL.GetSource (soundBuffer.SourceId, ALGetSourcei.SourceState, out sourceState);

			if (state == sourceState) {
				return true;
			}

			return false;
		}
		public void StopSound (OALSoundBuffer soundBuffer)
        {
            if (!CheckInitState())
            {
                return;
            }
            AL.SourceStop(soundBuffer.SourceId);

            AL.Source (soundBuffer.SourceId, ALSourcei.Buffer, 0);
            lock (playingSourcesCollection) {
                playingSourcesCollection.Remove (soundBuffer);
            }
            RecycleSource (soundBuffer);
		}
예제 #49
0
		public void PauseSound (OALSoundBuffer soundBuffer)
		{
            if (!CheckInitState())
            {
                return;
            }
            soundBuffer.Pause();
		}
예제 #50
0
		public void Dispose ()
		{
			soundBuffer.Reserved -= HandleSoundBufferReserved;
			soundBuffer.Recycled -= HandleSoundBufferRecycled;
			soundBuffer.Dispose ();
			soundBuffer = null;
			isDisposed = true;
		}