mirror of
https://github.com/digital-jellyfish/Virtu.git
synced 2024-05-31 15:41:33 +00:00
9fca4b220f
--HG-- extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4047214
74 lines
1.9 KiB
C#
74 lines
1.9 KiB
C#
using System;
|
|
using Jellyfish.Virtu.Services;
|
|
|
|
namespace Jellyfish.Virtu
|
|
{
|
|
public sealed class Speaker : MachineComponent
|
|
{
|
|
public Speaker(Machine machine) :
|
|
base(machine)
|
|
{
|
|
_flushOutputEvent = FlushOutputEvent; // cache delegates; avoids garbage
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
_audioService = Machine.Services.GetService<AudioService>();
|
|
|
|
UpdateSettings();
|
|
Machine.Video.VSync += (sender, e) => UpdateSettings();
|
|
|
|
Machine.Events.AddEvent(CyclesPerFlush * Machine.Settings.Cpu.Multiplier, _flushOutputEvent);
|
|
}
|
|
|
|
public override void Reset()
|
|
{
|
|
_audioService.Reset();
|
|
_isHigh = false;
|
|
_highCycles = _totalCycles = 0;
|
|
}
|
|
|
|
public void ToggleOutput()
|
|
{
|
|
UpdateCycles();
|
|
_isHigh ^= true;
|
|
}
|
|
|
|
private void FlushOutputEvent()
|
|
{
|
|
UpdateCycles();
|
|
_audioService.Output(_highCycles * short.MaxValue / _totalCycles); // quick and dirty decimation
|
|
_highCycles = _totalCycles = 0;
|
|
|
|
Machine.Events.AddEvent(CyclesPerFlush * Machine.Settings.Cpu.Multiplier, _flushOutputEvent);
|
|
}
|
|
|
|
private void UpdateCycles()
|
|
{
|
|
int delta = (int)(Machine.Cpu.Cycles - _lastCycles);
|
|
if (_isHigh)
|
|
{
|
|
_highCycles += delta;
|
|
}
|
|
_totalCycles += delta;
|
|
_lastCycles = Machine.Cpu.Cycles;
|
|
}
|
|
|
|
private void UpdateSettings()
|
|
{
|
|
_audioService.SetVolume(Machine.Settings.Audio.Volume);
|
|
}
|
|
|
|
private const int CyclesPerFlush = 23;
|
|
|
|
private Action _flushOutputEvent;
|
|
|
|
private bool _isHigh;
|
|
private int _highCycles;
|
|
private int _totalCycles;
|
|
private long _lastCycles;
|
|
|
|
private AudioService _audioService;
|
|
}
|
|
}
|