Modified audio sample bit depth to use 16-bits per sample.

Sound emulation for XNA now uses DynamicSoundEffectInstance.

--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4046563
This commit is contained in:
Sean Fausett 2010-05-20 10:12:56 +00:00
parent bc4caaf2b1
commit 6700099803
8 changed files with 16 additions and 60 deletions

View File

@ -108,9 +108,6 @@
<Compile Include="..\StringBuilderExtensions.cs">
<Link>StringBuilderExtensions.cs</Link>
</Compile>
<Compile Include="..\WaveFormat.cs">
<Link>WaveFormat.cs</Link>
</Compile>
<Compile Include="..\XmlSerializerHelpers.cs">
<Link>XmlSerializerHelpers.cs</Link>
</Compile>

View File

@ -120,9 +120,6 @@
<Compile Include="..\StringBuilderExtensions.cs">
<Link>StringBuilderExtensions.cs</Link>
</Compile>
<Compile Include="..\WaveFormat.cs">
<Link>WaveFormat.cs</Link>
</Compile>
<Compile Include="..\XmlSerializerHelpers.cs">
<Link>XmlSerializerHelpers.cs</Link>
</Compile>

View File

@ -101,12 +101,6 @@
<Compile Include="..\AssemblyCommentAttribute.cs">
<Link>AssemblyCommentAttribute.cs</Link>
</Compile>
<Compile Include="..\DirectSound.cs">
<Link>DirectSound.cs</Link>
</Compile>
<Compile Include="..\DirectSoundInterop.cs">
<Link>DirectSoundInterop.cs</Link>
</Compile>
<Compile Include="..\GCHandleHelpers.cs">
<Link>GCHandleHelpers.cs</Link>
</Compile>
@ -140,9 +134,6 @@
<Compile Include="..\StringBuilderExtensions.cs">
<Link>StringBuilderExtensions.cs</Link>
</Compile>
<Compile Include="..\WaveFormat.cs">
<Link>WaveFormat.cs</Link>
</Compile>
<Compile Include="..\XmlSerializerHelpers.cs">
<Link>XmlSerializerHelpers.cs</Link>
</Compile>

View File

@ -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<byte[], int> 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);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);
}
}