diff --git a/Library/Xna/Jellyfish.Library.Xna.Phone.csproj b/Library/Xna/Jellyfish.Library.Xna.Phone.csproj index 8d3462e..71d8c10 100644 --- a/Library/Xna/Jellyfish.Library.Xna.Phone.csproj +++ b/Library/Xna/Jellyfish.Library.Xna.Phone.csproj @@ -108,9 +108,6 @@ StringBuilderExtensions.cs - - WaveFormat.cs - XmlSerializerHelpers.cs diff --git a/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj b/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj index 70a86d8..0455017 100644 --- a/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj +++ b/Library/Xna/Jellyfish.Library.Xna.Xbox.csproj @@ -120,9 +120,6 @@ StringBuilderExtensions.cs - - WaveFormat.cs - XmlSerializerHelpers.cs diff --git a/Library/Xna/Jellyfish.Library.Xna.csproj b/Library/Xna/Jellyfish.Library.Xna.csproj index f93731f..2fe383d 100644 --- a/Library/Xna/Jellyfish.Library.Xna.csproj +++ b/Library/Xna/Jellyfish.Library.Xna.csproj @@ -101,12 +101,6 @@ AssemblyCommentAttribute.cs - - DirectSound.cs - - - DirectSoundInterop.cs - GCHandleHelpers.cs @@ -140,9 +134,6 @@ StringBuilderExtensions.cs - - WaveFormat.cs - XmlSerializerHelpers.cs diff --git a/Virtu/Services/AudioService.cs b/Virtu/Services/AudioService.cs index 9cd1919..e4796f1 100644 --- a/Virtu/Services/AudioService.cs +++ b/Virtu/Services/AudioService.cs @@ -12,16 +12,15 @@ public AudioService(Machine machine) : public void Output(int data) // machine thread { - _buffer[_index] = (byte)data; - _index = (_index + 1) % SampleSize; + _buffer[_index + 0] = (byte)(data & 0xFF); + _buffer[_index + 1] = (byte)((data >> 8) & 0xFF); + _index = (_index + 2) % SampleSize; if (_index == 0) { _readEvent.Set(); if (Machine.Settings.Cpu.IsThrottled) { -#if !WINDOWS_PHONE // TODO remove; implement phone audio _writeEvent.WaitOne(); -#endif } } } @@ -52,7 +51,7 @@ protected void Update(int bufferSize, Action updateBuffer) // audio public const int SampleRate = 44100; // hz public const int SampleChannels = 1; - public const int SampleBits = 8; + public const int SampleBits = 16; public const int SampleLatency = 40; // ms public const int SampleSize = (SampleRate * SampleLatency / 1000) * SampleChannels * (SampleBits / 8); diff --git a/Virtu/Silverlight/Services/SilverlightAudioService.cs b/Virtu/Silverlight/Services/SilverlightAudioService.cs index 562df37..b52f5be 100644 --- a/Virtu/Silverlight/Services/SilverlightAudioService.cs +++ b/Virtu/Silverlight/Services/SilverlightAudioService.cs @@ -38,10 +38,7 @@ private void OnMediaSourceUpdate(object sender, WaveMediaStreamSourceUpdateEvent // }); //} - Update(e.BufferSize, (source, count) => - { - Buffer.BlockCopy(source, 0, e.Buffer, 0, count); - }); + Update(e.BufferSize, (source, count) => Buffer.BlockCopy(source, 0, e.Buffer, 0, count)); } private UserControl _page; diff --git a/Virtu/Speaker.cs b/Virtu/Speaker.cs index 663f1de..dfb58df 100644 --- a/Virtu/Speaker.cs +++ b/Virtu/Speaker.cs @@ -34,7 +34,7 @@ public void ToggleOutput() private void FlushOutputEvent() { UpdateCycles(); - _audioService.Output(_highCycles * 255 / _totalCycles); // quick and dirty decimation + _audioService.Output(_highCycles * short.MaxValue / _totalCycles); // quick and dirty decimation _highCycles = _totalCycles = 0; Machine.Events.AddEvent(CyclesPerFlush * Machine.Settings.Cpu.Multiplier, _flushOutputEvent); diff --git a/Virtu/Wpf/Services/WpfAudioService.cs b/Virtu/Wpf/Services/WpfAudioService.cs index bb7a49f..578871a 100644 --- a/Virtu/Wpf/Services/WpfAudioService.cs +++ b/Virtu/Wpf/Services/WpfAudioService.cs @@ -44,10 +44,7 @@ private void OnDirectSoundUpdate(object sender, DirectSoundUpdateEventArgs e) // // }); //} - Update(e.BufferSize, (source, count) => - { - Marshal.Copy(source, 0, e.Buffer, count); - }); + Update(e.BufferSize, (source, count) => Marshal.Copy(source, 0, e.Buffer, count)); } private Window _window; diff --git a/Virtu/Xna/Services/XnaAudioService.cs b/Virtu/Xna/Services/XnaAudioService.cs index 2a9568d..9966b6f 100644 --- a/Virtu/Xna/Services/XnaAudioService.cs +++ b/Virtu/Xna/Services/XnaAudioService.cs @@ -1,14 +1,11 @@ using System; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; -using System.Threading; using Jellyfish.Library; +using Microsoft.Xna.Framework.Audio; namespace Jellyfish.Virtu.Services { public sealed class XnaAudioService : AudioService { - [SuppressMessage("Microsoft.Mobility", "CA1601:DoNotUseTimersThatPreventPowerStateChanges")] public XnaAudioService(Machine machine, GameBase game) : base(machine) { @@ -19,47 +16,28 @@ public XnaAudioService(Machine machine, GameBase game) : _game = game; -#if WINDOWS - _directSound.Start(_game.Window.Handle); - _directSound.Update += OnDirectSoundUpdate; - _game.Exiting += (sender, e) => _directSound.Stop(); -#else - _timer = new Timer(OnTimerUpdate, null, 0, SampleLatency); - _game.Exiting += (sender, e) => _timer.Change(Timeout.Infinite, Timeout.Infinite); -#endif + _dynamicSoundEffect.BufferNeeded += OnDynamicSoundEffectBufferNeeded; + _game.Exiting += (sender, e) => _dynamicSoundEffect.Stop(); + + _dynamicSoundEffect.Play(); } protected override void Dispose(bool disposing) { if (disposing) { -#if WINDOWS - _directSound.Dispose(); -#else - _timer.Dispose(); -#endif + _dynamicSoundEffect.Dispose(); } base.Dispose(disposing); } -#if WINDOWS - private void OnDirectSoundUpdate(object sender, DirectSoundUpdateEventArgs e) // audio thread + private void OnDynamicSoundEffectBufferNeeded(object sender, EventArgs e) // audio thread { - Update(e.BufferSize, (source, count) => Marshal.Copy(source, 0, e.Buffer, count)); + Update(SampleSize, (source, count) => _dynamicSoundEffect.SubmitBuffer(source, 0, count)); } -#else - private void OnTimerUpdate(object state) // audio thread - { - Update(0, null); - } -#endif private GameBase _game; -#if WINDOWS - private DirectSound _directSound = new DirectSound(SampleRate, SampleChannels, SampleBits, SampleSize); -#else - private Timer _timer; -#endif + private DynamicSoundEffectInstance _dynamicSoundEffect = new DynamicSoundEffectInstance(SampleRate, (AudioChannels)SampleChannels); } }