TASK 4 audio follow-up: game audio bar + mic status dot + always-on capture — the footer's second audio control (desktop/game, a mirror of the mic bar: meter + mute + volume) appears only while a full-screen game is producing sound (IGameAudioDetector seam + GameAudioHysteresis: show ~500ms of fullscreen+sound, hide ~1s after leaving fullscreen, silence never hides an active bar; VM polls on a 250ms timer); capture now runs for the app's lifetime so both meters preview live (started at startup via StartMicCaptureAsync, disposed in Shutdown — no longer go-live driven); MIC label is a button with a status dot (Models/MicStatus: green via the source Started event, yellow = mic problem, red = no device); PickMicrophone swaps the live device immediately via AudioMixer.RestartMic; fixed a latent ?.Invoke(meter.Push(...)) short-circuit that skipped the meter update when nothing subscribed — 167 tests passing, 0 warnings
This commit is contained in:
@@ -18,6 +18,7 @@ public class AudioMixerTests
|
||||
public int StartCount { get; private set; }
|
||||
public int StopCount { get; private set; }
|
||||
public bool Disposed { get; private set; }
|
||||
public event Action? Started;
|
||||
public event Action<AudioSample>? SampleReady;
|
||||
public event Action<Exception>? Failed;
|
||||
|
||||
@@ -25,6 +26,7 @@ public class AudioMixerTests
|
||||
public void Stop() => StopCount++;
|
||||
public void Dispose() => Disposed = true;
|
||||
|
||||
public void MarkStarted() => Started?.Invoke();
|
||||
public void Emit(AudioSample sample) => SampleReady?.Invoke(sample);
|
||||
public void Fail(Exception ex) => Failed?.Invoke(ex);
|
||||
}
|
||||
@@ -115,6 +117,98 @@ public class AudioMixerTests
|
||||
Assert.Equal(0f, mixer.MicLevel);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MicSamples_DoNotChangeLoopbackLevel()
|
||||
{
|
||||
var mic = new FakeSource();
|
||||
var mixer = new AudioMixer(mic, new FakeSource());
|
||||
var levels = new List<float>();
|
||||
mixer.LoopbackLevelChanged += l => levels.Add(l);
|
||||
|
||||
mixer.Start();
|
||||
mic.Emit(new AudioSample(new[] { 1f, 1f, 1f, 1f }, 48000, 1));
|
||||
|
||||
Assert.Empty(levels);
|
||||
Assert.Equal(0f, mixer.LoopbackLevel);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoopbackSamples_DriveLoopbackLevelChanged()
|
||||
{
|
||||
var loopback = new FakeSource();
|
||||
var mixer = new AudioMixer(new FakeSource(), loopback);
|
||||
var levels = new List<float>();
|
||||
mixer.LoopbackLevelChanged += l => levels.Add(l);
|
||||
|
||||
mixer.Start();
|
||||
loopback.Emit(new AudioSample(new[] { 1f, 1f, 1f, 1f }, 48000, 2));
|
||||
loopback.Emit(new AudioSample(new[] { -1f, -1f, -1f, -1f }, 48000, 2));
|
||||
|
||||
Assert.NotEmpty(levels);
|
||||
Assert.All(levels, l => Assert.InRange(l, 0f, 1f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MicStarted_RaisesMicConnected()
|
||||
{
|
||||
var mic = new FakeSource();
|
||||
var mixer = new AudioMixer(mic, new FakeSource());
|
||||
var connected = 0;
|
||||
mixer.MicConnected += () => connected++;
|
||||
|
||||
mixer.Start();
|
||||
mic.MarkStarted();
|
||||
|
||||
Assert.Equal(1, connected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MicFailure_RaisesMicFailed()
|
||||
{
|
||||
var mic = new FakeSource();
|
||||
var mixer = new AudioMixer(mic, new FakeSource());
|
||||
Exception? failed = null;
|
||||
mixer.MicFailed += ex => failed = ex;
|
||||
|
||||
mixer.Start();
|
||||
mic.Fail(new InvalidOperationException("boom"));
|
||||
|
||||
Assert.NotNull(failed);
|
||||
Assert.Equal("boom", failed!.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RestartMic_StopsAndRestartsMic_KeepsLoopbackRunning()
|
||||
{
|
||||
var mic = new FakeSource();
|
||||
var loopback = new FakeSource();
|
||||
var mixer = new AudioMixer(mic, loopback);
|
||||
mixer.Start();
|
||||
|
||||
mixer.RestartMic();
|
||||
|
||||
Assert.Equal(2, mic.StartCount);
|
||||
Assert.Equal(1, mic.StopCount);
|
||||
Assert.Equal(1, loopback.StartCount);
|
||||
Assert.Equal(0, loopback.StopCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RestartMic_ResetsLevel()
|
||||
{
|
||||
var mic = new FakeSource();
|
||||
var mixer = new AudioMixer(mic, new FakeSource());
|
||||
mixer.Start();
|
||||
mic.Emit(new AudioSample(new[] { 1f, 1f, 1f, 1f }, 48000, 1));
|
||||
Assert.True(mixer.MicLevel > 0);
|
||||
|
||||
float? reset = null;
|
||||
mixer.MicLevelChanged += l => reset = l;
|
||||
mixer.RestartMic();
|
||||
|
||||
Assert.Equal(0f, reset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MicFailure_LogsAndResetsLevel()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using Xunit;
|
||||
using ytLive.Services;
|
||||
|
||||
namespace ytLive.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// TASK 4 game audio bar: the default detector's provider wiring — it composes
|
||||
/// the full-screen monitor + loopback level into the hysteresis and raises
|
||||
/// IsGameAudioActiveChanged on transitions. The transition math itself lives in
|
||||
/// GameAudioHysteresisTests.
|
||||
/// </summary>
|
||||
public class GameAudioDetectorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Poll_RaisesChanged_WhenGameAppearsAndLeaves()
|
||||
{
|
||||
var now = new DateTime(2026, 8, 13, 12, 0, 0);
|
||||
int? monitor = 0;
|
||||
var level = 0f;
|
||||
var detector = new GameAudioDetector(() => monitor, () => level, () => now);
|
||||
var changes = new List<bool>();
|
||||
detector.IsGameAudioActiveChanged += a => changes.Add(a);
|
||||
|
||||
level = 0.9f;
|
||||
detector.Poll();
|
||||
Assert.False(detector.IsGameAudioActive);
|
||||
|
||||
now = now.AddMilliseconds(600);
|
||||
detector.Poll();
|
||||
Assert.True(detector.IsGameAudioActive);
|
||||
Assert.Equal(new[] { true }, changes);
|
||||
|
||||
level = 0f;
|
||||
now = now.AddSeconds(2);
|
||||
detector.Poll();
|
||||
Assert.True(detector.IsGameAudioActive);
|
||||
Assert.Equal(new[] { true }, changes);
|
||||
|
||||
monitor = null;
|
||||
detector.Poll();
|
||||
Assert.True(detector.IsGameAudioActive);
|
||||
|
||||
now = now.AddSeconds(2);
|
||||
detector.Poll();
|
||||
Assert.False(detector.IsGameAudioActive);
|
||||
Assert.Equal(new[] { true, false }, changes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Poll_StaysInactive_WhenNoFullScreenMonitor()
|
||||
{
|
||||
var detector = new GameAudioDetector(() => null, () => 0.9f);
|
||||
var changes = 0;
|
||||
detector.IsGameAudioActiveChanged += _ => changes++;
|
||||
|
||||
for (var i = 0; i < 5; i++)
|
||||
detector.Poll();
|
||||
|
||||
Assert.False(detector.IsGameAudioActive);
|
||||
Assert.Equal(0, changes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Poll_StaysInactive_WhileLoopbackSilent()
|
||||
{
|
||||
var detector = new GameAudioDetector(() => 0, () => 0f);
|
||||
var changes = 0;
|
||||
detector.IsGameAudioActiveChanged += _ => changes++;
|
||||
|
||||
for (var i = 0; i < 5; i++)
|
||||
detector.Poll();
|
||||
|
||||
Assert.False(detector.IsGameAudioActive);
|
||||
Assert.Equal(0, changes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using Xunit;
|
||||
using ytLive.Services;
|
||||
|
||||
namespace ytLive.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// TASK 4 game audio bar: the pure show/hide state machine. Show = a full-screen
|
||||
/// app holds sound for half a second; hide = the app leaves fullscreen for a
|
||||
/// second. Silence never hides an active bar — only the game leaving the preview
|
||||
/// does (per the creator's rule).
|
||||
/// </summary>
|
||||
public class GameAudioHysteresisTests
|
||||
{
|
||||
private static readonly DateTime T0 = new(2026, 8, 13, 12, 0, 0);
|
||||
|
||||
[Fact]
|
||||
public void StaysInactive_WhileSilent()
|
||||
{
|
||||
var h = new GameAudioHysteresis();
|
||||
|
||||
for (var i = 0; i < 30; i++)
|
||||
{
|
||||
h.Update(true, false, T0.AddSeconds(i));
|
||||
Assert.False(h.IsActive);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaysInactive_UntilSoundHoldsHalfSecond()
|
||||
{
|
||||
var h = new GameAudioHysteresis();
|
||||
h.Update(true, true, T0);
|
||||
Assert.False(h.IsActive);
|
||||
h.Update(true, true, T0.AddMilliseconds(400));
|
||||
Assert.False(h.IsActive);
|
||||
|
||||
h.Update(true, true, T0.AddMilliseconds(600));
|
||||
Assert.True(h.IsActive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NeverHides_WhileGameStillFullScreen_EvenWhenSilent()
|
||||
{
|
||||
var h = new GameAudioHysteresis();
|
||||
h.Update(true, true, T0);
|
||||
h.Update(true, true, T0.AddSeconds(1));
|
||||
Assert.True(h.IsActive);
|
||||
|
||||
for (var i = 2; i < 40; i++)
|
||||
{
|
||||
h.Update(true, false, T0.AddSeconds(i));
|
||||
Assert.True(h.IsActive);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hides_AfterAppLeavesFullScreen()
|
||||
{
|
||||
var h = new GameAudioHysteresis();
|
||||
h.Update(true, true, T0);
|
||||
h.Update(true, true, T0.AddSeconds(1));
|
||||
Assert.True(h.IsActive);
|
||||
|
||||
h.Update(false, true, T0.AddSeconds(2));
|
||||
Assert.True(h.IsActive);
|
||||
|
||||
h.Update(false, true, T0.AddSeconds(4));
|
||||
Assert.False(h.IsActive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaysInactive_WhenNoFullScreen_EvenWithSound()
|
||||
{
|
||||
var h = new GameAudioHysteresis();
|
||||
|
||||
for (var i = 0; i < 30; i++)
|
||||
{
|
||||
h.Update(false, true, T0.AddSeconds(i));
|
||||
Assert.False(h.IsActive);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user