using Xunit; using ytLive.Services.Audio; namespace ytLive.Tests; /// The dock shows ONE ladder per source, but the creator is profoundly deaf on the right, /// so the meter has to know which side a signal is on: hard-panned content is inaudible, not merely /// quieter. These levels drive the imbalance flag, and they must be measured — never decorative. /// public class StereoMeterTests { private const int Rate = 48000; private static AudioLevelMeter Pushed(int channels, float left, float right, int frames = 64, int pushes = 12) { var meter = new AudioLevelMeter(); var buffer = new float[frames * channels]; for (var f = 0; f < frames; f++) { buffer[f * channels] = left; if (channels > 1) buffer[f * channels + 1] = right; } for (var i = 0; i < pushes; i++) meter.Push(new AudioSample(buffer, Rate, channels)); return meter; } [Fact] public void The_meter_knows_which_side_the_signal_is_on() { // Hard left: the left side carries it, the right is silent, and the gap is reported. var hardLeft = Pushed(channels: 2, left: 0.5f, right: 0f); Assert.True(hardLeft.Left > 0.1f, $"left should carry the tone, got {hardLeft.Left}"); Assert.True(hardLeft.Right < 0.01f, $"right should be silent, got {hardLeft.Right}"); Assert.True(hardLeft.ImbalanceDb > 20f, "a one-sided signal must report a large imbalance"); // Hard right is the mirror case — the one this creator cannot hear. var hardRight = Pushed(channels: 2, left: 0f, right: 0.5f); Assert.True(hardRight.Right > 0.1f); Assert.True(hardRight.Left < 0.01f); // Balanced stereo reports no imbalance. var centred = Pushed(channels: 2, left: 0.5f, right: 0.5f); Assert.True(centred.Left > 0.1f && centred.Right > 0.1f); Assert.True(centred.ImbalanceDb < 1f, $"balanced audio must not flag, got {centred.ImbalanceDb}"); // Mono is centred by definition: both sides equal, no flag — never "one-sided". var mono = Pushed(channels: 1, left: 0.5f, right: 0f); Assert.True(mono.Left > 0.1f); Assert.Equal(mono.Left, mono.Right, 5); Assert.Equal(0f, mono.ImbalanceDb); // Silence on both sides is not an imbalance (nothing to compare). var silent = Pushed(channels: 2, left: 0f, right: 0f); Assert.Equal(0f, silent.ImbalanceDb); } }