From c99598446fe3168d9fd9facfd13ea48282a2d46c Mon Sep 17 00:00:00 2001 From: "SND\\AstrorEnales_cp" Date: Thu, 4 Oct 2012 07:07:18 +0000 Subject: [PATCH] Fixed that Uri produces %20 in the paths and fixed an OpenALSoundEffect constructor --- .../ANX.SoundSystem.OpenAL/Creator.cs | 6 +- .../OpenALAudioListener.cs | 2 + .../ANX.SoundSystem.OpenAL/OpenALSong.cs | 3 +- .../OpenALSoundEffect.cs | 56 ++++++++++--------- .../OpenALSoundEffectInstance.cs | 4 +- .../SupportedPlatformsImpl.cs | 2 +- .../ANX.SoundSystem.Windows.XAudio/Creator.cs | 4 +- .../XAudioSong.cs | 3 +- .../XAudioSoundEffect.cs | 21 +++---- .../XAudioSoundEffectInstance.cs | 2 + Support/WaveUtils/WaveInfo.cs | 4 +- 11 files changed, 54 insertions(+), 53 deletions(-) diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs b/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs index 17dcb135..446e9338 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/Creator.cs @@ -106,16 +106,14 @@ namespace ANX.SoundSystem.OpenAL public ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream) { PreventSystemChange(); - return new OpenALSoundEffect(parent, stream); + return new OpenALSoundEffect(stream); } - #endregion - #region CreateSoundEffect public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength) { PreventSystemChange(); - return new OpenALSoundEffect(parent, buffer, offset, count, sampleRate, channels, loopStart, loopLength); + return new OpenALSoundEffect(buffer, offset, count, sampleRate, channels, loopStart, loopLength); } #endregion diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALAudioListener.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALAudioListener.cs index 6a4b85fe..db352fc7 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALAudioListener.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALAudioListener.cs @@ -1,5 +1,6 @@ using System; using ANX.Framework; +using ANX.Framework.NonXNA.Development; using ANX.Framework.NonXNA.SoundSystem; using OpenTK.Audio.OpenAL; @@ -9,6 +10,7 @@ using OpenTK.Audio.OpenAL; namespace ANX.SoundSystem.OpenAL { + [Developer("AstrorEnales")] public class OpenALAudioListener : IAudioListener { #region Private diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSong.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSong.cs index ae41779a..c0d1be27 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSong.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSong.cs @@ -30,8 +30,9 @@ namespace ANX.SoundSystem.OpenAL public OpenALSong(Song setParent, Uri uri) { + string path = uri.AbsolutePath.Replace("%20", ""); parent = setParent; - Init(uri.AbsolutePath); + Init(path); // TODO: duration } diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs index 1721cbd0..98386b74 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffect.cs @@ -1,6 +1,7 @@ using System; using System.IO; using ANX.Framework.Audio; +using ANX.Framework.NonXNA.Development; using ANX.Framework.NonXNA.SoundSystem; using OpenTK.Audio.OpenAL; using WaveUtils; @@ -12,45 +13,47 @@ using ALFormat = OpenTK.Audio.OpenAL.ALFormat; namespace ANX.SoundSystem.OpenAL { + [Developer("AstrorEnales")] public class OpenALSoundEffect : ISoundEffect { #region Private - internal SoundEffect parent; private WaveInfo waveInfo; private TimeSpan duration; - internal int bufferHandle; + internal int BufferHandle { get; private set; } #endregion #region Public - public TimeSpan Duration - { - get - { - return duration; - } - } - #endregion + public TimeSpan Duration + { + get { return duration; } + } + #endregion #region Constructor - internal OpenALSoundEffect(SoundEffect setParent, Stream stream) + internal OpenALSoundEffect(Stream stream) { - parent = setParent; CreateFromStream(stream); } - internal OpenALSoundEffect(SoundEffect setParent, byte[] buffer, int offset, int count, int sampleRate, - AudioChannels channels, int loopStart, int loopLength) + internal OpenALSoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, + int loopStart, int loopLength) { - parent = setParent; + // TODO: loopStart and loopLength - using (MemoryStream stream = new MemoryStream()) - { - BinaryWriter writer = new BinaryWriter(stream); - writer.Write(buffer, offset, count); - stream.Position = 0; + byte[] subBuffer = new byte[count]; + Array.Copy(buffer, offset, subBuffer, 0, count); + BufferHandle = AL.GenBuffer(); - CreateFromStream(stream); - } + // TODO: evaluate if 8bit or 16bit!! + ALFormat format = channels == AudioChannels.Mono ? ALFormat.Mono8 : ALFormat.Stereo8; + AL.BufferData(BufferHandle, format, subBuffer, count, sampleRate); + + float sizeMulBlockAlign = count / ((int)channels * 2f); + duration = TimeSpan.FromMilliseconds(sizeMulBlockAlign * 1000f / sampleRate); + + ALError error = AL.GetError(); + if (error != ALError.NoError) + throw new Exception("OpenAL error " + error + ": " + AL.GetErrorString(error)); } #endregion @@ -60,14 +63,14 @@ namespace ANX.SoundSystem.OpenAL waveInfo = WaveFile.LoadData(stream); if (waveInfo.WaveFormat != WaveFormat.PCM) { - WaveConverter converter = new WaveConverter(waveInfo); + var converter = new WaveConverter(waveInfo); converter.ConvertToPcm(); } duration = waveInfo.CalculateDuration(); - bufferHandle = AL.GenBuffer(); - AL.BufferData(bufferHandle, (ALFormat)waveInfo.ALFormat, waveInfo.Data, waveInfo.Data.Length, waveInfo.SampleRate); + BufferHandle = AL.GenBuffer(); + AL.BufferData(BufferHandle, (ALFormat)waveInfo.ALFormat, waveInfo.Data, waveInfo.Data.Length, waveInfo.SampleRate); ALError error = AL.GetError(); if (error != ALError.NoError) @@ -79,8 +82,7 @@ namespace ANX.SoundSystem.OpenAL public void Dispose() { waveInfo = null; - - AL.DeleteBuffer(bufferHandle); + AL.DeleteBuffer(BufferHandle); } #endregion } diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs index 1a9da989..fcb7789f 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/OpenALSoundEffectInstance.cs @@ -1,5 +1,6 @@ using System; using ANX.Framework.Audio; +using ANX.Framework.NonXNA.Development; using ANX.Framework.NonXNA.SoundSystem; using OpenTK.Audio.OpenAL; @@ -9,6 +10,7 @@ using OpenTK.Audio.OpenAL; namespace ANX.SoundSystem.OpenAL { + [Developer("AstrorEnales")] public class OpenALSoundEffectInstance : ISoundEffectInstance { #region Private @@ -70,7 +72,7 @@ namespace ANX.SoundSystem.OpenAL parent = setParent; State = SoundState.Stopped; handle = AL.GenSource(); - AL.Source(handle, ALSourcei.Buffer, parent.bufferHandle); + AL.Source(handle, ALSourcei.Buffer, parent.BufferHandle); IsLooped = false; Pitch = 1f; Volume = 1f; diff --git a/SoundSystems/ANX.SoundSystem.OpenAL/SupportedPlatformsImpl.cs b/SoundSystems/ANX.SoundSystem.OpenAL/SupportedPlatformsImpl.cs index 4fc697e2..6520dc3e 100644 --- a/SoundSystems/ANX.SoundSystem.OpenAL/SupportedPlatformsImpl.cs +++ b/SoundSystems/ANX.SoundSystem.OpenAL/SupportedPlatformsImpl.cs @@ -13,7 +13,7 @@ namespace ANX.SoundSystem.OpenAL { get { - return new PlatformName[] + return new[] { PlatformName.Windows7, PlatformName.WindowsXP, diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs index c1c948b7..df59c3b0 100644 --- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs +++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/Creator.cs @@ -131,14 +131,14 @@ namespace ANX.SoundSystem.Windows.XAudio public ISoundEffect CreateSoundEffect(SoundEffect parent, Stream stream) { PreventSystemChange(); - return new XAudioSoundEffect(parent, stream); + return new XAudioSoundEffect(stream); } public ISoundEffect CreateSoundEffect(SoundEffect parent, byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength) { PreventSystemChange(); - return new XAudioSoundEffect(parent, buffer, offset, count, sampleRate, channels, loopStart, loopLength); + return new XAudioSoundEffect(buffer, offset, count, sampleRate, channels, loopStart, loopLength); } #endregion diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSong.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSong.cs index 715ab5da..243d1251 100644 --- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSong.cs +++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSong.cs @@ -29,7 +29,8 @@ namespace ANX.SoundSystem.Windows.XAudio public XAudioSong(XAudio2 device, Uri uri) { - Init(device, uri.AbsolutePath); + string path = uri.AbsolutePath.Replace("%20", ""); + Init(device, path); // TODO: duration } diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffect.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffect.cs index 44d49218..ecd10a72 100644 --- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffect.cs +++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffect.cs @@ -1,6 +1,7 @@ using System; using System.IO; using ANX.Framework.Audio; +using ANX.Framework.NonXNA.Development; using ANX.Framework.NonXNA.SoundSystem; using SharpDX.Multimedia; using SharpDX.XAudio2; @@ -11,10 +12,10 @@ using SharpDX.XAudio2; namespace ANX.SoundSystem.Windows.XAudio { + [Developer("AstrorEnales")] public class XAudioSoundEffect : ISoundEffect { #region Private - internal SoundEffect Parent; private TimeSpan duration; internal WaveFormat WaveFormat; internal AudioBuffer AudioBuffer; @@ -29,24 +30,16 @@ namespace ANX.SoundSystem.Windows.XAudio #endregion #region Constructor - internal XAudioSoundEffect(SoundEffect setParent, Stream stream) + internal XAudioSoundEffect(Stream stream) { - Parent = setParent; CreateFromStream(stream); } - internal XAudioSoundEffect(SoundEffect setParent, byte[] buffer, int offset, int count, int sampleRate, - AudioChannels channels, int loopStart, int loopLength) + internal XAudioSoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, + int loopStart, int loopLength) { - Parent = setParent; - - using (var stream = new MemoryStream()) - { - var writer = new BinaryWriter(stream); - writer.Write(buffer, offset, count); - stream.Position = 0; - CreateFromStream(stream); - } + // TODO: the buffer already contains the pcm data to be played! + throw new NotImplementedException(); } ~XAudioSoundEffect() diff --git a/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffectInstance.cs b/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffectInstance.cs index b227be95..cefc6284 100644 --- a/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffectInstance.cs +++ b/SoundSystems/ANX.SoundSystem.Windows.XAudio/XAudioSoundEffectInstance.cs @@ -1,6 +1,7 @@ using System; using ANX.Framework; using ANX.Framework.Audio; +using ANX.Framework.NonXNA.Development; using ANX.Framework.NonXNA.SoundSystem; using SharpDX.XAudio2; @@ -10,6 +11,7 @@ using SharpDX.XAudio2; namespace ANX.SoundSystem.Windows.XAudio { + [Developer("AstrorEnales")] public class XAudioSoundEffectInstance : ISoundEffectInstance { #region Private diff --git a/Support/WaveUtils/WaveInfo.cs b/Support/WaveUtils/WaveInfo.cs index 5bd66ca4..f73bbd59 100644 --- a/Support/WaveUtils/WaveInfo.cs +++ b/Support/WaveUtils/WaveInfo.cs @@ -24,8 +24,8 @@ namespace WaveUtils /// public TimeSpan CalculateDuration() { - float sizeMulBlockAlign = Data.Length / ((int)Channels * 2); - return TimeSpan.FromMilliseconds((double)(sizeMulBlockAlign * 1000f / (float)SampleRate)); + float sizeMulBlockAlign = Data.Length / (Channels * 2f); + return TimeSpan.FromMilliseconds(sizeMulBlockAlign * 1000f / SampleRate); } public WaveInfo()