Skip to content

Commit

Permalink
added timer to disable AudioDeviceProxy on EqualizerLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed May 16, 2024
1 parent ae32194 commit 0e31680
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Threading;
using System.Windows.Controls;
using AuroraRgb.EffectsEngine;
using AuroraRgb.Modules.AudioCapture;
Expand Down Expand Up @@ -245,13 +246,37 @@ private void DeviceChanged(object? sender, EventArgs e)
private int _freq = 48000;
private readonly SolidBrush _backgroundBrush = new(Color.Transparent);

private long _lastRender = Time.GetMillisecondsSinceEpoch();
private Timer? _aliveTimer;

public EqualizerLayerHandler(): base("EqualizerLayer")
{
_ffts = new Complex[FftLength];
_fftsPrev = new Complex[FftLength];

_sampleAggregator.FftCalculated += FftCalculated;
_sampleAggregator.PerformFft = true;

StartAliveTimer();
}

private void StartAliveTimer()
{
_aliveTimer = new Timer(AliveTimerCallback, null, TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(20));
}

private void AliveTimerCallback(object? state)
{
var now = Time.GetMillisecondsSinceEpoch();
if (now - _lastRender <= TimeSpan.FromSeconds(20).Milliseconds || _deviceProxy == null) return;

// 20 sec passed since last render, dispose proxy
var deviceProxy = _deviceProxy;
_deviceProxy = null;
deviceProxy.Dispose();

_aliveTimer?.Dispose();
_aliveTimer = null;
}

protected override UserControl CreateControl()
Expand All @@ -265,6 +290,11 @@ public override EffectLayer Render(IGameState gamestate)
if (DeviceProxy.Device == null)
return EffectLayer.EmptyLayer;

if (_aliveTimer == null)
{
StartAliveTimer();
}

// The system sound as a value between 0.0 and 1.0
var systemSoundNormalized = DeviceProxy.Device?.AudioMeterInformation?.MasterPeakValue ?? 1f;

Expand Down

0 comments on commit 0e31680

Please sign in to comment.