Audio cues: per-cue default-sound picker, keyboard-click typing feedback, passkey on password fields

Cue sounds now ship as numbered variants ("connect 1.wav", "connect 2.wav", ...); the
count is never hard-coded so more can be added with no code change.

- CueSounds.cs: discovers a cue's "<base> <n>.wav" variants (case-insensitive) and
  resolves the active default: per-profile custom WAV > machine-wide chosen variant >
  first variant > silent. Wired into MainForm.TryLoadCueSound, the startup cue in
  Program.cs, and PreferencesDialog.ResolveCueFilePath.
- AppConfig: DefaultCueSounds (machine-wide cueId -> chosen filename) and
  EnableKeyboardClicks (on by default).
- Preferences: a "Choose default sound" listbox under the cue checklist - it lists the
  selected cue's variants, arrowing it previews each sound and makes it that cue's
  default. Plus a "Play keyboard clicks when typing into any edit field" checkbox.
- KeyClickService.cs: an app-wide WM_CHAR message filter + a low-latency NAudio mixer.
  Typing into any edit field plays a random key click (key 1..N.wav); password fields
  also play passkey.wav at the same instant. On/off live from the Preferences toggle.
  Inert if the sounds are missing or the device won't open; never consumes the keystroke.
- csproj: ship every sounds\*.wav via a wildcard (variants, key clicks, passkey, future
  additions) instead of stale per-file canonical names.
- Tests: resource checks (self-test + run-tests.ps1) now verify each cue has >=1 variant
  and that key 1.wav / passkey.wav are present. Accessibility audit still green with the
  new Preferences controls (Alt+D, Alt+K - no mnemonic clashes).
- Manual: variant picker, keyboard clicks, and the new sound-file naming documented.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Ednunp
2026-06-13 00:06:07 +01:00
co-authored by Claude Opus 4.8
parent ad66fe5364
commit e526014e6a
11 changed files with 502 additions and 104 deletions
+173
View File
@@ -0,0 +1,173 @@
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using RemSound.Core;
namespace RemSound.App;
/// <summary>
/// Audible typing feedback. When enabled, every character typed into any edit field anywhere in
/// RemSound plays one of several soft key-click sounds (chosen at random), and password fields
/// additionally play a distinct "passkey" sound at the same instant - so a screen-reader user
/// knows they're typing, and knows when they're in a masked password field.
///
/// Built for speed and overlap: the click WAVs are decoded into memory once and rendered through a
/// single persistent mixer + output, so a click fires the instant a key is pressed and fast typing
/// simply layers clicks rather than cutting them off. The hook is a single application-wide message
/// filter watching for WM_CHAR; it never consumes the keystroke, so typing is unaffected. If the
/// click sounds are missing or the output device won't open, the whole thing stays silently inert.
/// </summary>
internal static class KeyClickService
{
private const int WM_CHAR = 0x0102;
private static KeyClickPlayer? player;
private static MessageFilter? filter;
/// <summary>Live on/off, read by the message filter on every keystroke. Set from Preferences.</summary>
public static bool Enabled { get; set; }
/// <summary>Preload the click sounds, open the output, and install the app-wide key hook. Call
/// once on the GUI thread after the message loop's owner exists. Best-effort - any failure
/// leaves the service inert (no clicks), never throwing into startup.</summary>
public static void Initialize(bool enabled)
{
Enabled = enabled;
try
{
player = new KeyClickPlayer(AppConfig.SoundsDirectory);
filter = new MessageFilter();
System.Windows.Forms.Application.AddMessageFilter(filter);
}
catch
{
player = null;
filter = null;
}
}
public static void Shutdown()
{
try { if (filter is not null) System.Windows.Forms.Application.RemoveMessageFilter(filter); } catch { /* ignore */ }
try { player?.Dispose(); } catch { /* ignore */ }
filter = null;
player = null;
}
private static void OnChar(IntPtr hwnd)
{
if (!Enabled || player is null) return;
// The WM_CHAR target window is the focused control. Click only when it's an edit field.
if (System.Windows.Forms.Control.FromHandle(hwnd) is not System.Windows.Forms.TextBoxBase edit) return;
var isPassword = edit is System.Windows.Forms.TextBox tb && (tb.UseSystemPasswordChar || tb.PasswordChar != '\0');
player.PlayClick(isPassword);
}
private sealed class MessageFilter : System.Windows.Forms.IMessageFilter
{
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_CHAR) OnChar(m.HWnd);
return false; // never consume - the character must still reach the edit field
}
}
/// <summary>A short sound decoded into memory once, at the mixer's format, ready to play
/// instantly and as many times over as fast typing needs.</summary>
private sealed class CachedSound
{
public float[] AudioData { get; }
public WaveFormat WaveFormat { get; }
public CachedSound(string path, WaveFormat target)
{
using var reader = new AudioFileReader(path);
ISampleProvider source = reader;
if (source.WaveFormat.SampleRate != target.SampleRate)
source = new WdlResamplingSampleProvider(source, target.SampleRate);
if (source.WaveFormat.Channels == 1 && target.Channels == 2)
source = new MonoToStereoSampleProvider(source);
else if (source.WaveFormat.Channels == 2 && target.Channels == 1)
source = new StereoToMonoSampleProvider(source);
WaveFormat = source.WaveFormat;
var data = new List<float>(target.SampleRate); // ~1s headroom; clicks are far shorter
var buffer = new float[target.SampleRate * target.Channels];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
for (var i = 0; i < read; i++) data.Add(buffer[i]);
}
AudioData = data.ToArray();
}
}
/// <summary>One-shot reader over a <see cref="CachedSound"/>; the mixer drops it when it ends.</summary>
private sealed class CachedSoundSampleProvider : ISampleProvider
{
private readonly CachedSound sound;
private long position;
public CachedSoundSampleProvider(CachedSound sound) => this.sound = sound;
public WaveFormat WaveFormat => sound.WaveFormat;
public int Read(float[] buffer, int offset, int count)
{
var available = sound.AudioData.Length - position;
var n = (int)Math.Min(available, count);
if (n > 0) Array.Copy(sound.AudioData, position, buffer, offset, n);
position += n;
return n;
}
}
private sealed class KeyClickPlayer : IDisposable
{
private readonly WaveOutEvent output;
private readonly MixingSampleProvider mixer;
private readonly List<CachedSound> keyClips = new();
private readonly CachedSound? passkeyClip;
private readonly bool ready;
public KeyClickPlayer(string soundsDir)
{
var format = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);
mixer = new MixingSampleProvider(format) { ReadFully = true };
// A modest buffer keeps the click snappy without risking dropouts under load.
output = new WaveOutEvent { DesiredLatency = 80, NumberOfBuffers = 3 };
try
{
for (var i = 1; i <= 8; i++) // discover "key 1.wav" upward; ships with 4 but don't hard-code
{
var p = Path.Combine(soundsDir, $"key {i}.wav");
if (File.Exists(p)) keyClips.Add(new CachedSound(p, format));
}
var passkeyPath = Path.Combine(soundsDir, "passkey.wav");
if (File.Exists(passkeyPath)) passkeyClip = new CachedSound(passkeyPath, format);
if (keyClips.Count > 0)
{
output.Init(mixer);
output.Play();
ready = true;
}
}
catch { ready = false; }
}
public void PlayClick(bool isPassword)
{
if (!ready || keyClips.Count == 0) return;
try
{
mixer.AddMixerInput(new CachedSoundSampleProvider(keyClips[Random.Shared.Next(keyClips.Count)]));
if (isPassword && passkeyClip is not null)
mixer.AddMixerInput(new CachedSoundSampleProvider(passkeyClip));
}
catch { /* a key click must never disturb anything */ }
}
public void Dispose()
{
try { output.Stop(); } catch { /* ignore */ }
try { output.Dispose(); } catch { /* ignore */ }
}
}
}