Files
LlamaCasty/Services/Audio/AlertAudioOutput.cs
T
gramps b00bf48b9d feat(alerts): sound byte under Built-in video, and alert audio that reaches the device
Creator request: a sound-byte browse + label under Built-in video, with an option to loop the
sound while the clip plays. The video itself is always one pass to completion.

- Source gains AlertAudioPath and AlertAudioLoop, persisted (column + migration + INSERT +
  SELECT + reader, the whole chain — that chain is where the blur crashed once).
- The sound byte plays through MediaPlayer on the default output device; MediaEnded re-seeks to
  zero while looping, and StopClip kills it so a tail cannot bleed into the next alert.
- The clip's OWN soundtrack moved off the mixer and onto the device as well
  (Services/Audio/AlertAudioOutput: one WaveOutEvent + buffered float provider).

Why the move: the creator asked that alert audio — the clip's track or the added sound byte —
appear in the desktop sound bar. Only audio that reaches the default device is picked up by the
WASAPI loopback that feeds both the meter and the stream. The old EnqueueAlertAudio route put the
sound in the stream but left the meter flat and the room silent.

Tests: the sound byte and its loop flag round-trip through SQLite, and a fresh alert layer starts
with no sound byte and no loop. Release 0 warnings.
2026-11-01 14:13:57 -08:00

84 lines
3.0 KiB
C#

using System;
using NAudio.Wave;
using ytLive.Helpers;
namespace ytLive.Services.Audio;
/// <summary>
/// Plays alert audio on the **default output device**, which is what makes it visible in the
/// desktop sound bar (creator ruling 2026-11-01): the app mixes for the encoder, but only audio
/// that actually reaches the speakers is picked up by the WASAPI loopback that feeds both the
/// meter and the stream. Injecting straight into the mixer, as this used to do, produced sound
/// nobody could see and nobody could hear.
///
/// <para>One long-lived <see cref="WaveOutEvent"/> with a buffered float provider: the alert clip
/// submits PCM as it decodes, and a sound byte can be submitted through the same device, so they
/// mix with each other and with everything else on the machine.</para>
/// </summary>
public sealed class AlertAudioOutput : IDisposable
{
private readonly object _gate = new();
private WaveOutEvent? _device;
private BufferedWaveProvider? _buffer;
private int _sampleRate;
private int _channels;
/// <summary>Queue one decoded pulse of the alert clip. Never blocks the decoder: if the device
/// cannot keep up the buffer simply grows, and it is cleared when the alert ends.</summary>
public void Submit(AudioSample sample)
{
if (sample.Samples.Length == 0) return;
lock (_gate)
{
try
{
EnsureDevice(sample.SampleRate, sample.Channels);
if (_buffer == null) return;
var bytes = new byte[sample.Samples.Length * sizeof(float)];
Buffer.BlockCopy(sample.Samples, 0, bytes, 0, bytes.Length);
_buffer.AddSamples(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
// No output device (a headless stream box, a locked-down session): the alert still
// renders, it is simply silent. Never let audio take the alert down.
AppLog.Write(ex, "Alert audio output failed");
Stop();
}
}
}
/// <summary>Stop and flush — called when the clip ends so a queued tail cannot bleed into the
/// next alert.</summary>
public void Stop()
{
lock (_gate)
{
try { _device?.Stop(); } catch { /* already stopped */ }
_buffer?.ClearBuffer();
_device?.Dispose();
_device = null;
_buffer = null;
}
}
private void EnsureDevice(int sampleRate, int channels)
{
if (_device != null && _sampleRate == sampleRate && _channels == channels) return;
_device?.Dispose();
_sampleRate = sampleRate;
_channels = channels;
_buffer = new BufferedWaveProvider(new WaveFormat(sampleRate, 32, channels))
{
BufferDuration = TimeSpan.FromSeconds(2),
DiscardOnBufferOverflow = true,
};
_device = new WaveOutEvent { DesiredLatency = 120 };
_device.Init(_buffer);
_device.Play();
}
public void Dispose() => Stop();
}