Virtu/Virtu/Xna/Services/XnaAudioService.cs
Sean Fausett 5f8c4ce9d8 Worked around lack of audio on XNA+XBox360 blocking machine thread.
Bumped version to 0.8.1 for next release.

--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4044502
2010-04-04 04:25:28 +00:00

64 lines
1.7 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
using Jellyfish.Library;
namespace Jellyfish.Virtu.Services
{
public sealed class XnaAudioService : AudioService
{
[SuppressMessage("Microsoft.Mobility", "CA1601:DoNotUseTimersThatPreventPowerStateChanges")]
public XnaAudioService(Machine machine, GameBase game) :
base(machine)
{
if (game == null)
{
throw new ArgumentNullException("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
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
#if WINDOWS
_directSound.Dispose();
#else
_timer.Dispose();
#endif
}
}
#if WINDOWS
private void OnDirectSoundUpdate(object sender, DirectSoundUpdateEventArgs e) // audio thread
{
Update(e.BufferSize, (source, count) => Marshal.Copy(source, 0, e.Buffer, 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
}
}