2012-01-25 15:31:58 +00:00
|
|
|
|
using System;
|
2012-02-12 11:28:59 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-11-15 19:52:09 +00:00
|
|
|
|
using System.IO;
|
2012-01-25 15:31:58 +00:00
|
|
|
|
using ANX.Framework.NonXNA;
|
2012-08-29 13:04:22 +00:00
|
|
|
|
using ANX.Framework.NonXNA.Development;
|
2012-08-29 18:07:54 +00:00
|
|
|
|
using ANX.Framework.NonXNA.SoundSystem;
|
2011-11-15 19:52:09 +00:00
|
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-11-15 19:52:09 +00:00
|
|
|
|
|
|
|
|
|
namespace ANX.Framework.Audio
|
|
|
|
|
{
|
2012-08-29 13:04:22 +00:00
|
|
|
|
[PercentageComplete(100)]
|
2012-09-30 11:14:44 +00:00
|
|
|
|
[Developer("AstrorEnales")]
|
|
|
|
|
[TestState(TestStateAttribute.TestState.InProgress)]
|
2012-01-25 15:31:58 +00:00
|
|
|
|
public sealed class SoundEffect : IDisposable
|
|
|
|
|
{
|
|
|
|
|
#region Static
|
2012-09-29 22:01:15 +00:00
|
|
|
|
public static float DistanceScale
|
|
|
|
|
{
|
2012-11-29 09:10:33 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
return creator.DistanceScale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
creator.DistanceScale = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("couldn't set DistanceScale because no supported SoundSystem was found");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float DopplerScale
|
|
|
|
|
{
|
2012-11-29 09:10:33 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
return creator.DopplerScale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
creator.DopplerScale = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("couldn't set DopplerScale because no supported SoundSystem was found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
|
|
|
|
|
|
public static float MasterVolume
|
|
|
|
|
{
|
2012-11-29 09:10:33 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
return creator.MasterVolume;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
creator.MasterVolume = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("couldn't set MasterVolume because no supported SoundSystem was found");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float SpeedOfSound
|
|
|
|
|
{
|
2012-11-29 09:10:33 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
return creator.SpeedOfSound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ISoundSystemCreator creator = GetCreator();
|
|
|
|
|
if (creator != null)
|
|
|
|
|
{
|
|
|
|
|
creator.SpeedOfSound = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("couldn't set SpeedOfSound because no supported SoundSystem was found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-25 15:31:58 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private
|
two commits were missing, both by KorsarNek:
"Removed the SupportedPlatformsImpl classes and replaced them with a new SupportedPlatforms attribute on the assembly level.
Removed a few class constructors which could cause problems when loading a game.
Made ResetElapsedTime in the game class reset to 0 instead of TimeSpan.MinValue.
Removed the restriction in the InputDeviceFactory for which InputDevices are supported.
Added a Logger for Metro which works with the current Logger implementation.
Changed that when a platform is recognized that is higher than Windows 8, it gets treated like Windows 8, not like Windows 7.
Due to the SupportedPlatforms change, the assembly loader is now faster in finding out which assemblies contains addIns. For not Metro system, it's also added that a warning gets written if an AddIn references a different ANX version than that of the running assembly.
OpenGL and DirectX have been updated to the newest versions.
XAudio system uses now the same SharpDX version as all the other systems.
ParameterBuffer for WindowsMetro gets now correctly created by considering the size constraints for constant buffers.
Fixed an erroneous finalizer in the xaudio system.
Made the metro projects convert to Windows 8.1, as Windows 8.0 is not supported by the newer SharpDX versions. It's now also necessary to use at least Visual Studio 2013 to build the Metro versions.
Made the samples work again on Windows."
"Fixed the creation of the swap chain for windows metro and removed the dependency of the Metro Rendersystem onto the Metro Platformsytem.
All occurrences of WindowHandles have been replaced with a custom WindowHandle type which should work out of the box in most cases, but does still represent a breaking change to XNA.
The ProjectConverter for Metro was adjusted so that with just changing the way the application is initialized, most projects that worked with ANX before should now work under win rt. The sample SimpleNoContent does now work out of the box for win rt, after a project conversion.
The application name for win rt apps is now a guid, the display name stayed the same though. That's to be more compliant with the way win rt apps are normally created.
The default namespace and namespace of the classes for the Sample "SimpleNoContent" is renamed from "SimpleModernUI" to "SimpleNoContent".
With the new way win rt apps are initialized for ANX, it's necessary to first create the WindowsGameHost for WinRT with a handler how to create the game instance and give that to the CoreApplication object to run it.
Also took care of a few annoying bugs when working with win rt and ANX where no InputDevices could be created on the first frame (Issue #1164 ) and that it wasn't possible to use the localfolder of the application on the first update and all the other stuff for which an instance of the Application class was necessary."
2015-03-29 13:48:33 +02:00
|
|
|
|
private static readonly List<SoundEffectInstance> fireAndForgetInstances = new List<SoundEffectInstance>();
|
|
|
|
|
private static ISoundSystemCreator creator;
|
2012-09-29 18:37:18 +00:00
|
|
|
|
private readonly List<WeakReference> children;
|
|
|
|
|
internal ISoundEffect NativeSoundEffect;
|
2012-01-25 15:31:58 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public
|
|
|
|
|
public TimeSpan Duration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-29 18:37:18 +00:00
|
|
|
|
return NativeSoundEffect.Duration;
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsDisposed
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
2012-08-26 12:53:00 +00:00
|
|
|
|
private SoundEffect()
|
|
|
|
|
{
|
|
|
|
|
children = new List<WeakReference>();
|
|
|
|
|
IsDisposed = false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-25 15:31:58 +00:00
|
|
|
|
private SoundEffect(Stream stream)
|
2012-08-26 12:53:00 +00:00
|
|
|
|
: this()
|
2012-01-25 15:31:58 +00:00
|
|
|
|
{
|
2012-08-29 13:04:22 +00:00
|
|
|
|
var creator = GetCreator();
|
2012-09-29 18:37:18 +00:00
|
|
|
|
NativeSoundEffect = creator.CreateSoundEffect(this, stream);
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels)
|
|
|
|
|
: this(buffer, 0, buffer.Length, sampleRate, channels, 0, 0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
|
public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart,
|
|
|
|
|
int loopLength)
|
2012-08-26 12:53:00 +00:00
|
|
|
|
: this()
|
2012-01-25 15:31:58 +00:00
|
|
|
|
{
|
2012-08-29 13:04:22 +00:00
|
|
|
|
var creator = GetCreator();
|
2012-09-29 22:01:15 +00:00
|
|
|
|
NativeSoundEffect = creator.CreateSoundEffect(this, buffer, offset, count, sampleRate, channels, loopStart,
|
|
|
|
|
loopLength);
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~SoundEffect()
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-02-11 23:53:03 +00:00
|
|
|
|
#region GetCreator
|
|
|
|
|
private static ISoundSystemCreator GetCreator()
|
|
|
|
|
{
|
two commits were missing, both by KorsarNek:
"Removed the SupportedPlatformsImpl classes and replaced them with a new SupportedPlatforms attribute on the assembly level.
Removed a few class constructors which could cause problems when loading a game.
Made ResetElapsedTime in the game class reset to 0 instead of TimeSpan.MinValue.
Removed the restriction in the InputDeviceFactory for which InputDevices are supported.
Added a Logger for Metro which works with the current Logger implementation.
Changed that when a platform is recognized that is higher than Windows 8, it gets treated like Windows 8, not like Windows 7.
Due to the SupportedPlatforms change, the assembly loader is now faster in finding out which assemblies contains addIns. For not Metro system, it's also added that a warning gets written if an AddIn references a different ANX version than that of the running assembly.
OpenGL and DirectX have been updated to the newest versions.
XAudio system uses now the same SharpDX version as all the other systems.
ParameterBuffer for WindowsMetro gets now correctly created by considering the size constraints for constant buffers.
Fixed an erroneous finalizer in the xaudio system.
Made the metro projects convert to Windows 8.1, as Windows 8.0 is not supported by the newer SharpDX versions. It's now also necessary to use at least Visual Studio 2013 to build the Metro versions.
Made the samples work again on Windows."
"Fixed the creation of the swap chain for windows metro and removed the dependency of the Metro Rendersystem onto the Metro Platformsytem.
All occurrences of WindowHandles have been replaced with a custom WindowHandle type which should work out of the box in most cases, but does still represent a breaking change to XNA.
The ProjectConverter for Metro was adjusted so that with just changing the way the application is initialized, most projects that worked with ANX before should now work under win rt. The sample SimpleNoContent does now work out of the box for win rt, after a project conversion.
The application name for win rt apps is now a guid, the display name stayed the same though. That's to be more compliant with the way win rt apps are normally created.
The default namespace and namespace of the classes for the Sample "SimpleNoContent" is renamed from "SimpleModernUI" to "SimpleNoContent".
With the new way win rt apps are initialized for ANX, it's necessary to first create the WindowsGameHost for WinRT with a handler how to create the game instance and give that to the CoreApplication object to run it.
Also took care of a few annoying bugs when working with win rt and ANX where no InputDevices could be created on the first frame (Issue #1164 ) and that it wasn't possible to use the localfolder of the application on the first update and all the other stuff for which an instance of the Application class was necessary."
2015-03-29 13:48:33 +02:00
|
|
|
|
if (creator == null)
|
|
|
|
|
{
|
|
|
|
|
creator = AddInSystemFactory.Instance.GetDefaultCreator<ISoundSystemCreator>();
|
|
|
|
|
AddInSystemFactory.Instance.PreventSystemChange(AddInType.SoundSystem);
|
|
|
|
|
|
|
|
|
|
//We are once setting the default values, which means, we don't support chaning the sound system later on, otherwise we would have
|
|
|
|
|
//uninitiliazed values.
|
|
|
|
|
MasterVolume = 1f;
|
|
|
|
|
SpeedOfSound = 343.5f;
|
|
|
|
|
DopplerScale = 1f;
|
|
|
|
|
DistanceScale = 1f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return creator;
|
2012-02-11 23:53:03 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-01-25 15:31:58 +00:00
|
|
|
|
#region CreateInstance
|
|
|
|
|
public SoundEffectInstance CreateInstance()
|
|
|
|
|
{
|
2012-02-12 11:28:59 +00:00
|
|
|
|
return new SoundEffectInstance(this, false);
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region FromStream
|
|
|
|
|
public static SoundEffect FromStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
return new SoundEffect(stream);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region GetSampleDuration
|
2012-08-26 12:53:00 +00:00
|
|
|
|
public static TimeSpan GetSampleDuration(int sizeInBytes, int sampleRate, AudioChannels channels)
|
2012-01-25 15:31:58 +00:00
|
|
|
|
{
|
2012-09-29 18:37:18 +00:00
|
|
|
|
float sizeMulBlockAlign = (float)sizeInBytes / ((int)channels * 2);
|
|
|
|
|
return TimeSpan.FromMilliseconds(sizeMulBlockAlign * 1000f / sampleRate);
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region GetSampleSizeInBytes
|
2012-09-29 18:37:18 +00:00
|
|
|
|
public static int GetSampleSizeInBytes(TimeSpan duration, int sampleRate, AudioChannels channels)
|
2012-01-25 15:31:58 +00:00
|
|
|
|
{
|
2012-09-29 18:37:18 +00:00
|
|
|
|
int timeMulSamples = (int)(duration.TotalMilliseconds * (sampleRate / 1000f));
|
2012-08-26 12:53:00 +00:00
|
|
|
|
return (timeMulSamples + timeMulSamples % (int)channels) * ((int)channels * 2);
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-02-12 11:28:59 +00:00
|
|
|
|
#region Play
|
2012-01-25 15:31:58 +00:00
|
|
|
|
public bool Play()
|
|
|
|
|
{
|
2012-08-29 13:04:22 +00:00
|
|
|
|
return Play(1f, 1f, 0f);
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Play(float volume, float pitch, float pan)
|
|
|
|
|
{
|
2012-02-12 11:28:59 +00:00
|
|
|
|
if (IsDisposed)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-09-29 18:37:18 +00:00
|
|
|
|
var newInstance = new SoundEffectInstance(this, true)
|
|
|
|
|
{
|
|
|
|
|
Volume = volume,
|
|
|
|
|
Pitch = pitch,
|
|
|
|
|
Pan = pan,
|
|
|
|
|
};
|
2012-02-12 11:28:59 +00:00
|
|
|
|
|
|
|
|
|
children.Add(new WeakReference(newInstance));
|
|
|
|
|
|
|
|
|
|
lock (fireAndForgetInstances)
|
|
|
|
|
fireAndForgetInstances.Add(newInstance);
|
|
|
|
|
|
|
|
|
|
newInstance.Play();
|
|
|
|
|
}
|
2012-08-29 13:04:22 +00:00
|
|
|
|
catch (Exception ex)
|
2012-02-12 11:28:59 +00:00
|
|
|
|
{
|
2012-08-29 13:04:22 +00:00
|
|
|
|
Logger.Warning("Failed to play sound effect cause of: " + ex);
|
2012-02-12 11:28:59 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
|
#region RecycleStoppedFireAndForgetInstances
|
|
|
|
|
internal static void RecycleStoppedFireAndForgetInstances()
|
|
|
|
|
{
|
|
|
|
|
lock (fireAndForgetInstances)
|
|
|
|
|
{
|
|
|
|
|
var instancesToDispose = new List<SoundEffectInstance>();
|
|
|
|
|
foreach (SoundEffectInstance current in fireAndForgetInstances)
|
|
|
|
|
if (current.State == SoundState.Stopped)
|
|
|
|
|
instancesToDispose.Add(current);
|
|
|
|
|
|
|
|
|
|
foreach (SoundEffectInstance current in instancesToDispose)
|
|
|
|
|
{
|
|
|
|
|
current.Dispose();
|
|
|
|
|
fireAndForgetInstances.Remove(current);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Dispose
|
|
|
|
|
public void Dispose()
|
2012-01-25 15:31:58 +00:00
|
|
|
|
{
|
2012-02-12 11:28:59 +00:00
|
|
|
|
if (IsDisposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
2012-09-29 18:37:18 +00:00
|
|
|
|
NativeSoundEffect.Dispose();
|
|
|
|
|
NativeSoundEffect = null;
|
2012-02-12 11:28:59 +00:00
|
|
|
|
|
2012-09-29 18:37:18 +00:00
|
|
|
|
var weakRefs = new List<WeakReference>(children);
|
2012-02-12 11:28:59 +00:00
|
|
|
|
|
|
|
|
|
lock (fireAndForgetInstances)
|
|
|
|
|
{
|
|
|
|
|
foreach (WeakReference current in weakRefs)
|
|
|
|
|
{
|
2012-09-29 18:37:18 +00:00
|
|
|
|
var soundInstance = current.Target as SoundEffectInstance;
|
|
|
|
|
if (soundInstance == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (soundInstance.IsFireAndForget)
|
|
|
|
|
fireAndForgetInstances.Remove(soundInstance);
|
|
|
|
|
soundInstance.Dispose();
|
2012-02-12 11:28:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
weakRefs.Clear();
|
|
|
|
|
children.Clear();
|
2012-01-25 15:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2011-11-15 19:52:09 +00:00
|
|
|
|
}
|