- SoundEffect and SoundEffectInstance now have all members implemented to use the native implementation

- Started OpenAL SoundSystem
This commit is contained in:
SND\AstrorEnales_cp 2012-02-12 11:28:59 +00:00
parent d8ab2ef4cc
commit 70f30907ff
9 changed files with 295 additions and 78 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using ANX.Framework.NonXNA; using ANX.Framework.NonXNA;
using ANX.Framework.NonXNA.SoundSystem; using ANX.Framework.NonXNA.SoundSystem;
@ -113,7 +114,11 @@ namespace ANX.Framework.Audio
#endregion #endregion
#region Private #region Private
private ISoundEffect nativeSoundEffect; internal ISoundEffect nativeSoundEffect;
private static List<SoundEffectInstance> fireAndForgetInstances;
private List<WeakReference> children = new List<WeakReference>();
#endregion #endregion
#region Public #region Public
@ -139,9 +144,17 @@ namespace ANX.Framework.Audio
#endregion #endregion
#region Constructor #region Constructor
static SoundEffect()
{
MasterVolume = 1f;
SpeedOfSound = 343.5f;
DopplerScale = 1f;
DistanceScale = 1f;
}
private SoundEffect(Stream stream) private SoundEffect(Stream stream)
{ {
nativeSoundEffect = GetCreator().CreateSoundEffect(stream); nativeSoundEffect = GetCreator().CreateSoundEffect(this, stream);
} }
public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels) 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, public SoundEffect(byte[] buffer, int offset, int count, int sampleRate,
AudioChannels channels, int loopStart, int loopLength) AudioChannels channels, int loopStart, int loopLength)
{ {
nativeSoundEffect = GetCreator().CreateSoundEffect(buffer, offset, nativeSoundEffect = GetCreator().CreateSoundEffect(this, buffer, offset,
count, sampleRate, channels, loopStart, loopLength); count, sampleRate, channels, loopStart, loopLength);
} }
@ -172,7 +185,7 @@ namespace ANX.Framework.Audio
#region CreateInstance #region CreateInstance
public SoundEffectInstance CreateInstance() public SoundEffectInstance CreateInstance()
{ {
return new SoundEffectInstance(this); return new SoundEffectInstance(this, false);
} }
#endregion #endregion
@ -204,7 +217,7 @@ namespace ANX.Framework.Audio
} }
#endregion #endregion
#region Play (TODO) #region Play
public bool Play() public bool Play()
{ {
return Play(1f, 0f, 0f); return Play(1f, 0f, 0f);
@ -212,20 +225,72 @@ namespace ANX.Framework.Audio
public bool Play(float volume, float pitch, float pan) public bool Play(float volume, float pitch, float pan)
{ {
// TODO: fire and forget play if (IsDisposed)
throw new NotImplementedException(); {
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 #endregion
#region Dispose #region Dispose
public void Dispose() public void Dispose()
{ {
if (IsDisposed == false) if (IsDisposed)
{ {
IsDisposed = true; return;
nativeSoundEffect.Dispose();
nativeSoundEffect = null;
} }
IsDisposed = true;
nativeSoundEffect.Dispose();
nativeSoundEffect = null;
List<WeakReference> weakRefs = new List<WeakReference>(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 #endregion
} }

View File

@ -58,6 +58,12 @@ namespace ANX.Framework.Audio
private SoundEffect parent; private SoundEffect parent;
private ISoundEffectInstance nativeInstance; private ISoundEffectInstance nativeInstance;
internal bool IsFireAndForget
{
get;
private set;
}
#endregion #endregion
#region Public #region Public
@ -141,11 +147,13 @@ namespace ANX.Framework.Audio
{ {
} }
internal SoundEffectInstance(SoundEffect setParent) internal SoundEffectInstance(SoundEffect setParent, bool setIsFireAndForget)
{ {
parent = setParent; parent = setParent;
IsFireAndForget = setIsFireAndForget;
nativeInstance = GetCreator().CreateSoundEffectInstance(setParent); nativeInstance = GetCreator().CreateSoundEffectInstance(
setParent.nativeSoundEffect);
} }
~SoundEffectInstance() ~SoundEffectInstance()

View File

@ -81,11 +81,12 @@ namespace ANX.Framework.NonXNA
IAudioEmitter CreateAudioEmitter(); IAudioEmitter CreateAudioEmitter();
ISoundEffect CreateSoundEffect(Stream stream); ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream);
ISoundEffect CreateSoundEffect(byte[] buffer, int offset, int count, ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset,
int sampleRate, AudioChannels channels, int loopStart, int loopLength); int count, int sampleRate, AudioChannels channels, int loopStart,
int loopLength);
ISoundEffectInstance CreateSoundEffectInstance(SoundEffect parent); ISoundEffectInstance CreateSoundEffectInstance(ISoundEffect nativeSoundEffect);
} }
} }

View File

@ -40,12 +40,6 @@
<HintPath>..\..\lib\OpenTK\OpenTK.Compatibility.dll</HintPath> <HintPath>..\..\lib\OpenTK\OpenTK.Compatibility.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Creator.cs" /> <Compile Include="Creator.cs" />
@ -54,6 +48,8 @@
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
</Compile> </Compile>
<Compile Include="OpenALSoundEffect.cs" />
<Compile Include="OpenALSoundEffectInstance.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -153,23 +153,27 @@ namespace ANX.SoundSystem.OpenAL
} }
#endregion #endregion
#region CreateSoundEffectInstance (TODO) #region CreateSoundEffectInstance
public ISoundEffectInstance CreateSoundEffectInstance(SoundEffect parent) public ISoundEffectInstance CreateSoundEffectInstance(
ISoundEffect nativeSoundEffect)
{ {
AddInSystemFactory.Instance.PreventSoundSystemChange(); 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 #endregion
#region CreateSoundEffect (TODO) #region CreateSoundEffect (TODO)
public ISoundEffect CreateSoundEffect(Stream stream) public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer,
{ int offset, int count, int sampleRate, AudioChannels channels,
AddInSystemFactory.Instance.PreventSoundSystemChange(); int loopStart, int loopLength)
throw new NotImplementedException();
}
public ISoundEffect CreateSoundEffect(byte[] buffer, int offset, int count,
int sampleRate, AudioChannels channels, int loopStart, int loopLength)
{ {
AddInSystemFactory.Instance.PreventSoundSystemChange(); AddInSystemFactory.Instance.PreventSoundSystemChange();
throw new NotImplementedException(); throw new NotImplementedException();
@ -179,6 +183,7 @@ namespace ANX.SoundSystem.OpenAL
#region CreateAudioListener (TODO) #region CreateAudioListener (TODO)
public IAudioListener CreateAudioListener() public IAudioListener CreateAudioListener()
{ {
AddInSystemFactory.Instance.PreventSoundSystemChange();
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion #endregion
@ -186,6 +191,7 @@ namespace ANX.SoundSystem.OpenAL
#region CreateAudioEmitter (TODO) #region CreateAudioEmitter (TODO)
public IAudioEmitter CreateAudioEmitter() public IAudioEmitter CreateAudioEmitter()
{ {
AddInSystemFactory.Instance.PreventSoundSystemChange();
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion #endregion

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -41,12 +41,6 @@
<HintPath>..\..\lib\SharpDX\Bin\SharpDX.XAudio2.dll</HintPath> <HintPath>..\..\lib\SharpDX\Bin\SharpDX.XAudio2.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Creator.cs" /> <Compile Include="Creator.cs" />

View File

@ -1,15 +1,8 @@
#region Using Statements using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using ANX.Framework.Audio;
using ANX.Framework.NonXNA; using ANX.Framework.NonXNA;
using ANX.Framework.NonXNA.SoundSystem; using ANX.Framework.NonXNA.SoundSystem;
using ANX.Framework.Audio;
#endregion // Using Statements
#region License #region License
@ -62,29 +55,42 @@ namespace ANX.SoundSystem.Windows.XAudio
{ {
public class Creator : ISoundSystemCreator public class Creator : ISoundSystemCreator
{ {
public void RegisterCreator(AddInSystemFactory factory) #region Public
{ #region Name
factory.AddCreator(this);
}
public string Name public string Name
{ {
get { return "XAudio"; } get { return "XAudio"; }
} }
#endregion
#region Priority
public int Priority public int Priority
{ {
get { return 10; } get { return 10; }
} }
#endregion
#region IsSupported
public bool IsSupported public bool IsSupported
{ {
get get
{ {
//TODO: this is just a very basic version of test for support //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 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() public IAudioListener CreateAudioListener()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion
#region CreateAudioEmitter (TODO)
public IAudioEmitter CreateAudioEmitter() public IAudioEmitter CreateAudioEmitter()
{ {
throw new NotImplementedException(); 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 #endregion
} }
} }