Added audio volume to machine settings.

--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4047214
This commit is contained in:
Sean Fausett 2010-06-11 12:00:48 +00:00
parent d22639c563
commit 9fca4b220f
10 changed files with 60 additions and 11 deletions

View File

@ -26,6 +26,15 @@ public void Dispose()
_stopEvent.Close(); _stopEvent.Close();
} }
public void SetVolume(double volume)
{
int attenuation = (volume < 0.01) ? (int)BufferVolume.Min : (int)Math.Floor(100 * 20 * Math.Log10(volume)); // 100 db
if (_buffer != null)
{
_buffer.SetVolume(attenuation);
}
}
public void Start(IntPtr window) public void Start(IntPtr window)
{ {
_window = window; _window = window;
@ -62,8 +71,6 @@ private void Initialize()
}; };
((IDirectSoundNotify)_buffer).SetNotificationPositions(positionEvents.Length, positionEvents); ((IDirectSoundNotify)_buffer).SetNotificationPositions(positionEvents.Length, positionEvents);
_buffer.SetVolume(-1500); // 50 %
_buffer.Play(0, 0, BufferPlay.Looping); _buffer.Play(0, 0, BufferPlay.Looping);
} }
@ -113,10 +120,12 @@ private void Uninitialize()
{ {
_buffer.Stop(); _buffer.Stop();
Marshal.ReleaseComObject(_buffer); Marshal.ReleaseComObject(_buffer);
_buffer = null;
} }
if (_device != null) if (_device != null)
{ {
Marshal.ReleaseComObject(_device); Marshal.ReleaseComObject(_device);
_device = null;
} }
} }

View File

@ -20,6 +20,8 @@ private enum BufferPlay { Looping = 0x00000001 }
[Flags] [Flags]
private enum BufferStatus { Playing = 0x00000001, BufferLost = 0x00000002, Looping = 0x00000004, Terminated = 0x00000020 } private enum BufferStatus { Playing = 0x00000001, BufferLost = 0x00000002, Looping = 0x00000004, Terminated = 0x00000020 }
private enum BufferVolume { Min = -10000, Max = 0 }
private enum CooperativeLevel { Normal = 1, Priority = 2 } private enum CooperativeLevel { Normal = 1, Priority = 2 }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]

View File

@ -7,6 +7,11 @@ public static int Clamp(int value, int min, int max)
return (value < min) ? min : (value > max) ? max : value; return (value < min) ? min : (value > max) ? max : value;
} }
public static double Clamp(double value, double min, double max)
{
return (value < min) ? min : (value > max) ? max : value;
}
public static int ClampByte(int value) public static int ClampByte(int value)
{ {
return Clamp(value, byte.MinValue, byte.MaxValue); return Clamp(value, byte.MinValue, byte.MaxValue);

View File

@ -154,8 +154,7 @@ public override void Initialize()
_memory = Machine.Memory; _memory = Machine.Memory;
UpdateSettings(); UpdateSettings();
Machine.Video.VSync += (sender, e) => UpdateSettings();
Machine.Video.VSync += OnVideoVSync;
} }
public override void Reset() public override void Reset()
@ -3205,11 +3204,6 @@ private void Execute65C02Tsb0C() // tsb abs
} }
#endregion #endregion
private void OnVideoVSync(object sender, EventArgs e)
{
UpdateSettings();
}
private void UpdateSettings() private void UpdateSettings()
{ {
_executeOpCode = Machine.Settings.Cpu.Is65C02 ? ExecuteOpCode65C02 : ExecuteOpCode65N02; _executeOpCode = Machine.Settings.Cpu.Is65C02 ? ExecuteOpCode65C02 : ExecuteOpCode65N02;

View File

@ -36,6 +36,7 @@ public MachineSettings()
Button0 = 0, Button1 = 0, Button2 = 0 Button0 = 0, Button1 = 0, Button2 = 0
} }
}; };
Audio = new AudioSettings { Volume = 0.5 };
Video = new VideoSettings Video = new VideoSettings
{ {
IsFullScreen = false, IsMonochrome = false, ScannerOptions = ScannerOptions.None, IsFullScreen = false, IsMonochrome = false, ScannerOptions = ScannerOptions.None,
@ -168,6 +169,11 @@ public void Deserialize(Stream stream)
Button2 = (int)buttons.Attribute("Button2") Button2 = (int)buttons.Attribute("Button2")
} }
}; };
var audio = root.Element(ns + "Audio");
Audio = new AudioSettings
{
Volume = (double)audio.Attribute("Volume")
};
var video = root.Element(ns + "Video"); var video = root.Element(ns + "Video");
var color = video.Element(ns + "Color"); var color = video.Element(ns + "Color");
Video = new VideoSettings Video = new VideoSettings
@ -268,6 +274,8 @@ public void Serialize(Stream stream)
new XAttribute("Button0", Keyboard.Key.Button0), new XAttribute("Button0", Keyboard.Key.Button0),
new XAttribute("Button1", Keyboard.Key.Button1), new XAttribute("Button1", Keyboard.Key.Button1),
new XAttribute("Button2", Keyboard.Key.Button2)))), new XAttribute("Button2", Keyboard.Key.Button2)))),
new XElement(ns + "Audio",
new XAttribute("Volume", Audio.Volume)),
new XElement(ns + "Video", new XElement(ns + "Video",
new XAttribute("IsFullScreen", Video.IsFullScreen), new XAttribute("IsFullScreen", Video.IsFullScreen),
new XAttribute("IsMonochrome", Video.IsMonochrome), new XAttribute("IsMonochrome", Video.IsMonochrome),
@ -301,6 +309,7 @@ public void Serialize(Stream stream)
public DiskIISettings DiskII { get; set; } public DiskIISettings DiskII { get; set; }
public KeyboardSettings Keyboard { get; set; } public KeyboardSettings Keyboard { get; set; }
public GamePortSettings GamePort { get; set; } public GamePortSettings GamePort { get; set; }
public AudioSettings Audio { get; set; }
public VideoSettings Video { get; set; } public VideoSettings Video { get; set; }
public const string FileName = "Settings.xml"; public const string FileName = "Settings.xml";
@ -360,6 +369,11 @@ public sealed class GamePortSettings
public KeySettings Key { get; set; } public KeySettings Key { get; set; }
} }
public sealed class AudioSettings
{
public double Volume { get; set; }
};
public sealed class ColorSettings public sealed class ColorSettings
{ {
public uint Black { get; set; } public uint Black { get; set; }

View File

@ -4,9 +4,9 @@
namespace Jellyfish.Virtu.Services namespace Jellyfish.Virtu.Services
{ {
public class AudioService : MachineService public abstract class AudioService : MachineService
{ {
public AudioService(Machine machine) : protected AudioService(Machine machine) :
base(machine) base(machine)
{ {
} }
@ -31,6 +31,8 @@ public void Reset()
Buffer.BlockCopy(SampleZero, 0, _buffer, 0, SampleSize); Buffer.BlockCopy(SampleZero, 0, _buffer, 0, SampleSize);
} }
public abstract void SetVolume(double volume); // machine thread
public override void Stop() // main thread public override void Stop() // main thread
{ {
_readEvent.Set(); // signal events; avoids deadlock _readEvent.Set(); // signal events; avoids deadlock

View File

@ -29,6 +29,11 @@ public SilverlightAudioService(Machine machine, UserControl page, MediaElement m
#endif #endif
} }
public override void SetVolume(double volume) // machine thread
{
_media.Dispatcher.BeginInvoke(() => _media.Volume = volume);
}
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (disposing) if (disposing)

View File

@ -15,6 +15,9 @@ public override void Initialize()
{ {
_audioService = Machine.Services.GetService<AudioService>(); _audioService = Machine.Services.GetService<AudioService>();
UpdateSettings();
Machine.Video.VSync += (sender, e) => UpdateSettings();
Machine.Events.AddEvent(CyclesPerFlush * Machine.Settings.Cpu.Multiplier, _flushOutputEvent); Machine.Events.AddEvent(CyclesPerFlush * Machine.Settings.Cpu.Multiplier, _flushOutputEvent);
} }
@ -51,6 +54,11 @@ private void UpdateCycles()
_lastCycles = Machine.Cpu.Cycles; _lastCycles = Machine.Cpu.Cycles;
} }
private void UpdateSettings()
{
_audioService.SetVolume(Machine.Settings.Audio.Volume);
}
private const int CyclesPerFlush = 23; private const int CyclesPerFlush = 23;
private Action _flushOutputEvent; private Action _flushOutputEvent;

View File

@ -24,6 +24,11 @@ public WpfAudioService(Machine machine, Window window) :
_window.Closed += (sender, e) => _directSound.Stop(); _window.Closed += (sender, e) => _directSound.Stop();
} }
public override void SetVolume(double volume) // machine thread
{
_directSound.SetVolume(volume);
}
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (disposing) if (disposing)

View File

@ -23,6 +23,11 @@ public XnaAudioService(Machine machine, GameBase game) :
_dynamicSoundEffect.Play(); _dynamicSoundEffect.Play();
} }
public override void SetVolume(double volume) // machine thread
{
_dynamicSoundEffect.Volume = (float)volume;
}
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (disposing) if (disposing)