2012-08-09 09:45:04 +00:00
|
|
|
using System;
|
2012-08-29 20:17:44 +00:00
|
|
|
using System.Collections.ObjectModel;
|
2011-10-31 05:36:24 +00:00
|
|
|
using System.IO;
|
2012-02-12 11:28:59 +00:00
|
|
|
using ANX.Framework.Audio;
|
2012-09-29 22:01:15 +00:00
|
|
|
using ANX.Framework.Media;
|
2011-10-31 05:36:24 +00:00
|
|
|
using ANX.Framework.NonXNA;
|
2012-02-11 23:53:03 +00:00
|
|
|
using ANX.Framework.NonXNA.SoundSystem;
|
2012-08-29 20:17:44 +00:00
|
|
|
using SharpDX.XAudio2;
|
2011-10-31 05:36:24 +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-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
namespace ANX.SoundSystem.Windows.XAudio
|
|
|
|
{
|
2012-02-11 23:53:03 +00:00
|
|
|
public class Creator : ISoundSystemCreator
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
private float distanceScale;
|
|
|
|
private float dopplerScale;
|
|
|
|
private float speedOfSound;
|
2012-10-03 20:47:56 +00:00
|
|
|
private XAudio2 device;
|
2012-09-29 18:37:18 +00:00
|
|
|
internal static MasteringVoice MasteringVoice { get; private set; }
|
2012-08-29 20:17:44 +00:00
|
|
|
|
2012-02-12 11:28:59 +00:00
|
|
|
#region Public
|
2012-02-11 23:53:03 +00:00
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return "XAudio"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Priority
|
|
|
|
{
|
|
|
|
get { return 10; }
|
|
|
|
}
|
|
|
|
|
2012-09-29 18:37:18 +00:00
|
|
|
public bool IsSupported
|
|
|
|
{
|
2012-10-03 20:47:56 +00:00
|
|
|
get { return OSInformation.IsWindows || OSInformation.GetName() == PlatformName.Windows8ModernUI; }
|
2012-09-29 18:37:18 +00:00
|
|
|
}
|
2012-02-11 23:53:03 +00:00
|
|
|
|
2012-09-29 18:37:18 +00:00
|
|
|
public float DistanceScale
|
|
|
|
{
|
|
|
|
get { return distanceScale; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
distanceScale = value;
|
|
|
|
// TODO: actually set the parameter to XAudio
|
|
|
|
}
|
2012-02-11 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public float DopplerScale
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
get { return dopplerScale; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
dopplerScale = value;
|
|
|
|
// TODO: actually set the parameter to XAudio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float MasterVolume
|
|
|
|
{
|
2012-10-03 20:47:56 +00:00
|
|
|
get { return MasteringVoice != null ? MasteringVoice.Volume : 0f; }
|
2012-10-01 11:39:30 +00:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (MasteringVoice != null)
|
|
|
|
MasteringVoice.SetVolume(value, 0);
|
|
|
|
}
|
2012-09-29 18:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public float SpeedOfSound
|
|
|
|
{
|
|
|
|
get { return speedOfSound; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
speedOfSound = value;
|
|
|
|
// TODO: actually set the parameter to XAudio
|
|
|
|
}
|
2012-08-29 20:17:44 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructor
|
2012-10-03 20:47:56 +00:00
|
|
|
public Creator()
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
2012-10-03 20:47:56 +00:00
|
|
|
distanceScale = 1f;
|
2012-09-29 18:37:18 +00:00
|
|
|
dopplerScale = 1f;
|
|
|
|
speedOfSound = 343.5f;
|
2012-10-01 11:39:30 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
device = new XAudio2();
|
|
|
|
}
|
2012-10-03 20:47:56 +00:00
|
|
|
catch (Exception)
|
2012-10-01 11:39:30 +00:00
|
|
|
{
|
|
|
|
device = null;
|
|
|
|
//TODO: error handling
|
|
|
|
System.Diagnostics.Debugger.Break();
|
|
|
|
}
|
2012-10-03 20:47:56 +00:00
|
|
|
|
2012-10-01 11:39:30 +00:00
|
|
|
if (device != null)
|
|
|
|
MasteringVoice = new MasteringVoice(device, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate);
|
2012-09-29 18:37:18 +00:00
|
|
|
}
|
2012-08-29 20:17:44 +00:00
|
|
|
|
2012-10-03 20:47:56 +00:00
|
|
|
~Creator()
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
if (MasteringVoice != null)
|
2012-09-29 22:01:15 +00:00
|
|
|
{
|
|
|
|
MasteringVoice.DestroyVoice();
|
2012-09-29 18:37:18 +00:00
|
|
|
MasteringVoice.Dispose();
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
2012-09-29 18:37:18 +00:00
|
|
|
|
|
|
|
if (device != null)
|
|
|
|
device.Dispose();
|
|
|
|
|
|
|
|
MasteringVoice = null;
|
|
|
|
device = null;
|
|
|
|
}
|
|
|
|
#endregion
|
2012-02-11 23:53:03 +00:00
|
|
|
|
2012-02-12 11:28:59 +00:00
|
|
|
public IAudioListener CreateAudioListener()
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-02-11 23:53:03 +00:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-02-12 11:28:59 +00:00
|
|
|
public IAudioEmitter CreateAudioEmitter()
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-02-11 23:53:03 +00:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-08-29 20:17:44 +00:00
|
|
|
#region CreateSoundEffect
|
2012-02-12 11:28:59 +00:00
|
|
|
public ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream)
|
2012-02-11 23:53:03 +00:00
|
|
|
{
|
2012-08-29 20:17:44 +00:00
|
|
|
PreventSystemChange();
|
|
|
|
return new XAudioSoundEffect(parent, stream);
|
2012-02-11 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2012-08-29 20:17:44 +00:00
|
|
|
public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset, int count, int sampleRate,
|
|
|
|
AudioChannels channels, int loopStart, int loopLength)
|
2012-02-11 23:53:03 +00:00
|
|
|
{
|
2012-08-29 20:17:44 +00:00
|
|
|
PreventSystemChange();
|
|
|
|
return new XAudioSoundEffect(parent, buffer, offset, count, sampleRate, channels, loopStart, loopLength);
|
2012-02-11 23:53:03 +00:00
|
|
|
}
|
2012-08-29 20:17:44 +00:00
|
|
|
#endregion
|
2012-02-11 23:53:03 +00:00
|
|
|
|
2012-08-29 20:17:44 +00:00
|
|
|
#region CreateSoundEffectInstance
|
2012-02-12 11:28:59 +00:00
|
|
|
public ISoundEffectInstance CreateSoundEffectInstance(ISoundEffect nativeSoundEffect)
|
2012-02-11 23:53:03 +00:00
|
|
|
{
|
2012-08-29 20:17:44 +00:00
|
|
|
PreventSystemChange();
|
|
|
|
return new XAudioSoundEffectInstance(device, nativeSoundEffect as XAudioSoundEffect);
|
2012-02-11 23:53:03 +00:00
|
|
|
}
|
|
|
|
#endregion
|
2012-08-22 14:28:22 +00:00
|
|
|
|
2012-10-03 20:47:56 +00:00
|
|
|
public IDynamicSoundEffectInstance CreateDynamicSoundEffectInstance()
|
|
|
|
{
|
|
|
|
PreventSystemChange();
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-08-22 14:28:22 +00:00
|
|
|
public IMicrophone CreateMicrophone(Microphone managedMicrophone)
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-08-22 14:28:22 +00:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-08-29 20:17:44 +00:00
|
|
|
public ReadOnlyCollection<Microphone> GetAllMicrophones()
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-08-22 14:28:22 +00:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-08-29 20:17:44 +00:00
|
|
|
public int GetDefaultMicrophone(ReadOnlyCollection<Microphone> allMicrophones)
|
2012-09-29 18:37:18 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-08-22 14:28:22 +00:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-10-03 20:47:56 +00:00
|
|
|
#region CreateSong
|
2012-09-29 22:01:15 +00:00
|
|
|
public ISong CreateSong(Song parentSong, Uri uri)
|
2012-09-30 08:28:17 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-10-03 20:47:56 +00:00
|
|
|
return new XAudioSong(device, uri);
|
2012-09-30 08:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 20:47:56 +00:00
|
|
|
public ISong CreateSong(Song parentSong, string filepath, int duration)
|
2012-09-29 22:01:15 +00:00
|
|
|
{
|
|
|
|
PreventSystemChange();
|
2012-10-03 20:47:56 +00:00
|
|
|
return new XAudioSong(device, filepath, duration);
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
2012-10-03 20:47:56 +00:00
|
|
|
#endregion
|
2012-09-29 22:01:15 +00:00
|
|
|
|
2012-09-29 18:37:18 +00:00
|
|
|
private static void PreventSystemChange()
|
2012-08-29 20:17:44 +00:00
|
|
|
{
|
|
|
|
AddInSystemFactory.Instance.PreventSystemChange(AddInType.SoundSystem);
|
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|