diff --git a/ANX.Framework/Audio/SoundEffect.cs b/ANX.Framework/Audio/SoundEffect.cs index f8ab5ad2..152b31ad 100644 --- a/ANX.Framework/Audio/SoundEffect.cs +++ b/ANX.Framework/Audio/SoundEffect.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using ANX.Framework.NonXNA; using ANX.Framework.NonXNA.SoundSystem; @@ -113,7 +114,11 @@ namespace ANX.Framework.Audio #endregion #region Private - private ISoundEffect nativeSoundEffect; + internal ISoundEffect nativeSoundEffect; + + private static List fireAndForgetInstances; + + private List children = new List(); #endregion #region Public @@ -139,9 +144,17 @@ namespace ANX.Framework.Audio #endregion #region Constructor + static SoundEffect() + { + MasterVolume = 1f; + SpeedOfSound = 343.5f; + DopplerScale = 1f; + DistanceScale = 1f; + } + private SoundEffect(Stream stream) { - nativeSoundEffect = GetCreator().CreateSoundEffect(stream); + nativeSoundEffect = GetCreator().CreateSoundEffect(this, stream); } public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels) @@ -152,7 +165,7 @@ namespace ANX.Framework.Audio public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength) { - nativeSoundEffect = GetCreator().CreateSoundEffect(buffer, offset, + nativeSoundEffect = GetCreator().CreateSoundEffect(this, buffer, offset, count, sampleRate, channels, loopStart, loopLength); } @@ -172,7 +185,7 @@ namespace ANX.Framework.Audio #region CreateInstance public SoundEffectInstance CreateInstance() { - return new SoundEffectInstance(this); + return new SoundEffectInstance(this, false); } #endregion @@ -204,7 +217,7 @@ namespace ANX.Framework.Audio } #endregion - #region Play (TODO) + #region Play public bool Play() { return Play(1f, 0f, 0f); @@ -212,20 +225,72 @@ namespace ANX.Framework.Audio public bool Play(float volume, float pitch, float pan) { - // TODO: fire and forget play - throw new NotImplementedException(); + if (IsDisposed) + { + return false; + } + + try + { + SoundEffectInstance newInstance = new SoundEffectInstance(this, true) + { + Volume = volume, + Pitch = pitch, + Pan = pan, + }; + + children.Add(new WeakReference(newInstance)); + + lock (fireAndForgetInstances) + { + fireAndForgetInstances.Add(newInstance); + } + + newInstance.Play(); + } + catch + { + return false; + } + + return true; } #endregion #region Dispose public void Dispose() { - if (IsDisposed == false) + if (IsDisposed) { - IsDisposed = true; - nativeSoundEffect.Dispose(); - nativeSoundEffect = null; + return; } + + IsDisposed = true; + nativeSoundEffect.Dispose(); + nativeSoundEffect = null; + + List weakRefs = new List(children); + + lock (fireAndForgetInstances) + { + foreach (WeakReference current in weakRefs) + { + SoundEffectInstance soundInstance = + current.Target as SoundEffectInstance; + + if (soundInstance != null) + { + if (soundInstance.IsFireAndForget) + { + fireAndForgetInstances.Remove(soundInstance); + } + soundInstance.Dispose(); + } + } + } + + weakRefs.Clear(); + children.Clear(); } #endregion } diff --git a/ANX.Framework/Audio/SoundEffectInstance.cs b/ANX.Framework/Audio/SoundEffectInstance.cs index cebe142c..927863c9 100644 --- a/ANX.Framework/Audio/SoundEffectInstance.cs +++ b/ANX.Framework/Audio/SoundEffectInstance.cs @@ -58,6 +58,12 @@ namespace ANX.Framework.Audio private SoundEffect parent; private ISoundEffectInstance nativeInstance; + + internal bool IsFireAndForget + { + get; + private set; + } #endregion #region Public @@ -141,11 +147,13 @@ namespace ANX.Framework.Audio { } - internal SoundEffectInstance(SoundEffect setParent) + internal SoundEffectInstance(SoundEffect setParent, bool setIsFireAndForget) { parent = setParent; + IsFireAndForget = setIsFireAndForget; - nativeInstance = GetCreator().CreateSoundEffectInstance(setParent); + nativeInstance = GetCreator().CreateSoundEffectInstance( + setParent.nativeSoundEffect); } ~SoundEffectInstance() diff --git a/ANX.Framework/NonXNA/SoundSystem/ISoundSystemCreator.cs b/ANX.Framework/NonXNA/SoundSystem/ISoundSystemCreator.cs index 8507770c..2dae36db 100644 --- a/ANX.Framework/NonXNA/SoundSystem/ISoundSystemCreator.cs +++ b/ANX.Framework/NonXNA/SoundSystem/ISoundSystemCreator.cs @@ -81,11 +81,12 @@ namespace ANX.Framework.NonXNA IAudioEmitter CreateAudioEmitter(); - ISoundEffect CreateSoundEffect(Stream stream); + ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream); - ISoundEffect CreateSoundEffect(byte[] buffer, int offset, int count, - int sampleRate, AudioChannels channels, int loopStart, int loopLength); + ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset, + int count, int sampleRate, AudioChannels channels, int loopStart, + int loopLength); - ISoundEffectInstance CreateSoundEffectInstance(SoundEffect parent); + ISoundEffectInstance CreateSoundEffectInstance(ISoundEffect nativeSoundEffect); } } diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj index 70488076..f79df834 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj +++ b/SoundSystems/ANX.SoundSystem.OpenAL/ANX.SoundSystem.OpenAL.csproj @@ -40,12 +40,6 @@ ..\..\lib\OpenTK\OpenTK.Compatibility.dll - - - - - - @@ -54,6 +48,8 @@ True True + + diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs b/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs index d19c0574..c387d13b 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs @@ -153,23 +153,27 @@ namespace ANX.SoundSystem.OpenAL } #endregion - #region CreateSoundEffectInstance (TODO) - public ISoundEffectInstance CreateSoundEffectInstance(SoundEffect parent) + #region CreateSoundEffectInstance + public ISoundEffectInstance CreateSoundEffectInstance( + ISoundEffect nativeSoundEffect) { AddInSystemFactory.Instance.PreventSoundSystemChange(); - throw new NotImplementedException(); + return new OpenALSoundEffectInstance((OpenALSoundEffect)nativeSoundEffect); + } + #endregion + + #region CreateSoundEffect + public ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream) + { + AddInSystemFactory.Instance.PreventSoundSystemChange(); + return new OpenALSoundEffect(parent, stream); } #endregion #region CreateSoundEffect (TODO) - public ISoundEffect CreateSoundEffect(Stream stream) - { - AddInSystemFactory.Instance.PreventSoundSystemChange(); - throw new NotImplementedException(); - } - - public ISoundEffect CreateSoundEffect(byte[] buffer, int offset, int count, - int sampleRate, AudioChannels channels, int loopStart, int loopLength) + public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, + int offset, int count, int sampleRate, AudioChannels channels, + int loopStart, int loopLength) { AddInSystemFactory.Instance.PreventSoundSystemChange(); throw new NotImplementedException(); @@ -179,6 +183,7 @@ namespace ANX.SoundSystem.OpenAL #region CreateAudioListener (TODO) public IAudioListener CreateAudioListener() { + AddInSystemFactory.Instance.PreventSoundSystemChange(); throw new NotImplementedException(); } #endregion @@ -186,6 +191,7 @@ namespace ANX.SoundSystem.OpenAL #region CreateAudioEmitter (TODO) public IAudioEmitter CreateAudioEmitter() { + AddInSystemFactory.Instance.PreventSoundSystemChange(); throw new NotImplementedException(); } #endregion diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs new file mode 100644 index 00000000..bfc70a1c --- /dev/null +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs @@ -0,0 +1,38 @@ +using System; +using System.IO; +using ANX.Framework.Audio; +using ANX.Framework.NonXNA.SoundSystem; + +namespace ANX.SoundSystem.OpenAL +{ + public class OpenALSoundEffect : ISoundEffect + { + #region Private + internal SoundEffect parent; + #endregion + + #region Public (TODO) + public TimeSpan Duration + { + get + { + throw new NotImplementedException(); + } + } + #endregion + + #region Constructor + internal OpenALSoundEffect(SoundEffect setParent, Stream stream) + { + parent = setParent; + } + #endregion + + #region Dispose (TODO) + public void Dispose() + { + throw new NotImplementedException(); + } + #endregion + } +} diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs new file mode 100644 index 00000000..9b414f6d --- /dev/null +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs @@ -0,0 +1,110 @@ +using System; +using ANX.Framework.Audio; +using ANX.Framework.NonXNA.SoundSystem; + +namespace ANX.SoundSystem.OpenAL +{ + public class OpenALSoundEffectInstance : ISoundEffectInstance + { + #region Private + private OpenALSoundEffect parent; + #endregion + + #region Public (TODO) + public bool IsLooped + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + + public float Pan + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + + public float Pitch + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + + public SoundState State + { + get + { + throw new NotImplementedException(); + } + } + + public float Volume + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + #endregion + + #region Constructor + internal OpenALSoundEffectInstance(OpenALSoundEffect setParent) + { + parent = setParent; + } + #endregion + + public void Play() + { + throw new NotImplementedException(); + } + + public void Pause() + { + throw new NotImplementedException(); + } + + public void Stop(bool immediate) + { + throw new NotImplementedException(); + } + + public void Resume() + { + throw new NotImplementedException(); + } + + public void Apply3D(Framework.Audio.AudioListener[] listeners, Framework.Audio.AudioEmitter emitter) + { + throw new NotImplementedException(); + } + + #region Dispose (TODO) + public void Dispose() + { + throw new NotImplementedException(); + } + #endregion + } +} diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/ANX.SoundSystem.Windows.XAudio.csproj b/SoundSystems/ANX.SoundSystem.Windows.XAudio/ANX.SoundSystem.Windows.XAudio.csproj index 3020c6e5..78851be1 100644 --- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/ANX.SoundSystem.Windows.XAudio.csproj +++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/ANX.SoundSystem.Windows.XAudio.csproj @@ -41,12 +41,6 @@ ..\..\lib\SharpDX\Bin\SharpDX.XAudio2.dll - - - - - - diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs index fdac5b08..4f448795 100644 --- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs +++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs @@ -1,15 +1,8 @@ -#region Using Statements -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System; using System.IO; -using System.Runtime.InteropServices; +using ANX.Framework.Audio; using ANX.Framework.NonXNA; using ANX.Framework.NonXNA.SoundSystem; -using ANX.Framework.Audio; - -#endregion // Using Statements #region License @@ -62,29 +55,42 @@ namespace ANX.SoundSystem.Windows.XAudio { public class Creator : ISoundSystemCreator { - public void RegisterCreator(AddInSystemFactory factory) - { - factory.AddCreator(this); - } - + #region Public + #region Name public string Name { get { return "XAudio"; } } + #endregion + #region Priority public int Priority { get { return 10; } } + #endregion + #region IsSupported public bool IsSupported { get { //TODO: this is just a very basic version of test for support - return AddInSystemFactory.Instance.OperatingSystem.Platform == PlatformID.Win32NT; + return AddInSystemFactory.Instance.OperatingSystem.Platform == + PlatformID.Win32NT; } } + #endregion + #endregion + + #region RegisterCreator + public void RegisterCreator(AddInSystemFactory factory) + { + factory.AddCreator(this); + } + #endregion + + #region ISoundSystemCreator Member public float DistanceScale { @@ -134,38 +140,31 @@ namespace ANX.SoundSystem.Windows.XAudio } } - - #region ISoundSystemCreator Member - - public ISoundEffect CreateSoundEffect(Stream stream) - { - throw new NotImplementedException(); - } - - public ISoundEffect CreateSoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength) - { - throw new NotImplementedException(); - } - - public ISoundEffectInstance CreateSoundEffectInstance(SoundEffect parent) - { - throw new NotImplementedException(); - } - - #endregion - - #region CreateAudioListener (TODO) public IAudioListener CreateAudioListener() { throw new NotImplementedException(); } - #endregion - #region CreateAudioEmitter (TODO) public IAudioEmitter CreateAudioEmitter() { throw new NotImplementedException(); } + + public ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream) + { + throw new NotImplementedException(); + } + + public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength) + { + throw new NotImplementedException(); + } + + public ISoundEffectInstance CreateSoundEffectInstance(ISoundEffect nativeSoundEffect) + { + throw new NotImplementedException(); + } + #endregion } }