2012-08-29 20:17:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using ANX.Framework.Audio;
|
2012-10-04 07:07:18 +00:00
|
|
|
|
using ANX.Framework.NonXNA.Development;
|
2012-08-29 20:17:44 +00:00
|
|
|
|
using ANX.Framework.NonXNA.SoundSystem;
|
2012-10-08 11:28:38 +00:00
|
|
|
|
using SharpDX;
|
2012-08-29 20:17:44 +00:00
|
|
|
|
using SharpDX.Multimedia;
|
2012-09-29 18:37:18 +00:00
|
|
|
|
using SharpDX.XAudio2;
|
2012-08-29 20:17:44 +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
|
|
|
|
|
|
|
|
|
|
namespace ANX.SoundSystem.Windows.XAudio
|
|
|
|
|
{
|
2012-10-04 07:07:18 +00:00
|
|
|
|
[Developer("AstrorEnales")]
|
2012-08-29 20:17:44 +00:00
|
|
|
|
public class XAudioSoundEffect : ISoundEffect
|
|
|
|
|
{
|
|
|
|
|
#region Private
|
|
|
|
|
private TimeSpan duration;
|
2012-09-29 18:37:18 +00:00
|
|
|
|
internal WaveFormat WaveFormat;
|
|
|
|
|
internal AudioBuffer AudioBuffer;
|
2012-08-29 20:17:44 +00:00
|
|
|
|
internal uint[] DecodedPacketsInfo;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public
|
2012-09-29 18:37:18 +00:00
|
|
|
|
public TimeSpan Duration
|
|
|
|
|
{
|
|
|
|
|
get { return duration; }
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2012-08-29 20:17:44 +00:00
|
|
|
|
|
|
|
|
|
#region Constructor
|
2012-10-04 07:07:18 +00:00
|
|
|
|
internal XAudioSoundEffect(Stream stream)
|
2012-08-29 20:17:44 +00:00
|
|
|
|
{
|
|
|
|
|
CreateFromStream(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 07:07:18 +00:00
|
|
|
|
internal XAudioSoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels,
|
|
|
|
|
int loopStart, int loopLength)
|
2012-10-08 11:28:38 +00:00
|
|
|
|
{
|
|
|
|
|
WaveFormat = new WaveFormat(sampleRate, (int)channels);
|
|
|
|
|
AudioBuffer = new AudioBuffer
|
|
|
|
|
{
|
|
|
|
|
LoopBegin = loopStart,
|
|
|
|
|
LoopLength = loopLength,
|
|
|
|
|
AudioBytes = count,
|
|
|
|
|
Flags = BufferFlags.EndOfStream
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IntPtr handle;
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
fixed (byte* ptr = &buffer[0])
|
|
|
|
|
handle = (IntPtr)(ptr + offset);
|
|
|
|
|
}
|
|
|
|
|
AudioBuffer.Stream = new DataStream(handle, count, false, false);
|
|
|
|
|
|
|
|
|
|
float sizeMulBlockAlign = (float)count / (WaveFormat.Channels * 2);
|
|
|
|
|
duration = TimeSpan.FromMilliseconds(sizeMulBlockAlign * 1000f / WaveFormat.SampleRate);
|
2012-08-29 20:17:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~XAudioSoundEffect()
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region CreateFromStream
|
|
|
|
|
private void CreateFromStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
var soundStream = new SoundStream(stream);
|
2012-09-29 18:37:18 +00:00
|
|
|
|
WaveFormat = soundStream.Format;
|
|
|
|
|
AudioBuffer = new AudioBuffer
|
2012-08-29 20:17:44 +00:00
|
|
|
|
{
|
|
|
|
|
Stream = soundStream.ToDataStream(),
|
|
|
|
|
AudioBytes = (int)stream.Length,
|
|
|
|
|
Flags = BufferFlags.EndOfStream
|
|
|
|
|
};
|
|
|
|
|
|
2012-09-29 18:37:18 +00:00
|
|
|
|
float sizeMulBlockAlign = (float)soundStream.Length / (WaveFormat.Channels * 2);
|
|
|
|
|
duration = TimeSpan.FromMilliseconds(sizeMulBlockAlign * 1000f / WaveFormat.SampleRate);
|
2012-08-29 20:17:44 +00:00
|
|
|
|
|
|
|
|
|
DecodedPacketsInfo = soundStream.DecodedPacketsInfo;
|
2012-09-08 09:07:23 +00:00
|
|
|
|
|
|
|
|
|
soundStream.Dispose();
|
2012-08-29 20:17:44 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Dispose
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2012-09-29 18:37:18 +00:00
|
|
|
|
WaveFormat = null;
|
|
|
|
|
AudioBuffer = null;
|
2012-08-29 20:17:44 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|