diff --git a/Cargo.lock b/Cargo.lock index a005880..b4c2816 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4908,7 +4908,7 @@ checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "slidr" -version = "0.4.0" +version = "0.5.0" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 8b6a2a6..2687c26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "slidr" -version = "0.4.0" +version = "0.5.0" edition = "2021" description = "Cross-platform Arduino-based audio/macro pad controller" license = "MIT" @@ -69,6 +69,8 @@ rfd = "0.15" # to the same version `windows` re-exports as `windows::core` (shared types). windows-core = "0.58" windows = { version = "0.58", features = [ + # `#[implement]` for COM callbacks (examples/loopback_poc.rs). + "implement", "Win32_Foundation", "Win32_Media_Audio", "Win32_Media_Audio_Endpoints", @@ -78,6 +80,7 @@ windows = { version = "0.58", features = [ "Win32_System_Com_StructuredStorage", "Win32_System_Variant", "Win32_System_Registry", + "Win32_System_WinRT", "Win32_UI_Shell", "Win32_UI_Shell_PropertiesSystem", "Win32_Devices_FunctionDiscovery", diff --git a/assets/sounds/grain/eq_off.wav b/assets/sounds/grain/eq_off.wav new file mode 100644 index 0000000..dafa772 Binary files /dev/null and b/assets/sounds/grain/eq_off.wav differ diff --git a/assets/sounds/grain/eq_on.wav b/assets/sounds/grain/eq_on.wav new file mode 100644 index 0000000..929a7d6 Binary files /dev/null and b/assets/sounds/grain/eq_on.wav differ diff --git a/assets/sounds/grain/mic_off.wav b/assets/sounds/grain/mic_off.wav new file mode 100644 index 0000000..6d89234 Binary files /dev/null and b/assets/sounds/grain/mic_off.wav differ diff --git a/assets/sounds/grain/mic_on.wav b/assets/sounds/grain/mic_on.wav new file mode 100644 index 0000000..7475d28 Binary files /dev/null and b/assets/sounds/grain/mic_on.wav differ diff --git a/assets/sounds/grain/mute_off.wav b/assets/sounds/grain/mute_off.wav new file mode 100644 index 0000000..1ee56c6 Binary files /dev/null and b/assets/sounds/grain/mute_off.wav differ diff --git a/assets/sounds/grain/mute_on.wav b/assets/sounds/grain/mute_on.wav new file mode 100644 index 0000000..f580d96 Binary files /dev/null and b/assets/sounds/grain/mute_on.wav differ diff --git a/assets/sounds/grain/power_off.wav b/assets/sounds/grain/power_off.wav new file mode 100644 index 0000000..3abf772 Binary files /dev/null and b/assets/sounds/grain/power_off.wav differ diff --git a/assets/sounds/grain/power_on.wav b/assets/sounds/grain/power_on.wav new file mode 100644 index 0000000..966fb7d Binary files /dev/null and b/assets/sounds/grain/power_on.wav differ diff --git a/assets/sounds/grain/replace_off.wav b/assets/sounds/grain/replace_off.wav new file mode 100644 index 0000000..3dd5830 Binary files /dev/null and b/assets/sounds/grain/replace_off.wav differ diff --git a/assets/sounds/grain/replace_on.wav b/assets/sounds/grain/replace_on.wav new file mode 100644 index 0000000..56de86e Binary files /dev/null and b/assets/sounds/grain/replace_on.wav differ diff --git a/assets/sounds/grid/eq_off.wav b/assets/sounds/grid/eq_off.wav new file mode 100644 index 0000000..85bd945 Binary files /dev/null and b/assets/sounds/grid/eq_off.wav differ diff --git a/assets/sounds/grid/eq_on.wav b/assets/sounds/grid/eq_on.wav new file mode 100644 index 0000000..1b315c0 Binary files /dev/null and b/assets/sounds/grid/eq_on.wav differ diff --git a/assets/sounds/grid/mic_off.wav b/assets/sounds/grid/mic_off.wav new file mode 100644 index 0000000..c309dae Binary files /dev/null and b/assets/sounds/grid/mic_off.wav differ diff --git a/assets/sounds/grid/mic_on.wav b/assets/sounds/grid/mic_on.wav new file mode 100644 index 0000000..f941acf Binary files /dev/null and b/assets/sounds/grid/mic_on.wav differ diff --git a/assets/sounds/grid/mute_off.wav b/assets/sounds/grid/mute_off.wav new file mode 100644 index 0000000..a25432c Binary files /dev/null and b/assets/sounds/grid/mute_off.wav differ diff --git a/assets/sounds/grid/mute_on.wav b/assets/sounds/grid/mute_on.wav new file mode 100644 index 0000000..60adefe Binary files /dev/null and b/assets/sounds/grid/mute_on.wav differ diff --git a/assets/sounds/grid/power_off.wav b/assets/sounds/grid/power_off.wav new file mode 100644 index 0000000..50b9bba Binary files /dev/null and b/assets/sounds/grid/power_off.wav differ diff --git a/assets/sounds/grid/power_on.wav b/assets/sounds/grid/power_on.wav new file mode 100644 index 0000000..eda5ba6 Binary files /dev/null and b/assets/sounds/grid/power_on.wav differ diff --git a/assets/sounds/grid/replace_off.wav b/assets/sounds/grid/replace_off.wav new file mode 100644 index 0000000..28c9757 Binary files /dev/null and b/assets/sounds/grid/replace_off.wav differ diff --git a/assets/sounds/grid/replace_on.wav b/assets/sounds/grid/replace_on.wav new file mode 100644 index 0000000..56cbd73 Binary files /dev/null and b/assets/sounds/grid/replace_on.wav differ diff --git a/assets/sounds/tide/eq_off.wav b/assets/sounds/tide/eq_off.wav new file mode 100644 index 0000000..3db6e52 Binary files /dev/null and b/assets/sounds/tide/eq_off.wav differ diff --git a/assets/sounds/tide/eq_on.wav b/assets/sounds/tide/eq_on.wav new file mode 100644 index 0000000..441b306 Binary files /dev/null and b/assets/sounds/tide/eq_on.wav differ diff --git a/assets/sounds/tide/mic_off.wav b/assets/sounds/tide/mic_off.wav new file mode 100644 index 0000000..96fbcad Binary files /dev/null and b/assets/sounds/tide/mic_off.wav differ diff --git a/assets/sounds/tide/mic_on.wav b/assets/sounds/tide/mic_on.wav new file mode 100644 index 0000000..3c8868a Binary files /dev/null and b/assets/sounds/tide/mic_on.wav differ diff --git a/assets/sounds/tide/mute_off.wav b/assets/sounds/tide/mute_off.wav new file mode 100644 index 0000000..3f77e55 Binary files /dev/null and b/assets/sounds/tide/mute_off.wav differ diff --git a/assets/sounds/tide/mute_on.wav b/assets/sounds/tide/mute_on.wav new file mode 100644 index 0000000..487d744 Binary files /dev/null and b/assets/sounds/tide/mute_on.wav differ diff --git a/assets/sounds/tide/power_off.wav b/assets/sounds/tide/power_off.wav new file mode 100644 index 0000000..95eec46 Binary files /dev/null and b/assets/sounds/tide/power_off.wav differ diff --git a/assets/sounds/tide/power_on.wav b/assets/sounds/tide/power_on.wav new file mode 100644 index 0000000..848fe0b Binary files /dev/null and b/assets/sounds/tide/power_on.wav differ diff --git a/assets/sounds/tide/replace_off.wav b/assets/sounds/tide/replace_off.wav new file mode 100644 index 0000000..27139ef Binary files /dev/null and b/assets/sounds/tide/replace_off.wav differ diff --git a/assets/sounds/tide/replace_on.wav b/assets/sounds/tide/replace_on.wav new file mode 100644 index 0000000..3b7129d Binary files /dev/null and b/assets/sounds/tide/replace_on.wav differ diff --git a/docs/mixer-plan.md b/docs/mixer-plan.md new file mode 100644 index 0000000..9337078 --- /dev/null +++ b/docs/mixer-plan.md @@ -0,0 +1,135 @@ +# Mixer plan (Wave-Link-style routing, mixing and EQ) + +Status: planned. Proof of concept in `examples/loopback_poc.rs`. + +## Goal + +- Programs are assigned to **channels** ("Music" = Spotify, "Game", "Soundboard", …). +- Slidr mixes the channels (fader, mute, EQ) and plays the result on the + user's headset — the original stream is no longer heard. +- A **Slidr Mic** carries the real microphone (with a voice chain) plus any + channels the user sends to it; Discord etc. use it as their input. +- The mixer runs whether or not a board is connected. The board only controls it. + +## What the proof of concept established (Windows 11 22631) + +| Question | Result | +|---|---| +| Can one program's audio be captured on its own? | Yes — `VAD\Process_Loopback` (Win10 2004+), steady 48 kHz, no driver. | +| Does the capture survive muting the program's session / volume 0? | **No** — the capture is post-session-volume and goes digitally silent. Hiding the original therefore needs rerouting. | +| Can a running program be moved to another output? | Yes — `IAudioPolicyConfigFactory::SetPersistedDefaultAudioEndpoint` moved a playing Spotify between endpoints live, and clearing it moved it back. | +| Does the capture follow the move? | Yes — uninterrupted while the program switched endpoints. | +| Caveats | The override is refused (`E_INVALIDARG`) until the program has an audio session. Legacy WinMM streams (`Media.SoundPlayer`) did not move live. | + +## Architecture + +``` + ┌──────────── Slidr audio engine (own thread) ────────────┐ +Spotify ─rerouted───► │ "speaker" sink (nobody listens) │ + (per-process capture)► channel "Music" ─ fader/mute/EQ ─┬─► monitor mix ──────┼─► headset +Game ───────────────► │ channel "Game" ─ fader/mute/EQ ─┤ │ +real mic ───────────► │ voice ─ HPF/EQ/gate/comp ────────┴─► mic mix ───────────┼─► cable ─► Discord + └──────────────────────────────────────────────────────────┘ +``` + +New module `src/mixer/`: + +- `dsp.rs` — RBJ biquads (peak, shelf, high/low pass), EQ presets, gain, mixing. + Pure Rust, not platform-gated, fully unit-tested (like `audio/pulse_parse.rs`). +- `engine.rs` — real-time thread, 48 kHz f32, 10 ms blocks. One ring buffer per + source absorbs clock drift between devices. Controlled through a command + channel, like `actuator`. +- `capture.rs` — per-process loopback capture (process tree mode) and the real + mic input. +- `route.rs` — reroutes channel programs to the speaker sink once their first + session appears. Every override goes into a journal file and is cleared on + exit, on the next start after a crash, from a tray entry + ("Reset audio routing") and on uninstall (`slidr.exe --restore-audio`). +- Per-channel option **"don't reroute"**: the program keeps playing normally + and is only copied into the mic mix (for games, where the extra 20–40 ms of + monitor latency would hurt). +- Guard: the monitor device may never be the speaker sink (feedback loop). + +### Devices: how many virtual cables? + +- **Mic:** needs a real capture endpoint → one virtual cable (free VB-CABLE). +- **Speaker sink:** only needs *some* output endpoint nobody listens to. That + can be a second cable, but equally an existing unused output (e.g. a monitor's + HDMI audio without speakers, "Steam Streaming Speakers"). The sink is a + device setting; Slidr suggests candidates and only recommends a second cable + when none exists. +- Degraded modes: mic cable only → Slidr Mic without rerouting; sink only → + mixing/EQ for the user's own ears. + +## Data model + +- **Per profile** (`Preset`): `MixerConfig` with + - channels: name, programs (process / path / `@group:` refs from + `app_groups`), level, mute, EQ preset + enabled, send to mic + send level, + reroute on/off; + - voice chain (EQ, gate, compressor); + - EQ preset list. + Switching profiles re-applies routing (programs leaving all channels are + restored to the system default). +- **Global** (`Settings`): hardware only — monitor device, mic input, speaker + sink, mic cable. These are properties of the machine, not of a profile. +- New profile fields go through the existing profile version guard. + +## Board integration + +- Slider targets in `AudioStream`: "mixer channel" and "mic send of channel X". + The actuator forwards these to the engine instead of WASAPI. +- New `ActionKind`s: mute channel, toggle EQ, cycle EQ presets, set EQ preset, + toggle "channel → mic" (e.g. soundboard on/off in Discord). +- LED conditions: channel muted, EQ active. +- Mic volume via the engine is software gain on the Slidr Mic and therefore + independent of the capture driver. + +## UI + +New "Mixer" page in `ui/app.slint`: channel strips (fader, mute, EQ, → mic with +send level, program list), a voice strip, device selection, and an EQ editor +(5-band presets first, curve view later). Missing cables show a hint with an +install button. + +## Installer + +VB-CABLE is Windows-only and not installed by Slidr's installer: the end-user +licence in its package says it may not be integrated into another software's +installation procedure without VB-Audio's agreement (the website's licensing +page is more permissive; the package licence wins until VB-Audio agrees). + +So the installer's finish page points to it instead: a short note naming +VB-Audio, www.vb-cable.com and "donationware, all participations are welcome", +and an unchecked "Open the VB-CABLE download page" box. Both are hidden when +the driver is installed already (its `VBAudioVACMME` / `VBAudioVACWDM` service +is registered). The Mixer page shows the same hint with a download button when +no cable is found. Setup needs administrator rights and a restart; that stays +VB-Audio's own setup, run by the user. + +Linux needs no driver (see below). + +## Linux + +PipeWire/Pulse needs no driver and has no cable limit: `module-null-sink` as +the sink, `move-sink-input` for rerouting, monitor sources for capture, +`module-remap-source` / a null sink's monitor as the Slidr Mic. Can be developed +alongside Windows or right after it; the engine and DSP are shared. + +## Phases + +1. Fix mic volume on Windows (independent, own PR). +2. DSP + engine core without devices, unit-tested. +3. Windows I/O + routing with journal/restore. +4. Data model, persistence, Mixer page. +5. Board: slider targets, actions, LED conditions. +6. Installer section + device detection. +7. Linux backend. +8. Later: noise suppression (`nnnoiseless`), EQ curve editor. + +## Risks + +- Programs using legacy WinMM or exclusive mode can't be moved live / captured. +- If Slidr dies, rerouted programs stay on the sink until the next start or a + manual reset (journal + tray entry mitigate this). +- Monitor latency of ~20–40 ms for rerouted programs. diff --git a/examples/loopback_poc.rs b/examples/loopback_poc.rs new file mode 100644 index 0000000..e99d2dd --- /dev/null +++ b/examples/loopback_poc.rs @@ -0,0 +1,639 @@ +//! Proof of concept: per-process loopback capture for the planned mixer. +//! +//! Captures one program's audio via the Windows process-loopback virtual device +//! (`VAD\Process_Loopback`, Windows 10 2004+) and answers the question the +//! mixer design hinges on: does the capture still carry audio once the +//! program's own session is muted / turned down to 0 in the volume mixer? +//! If yes, Slidr can silence the original and play its own mix without a +//! virtual speaker device. +//! +//! Runs three phases (normal, session muted, session volume 0), prints the +//! level captured in each, then restores the session state. Optionally plays +//! the capture to an output device (substring of its name) so it can be heard +//! / fed into a virtual cable. +//! +//! cargo run --example loopback_poc -- spotify +//! cargo run --example loopback_poc -- spotify "Voicemeeter Input" 6 +//! +//! `--route [secs]` instead tests per-app output +//! routing: moves the program to device A, captures, moves it to device B +//! mid-stream, captures again, then clears the override. +//! +//! cargo run --example loopback_poc -- --route 1234 "Steam Streaming Speakers" "Voicemeeter In 1" + +#[cfg(not(target_os = "windows"))] +fn main() { + eprintln!("loopback_poc is Windows-only"); +} + +#[cfg(target_os = "windows")] +fn main() -> anyhow::Result<()> { + poc::run() +} + +#[cfg(target_os = "windows")] +mod poc { + use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::mpsc; + use std::time::{Duration, Instant}; + + use anyhow::{anyhow, bail, Context}; + use windows::core::{implement, Interface, IUnknown, HRESULT, PWSTR}; + use windows::Win32::Devices::FunctionDiscovery::PKEY_Device_FriendlyName; + use windows::Win32::Foundation::{CloseHandle, BOOL, HANDLE, WAIT_OBJECT_0}; + use windows::Win32::Media::Audio::{ + eRender, ActivateAudioInterfaceAsync, IActivateAudioInterfaceAsyncOperation, + IActivateAudioInterfaceCompletionHandler, IActivateAudioInterfaceCompletionHandler_Impl, + IAudioCaptureClient, IAudioClient, IAudioRenderClient, IAudioSessionControl2, + IAudioSessionManager2, IMMDevice, IMMDeviceEnumerator, ISimpleAudioVolume, + MMDeviceEnumerator, AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM, + AUDCLNT_STREAMFLAGS_EVENTCALLBACK, AUDCLNT_STREAMFLAGS_LOOPBACK, + AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, AUDIOCLIENT_ACTIVATION_PARAMS, + AUDIOCLIENT_ACTIVATION_PARAMS_0, AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK, + AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS, DEVICE_STATE_ACTIVE, + PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE, VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, + WAVEFORMATEX, + }; + use windows::Win32::System::Com::{ + CoCreateInstance, CoInitializeEx, CLSCTX_ALL, COINIT_MULTITHREADED, STGM_READ, + }; + use windows::Win32::System::Console::SetConsoleCtrlHandler; + use windows::Win32::System::Threading::{ + CreateEventW, OpenProcess, QueryFullProcessImageNameW, WaitForSingleObject, + PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION, + }; + + const RATE: u32 = 48_000; + const CHANNELS: u16 = 2; + const BITS: u16 = 16; + const BLOCK: usize = (CHANNELS as usize) * (BITS as usize / 8); + /// 100 ms in REFERENCE_TIME (100 ns) units. + const BUFFER_100MS: i64 = 1_000_000; + + static STOP: AtomicBool = AtomicBool::new(false); + + unsafe extern "system" fn on_ctrl(_: u32) -> BOOL { + STOP.store(true, Ordering::SeqCst); + true.into() + } + + pub fn run() -> anyhow::Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + if args.first().map(String::as_str) == Some("--route") { + unsafe { + CoInitializeEx(None, COINIT_MULTITHREADED).ok()?; + SetConsoleCtrlHandler(Some(on_ctrl), true)?; + } + return route::run(&args[1..]); + } + let Some(target) = args.first().map(|s| s.to_lowercase()) else { + bail!("usage: loopback_poc [output-device-substring] [seconds-per-phase]"); + }; + let out_name = args.get(1).filter(|s| !s.is_empty()).cloned(); + let secs: u64 = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(5); + + unsafe { + CoInitializeEx(None, COINIT_MULTITHREADED).ok()?; + SetConsoleCtrlHandler(Some(on_ctrl), true)?; + } + let enumerator: IMMDeviceEnumerator = + unsafe { CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL)? }; + + let sessions = find_sessions(&enumerator, &target)?; + if sessions.is_empty() { + bail!("no audio session found for '{target}' - is it running and has it played sound yet?"); + } + let pid = sessions[0].pid; + println!( + "target: {} session(s), capturing process tree of pid {pid} ({})", + sessions.len(), + sessions[0].exe + ); + + let capture = activate_process_loopback(pid)?; + let render = match &out_name { + Some(name) => Some(open_render(&enumerator, name)?), + None => None, + }; + + let original: Vec<(BOOL, f32)> = sessions + .iter() + .map(|s| unsafe { (s.vol.GetMute().unwrap_or_default(), s.vol.GetMasterVolume().unwrap_or(1.0)) }) + .collect(); + + let phases: [(&str, Option, Option); 3] = [ + ("normal", Some(false), None), + ("session muted", Some(true), None), + ("session volume 0", Some(false), Some(0.0)), + ]; + let mut results = Vec::new(); + let outcome = (|| -> anyhow::Result<()> { + for (label, mute, vol) in phases { + for (s, (_, orig_vol)) in sessions.iter().zip(&original) { + unsafe { + if let Some(m) = mute { + s.vol.SetMute(m, std::ptr::null())?; + } + s.vol.SetMasterVolume(vol.unwrap_or(*orig_vol), std::ptr::null())?; + } + } + println!("phase '{label}' for {secs}s ..."); + let stats = capture.pump(Duration::from_secs(secs), render.as_ref())?; + println!(" -> {stats}"); + results.push((label, stats)); + if STOP.load(Ordering::SeqCst) { + break; + } + } + Ok(()) + })(); + + for (s, (mute, vol)) in sessions.iter().zip(&original) { + unsafe { + let _ = s.vol.SetMute(*mute, std::ptr::null()); + let _ = s.vol.SetMasterVolume(*vol, std::ptr::null()); + } + } + println!("session state restored"); + outcome?; + + println!("\nsummary:"); + for (label, stats) in &results { + println!(" {label:<18} {stats}"); + } + Ok(()) + } + + struct Session { + pid: u32, + exe: String, + vol: ISimpleAudioVolume, + } + + /// Sessions of `target` on every active render endpoint (the program may not + /// be playing on the default device). + fn find_sessions(enumerator: &IMMDeviceEnumerator, target: &str) -> anyhow::Result> { + let needle = target.trim_end_matches(".exe"); + let mut out = Vec::new(); + unsafe { + let coll = enumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?; + for i in 0..coll.GetCount()? { + let dev = coll.Item(i)?; + let Ok(mgr) = dev.Activate::(CLSCTX_ALL, None) else { continue }; + let list = mgr.GetSessionEnumerator()?; + for j in 0..list.GetCount()? { + let ctl = list.GetSession(j)?; + let ctl2: IAudioSessionControl2 = ctl.cast()?; + let pid = ctl2.GetProcessId().unwrap_or(0); + if pid == 0 { + continue; + } + let Some(exe) = process_name(pid) else { continue }; + if exe.to_lowercase().trim_end_matches(".exe").contains(needle) { + out.push(Session { pid, exe, vol: ctl.cast()? }); + } + } + } + } + Ok(out) + } + + fn process_name(pid: u32) -> Option { + unsafe { + let h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid).ok()?; + let mut buf = [0u16; 512]; + let mut len = buf.len() as u32; + let ok = QueryFullProcessImageNameW(h, PROCESS_NAME_WIN32, PWSTR(buf.as_mut_ptr()), &mut len).is_ok(); + let _ = CloseHandle(h); + if !ok { + return None; + } + let path = String::from_utf16_lossy(&buf[..len as usize]); + path.rsplit('\\').next().map(str::to_string) + } + } + + fn pcm_format() -> WAVEFORMATEX { + WAVEFORMATEX { + wFormatTag: 1, // WAVE_FORMAT_PCM + nChannels: CHANNELS, + nSamplesPerSec: RATE, + nAvgBytesPerSec: RATE * BLOCK as u32, + nBlockAlign: BLOCK as u16, + wBitsPerSample: BITS, + cbSize: 0, + } + } + + #[implement(IActivateAudioInterfaceCompletionHandler)] + struct Done(mpsc::Sender<()>); + + impl IActivateAudioInterfaceCompletionHandler_Impl for Done_Impl { + fn ActivateCompleted( + &self, + _op: Option<&IActivateAudioInterfaceAsyncOperation>, + ) -> windows::core::Result<()> { + let _ = self.0.send(()); + Ok(()) + } + } + + /// Raw PROPVARIANT holding a VT_BLOB (x64 layout: 8-byte header + 16-byte union). + #[repr(C)] + struct BlobVariant { + vt: u16, + _r: [u16; 3], + cb: u32, + ptr: *const u8, + _pad: u64, + } + const VT_BLOB: u16 = 65; + + struct Capture { + client: IAudioClient, + cap: IAudioCaptureClient, + event: HANDLE, + } + + fn activate_process_loopback(pid: u32) -> anyhow::Result { + let params = AUDIOCLIENT_ACTIVATION_PARAMS { + ActivationType: AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK, + Anonymous: AUDIOCLIENT_ACTIVATION_PARAMS_0 { + ProcessLoopbackParams: AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS { + TargetProcessId: pid, + ProcessLoopbackMode: PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE, + }, + }, + }; + let var = BlobVariant { + vt: VT_BLOB, + _r: [0; 3], + cb: std::mem::size_of::() as u32, + ptr: ¶ms as *const _ as *const u8, + _pad: 0, + }; + let (tx, rx) = mpsc::channel(); + let handler: IActivateAudioInterfaceCompletionHandler = Done(tx).into(); + unsafe { + let op = ActivateAudioInterfaceAsync( + VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, + &IAudioClient::IID, + Some(&var as *const _ as *const windows::core::PROPVARIANT), + &handler, + ) + .context("ActivateAudioInterfaceAsync")?; + rx.recv_timeout(Duration::from_secs(5)).map_err(|_| anyhow!("activation timed out"))?; + let mut hr = HRESULT(0); + let mut unk: Option = None; + op.GetActivateResult(&mut hr, &mut unk)?; + hr.ok().context("process loopback activation")?; + let client: IAudioClient = unk.ok_or_else(|| anyhow!("no interface"))?.cast()?; + + let fmt = pcm_format(); + client + .Initialize( + AUDCLNT_SHAREMODE_SHARED, + AUDCLNT_STREAMFLAGS_LOOPBACK + | AUDCLNT_STREAMFLAGS_EVENTCALLBACK + | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM, + BUFFER_100MS, + 0, + &fmt, + None, + ) + .context("capture Initialize")?; + let event = CreateEventW(None, false, false, None)?; + client.SetEventHandle(event)?; + let cap: IAudioCaptureClient = client.GetService()?; + Ok(Capture { client, cap, event }) + } + } + + struct Render { + client: IAudioClient, + rc: IAudioRenderClient, + frames: u32, + name: String, + } + + fn friendly_name(dev: &IMMDevice) -> anyhow::Result { + unsafe { + let store = dev.OpenPropertyStore(STGM_READ)?; + Ok(format!("{}", store.GetValue(&PKEY_Device_FriendlyName)?)) + } + } + + /// First active output endpoint whose friendly name contains `name` (case-insensitive). + fn find_render(enumerator: &IMMDeviceEnumerator, name: &str) -> anyhow::Result<(IMMDevice, String)> { + let needle = name.to_lowercase(); + unsafe { + let coll = enumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?; + for i in 0..coll.GetCount()? { + let dev = coll.Item(i)?; + let friendly = friendly_name(&dev)?; + if friendly.to_lowercase().contains(&needle) { + return Ok((dev, friendly)); + } + } + } + bail!("no output device matching '{name}'") + } + + fn open_render(enumerator: &IMMDeviceEnumerator, name: &str) -> anyhow::Result { + unsafe { + let (dev, friendly) = find_render(enumerator, name)?; + let client: IAudioClient = dev.Activate(CLSCTX_ALL, None)?; + let fmt = pcm_format(); + client + .Initialize( + AUDCLNT_SHAREMODE_SHARED, + AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, + BUFFER_100MS * 2, + 0, + &fmt, + None, + ) + .context("render Initialize")?; + let frames = client.GetBufferSize()?; + let rc: IAudioRenderClient = client.GetService()?; + println!("playing capture on: {friendly}"); + Ok(Render { client, rc, frames, name: friendly }) + } + } + + impl Render { + /// Queue as much of `data` as fits; the rest is dropped (POC, no resampling/drift handling). + fn write(&self, data: &[u8]) -> windows::core::Result<()> { + unsafe { + let free = self.frames - self.client.GetCurrentPadding()?; + let n = free.min((data.len() / BLOCK) as u32); + if n == 0 { + return Ok(()); + } + let buf = self.rc.GetBuffer(n)?; + std::ptr::copy_nonoverlapping(data.as_ptr(), buf, n as usize * BLOCK); + self.rc.ReleaseBuffer(n, 0) + } + } + } + + #[derive(Default)] + pub struct Stats { + frames: u64, + sum_sq: f64, + peak: f32, + silent_packets: u32, + packets: u32, + } + + impl std::fmt::Display for Stats { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let samples = self.frames * CHANNELS as u64; + let rms = if samples == 0 { 0.0 } else { (self.sum_sq / samples as f64).sqrt() }; + let db = |v: f64| if v <= 0.0 { f64::NEG_INFINITY } else { 20.0 * v.log10() }; + write!( + f, + "frames {:>7} packets {:>4} (silent flag {:>4}) rms {:>7.1} dBFS peak {:>7.1} dBFS", + self.frames, + self.packets, + self.silent_packets, + db(rms), + db(self.peak as f64) + ) + } + } + + impl Capture { + fn pump(&self, dur: Duration, render: Option<&Render>) -> anyhow::Result { + const SILENT: u32 = 0x2; // AUDCLNT_BUFFERFLAGS_SILENT + let mut stats = Stats::default(); + let mut scratch: Vec = Vec::new(); + unsafe { + self.client.Start()?; + if let Some(r) = render { + r.client.Start()?; + } + let end = Instant::now() + dur; + while Instant::now() < end && !STOP.load(Ordering::SeqCst) { + if WaitForSingleObject(self.event, 100) != WAIT_OBJECT_0 { + continue; + } + loop { + let mut data = std::ptr::null_mut(); + let mut n = 0u32; + let mut flags = 0u32; + self.cap.GetBuffer(&mut data, &mut n, &mut flags, None, None)?; + if n == 0 { + self.cap.ReleaseBuffer(0)?; + break; + } + stats.packets += 1; + stats.frames += n as u64; + let bytes = n as usize * BLOCK; + scratch.clear(); + if flags & SILENT != 0 { + stats.silent_packets += 1; + scratch.resize(bytes, 0); + } else { + scratch.extend_from_slice(std::slice::from_raw_parts(data, bytes)); + } + self.cap.ReleaseBuffer(n)?; + for s in scratch.chunks_exact(2) { + let v = i16::from_le_bytes([s[0], s[1]]) as f32 / 32768.0; + stats.sum_sq += (v * v) as f64; + stats.peak = stats.peak.max(v.abs()); + } + if let Some(r) = render { + r.write(&scratch).with_context(|| format!("render to {}", r.name))?; + } + } + } + self.client.Stop()?; + if let Some(r) = render { + r.client.Stop()?; + } + } + Ok(stats) + } + } + + impl Drop for Capture { + fn drop(&mut self) { + unsafe { + let _ = CloseHandle(self.event); + } + } + } + + /// Per-app default output, i.e. what Settings > "App volume and device + /// preferences" changes. Windows persists it per executable until cleared. + #[allow(non_snake_case)] + mod route { + use super::*; + use std::ffi::c_void; + use windows::core::{interface, IUnknown_Vtbl, HSTRING}; + use windows::Win32::Media::Audio::{ + eConsole, eMultimedia, AudioSessionStateActive, EDataFlow, ERole, + }; + use windows::Win32::System::Com::CoTaskMemFree; + use windows::Win32::System::WinRT::RoGetActivationFactory; + + /// KSCATEGORY_AUDIO render interface suffix of an MMDevice interface path. + const RENDER_INTERFACE: &str = "{e6327cad-dcec-4949-ae8a-991e976a79d2}"; + + // Undocumented activation factory behind `Windows.Media.Internal.AudioPolicyConfig` + // (Win10 21H2+ IID; EarTrumpet uses the same layout). Only the three + // persisted-endpoint methods are called; the rest are vtable placeholders. + #[interface("ab3d4648-e242-459f-b02f-541c70306324")] + unsafe trait IAudioPolicyConfigFactory: IUnknown { + // IInspectable + fn GetIids(&self) -> HRESULT; + fn GetRuntimeClassName(&self) -> HRESULT; + fn GetTrustLevel(&self) -> HRESULT; + fn p00(&self) -> HRESULT; + fn p01(&self) -> HRESULT; + fn p02(&self) -> HRESULT; + fn p03(&self) -> HRESULT; + fn p04(&self) -> HRESULT; + fn p05(&self) -> HRESULT; + fn p06(&self) -> HRESULT; + fn p07(&self) -> HRESULT; + fn p08(&self) -> HRESULT; + fn p09(&self) -> HRESULT; + fn p10(&self) -> HRESULT; + fn p11(&self) -> HRESULT; + fn p12(&self) -> HRESULT; + fn p13(&self) -> HRESULT; + fn p14(&self) -> HRESULT; + fn p15(&self) -> HRESULT; + fn p16(&self) -> HRESULT; + fn p17(&self) -> HRESULT; + fn p18(&self) -> HRESULT; + fn SetPersistedDefaultAudioEndpoint( + &self, + pid: u32, + flow: EDataFlow, + role: ERole, + device: *mut c_void, + ) -> HRESULT; + fn GetPersistedDefaultAudioEndpoint( + &self, + pid: u32, + flow: EDataFlow, + role: ERole, + device: *mut *mut c_void, + ) -> HRESULT; + fn ClearAllPersistedApplicationDefaultEndpoints(&self) -> HRESULT; + } + + fn factory() -> anyhow::Result { + unsafe { + RoGetActivationFactory(&HSTRING::from("Windows.Media.Internal.AudioPolicyConfig")) + .context("AudioPolicyConfig factory") + } + } + + fn interface_path(dev: &IMMDevice) -> anyhow::Result { + unsafe { + let id = dev.GetId()?; + let s = id.to_string(); + CoTaskMemFree(Some(id.0 as _)); + Ok(format!(r"\\?\SWD#MMDEVAPI#{}#{RENDER_INTERFACE}", s?)) + } + } + + /// Route `pid`'s output to `dev`, or back to the system default with `None`. + fn set(f: &IAudioPolicyConfigFactory, pid: u32, dev: Option<&IMMDevice>) -> anyhow::Result<()> { + let path = dev.map(interface_path).transpose()?.map(HSTRING::from); + for role in [eConsole, eMultimedia] { + unsafe { + let raw: *mut c_void = match &path { + Some(h) => std::mem::transmute_copy(h), + None => std::ptr::null_mut(), + }; + f.SetPersistedDefaultAudioEndpoint(pid, eRender, role, raw) + .ok() + .context("SetPersistedDefaultAudioEndpoint")?; + } + } + Ok(()) + } + + fn get(f: &IAudioPolicyConfigFactory, pid: u32) -> String { + unsafe { + let mut raw: *mut c_void = std::ptr::null_mut(); + if let Err(e) = f.GetPersistedDefaultAudioEndpoint(pid, eRender, eMultimedia, &mut raw).ok() { + return format!(""); + } + let h: HSTRING = std::mem::transmute(raw); + if h.is_empty() { "".into() } else { h.to_string() } + } + } + + /// "device [active|idle]" for every session of `pid`. + fn session_devices(enumerator: &IMMDeviceEnumerator, pid: u32) -> anyhow::Result> { + let mut out = Vec::new(); + unsafe { + let coll = enumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?; + for i in 0..coll.GetCount()? { + let dev = coll.Item(i)?; + let Ok(mgr) = dev.Activate::(CLSCTX_ALL, None) else { continue }; + let list = mgr.GetSessionEnumerator()?; + for j in 0..list.GetCount()? { + let ctl = list.GetSession(j)?; + let ctl2: IAudioSessionControl2 = ctl.cast()?; + if ctl2.GetProcessId().unwrap_or(0) == pid { + let state = if ctl.GetState()? == AudioSessionStateActive { "active" } else { "idle" }; + out.push(format!("{} [{state}]", friendly_name(&dev)?)); + } + } + } + } + Ok(out) + } + + pub fn run(args: &[String]) -> anyhow::Result<()> { + let (Some(pid), Some(a), Some(b)) = (args.first(), args.get(1), args.get(2)) else { + bail!("usage: loopback_poc --route [seconds]"); + }; + let pid: u32 = pid.parse().context("pid")?; + let secs: u64 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(4); + let enumerator: IMMDeviceEnumerator = + unsafe { CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL)? }; + let f = factory()?; + let (dev_a, name_a) = find_render(&enumerator, a)?; + let (dev_b, name_b) = find_render(&enumerator, b)?; + let show = |label: &str| { + println!( + "{label}: override {} | sessions {:?}", + get(&f, pid), + session_devices(&enumerator, pid).unwrap_or_default() + ); + }; + + show("before"); + let outcome = (|| -> anyhow::Result<()> { + set(&f, pid, Some(&dev_a))?; + show(&format!("routed to '{name_a}'")); + // Lets a test player wait until it is routed before it makes a sound. + if let Ok(flag) = std::env::var("POC_READY_FILE") { + std::fs::write(flag, b"")?; + } + let capture = activate_process_loopback(pid)?; + let s = capture.pump(Duration::from_secs(secs), None)?; + show("after capture A"); + println!(" capture A -> {s}"); + set(&f, pid, Some(&dev_b))?; + std::thread::sleep(Duration::from_millis(500)); + show(&format!("moved to '{name_b}' mid-stream")); + let s = capture.pump(Duration::from_secs(secs), None)?; + show("after capture B"); + println!(" capture B -> {s}"); + Ok(()) + })(); + set(&f, pid, None)?; + show("cleared"); + outcome + } + } +} diff --git a/installer/slidr.nsi b/installer/slidr.nsi index 918758a..8c8cd55 100644 --- a/installer/slidr.nsi +++ b/installer/slidr.nsi @@ -28,6 +28,16 @@ BrandingText "Slidr ${VERSION}" !insertmacro MUI_PAGE_INSTFILES !define MUI_FINISHPAGE_RUN "$INSTDIR\slidr.exe" !define MUI_FINISHPAGE_RUN_TEXT "Launch Slidr" +; Slidr's mixer needs a virtual audio cable for the Slidr Mic. VB-CABLE's +; licence doesn't allow building it into another installer without VB-Audio's +; agreement, so the finish page only points to it (unchecked, and not at all +; when it is installed already). +!define MUI_FINISHPAGE_TEXT "Slidr has been installed.$\r$\n$\r$\nFor the mixer's Slidr Mic you also need a virtual audio cable: VB-CABLE by VB-Audio (www.vb-cable.com). VB-CABLE is donationware, all participations are welcome." +!define MUI_FINISHPAGE_SHOWREADME "" +!define MUI_FINISHPAGE_SHOWREADME_TEXT "Open the VB-CABLE download page" +!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED +!define MUI_FINISHPAGE_SHOWREADME_FUNCTION OpenCablePage +!define MUI_PAGE_CUSTOMFUNCTION_SHOW FinishShow !insertmacro MUI_PAGE_FINISH ; ── Uninstaller pages ── @@ -70,6 +80,36 @@ LangString DESC_SecDesktop ${LANG_ENGLISH} "Place a shortcut on the Desktop." !insertmacro MUI_DESCRIPTION_TEXT ${SecDesktop} $(DESC_SecDesktop) !insertmacro MUI_FUNCTION_DESCRIPTION_END +; ── VB-CABLE hint on the finish page ── +!define CABLE_PAGE "https://vb-audio.com/Cable/" + +; $0 = 1 when VB-CABLE's driver is installed (its service is registered). +Function CableInstalled + StrCpy $0 0 + ClearErrors + EnumRegKey $1 HKLM "SYSTEM\CurrentControlSet\Services\VBAudioVACMME" 0 + IfErrors 0 found + ClearErrors + EnumRegKey $1 HKLM "SYSTEM\CurrentControlSet\Services\VBAudioVACWDM" 0 + IfErrors done + found: + StrCpy $0 1 + done: +FunctionEnd + +Function FinishShow + Call CableInstalled + StrCmp $0 1 0 keep + ; Already there: plain "installed" text, no checkbox. + ShowWindow $mui.FinishPage.ShowReadme ${SW_HIDE} + SendMessage $mui.FinishPage.Text ${WM_SETTEXT} 0 "STR:Slidr has been installed." + keep: +FunctionEnd + +Function OpenCablePage + ExecShell "open" "${CABLE_PAGE}" +FunctionEnd + Section "Uninstall" Delete "$INSTDIR\slidr.exe" Delete "$INSTDIR\logo.ico" diff --git a/src/actuator.rs b/src/actuator.rs index a3bb82e..f10db74 100644 --- a/src/actuator.rs +++ b/src/actuator.rs @@ -111,7 +111,8 @@ const OUTPUT_REAPPLY_DELAYS: [Duration; 2] = /// which is the jump the user sees. Mute state is deliberately left alone: the /// switch shouldn't un-mute anything the user muted on purpose. fn reapply_volumes(audio: &dyn AudioBackend, applied: &HashMap) { - for (target, value) in applied.values() { + // Mixer levels live in Slidr and don't move with the output device. + for (target, value) in applied.values().filter(|(t, _)| !matches!(t, Target::Mixer(_))) { audio.set_volume(vol_target(target), *value); } } @@ -142,6 +143,7 @@ fn target_key(t: &Target) -> String { Target::Process(p) => format!("p:{p}"), Target::Mic(m) => format!("m:{m}"), Target::System(d) => format!("s:{}", d.as_deref().unwrap_or("")), + Target::Mixer(m) => format!("x:{}", m.key()), } } @@ -159,6 +161,13 @@ fn apply( Cmd::SerialConnected => led.on_connected(), Cmd::LedSaveState => led.save_state(), Cmd::LedClearSaved => led.clear_and_save(), + Cmd::SetVolume { target: Target::Mixer(m), value, unmute } => { + if unmute { + crate::mixer::set_mute(m, false); + } + crate::mixer::set_level(m, value); + } + Cmd::ToggleMute(Target::Mixer(m)) => crate::mixer::toggle_mute(m), Cmd::SetVolume { target, value, unmute } => { if unmute { audio.set_mute(mute_target(&target), false); @@ -236,6 +245,7 @@ fn vol_target(t: &Target) -> VolumeTarget<'_> { Target::Process(p) => VolumeTarget::Process(p), Target::Mic(m) => VolumeTarget::Mic(m), Target::System(d) => VolumeTarget::System(d.as_deref()), + Target::Mixer(_) => unreachable!("mixer targets are handled before the audio backend"), } } @@ -244,5 +254,6 @@ fn mute_target(t: &Target) -> MuteTarget<'_> { Target::Process(p) => MuteTarget::Process(p), Target::Mic(m) => MuteTarget::Mic(m), Target::System(d) => MuteTarget::System(d.as_deref()), + Target::Mixer(_) => unreachable!("mixer targets are handled before the audio backend"), } } diff --git a/src/audio/wasapi.rs b/src/audio/wasapi.rs index d3b9f4c..ef9b06e 100644 --- a/src/audio/wasapi.rs +++ b/src/audio/wasapi.rs @@ -125,47 +125,55 @@ impl WasapiBackend { mut visit: F, ) { let _guard = self.lock.lock().unwrap(); - let group = crate::app_groups::parse_ref(process); - let needle = strip_exe(&process.to_lowercase()); unsafe { - let Ok(dev) = self.default_device(0) else { return }; - let Ok(mgr) = self.session_manager(&dev) else { return }; - let Ok(enumerator): Result = mgr.GetSessionEnumerator() - else { - return; - }; - let Ok(count) = enumerator.GetCount() else { return }; - for i in 0..count { - let Ok(ctl): Result = enumerator.GetSession(i) else { + // Every active output, not just the default: a program can play + // elsewhere (its own device choice, or moved to the mixer's + // silent output), and its volume still lives on that session. + let Ok(devices) = self.enumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE) else { return }; + for d in 0..devices.GetCount().unwrap_or(0) { + let Ok(dev) = devices.Item(d) else { continue }; + let Ok(mgr) = self.session_manager(&dev) else { continue }; + let Ok(enumerator): Result = mgr.GetSessionEnumerator() + else { continue; }; - let Ok(ctl2) = ctl.cast::() else { continue }; - let Ok(pid) = ctl2.GetProcessId() else { continue }; - if pid == 0 { - continue; - } - let Some(name) = process_name(pid) else { continue }; - let hit = match group { - // Group membership also considers where the program lives - // (game libraries), so it needs the full image path. - Some(id) => crate::app_groups::matches( - id, - &name, - crate::app_groups::process_path(pid).as_deref(), - ), - None => { - strip_exe(&name.to_lowercase()).contains(&needle) - || needle.contains(&strip_exe(&name.to_lowercase())) + let Ok(count) = enumerator.GetCount() else { continue }; + for i in 0..count { + let Ok(ctl): Result = enumerator.GetSession(i) else { + continue; + }; + let Ok(ctl2) = ctl.cast::() else { continue }; + let Ok(pid) = ctl2.GetProcessId() else { continue }; + if pid == 0 { + continue; + } + let Some(name) = process_name(pid) else { continue }; + if program_matches(process, pid, &name) { + visit(&ctl2, pid); } - }; - if hit { - visit(&ctl2, pid); } } } } } +/// Does the running program `pid` (image name `exe`) belong to `program`? +/// +/// `program` is either a plain executable name (matched loosely, so "chrome" +/// finds "chrome.exe") or a program-group reference such as `@group:games`. +pub(crate) fn program_matches(program: &str, pid: u32, exe: &str) -> bool { + match crate::app_groups::parse_ref(program) { + // Group membership also considers where the program lives (game + // libraries), so it needs the full image path. + Some(id) => crate::app_groups::matches(id, exe, crate::app_groups::process_path(pid).as_deref()), + None => { + let needle = strip_exe(&program.to_lowercase()); + let name = strip_exe(&exe.to_lowercase()); + name.contains(&needle) || needle.contains(&name) + } + } +} + fn strip_exe(s: &str) -> String { let s = s.strip_suffix(".exe").unwrap_or(s); s.rsplit(['/', '\\']).next().unwrap_or(s).to_string() @@ -515,7 +523,7 @@ unsafe trait IPolicyConfig: IUnknown { fn SetEndpointVisibility(&self) -> HRESULT; } -fn process_name(pid: u32) -> Option { +pub(crate) fn process_name(pid: u32) -> Option { use windows::core::PWSTR; use windows::Win32::System::Threading::QueryFullProcessImageNameW; use windows::Win32::System::Threading::PROCESS_NAME_WIN32; diff --git a/src/events.rs b/src/events.rs index 92dde3e..cc1f310 100644 --- a/src/events.rs +++ b/src/events.rs @@ -15,6 +15,8 @@ pub enum Target { Process(String), Mic(String), System(Option), + /// A strip of Slidr's own mixer. + Mixer(crate::model::MixTarget), } /// A side-effect emitted by the engine, executed off the UI thread by the @@ -291,7 +293,9 @@ fn api_cmd(api: &crate::model::ApiCall, percent: Option) -> Cmd { } fn stream_target(s: &crate::model::AudioStream) -> Target { - if let Some(p) = &s.process { + if let Some(m) = s.mixer { + Target::Mixer(m) + } else if let Some(p) = &s.process { Target::Process(p.clone()) } else if let Some(m) = &s.mic_name { Target::Mic(m.clone()) @@ -360,6 +364,13 @@ fn push_button_cmds( } } } + ActionKind::MuteMixer => { + if just_pressed { + if let Some(m) = action.mixer { + out.push(Cmd::ToggleMute(Target::Mixer(m))); + } + } + } ActionKind::OpenProcess | ActionKind::OpenWebsite => { if just_pressed { if let Some(t) = &action.property { diff --git a/src/glue.rs b/src/glue.rs index c1b0dcd..684c3ac 100644 --- a/src/glue.rs +++ b/src/glue.rs @@ -128,6 +128,17 @@ impl Shared { .cmd_tx .send(crate::events::Cmd::SetLeds(Box::new(self.preset.leds.clone()))); } + + /// Hand the profile's mixer layout and the mixer devices to the mixer. + /// Call wherever `push_leds` is called (profile load/switch, settings). + pub fn push_mixer(&self) { + crate::mixer::configure(self.preset.mixer.clone(), self.settings.mixer_devices.clone()); + crate::mixer::sound_prefs( + self.settings.mixer_sounds, + self.settings.mixer_sound_volume, + &self.settings.mixer_sound_set, + ); + } } pub fn wire( @@ -146,6 +157,7 @@ pub fn wire( push_preset_to_ui(ui, &s.preset); apply_appearance(ui, &s.preset.settings); s.push_leds(); + s.push_mixer(); } // Warn if the active profile was written by a newer Slidr (and block @@ -281,6 +293,7 @@ pub fn wire( push_settings_to_ui(&ui, &s.settings, &s.preset.settings); apply_appearance(&ui, &s.preset.settings); s.push_leds(); + s.push_mixer(); refresh_home(&ui, &s); toast(&ui, &format!("Created profile: {name}")); } @@ -445,6 +458,7 @@ pub fn wire( let Some(c) = s.preset.slider_categories.iter().find(|c| c.id == cat_id) else { return }; let Some(stream) = c.streams.get(idx) else { return }; if let Some(api) = &stream.api { (3, String::new(), String::new(), Some(api.clone())) } + else if let Some(m) = stream.mixer { (4, m.to_value(), s.preset.mixer.label(m), None) } else if let Some(p) = &stream.process { (0, p.clone(), p.clone(), None) } else if let Some(m) = &stream.mic_name { (1, m.clone(), m.clone(), None) } else { (2, stream.device_name.clone().unwrap_or_default(), @@ -453,7 +467,10 @@ pub fn wire( let Some(c) = s.preset.button_categories.iter().find(|c| c.id == cat_id) else { return }; let Some(a) = c.actions.get(idx) else { return }; let k = action_kind_index(a.kind); - let prop = a.property.clone().unwrap_or_default(); + let prop = match a.mixer { + Some(m) => m.to_value(), + None => a.property.clone().unwrap_or_default(), + }; // For KeyPress, show the friendly name in the picker. let shown = if a.kind == ActionKind::KeyPress { prop.parse::().ok() @@ -550,6 +567,10 @@ pub fn wire( 0 => AudioStream { process: Some(result.property.into()), ..Default::default() }, 1 => AudioStream { mic_name: Some(result.property.into()), ..Default::default() }, 2 => AudioStream { device_name: Some(result.property.into()), ..Default::default() }, + 4 => AudioStream { + mixer: crate::model::MixTarget::from_value(&result.property), + ..Default::default() + }, _ => AudioStream { api: Some(api_from_result(&result)), ..Default::default() }, }; match editing { @@ -562,7 +583,7 @@ pub fn wire( if kind == ActionKind::ApiCall { let action = ButtonAction { kind, property: None, display: None, - api: Some(api_from_result(&result)), + api: Some(api_from_result(&result)), mixer: None, }; match editing { Some(i) if i < c.actions.len() => c.actions[i] = action, @@ -595,7 +616,12 @@ pub fn wire( let disp = if result.display.is_empty() { None } else { Some(result.display.to_string()) }; (prop, disp) }; - let action = ButtonAction { kind, property: prop, display: disp, api: None }; + // A mixer action keeps its strip in `mixer`, not in the string property. + let mixer = (kind == ActionKind::MuteMixer) + .then(|| crate::model::MixTarget::from_value(&result.property)) + .flatten(); + let prop = if mixer.is_some() { None } else { prop }; + let action = ButtonAction { kind, property: prop, display: disp, api: None, mixer }; match editing { Some(i) if i < c.actions.len() => c.actions[i] = action, _ => c.actions.push(action), @@ -807,6 +833,7 @@ pub fn wire( apply_appearance(&ui, &s.preset.settings); // LED brightness / experimental flag may have changed. s.push_leds(); + s.push_mixer(); // Re-point the Discord worker if the application changed (a blank id // stops it). Cheap and idempotent when nothing moved. crate::discord::configure(&s.settings.discord_client_id, &s.settings.discord_client_secret); @@ -856,6 +883,7 @@ pub fn wire( push_settings_to_ui(&ui, &s.settings, &s.preset.settings); apply_appearance(&ui, &s.preset.settings); s.push_leds(); + s.push_mixer(); // Refresh Home immediately with the new assignments using the // last live readings — no board input required. refresh_home(&ui, &s); @@ -977,6 +1005,7 @@ pub fn wire( s.preset.leds[led] = Some(cfg); s.save_preset(); s.push_leds(); + s.push_mixer(); drop(s); ui.set_led_popup_open(false); toast(&ui, "LED saved."); @@ -1147,6 +1176,7 @@ pub fn wire( ui.set_retry_eta_secs(-1.0); // Board reset on connect; re-push all desired LED state. s.push_leds(); + s.push_mixer(); changed = true; } SerialEvent::Disconnected { reason, retrying_in } => { @@ -1187,6 +1217,11 @@ pub fn wire( ui.set_led_live_active(active); } + // Mixer faders follow the board while the page is open. + if ui.get_current_page() == 4 { + crate::mixer_ui::follow_levels(&ui, &s); + } + // The Discord worker connects (or fails) seconds after the settings are // saved, and the media poller updates once a second — so refresh both // status lines while the user is looking at them, or they would sit on @@ -1368,7 +1403,10 @@ fn push_preset_to_ui(ui: &AppWindow, preset: &Preset) { lines: ModelRc::new(VecModel::from( c.streams.iter().enumerate().map(|(i, s)| LineItem { id: i as i32, - primary: s.label().into(), + primary: match s.mixer { + Some(m) => preset.mixer.label(m).into(), + None => s.label().into(), + }, secondary: stream_secondary(s).into(), icon_kind: stream_icon_kind(s), }).collect::>(), @@ -1403,6 +1441,7 @@ fn push_preset_to_ui(ui: &AppWindow, preset: &Preset) { fn stream_icon_kind(s: &AudioStream) -> i32 { if s.api.is_some() { 3 } + else if s.mixer.is_some() { 5 } else if s.process.as_deref().is_some_and(|p| crate::app_groups::parse_ref(p).is_some()) { 6 } else if s.process.is_some() { 0 } else if s.mic_name.is_some() { 1 } @@ -1444,12 +1483,15 @@ fn action_icon_kind(k: ActionKind) -> i32 { match k { MuteProcess => 0, MuteMic => 1, MuteMainAudio => 5, OpenProcess => 0, OpenWebsite => 3, KeyPress => 4, CycleOutput => 2, ApiCall => 3, + MuteMixer => 5, } } fn stream_secondary(s: &AudioStream) -> String { if s.api.is_some() { "API call".into() + } else if s.mixer.is_some() { + "Slidr mixer".into() } else if let Some(p) = &s.process { match crate::app_groups::parse_ref(p) { Some(_) => "program category".into(), @@ -1476,6 +1518,14 @@ fn push_settings_to_ui(ui: &AppWindow, global: &Settings, profile: &crate::model ui.set_minimize_to_tray(global.minimize_to_tray); ui.set_led_experimental(global.led_experimental); ui.set_update_check(global.update_check); + ui.set_sounds_on(global.mixer_sounds); + ui.set_sound_volume(global.mixer_sound_volume * 100.0); + let sets = crate::mixer::SOUND_SETS; + ui.set_sound_sets(ModelRc::new(VecModel::from(sets.iter().map(|s| SharedString::from(s.1)).collect::>()))); + ui.set_sound_set_index(sets.iter().position(|s| s.0 == global.mixer_sound_set).unwrap_or(0) as i32); + ui.set_grid_sliders(global.grid_sliders); + ui.set_grid_buttons(global.grid_buttons); + crate::presets_ui::push(ui, global); ui.set_discord_client_id(global.discord_client_id.clone().into()); ui.set_discord_client_secret(global.discord_client_secret.clone().into()); ui.set_discord_status(discord_hint(global).into()); @@ -1510,6 +1560,13 @@ fn pull_global_from_ui(ui: &AppWindow, g: &mut Settings) { g.update_check = ui.get_update_check(); g.discord_client_id = ui.get_discord_client_id().to_string(); g.discord_client_secret = ui.get_discord_client_secret().to_string(); + g.mixer_sounds = ui.get_sounds_on(); + g.mixer_sound_volume = (ui.get_sound_volume() / 100.0).clamp(0.0, 1.0); + if let Some(set) = crate::mixer::SOUND_SETS.get(ui.get_sound_set_index() as usize) { + g.mixer_sound_set = set.0.to_string(); + } + g.grid_sliders = ui.get_grid_sliders(); + g.grid_buttons = ui.get_grid_buttons(); } fn pull_profile_from_ui(ui: &AppWindow, p: &mut crate::model::ProfileSettings) { @@ -1578,6 +1635,7 @@ fn action_kind_index(k: ActionKind) -> i32 { match k { MuteProcess => 0, MuteMainAudio => 1, MuteMic => 2, OpenProcess => 3, OpenWebsite => 4, KeyPress => 5, CycleOutput => 6, ApiCall => 7, + MuteMixer => 8, } } @@ -1585,7 +1643,8 @@ fn button_kind_from_index(i: i32) -> ActionKind { use ActionKind::*; match i { 0 => MuteProcess, 1 => MuteMainAudio, 2 => MuteMic, - 3 => OpenProcess, 4 => OpenWebsite, 6 => CycleOutput, 7 => ApiCall, _ => KeyPress, + 3 => OpenProcess, 4 => OpenWebsite, 6 => CycleOutput, 7 => ApiCall, 8 => MuteMixer, + _ => KeyPress, } } @@ -1604,6 +1663,38 @@ fn push_wizard_picker(ui: &AppWindow, shared: Arc>) { let kind = ui.get_wizard_kind(); let filter = ui.get_wizard_filter().to_string().to_lowercase(); + // Mixer strips: fixed list from the profile, labelled by channel name. + if (target_kind, kind) == (0, 4) || (target_kind, kind) == (1, 8) { + use crate::model::MixTarget; + let s = shared.lock(); + let m = &s.preset.mixer; + // The microphone is a channel now (see `Preset::move_voice_to_channel`). + let mut targets = Vec::new(); + for c in &m.channels { + targets.push(MixTarget::Channel(c.id)); + // A button mutes a whole channel; only a slider can drive the send. + if target_kind == 0 { + targets.push(MixTarget::Send(c.id)); + } + } + let current = ui.get_wizard_property().to_string(); + let rows: Vec = targets + .into_iter() + .map(|t| (m.label(t), t.to_value())) + .filter(|(name, _)| filter.is_empty() || name.to_lowercase().contains(filter.trim())) + .map(|(name, value)| crate::PickerEntry { + selected: value == current, + name: name.into(), + value: value.into(), + icon_kind: 5, + is_group: false, + }) + .collect(); + drop(s); + ui.set_wizard_picker_source(ModelRc::new(VecModel::from(rows))); + return; + } + let s = shared.lock(); let source: Vec = match (target_kind, kind) { // Slider streams @@ -1719,7 +1810,7 @@ fn is_program_kind(target_kind: i32, kind: i32) -> bool { /// Merge sessions-with-audio with all running processes, so the picker isn't /// empty when no audio is playing yet. -fn merged_processes(audio: &dyn AudioBackend) -> Vec { +pub(crate) fn merged_processes(audio: &dyn AudioBackend) -> Vec { let mut out = audio.list_processes(); out.extend(list_running_processes()); out @@ -2049,7 +2140,7 @@ fn spawn_update_check(ui: &AppWindow) { }); } -fn toast(ui: &AppWindow, msg: &str) { +pub(crate) fn toast(ui: &AppWindow, msg: &str) { ui.set_toast_text(msg.into()); let weak = ui.as_weak(); let timer = slint::Timer::default(); diff --git a/src/main.rs b/src/main.rs index 73c1c73..96f2df2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,9 +23,15 @@ mod keys_library; mod keys_vk; mod led; mod media; +mod mixer; +mod mixer_ui; mod model; +mod presets_ui; mod protocol; +#[cfg(target_os = "windows")] +mod quit_signal; mod serial; +mod sidebar; mod storage; mod update; mod version; @@ -52,6 +58,15 @@ fn main() -> anyhow::Result<()> { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); + // `slidr --quit`: close a running Slidr properly (see `quit_signal`). + #[cfg(target_os = "windows")] + if std::env::args().any(|a| a == "--quit") { + if !quit_signal::signal() { + eprintln!("no running Slidr to quit"); + } + return Ok(()); + } + // Preview / design-iteration mode: spawn a window pre-populated with mock state // so the UI can be screenshotted headlessly without an Arduino. // --demo seed demo data + mark connected @@ -162,6 +177,11 @@ fn main() -> anyhow::Result<()> { let ui = AppWindow::new()?; glue::wire(&ui, shared.clone(), rx, BUILD_STAMP); + mixer_ui::wire(&ui, shared.clone()); + sidebar::wire(&ui, shared.clone()); + presets_ui::wire(&ui, shared.clone()); + #[cfg(target_os = "windows")] + quit_signal::listen(); if demo { use slint::{ModelRc, SharedString, VecModel}; @@ -347,5 +367,7 @@ fn main() -> anyhow::Result<()> { let mut s = shared.lock(); let _ = storage::save_settings(&s.settings); s.save_preset(); + drop(s); + mixer::shutdown(); Ok(()) } diff --git a/src/mixer/dsp.rs b/src/mixer/dsp.rs new file mode 100644 index 0000000..c03952d --- /dev/null +++ b/src/mixer/dsp.rs @@ -0,0 +1,346 @@ +//! Signal processing for the mixer: EQ filters and gain ramps. +//! +//! Everything works on interleaved stereo `f32` blocks at [`RATE`]. Pure Rust +//! with no platform code, so it is unit-tested on any host. + +use crate::model::{EqBand, EqBandKind}; + +/// Sample rate of the whole mixer. +pub const RATE: u32 = 48_000; +/// Frames per processing block (10 ms). +pub const BLOCK_FRAMES: usize = 480; +/// Samples per block (stereo, interleaved). +pub const BLOCK_SAMPLES: usize = BLOCK_FRAMES * 2; + +/// Fader position [0, 1] → linear gain. Squared, so the travel feels like a +/// volume knob (half way ≈ −12 dB) rather than a switch at the top end. +pub fn fader_gain(level: f32) -> f32 { + let l = level.clamp(0.0, 1.0); + l * l +} + +/// A value moving linearly to a target over a number of frames. Setting the +/// target it already heads for changes nothing, so an unrelated update can't +/// cut a running fade short. +#[derive(Debug, Clone, Copy)] +pub struct Fade { + current: f32, + target: f32, + step: f32, + left: u32, +} + +impl Fade { + pub fn new(v: f32) -> Self { + Self { current: v, target: v, step: 0.0, left: 0 } + } + + pub fn set_over(&mut self, target: f32, frames: u32) { + if target == self.target { + return; + } + self.target = target; + self.left = frames.max(1); + self.step = (target - self.current) / self.left as f32; + } + + /// This frame's value, then advance. + pub fn next(&mut self) -> f32 { + let v = self.current; + if self.left > 0 { + self.left -= 1; + self.current = if self.left == 0 { self.target } else { self.current + self.step }; + } + v + } + + /// Resting at `v` (no fade running). + pub fn is_at(&self, v: f32) -> bool { + self.left == 0 && self.current == v + } +} + +/// A gain applied while mixing: moves to a new value over a block (so fader +/// moves don't click) or longer for a switch (see `sfx`). +#[derive(Debug, Clone, Copy)] +pub struct Ramp(Fade); + +impl Ramp { + pub fn new(gain: f32) -> Self { + Self(Fade::new(gain)) + } + + pub fn set_over(&mut self, gain: f32, frames: u32) { + self.0.set_over(gain, frames); + } + + /// `out += input * gain`, ramping the gain across the block. + pub fn mix_into(&mut self, input: &[f32], out: &mut [f32]) { + if self.0.is_at(0.0) { + return; + } + for (i, o) in input.chunks_exact(2).zip(out.chunks_exact_mut(2)) { + let g = self.0.next(); + o[0] += i[0] * g; + o[1] += i[1] * g; + } + } + + /// Advance as if a block had been mixed (no input this block). + pub fn skip(&mut self, frames: usize) { + for _ in 0..frames { + self.0.next(); + } + } +} + +/// One second-order section (RBJ "Audio EQ Cookbook"), separate state per channel. +#[derive(Debug, Clone, Copy)] +pub struct Biquad { + b0: f32, + b1: f32, + b2: f32, + a1: f32, + a2: f32, + /// Direct form II transposed state, per channel. + z: [[f32; 2]; 2], +} + +impl Biquad { + pub fn new(band: &EqBand) -> Self { + let f0 = band.freq.clamp(10.0, RATE as f32 * 0.45); + let q = band.q.max(0.05); + let a = 10f32.powf(band.gain_db / 40.0); + let w0 = 2.0 * std::f32::consts::PI * f0 / RATE as f32; + let (sin, cos) = w0.sin_cos(); + let alpha = sin / (2.0 * q); + let (b0, b1, b2, a0, a1, a2) = match band.kind { + EqBandKind::Peak => ( + 1.0 + alpha * a, + -2.0 * cos, + 1.0 - alpha * a, + 1.0 + alpha / a, + -2.0 * cos, + 1.0 - alpha / a, + ), + EqBandKind::LowShelf => { + let s = 2.0 * a.sqrt() * alpha; + ( + a * ((a + 1.0) - (a - 1.0) * cos + s), + 2.0 * a * ((a - 1.0) - (a + 1.0) * cos), + a * ((a + 1.0) - (a - 1.0) * cos - s), + (a + 1.0) + (a - 1.0) * cos + s, + -2.0 * ((a - 1.0) + (a + 1.0) * cos), + (a + 1.0) + (a - 1.0) * cos - s, + ) + } + EqBandKind::HighShelf => { + let s = 2.0 * a.sqrt() * alpha; + ( + a * ((a + 1.0) + (a - 1.0) * cos + s), + -2.0 * a * ((a - 1.0) + (a + 1.0) * cos), + a * ((a + 1.0) + (a - 1.0) * cos - s), + (a + 1.0) - (a - 1.0) * cos + s, + 2.0 * ((a - 1.0) - (a + 1.0) * cos), + (a + 1.0) - (a - 1.0) * cos - s, + ) + } + EqBandKind::HighPass => ( + (1.0 + cos) / 2.0, + -(1.0 + cos), + (1.0 + cos) / 2.0, + 1.0 + alpha, + -2.0 * cos, + 1.0 - alpha, + ), + EqBandKind::LowPass => ( + (1.0 - cos) / 2.0, + 1.0 - cos, + (1.0 - cos) / 2.0, + 1.0 + alpha, + -2.0 * cos, + 1.0 - alpha, + ), + }; + Self { b0: b0 / a0, b1: b1 / a0, b2: b2 / a0, a1: a1 / a0, a2: a2 / a0, z: [[0.0; 2]; 2] } + } + + /// Gain in dB this section applies at `freq`. + pub fn response_db(&self, freq: f32) -> f32 { + let w = 2.0 * std::f32::consts::PI * freq / RATE as f32; + let (s1, c1) = w.sin_cos(); + let (s2, c2) = (2.0 * w).sin_cos(); + let num = (self.b0 + self.b1 * c1 + self.b2 * c2).powi(2) + (self.b1 * s1 + self.b2 * s2).powi(2); + let den = (1.0 + self.a1 * c1 + self.a2 * c2).powi(2) + (self.a1 * s1 + self.a2 * s2).powi(2); + 10.0 * (num / den).log10() + } + + /// Filter an interleaved stereo block in place. + pub fn process(&mut self, buf: &mut [f32]) { + for frame in buf.chunks_exact_mut(2) { + for (ch, x) in frame.iter_mut().enumerate() { + let z = &mut self.z[ch]; + let y = self.b0 * *x + z[0]; + z[0] = self.b1 * *x - self.a1 * y + z[1]; + z[1] = self.b2 * *x - self.a2 * y; + *x = y; + } + } + } +} + +/// A chain of bands. +#[derive(Debug, Clone, Default)] +pub struct Eq { + filters: Vec, +} + +impl Eq { + pub fn new(bands: &[EqBand]) -> Self { + Self { filters: bands.iter().map(Biquad::new).collect() } + } + + pub fn process(&mut self, buf: &mut [f32]) { + for f in &mut self.filters { + f.process(buf); + } + } + + /// Combined gain in dB at `freq` (what the editor draws). + pub fn response_db(&self, freq: f32) -> f32 { + self.filters.iter().map(|f| f.response_db(freq)).sum() + } +} + +/// Keep a bus inside [-1, 1] without the harsh edge of a hard clip: linear up +/// to 0.9, then a tanh knee into the remaining headroom. +pub fn soft_clip(buf: &mut [f32]) { + const KNEE: f32 = 0.9; + for s in buf { + let a = s.abs(); + if a > KNEE { + *s = s.signum() * (KNEE + (1.0 - KNEE) * ((a - KNEE) / (1.0 - KNEE)).tanh()); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sine(freq: f32, frames: usize) -> Vec { + (0..frames) + .flat_map(|i| { + let v = (2.0 * std::f32::consts::PI * freq * i as f32 / RATE as f32).sin() * 0.5; + [v, v] + }) + .collect() + } + + fn rms_db(buf: &[f32]) -> f32 { + let ms = buf.iter().map(|s| s * s).sum::() / buf.len() as f32; + 10.0 * ms.log10() + } + + /// Level change in dB the EQ applies to a steady sine (after settling). + fn eq_gain_db(bands: &[EqBand], freq: f32) -> f32 { + let input = sine(freq, RATE as usize / 2); + let mut out = input.clone(); + Eq::new(bands).process(&mut out); + let tail = input.len() / 2; + rms_db(&out[tail..]) - rms_db(&input[tail..]) + } + + fn band(kind: EqBandKind, freq: f32, gain_db: f32) -> EqBand { + EqBand { kind, freq, gain_db, q: std::f32::consts::FRAC_1_SQRT_2 } + } + + #[test] + fn peak_hits_its_gain_at_the_centre_and_leaves_far_bands_alone() { + let b = [EqBand { q: 1.0, ..band(EqBandKind::Peak, 1000.0, 6.0) }]; + assert!((eq_gain_db(&b, 1000.0) - 6.0).abs() < 0.3); + assert!(eq_gain_db(&b, 50.0).abs() < 0.3); + assert!(eq_gain_db(&b, 15000.0).abs() < 0.5); + } + + #[test] + fn drawn_response_matches_what_the_filter_does() { + let b = [EqBand { q: 1.0, ..band(EqBandKind::Peak, 1000.0, 6.0) }, band(EqBandKind::HighPass, 100.0, 0.0)]; + let eq = Eq::new(&b); + for f in [40.0, 300.0, 1000.0, 5000.0] { + assert!((eq.response_db(f) - eq_gain_db(&b, f)).abs() < 0.3, "{f} Hz"); + } + } + + #[test] + fn flat_eq_is_transparent() { + let b = [band(EqBandKind::Peak, 1000.0, 0.0), band(EqBandKind::LowShelf, 200.0, 0.0)]; + for f in [60.0, 1000.0, 8000.0] { + assert!(eq_gain_db(&b, f).abs() < 0.05, "{f} Hz"); + } + } + + #[test] + fn high_pass_removes_rumble_and_keeps_voice() { + let b = [band(EqBandKind::HighPass, 100.0, 0.0)]; + assert!(eq_gain_db(&b, 20.0) < -20.0); + assert!(eq_gain_db(&b, 1000.0).abs() < 0.2); + } + + #[test] + fn shelves_lift_their_side() { + let low = [band(EqBandKind::LowShelf, 200.0, 6.0)]; + assert!((eq_gain_db(&low, 40.0) - 6.0).abs() < 0.5); + assert!(eq_gain_db(&low, 5000.0).abs() < 0.3); + let high = [band(EqBandKind::HighShelf, 4000.0, -6.0)]; + assert!((eq_gain_db(&high, 16000.0) + 6.0).abs() < 0.6); + assert!(eq_gain_db(&high, 200.0).abs() < 0.3); + } + + #[test] + fn ramp_reaches_target_without_jumping() { + let mut r = Ramp::new(0.0); + r.set_over(1.0, BLOCK_FRAMES as u32); + let input = vec![1.0; BLOCK_SAMPLES]; + let mut out = vec![0.0; BLOCK_SAMPLES]; + r.mix_into(&input, &mut out); + assert_eq!(out[0], 0.0); + assert!(out.windows(2).all(|w| w[1] >= w[0])); + let mut out2 = vec![0.0; BLOCK_SAMPLES]; + r.mix_into(&input, &mut out2); + assert!(out2.iter().all(|&s| (s - 1.0).abs() < 1e-6)); + } + + #[test] + fn long_fade_spans_blocks_and_isnt_cut_short() { + let mut f = Fade::new(1.0); + f.set_over(0.0, 1000); + for _ in 0..500 { + f.next(); + } + f.set_over(0.0, 10); // same target again: keeps going at the old pace + let v = f.next(); + assert!((v - 0.5).abs() < 0.01, "{v}"); + for _ in 0..499 { + f.next(); + } + assert!(f.is_at(0.0)); + } + + #[test] + fn fader_taper() { + assert_eq!(fader_gain(0.0), 0.0); + assert_eq!(fader_gain(1.0), 1.0); + assert!((20.0 * fader_gain(0.5).log10() + 12.04).abs() < 0.1); + assert_eq!(fader_gain(2.0), 1.0); + } + + #[test] + fn soft_clip_bounds_and_keeps_quiet_signals() { + let mut b = vec![0.5, -0.5, 3.0, -3.0, 0.95]; + soft_clip(&mut b); + assert_eq!(&b[..2], &[0.5, -0.5]); + assert!(b[2] <= 1.0 && b[2] > 0.9 && b[3] >= -1.0); + assert!(b[4] > 0.9 && b[4] < 0.95); + } +} diff --git a/src/mixer/engine.rs b/src/mixer/engine.rs new file mode 100644 index 0000000..0e8f4c6 --- /dev/null +++ b/src/mixer/engine.rs @@ -0,0 +1,263 @@ +//! The mix itself: strips, levels, EQ and the two buses. No I/O — the platform +//! layer feeds one block per channel (and the voice) and plays the results. + +use std::collections::HashMap; + +use crate::model::{MixTarget, MixerConfig}; + +use super::dsp::{fader_gain, soft_clip, Ramp, BLOCK_FRAMES}; +use super::fx::FxChain; +use super::sfx::Fades; + +struct Strip { + level: f32, + muted: bool, + fx: FxChain, + /// Gain onto the monitor bus. + monitor: Ramp, + /// Gain onto the mic bus. + send: Ramp, + /// Share sent to the mic, `None` = not sent. Unused for the voice. + mic_send: Option, + /// Whether the strip is heard on the monitor. For channels this is set by + /// the platform layer: only rerouted programs may be played again, or the + /// user hears them twice. For the voice it is the "hear yourself" switch. + to_monitor: bool, +} + +impl Strip { + fn new() -> Self { + Self { + level: 1.0, + muted: false, + fx: FxChain::default(), + monitor: Ramp::new(0.0), + send: Ramp::new(0.0), + mic_send: None, + to_monitor: false, + } + } + + + /// Point the ramps at the current settings, arriving within `frames`. + fn update_gains(&mut self, is_voice: bool, frames: u32) { + let live = if self.muted { 0.0 } else { 1.0 }; + let level = fader_gain(self.level) * live; + self.monitor.set_over(if self.to_monitor { level } else { 0.0 }, frames); + self.send.set_over(if is_voice { level } else { self.mic_send.map_or(0.0, fader_gain) * live }, frames); + } +} + +pub struct Engine { + channels: HashMap, + voice: Strip, +} + +impl Default for Engine { + fn default() -> Self { + Self { channels: HashMap::new(), voice: Strip::new() } + } +} + +impl Engine { + /// Adopt a (new) layout. Strips that already exist keep their ramps and + /// filter state, so editing one channel doesn't click the others; what + /// was switched fades as long as `fades` says. + pub fn configure(&mut self, cfg: &MixerConfig, fades: &Fades) { + self.channels.retain(|id, _| cfg.channels.iter().any(|c| c.id == *id)); + for c in &cfg.channels { + let s = self.channels.entry(c.id).or_insert_with(Strip::new); + s.level = c.level; + s.muted = c.muted; + s.mic_send = c.mic_send; + let strip = MixTarget::Channel(c.id); + s.fx.update(&c.fx, &|id| fades.slot(strip, id)); + s.update_gains(false, fades.gain(strip)); + } + let v = &mut self.voice; + v.level = cfg.voice.level; + v.muted = cfg.voice.muted; + v.to_monitor = cfg.voice.to_monitor; + v.fx.update(&cfg.voice.fx, &|id| fades.slot(MixTarget::Voice, id)); + v.update_gains(true, fades.gain(MixTarget::Voice)); + } + + /// Whether a channel's programs were moved to the sink (see `Strip::to_monitor`). + pub fn set_monitored(&mut self, channel: u32, on: bool) { + if let Some(s) = self.channels.get_mut(&channel) { + s.to_monitor = on; + s.update_gains(false, BLOCK_FRAMES as u32); + } + } + + pub fn set_level(&mut self, target: MixTarget, value: f32) { + let value = value.clamp(0.0, 1.0); + match target { + MixTarget::Voice => { + self.voice.level = value; + self.voice.update_gains(true, BLOCK_FRAMES as u32); + } + MixTarget::Channel(id) => { + if let Some(s) = self.channels.get_mut(&id) { + s.level = value; + s.update_gains(false, BLOCK_FRAMES as u32); + } + } + MixTarget::Send(id) => { + if let Some(s) = self.channels.get_mut(&id) { + s.mic_send = Some(value); + s.update_gains(false, BLOCK_FRAMES as u32); + } + } + } + } + + /// Mute state of a strip; a send shares its channel's mute. + pub fn muted(&self, target: MixTarget) -> Option { + match target { + MixTarget::Voice => Some(self.voice.muted), + MixTarget::Channel(id) | MixTarget::Send(id) => self.channels.get(&id).map(|s| s.muted), + } + } + + /// Mute or unmute, fading over `frames`. + pub fn set_mute(&mut self, target: MixTarget, muted: bool, frames: u32) { + match target { + MixTarget::Voice => { + self.voice.muted = muted; + self.voice.update_gains(true, frames); + } + MixTarget::Channel(id) | MixTarget::Send(id) => { + if let Some(s) = self.channels.get_mut(&id) { + s.muted = muted; + s.update_gains(false, frames); + } + } + } + } + + /// Mix one block. `channels` holds each channel's summed input (consumed: + /// EQ runs in place); `voice` is the real mic. Both buses are overwritten. + pub fn process( + &mut self, + channels: &mut HashMap>, + voice: Option<&mut [f32]>, + monitor: &mut [f32], + mic: &mut [f32], + ) { + monitor.fill(0.0); + mic.fill(0.0); + for (id, strip) in &mut self.channels { + match channels.get_mut(id) { + Some(buf) => { + strip.fx.process(buf); + strip.monitor.mix_into(buf, monitor); + strip.send.mix_into(buf, mic); + } + None => { + // No input this block: still let the ramps arrive. + strip.monitor.skip(BLOCK_FRAMES); + strip.send.skip(BLOCK_FRAMES); + } + } + } + if let Some(v) = voice { + self.voice.fx.process(v); + self.voice.monitor.mix_into(v, monitor); + self.voice.send.mix_into(v, mic); + } + soft_clip(monitor); + soft_clip(mic); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::model::{Effects, MixChannel, VoiceStrip}; + use crate::mixer::dsp::BLOCK_SAMPLES; + + fn channel(id: u32, mic_send: Option) -> MixChannel { + MixChannel { + id, + name: format!("c{id}"), + programs: vec![], + level: 1.0, + muted: false, + fx: Effects::default(), + reroute: true, + mic_send, + collapsed: false, + } + } + + fn engine(channels: Vec, voice: VoiceStrip) -> Engine { + let mut e = Engine::default(); + e.configure(&MixerConfig { enabled: true, channels, voice, ..Default::default() }, &Fades::default()); + e + } + + /// Run two blocks (the first ramps) and return the second's bus peaks. + fn run(e: &mut Engine, inputs: &[u32], voice: bool) -> (f32, f32) { + let mut out = (0.0, 0.0); + for _ in 0..2 { + let mut ch: HashMap> = + inputs.iter().map(|id| (*id, vec![0.25; BLOCK_SAMPLES])).collect(); + let mut v = vec![0.25; BLOCK_SAMPLES]; + let (mut m, mut k) = (vec![0.0; BLOCK_SAMPLES], vec![0.0; BLOCK_SAMPLES]); + e.process(&mut ch, voice.then_some(&mut v[..]), &mut m, &mut k); + let peak = |b: &[f32]| b.iter().fold(0f32, |a, s| a.max(s.abs())); + out = (peak(&m), peak(&k)); + } + out + } + + #[test] + fn channel_reaches_monitor_only_when_rerouted() { + let mut e = engine(vec![channel(1, None)], VoiceStrip::default()); + assert_eq!(run(&mut e, &[1], false), (0.0, 0.0)); + e.set_monitored(1, true); + assert_eq!(run(&mut e, &[1], false), (0.25, 0.0)); + } + + #[test] + fn send_is_independent_of_the_fader_and_mute_silences_both() { + let mut e = engine(vec![channel(1, Some(1.0))], VoiceStrip::default()); + e.set_monitored(1, true); + e.set_level(MixTarget::Channel(1), 0.0); + assert_eq!(run(&mut e, &[1], false), (0.0, 0.25)); + e.set_mute(MixTarget::Send(1), true, BLOCK_FRAMES as u32); + assert_eq!(run(&mut e, &[1], false), (0.0, 0.0)); + assert_eq!(e.muted(MixTarget::Channel(1)), Some(true)); + } + + #[test] + fn voice_goes_to_the_mic_and_follows_its_fader() { + let mut e = engine(vec![], VoiceStrip::default()); + assert_eq!(run(&mut e, &[], true), (0.0, 0.25)); + e.set_level(MixTarget::Voice, 0.5); + let (_, mic) = run(&mut e, &[], true); + assert!((mic - 0.0625).abs() < 1e-6); + e.set_mute(MixTarget::Voice, true, BLOCK_FRAMES as u32); + assert_eq!(run(&mut e, &[], true), (0.0, 0.0)); + } + + #[test] + fn hear_yourself_puts_the_voice_on_the_monitor() { + let mut e = engine(vec![], VoiceStrip { to_monitor: true, ..VoiceStrip::default() }); + assert_eq!(run(&mut e, &[], true), (0.25, 0.25)); + } + + #[test] + fn reconfigure_keeps_live_channels_and_drops_removed_ones() { + let mut e = engine(vec![channel(1, Some(1.0)), channel(2, Some(1.0))], VoiceStrip::default()); + e.configure(&MixerConfig { + enabled: true, + channels: vec![channel(2, Some(1.0))], + voice: VoiceStrip::default(), + ..Default::default() + }, &Fades::default()); + assert_eq!(e.muted(MixTarget::Channel(1)), None); + assert_eq!(run(&mut e, &[1, 2], false), (0.0, 0.25)); + } +} diff --git a/src/mixer/fx.rs b/src/mixer/fx.rs new file mode 100644 index 0000000..2ea14a6 --- /dev/null +++ b/src/mixer/fx.rs @@ -0,0 +1,400 @@ +//! Effects and the per-strip chain that runs them in the user's order. +//! Stereo-linked (both channels get the same gain, so +//! the image doesn't wander), sample by sample on interleaved `f32` blocks. + +use crate::model::{Compressor, Effects, EqBand, Fx, Gate, Limiter, Normalizer}; + +use super::dsp::{Eq, Fade, RATE}; + +fn db_to_lin(db: f32) -> f32 { + 10f32.powf(db / 20.0) +} + +fn lin_to_db(x: f32) -> f32 { + 20.0 * x.max(1e-9).log10() +} + +/// One-pole smoothing coefficient for a time constant in milliseconds. +fn coeff(ms: f32) -> f32 { + (-1.0 / (ms.max(0.01) * 0.001 * RATE as f32)).exp() +} + +/// Mutes the signal while it stays below a threshold — room noise between words. +struct GateFx { + p: Gate, + env: f32, + gain: f32, + hold: u32, +} + +impl GateFx { + /// How long the gate stays open after the signal drops (keeps word ends). + const HOLD_MS: f32 = 60.0; + /// Attenuation while closed (-80 dB: silent, but no hard zero). + const FLOOR: f32 = 1e-4; + + fn new(p: Gate) -> Self { + Self { p, env: 0.0, gain: Self::FLOOR, hold: 0 } + } + + fn process(&mut self, buf: &mut [f32]) { + let env_rel = coeff(20.0); + let open_c = coeff(1.0); + let close_c = coeff(self.p.release_ms); + let threshold = db_to_lin(self.p.threshold_db); + let hold = (Self::HOLD_MS * 0.001 * RATE as f32) as u32; + for f in buf.chunks_exact_mut(2) { + let level = f[0].abs().max(f[1].abs()); + self.env = if level > self.env { level } else { self.env * env_rel + level * (1.0 - env_rel) }; + if self.env > threshold { + self.hold = hold; + } else { + self.hold = self.hold.saturating_sub(1); + } + let target = if self.hold > 0 { 1.0 } else { Self::FLOOR }; + let c = if target > self.gain { open_c } else { close_c }; + self.gain = target + (self.gain - target) * c; + f[0] *= self.gain; + f[1] *= self.gain; + } + } +} + +/// Turns loud parts down above a threshold, by `ratio`. +struct CompFx { + p: Compressor, + env_db: f32, +} + +impl CompFx { + fn new(p: Compressor) -> Self { + Self { p, env_db: -120.0 } + } + + fn process(&mut self, buf: &mut [f32]) { + let att = coeff(self.p.attack_ms); + let rel = coeff(self.p.release_ms); + let slope = 1.0 - 1.0 / self.p.ratio.max(1.0); + for f in buf.chunks_exact_mut(2) { + let level = lin_to_db(f[0].abs().max(f[1].abs())); + let c = if level > self.env_db { att } else { rel }; + self.env_db = level + (self.env_db - level) * c; + let over = self.env_db - self.p.threshold_db; + let reduction = if over > 0.0 { over * slope } else { 0.0 }; + let g = db_to_lin(self.p.makeup_db - reduction); + f[0] *= g; + f[1] *= g; + } + } +} + +/// Slowly steers the average loudness to a target — evens out a quiet video +/// next to a loud song. Never lifts silence. +struct NormFx { + p: Normalizer, + mean_sq: f32, + gain_db: f32, +} + +impl NormFx { + /// Loudness is measured over ~3 s and the gain moves with ~1 s. + const MEASURE_MS: f32 = 3000.0; + const MOVE_MS: f32 = 1000.0; + /// Below this the input counts as silence and the gain is left alone. + const SILENCE_DB: f32 = -50.0; + + fn new(p: Normalizer) -> Self { + Self { p, mean_sq: 0.0, gain_db: 0.0 } + } + + fn process(&mut self, buf: &mut [f32]) { + let m = coeff(Self::MEASURE_MS); + let g = coeff(Self::MOVE_MS); + let max = self.p.max_gain_db.max(0.0); + for f in buf.chunks_exact_mut(2) { + let sq = (f[0] * f[0] + f[1] * f[1]) * 0.5; + self.mean_sq = sq + (self.mean_sq - sq) * m; + let rms_db = 10.0 * self.mean_sq.max(1e-12).log10(); + if rms_db > Self::SILENCE_DB { + let want = (self.p.target_db - rms_db).clamp(-max, max); + self.gain_db = want + (self.gain_db - want) * g; + } + let lin = db_to_lin(self.gain_db); + f[0] *= lin; + f[1] *= lin; + } + } +} + +/// Keeps peaks under a ceiling: clamps instantly, recovers over 50 ms. +struct LimitFx { + p: Limiter, + gain: f32, +} + +impl LimitFx { + fn new(p: Limiter) -> Self { + Self { p, gain: 1.0 } + } + + fn process(&mut self, buf: &mut [f32]) { + let ceiling = db_to_lin(self.p.ceiling_db.min(0.0)); + let rel = coeff(50.0); + for f in buf.chunks_exact_mut(2) { + let peak = f[0].abs().max(f[1].abs()); + self.gain = 1.0 + (self.gain - 1.0) * rel; + if peak * self.gain > ceiling { + self.gain = ceiling / peak; + } + f[0] = (f[0] * self.gain).clamp(-ceiling, ceiling); + f[1] = (f[1] * self.gain).clamp(-ceiling, ceiling); + } + } +} + +/// A running effect. +enum Proc { + Gate(GateFx), + Eq(Vec, Eq), + Comp(CompFx), + Norm(NormFx), + Limit(LimitFx), +} + +impl Proc { + fn new(fx: &Fx) -> Self { + match fx { + Fx::Gate(p) => Proc::Gate(GateFx::new(*p)), + Fx::Eq { bands } => Proc::Eq(bands.clone(), Eq::new(bands)), + Fx::Compressor(p) => Proc::Comp(CompFx::new(*p)), + Fx::Normalizer(p) => Proc::Norm(NormFx::new(*p)), + Fx::Limiter(p) => Proc::Limit(LimitFx::new(*p)), + } + } + + /// Take new settings, keeping the running state. `false` if `fx` is a + /// different kind of effect (the slot was replaced). + fn retune(&mut self, fx: &Fx) -> bool { + match (self, fx) { + (Proc::Gate(x), Fx::Gate(p)) => x.p = *p, + (Proc::Comp(x), Fx::Compressor(p)) => x.p = *p, + (Proc::Norm(x), Fx::Normalizer(p)) => x.p = *p, + (Proc::Limit(x), Fx::Limiter(p)) => x.p = *p, + (Proc::Eq(old, eq), Fx::Eq { bands }) => { + if old != bands { + *eq = Eq::new(bands); + *old = bands.clone(); + } + } + _ => return false, + } + true + } + + fn process(&mut self, buf: &mut [f32]) { + match self { + Proc::Gate(x) => x.process(buf), + Proc::Eq(_, x) => x.process(buf), + Proc::Comp(x) => x.process(buf), + Proc::Norm(x) => x.process(buf), + Proc::Limit(x) => x.process(buf), + } + } +} + +struct Slot { + id: u32, + proc: Proc, + /// 0 = bypassed, 1 = fully applied; fades when the slot is switched. + wet: Fade, +} + +/// One strip's effects, in chain order. Each slot keeps its running state +/// (envelopes, gains) across edits and reorders, so nothing clicks; switching +/// one crossfades between the dry and the processed signal. +#[derive(Default)] +pub struct FxChain { + slots: Vec, + dry: Vec, +} + +impl FxChain { + /// Adopt `fx`. A slot that changes between on and off fades over + /// `fade(slot id)` frames. + pub fn update(&mut self, fx: &Effects, fade: &dyn Fn(u32) -> u32) { + let mut old = std::mem::take(&mut self.slots); + for s in &fx.chain { + let on = if s.on && fx.on { 1.0 } else { 0.0 }; + let kept = old + .iter() + .position(|o| o.id == s.id) + .map(|i| old.swap_remove(i)) + .and_then(|mut o| o.proc.retune(&s.fx).then_some(o)); + let mut slot = kept.unwrap_or_else(|| Slot { id: s.id, proc: Proc::new(&s.fx), wet: Fade::new(0.0) }); + slot.wet.set_over(on, fade(s.id)); + self.slots.push(slot); + } + } + + pub fn process(&mut self, buf: &mut [f32]) { + for slot in &mut self.slots { + if slot.wet.is_at(0.0) { + continue; + } + if slot.wet.is_at(1.0) { + slot.proc.process(buf); + continue; + } + self.dry.clear(); + self.dry.extend_from_slice(buf); + slot.proc.process(buf); + for (out, dry) in buf.chunks_exact_mut(2).zip(self.dry.chunks_exact(2)) { + let w = slot.wet.next(); + out[0] = dry[0] + (out[0] - dry[0]) * w; + out[1] = dry[1] + (out[1] - dry[1]) * w; + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::model::FxSlot; + + fn sine(amp: f32, secs: f32) -> Vec { + (0..(RATE as f32 * secs) as usize) + .flat_map(|i| { + let v = (2.0 * std::f32::consts::PI * 440.0 * i as f32 / RATE as f32).sin() * amp; + [v, v] + }) + .collect() + } + + fn peak_db(b: &[f32]) -> f32 { + lin_to_db(b.iter().fold(0f32, |a, s| a.max(s.abs()))) + } + + fn rms_db(b: &[f32]) -> f32 { + 10.0 * (b.iter().map(|s| s * s).sum::() / b.len() as f32).log10() + } + + fn effects(fx: Vec) -> Effects { + Effects { + on: true, + chain: fx.into_iter().enumerate().map(|(i, fx)| FxSlot { id: i as u32 + 1, on: true, fx }).collect(), + } + } + + /// Switches settle within a block; "instant" for the sake of the tests. + const QUICK: &dyn Fn(u32) -> u32 = &|_| 1; + + fn chain(fx: Vec) -> FxChain { + let mut c = FxChain::default(); + c.update(&effects(fx), QUICK); + c + } + + /// Run `secs` of a sine through the chain; return the last quarter. + fn run(c: &mut FxChain, amp: f32, secs: f32) -> Vec { + let mut b = sine(amp, secs); + for block in b.chunks_mut(960) { + c.process(block); + } + b[b.len() * 3 / 4..].to_vec() + } + + #[test] + fn gate_silences_noise_and_passes_speech() { + let mut c = chain(vec![Fx::Gate(Gate { threshold_db: -40.0, release_ms: 100.0 })]); + assert!(peak_db(&run(&mut c, 0.003, 1.0)) < -100.0, "-50 dB hum is gated"); + assert!((peak_db(&run(&mut c, 0.3, 1.0)) - lin_to_db(0.3)).abs() < 0.1, "speech level passes"); + } + + #[test] + fn compressor_reduces_by_its_ratio_above_threshold() { + let comp = Compressor { threshold_db: -20.0, ratio: 4.0, attack_ms: 5.0, release_ms: 200.0, makeup_db: 0.0 }; + // -6 dBFS is 14 dB over: 14 * (1 - 1/4) = 10.5 dB less. + let out = peak_db(&run(&mut chain(vec![Fx::Compressor(comp)]), 0.5, 1.0)); + assert!((out - (-6.02 - 10.5)).abs() < 1.5, "{out}"); + // Below the threshold nothing changes. + assert!((peak_db(&run(&mut chain(vec![Fx::Compressor(comp)]), 0.05, 1.0)) - lin_to_db(0.05)).abs() < 0.1); + } + + #[test] + fn normalizer_brings_quiet_audio_to_target_and_ignores_silence() { + let norm = Normalizer { target_db: -18.0, max_gain_db: 15.0 }; + // Sine at 0.1 peak = -23 dB RMS; should end near -18. + let out = rms_db(&run(&mut chain(vec![Fx::Normalizer(norm)]), 0.1, 12.0)); + assert!((out + 18.0).abs() < 1.0, "{out}"); + assert!(peak_db(&run(&mut chain(vec![Fx::Normalizer(norm)]), 0.0005, 5.0)) < -60.0, "silence is not lifted"); + } + + #[test] + fn limiter_holds_the_ceiling() { + let out = run(&mut chain(vec![Fx::Limiter(Limiter { ceiling_db: -3.0 })]), 0.95, 1.0); + assert!(peak_db(&out) <= -3.0 + 0.01); + assert!(peak_db(&out) > -4.0, "doesn't squash more than needed"); + } + + #[test] + fn stacked_effects_run_in_chain_order() { + // Limit to -12, then boost 10 dB with a compressor's makeup → -2 dB. + let boost = Fx::Compressor(Compressor { threshold_db: 0.0, ratio: 1.0, makeup_db: 10.0, ..Compressor::default() }); + let limit = Fx::Limiter(Limiter { ceiling_db: -12.0 }); + let out = peak_db(&run(&mut chain(vec![limit.clone(), boost.clone()]), 0.9, 0.5)); + assert!((out + 2.0).abs() < 0.3, "{out}"); + // The other way round the limiter has the last word. + let out = peak_db(&run(&mut chain(vec![boost, limit]), 0.9, 0.5)); + assert!(out <= -12.0 + 0.01, "{out}"); + } + + #[test] + fn empty_or_switched_off_chain_is_transparent() { + let input = sine(0.4, 0.1); + let mut slot_off = effects(vec![Fx::Limiter(Limiter { ceiling_db: -20.0 })]); + slot_off.chain[0].on = false; + let mut master_off = effects(vec![Fx::Limiter(Limiter { ceiling_db: -20.0 })]); + master_off.on = false; + for e in [Effects::default(), slot_off, master_off] { + let mut c = FxChain::default(); + c.update(&e, QUICK); + let mut out = input.clone(); + c.process(&mut out); + assert_eq!(input, out); + } + } + + #[test] + fn edits_and_reorders_keep_running_state() { + let comp = Compressor::default(); + let mut fx = effects(vec![Fx::Limiter(Limiter::default()), Fx::Compressor(comp)]); + let mut c = FxChain::default(); + c.update(&fx, QUICK); + run(&mut c, 0.9, 0.5); + let env = |c: &FxChain| c.slots.iter().find_map(|s| match &s.proc { Proc::Comp(x) => Some(x.env_db), _ => None }); + let before = env(&c); + fx.chain.swap(0, 1); + fx.chain[0].fx = Fx::Compressor(Compressor { ratio: 6.0, ..comp }); + c.update(&fx, QUICK); + assert_eq!(env(&c), before, "a knob turn or move keeps the envelope"); + assert!(matches!(c.slots[0].proc, Proc::Comp(_)), "new order applies"); + } + + #[test] + fn switching_an_effect_crossfades_over_the_given_length() { + let mut fx = effects(vec![Fx::Limiter(Limiter { ceiling_db: -20.0 })]); + let mut c = FxChain::default(); + c.update(&fx, QUICK); + run(&mut c, 0.5, 0.2); + fx.chain[0].on = false; + c.update(&fx, &|_| 4800); // 100 ms + let mut b = sine(0.5, 0.2); + c.process(&mut b); + // Halfway through the fade the level sits between limited and dry. + let mid = peak_db(&b[4000..5600]); + assert!(mid > -19.0 && mid < -6.5, "{mid}"); + // After it, dry. + assert!((peak_db(&b[12000..]) - lin_to_db(0.5)).abs() < 0.1); + } +} diff --git a/src/mixer/mod.rs b/src/mixer/mod.rs new file mode 100644 index 0000000..c0b6720 --- /dev/null +++ b/src/mixer/mod.rs @@ -0,0 +1,153 @@ +//! Software mixer: programs grouped into channels, mixed with faders and EQ +//! onto the user's headset ("monitor"), plus a "Slidr Mic" carrying the real +//! microphone and whatever channels are sent to it. See `docs/mixer-plan.md`. +//! +//! Runs on its own thread, independent of the board; the board (via the +//! actuator) and the UI only send it commands. The layout comes from the +//! active profile, the devices from the global settings. + +pub mod dsp; +mod engine; +mod fx; +mod sfx; +mod ring; +#[cfg(target_os = "windows")] +mod route; +#[cfg(target_os = "windows")] +mod windows_io; + +use std::collections::HashMap; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::Duration; + +use crossbeam_channel::Sender; +use once_cell::sync::{Lazy, OnceCell}; +use parking_lot::Mutex; + +use crate::model::{MixTarget, MixerConfig, MixerDevices}; + +pub use sfx::{DEFAULT_SET as DEFAULT_SOUND_SET, SETS as SOUND_SETS}; + +/// The audio adapter in an endpoint's friendly name — the part in the last +/// parentheses: "CABLE Output (VB-Audio Virtual Cable)" → "VB-Audio Virtual Cable". +/// Both ends of a virtual cable share it. +pub fn adapter(name: &str) -> Option<&str> { + let open = name.rfind('(')?; + name[open + 1..].strip_suffix(')').map(str::trim) +} + +/// Journal of programs rerouted by the mixer, in the config folder. +const JOURNAL_FILE: &str = "mixer-routing.json"; + +pub enum Cmd { + Configure(MixerConfig, MixerDevices), + /// The next `Configure` is Slidr starting with the mixer on: no power-on sound. + QuietStart, + SetLevel(MixTarget, f32), + SetMute(MixTarget, bool), + ToggleMute(MixTarget), + /// Switch sounds on/off, their volume (0..1) and the set they come from. + SoundPrefs(bool, f32, String), + /// Restore rerouted programs and stop; acknowledged on the sender. + Shutdown(Sender<()>), +} + +static TX: OnceCell>> = OnceCell::new(); + +/// A layout was handed over since launch. +static LAUNCHED: AtomicBool = AtomicBool::new(false); + +/// Levels the board set, and the layout they apply over — for the Mixer page, +/// which shows them. Kept by the same rule as the mixer's own: a board move +/// stays until the page changes that same level. +static BOARD: Lazy, MixerConfig)>> = Lazy::new(Default::default); + +/// Where a strip's fader really is: the board's last move, else the layout's. +pub fn shown_level(cfg: &MixerConfig, t: MixTarget) -> Option { + BOARD.lock().0.get(&t).copied().or_else(|| cfg.level(t)) +} + +/// The mixer thread's inbox, started on first use. `None` where the mixer +/// isn't implemented yet (Linux, see the plan). +fn sender() -> Option<&'static Sender> { + TX.get_or_init(spawn).as_ref() +} + +#[cfg(target_os = "windows")] +fn spawn() -> Option> { + let (tx, rx) = crossbeam_channel::unbounded(); + std::thread::Builder::new() + .name("mixer".into()) + .spawn(move || windows_io::run(rx)) + .map_err(|e| log::error!("mixer: could not start thread: {e}")) + .ok()?; + Some(tx) +} + +#[cfg(not(target_os = "windows"))] +fn spawn() -> Option> { + None +} + +fn send(cmd: Cmd) { + if let Some(tx) = sender() { + let _ = tx.send(cmd); + } +} + +/// Adopt a profile's layout and the mixer devices. +pub fn configure(mixer: MixerConfig, devices: MixerDevices) { + { + let mut board = BOARD.lock(); + let (levels, last) = &mut *board; + levels.retain(|t, _| last.level(*t) == mixer.level(*t)); + *last = mixer.clone(); + } + // The first layout after launch: the mixer comes up with the app, which + // isn't the user switching it on. + let launching = !LAUNCHED.swap(true, Ordering::Relaxed); + // Nothing to start for a profile that never used the mixer. + if !mixer.enabled && TX.get().is_none() && !journal_pending() { + return; + } + if launching && mixer.enabled { + send(Cmd::QuietStart); + } + send(Cmd::Configure(mixer, devices)); +} + +pub fn set_level(target: MixTarget, value: f32) { + BOARD.lock().0.insert(target, value); + send(Cmd::SetLevel(target, value)); +} + +pub fn set_mute(target: MixTarget, muted: bool) { + send(Cmd::SetMute(target, muted)); +} + +pub fn toggle_mute(target: MixTarget) { + send(Cmd::ToggleMute(target)); +} + +/// Switch sounds on/off, their volume and set. Only reaches a mixer that is +/// already running (one that isn't has nothing to click). +pub fn sound_prefs(on: bool, volume: f32, set: &str) { + if let Some(Some(tx)) = TX.get() { + let _ = tx.send(Cmd::SoundPrefs(on, volume, set.to_string())); + } +} + +/// Hand rerouted programs back to their normal output before the app exits. +pub fn shutdown() { + let Some(Some(tx)) = TX.get() else { return }; + let (ack_tx, ack_rx) = crossbeam_channel::bounded(1); + if tx.send(Cmd::Shutdown(ack_tx)).is_ok() { + let _ = ack_rx.recv_timeout(Duration::from_secs(3)); + } +} + +/// Programs rerouted by an earlier run that still need restoring — the mixer +/// thread must run for that even when no profile uses the mixer. +fn journal_pending() -> bool { + crate::storage::config_root().join(JOURNAL_FILE).exists() +} diff --git a/src/mixer/ring.rs b/src/mixer/ring.rs new file mode 100644 index 0000000..54c6f51 --- /dev/null +++ b/src/mixer/ring.rs @@ -0,0 +1,110 @@ +//! Hand-off between a capture thread and the mixer thread. +//! +//! The two sides run on different device clocks, so the fill level drifts. The +//! reader waits for a small cushion before it starts consuming, pads with +//! silence when the writer falls behind, and trims the backlog when it runs +//! ahead — a dropped or doubled sliver now and then instead of growing latency. + +use std::collections::VecDeque; + +use parking_lot::Mutex; + +use super::dsp::BLOCK_SAMPLES; + +/// Cushion the reader waits for before (re)starting: 20 ms. +const PRIME: usize = BLOCK_SAMPLES * 2; +/// Backlog above which the oldest audio is dropped back down to `PRIME`: 60 ms. +const HIGH: usize = BLOCK_SAMPLES * 6; +/// Hard cap while nobody reads. +const MAX: usize = BLOCK_SAMPLES * 20; + +#[derive(Default)] +pub struct Ring { + inner: Mutex, +} + +#[derive(Default)] +struct State { + q: VecDeque, + primed: bool, +} + +impl Ring { + pub fn push(&self, samples: &[f32]) { + let mut s = self.inner.lock(); + s.q.extend(samples); + let len = s.q.len(); + if len > MAX { + s.q.drain(..len - MAX); + } + } + + /// Fill `out` with the next block. Returns `false` (and writes silence for + /// the missing part) when there was not enough audio. + pub fn pop(&self, out: &mut [f32]) -> bool { + let mut s = self.inner.lock(); + if !s.primed { + if s.q.len() < PRIME { + out.fill(0.0); + return false; + } + s.primed = true; + } + let len = s.q.len(); + if len > HIGH { + s.q.drain(..len - PRIME); + } + let n = s.q.len().min(out.len()); + for (o, v) in out.iter_mut().zip(s.q.drain(..n)) { + *o = v; + } + out[n..].fill(0.0); + if n < out.len() { + s.primed = false; + return false; + } + true + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn waits_for_cushion_then_delivers_in_order() { + let r = Ring::default(); + let mut out = vec![9.0; BLOCK_SAMPLES]; + r.push(&vec![1.0; BLOCK_SAMPLES]); + assert!(!r.pop(&mut out)); + assert!(out.iter().all(|&s| s == 0.0)); + r.push(&vec![2.0; BLOCK_SAMPLES]); + assert!(r.pop(&mut out)); + assert!(out.iter().all(|&s| s == 1.0)); + assert!(r.pop(&mut out)); + assert!(out.iter().all(|&s| s == 2.0)); + } + + #[test] + fn underrun_pads_and_reprimes() { + let r = Ring::default(); + r.push(&vec![1.0; PRIME + BLOCK_SAMPLES / 2]); + let mut out = vec![0.0; BLOCK_SAMPLES]; + assert!(r.pop(&mut out)); + assert!(r.pop(&mut out)); + assert!(!r.pop(&mut out)); + assert_eq!(out[BLOCK_SAMPLES / 2], 0.0); + r.push(&vec![1.0; BLOCK_SAMPLES]); + assert!(!r.pop(&mut out), "must wait for the cushion again"); + } + + #[test] + fn backlog_is_trimmed() { + let r = Ring::default(); + r.push(&vec![0.0; HIGH * 2]); + r.push(&vec![5.0; PRIME]); + let mut out = vec![0.0; BLOCK_SAMPLES]; + assert!(r.pop(&mut out)); + assert!(out.iter().all(|&s| s == 5.0), "oldest audio dropped first"); + } +} diff --git a/src/mixer/route.rs b/src/mixer/route.rs new file mode 100644 index 0000000..ec0487e --- /dev/null +++ b/src/mixer/route.rs @@ -0,0 +1,212 @@ +//! Per-program output routing on Windows — what Settings > "App volume and +//! device preferences" changes. +//! +//! Windows persists such an override per executable until it is cleared, so a +//! program Slidr moved to the sink would stay silent if Slidr died. Every +//! override therefore goes into a journal first; on the next run the stale +//! entries are cleared as soon as their program shows up again. + +// The COM method names below are Windows'. +#![allow(non_snake_case)] + +use std::collections::HashMap; +use std::ffi::c_void; + +use serde::{Deserialize, Serialize}; +use windows::core::{interface, IUnknown, IUnknown_Vtbl, HRESULT, HSTRING}; +use windows::Win32::Media::Audio::{eConsole, eMultimedia, eRender, EDataFlow, ERole, IMMDevice}; +use windows::Win32::System::Com::CoTaskMemFree; +use windows::Win32::System::WinRT::RoGetActivationFactory; + +/// KSCATEGORY_AUDIO render interface suffix of an MMDevice interface path. +const RENDER_INTERFACE: &str = "{e6327cad-dcec-4949-ae8a-991e976a79d2}"; + +// Undocumented activation factory behind `Windows.Media.Internal.AudioPolicyConfig` +// (Win10 21H2+ IID; EarTrumpet uses the same layout). Only the persisted-endpoint +// method is called; the rest are vtable placeholders, starting with +// IInspectable's three (GetIids, GetRuntimeClassName, GetTrustLevel). +#[interface("ab3d4648-e242-459f-b02f-541c70306324")] +unsafe trait IAudioPolicyConfigFactory: IUnknown { + fn p00(&self) -> HRESULT; + fn p01(&self) -> HRESULT; + fn p02(&self) -> HRESULT; + fn p03(&self) -> HRESULT; + fn p04(&self) -> HRESULT; + fn p05(&self) -> HRESULT; + fn p06(&self) -> HRESULT; + fn p07(&self) -> HRESULT; + fn p08(&self) -> HRESULT; + fn p09(&self) -> HRESULT; + fn p10(&self) -> HRESULT; + fn p11(&self) -> HRESULT; + fn p12(&self) -> HRESULT; + fn p13(&self) -> HRESULT; + fn p14(&self) -> HRESULT; + fn p15(&self) -> HRESULT; + fn p16(&self) -> HRESULT; + fn p17(&self) -> HRESULT; + fn p18(&self) -> HRESULT; + fn p19(&self) -> HRESULT; + fn p20(&self) -> HRESULT; + fn p21(&self) -> HRESULT; + fn SetPersistedDefaultAudioEndpoint( + &self, + pid: u32, + flow: EDataFlow, + role: ERole, + device: *mut c_void, + ) -> HRESULT; +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct Entry { + pid: u32, + /// Image path, to find the same program again under a new pid. + exe: String, +} + +pub struct Router { + factory: IAudioPolicyConfigFactory, + /// Overrides Slidr owns, by pid; mirrored in the journal file. + journal: Vec, +} + +impl Router { + pub fn new() -> Option { + let factory = unsafe { + RoGetActivationFactory::(&HSTRING::from( + "Windows.Media.Internal.AudioPolicyConfig", + )) + }; + match factory { + Ok(factory) => Some(Self { factory, journal: load_journal() }), + Err(e) => { + log::warn!("mixer: per-program routing unavailable ({e}); nothing will be rerouted"); + None + } + } + } + + pub fn is_routed(&self, pid: u32) -> bool { + self.journal.iter().any(|e| e.pid == pid) + } + + /// Move `pid`'s output to `dev`. Fails while the program has no audio + /// session yet — the caller retries on its next scan. + pub fn route(&mut self, pid: u32, dev: &IMMDevice) -> windows::core::Result<()> { + let path = HSTRING::from(interface_path(dev)?); + // Journal first: if we die between the two, the next run clears an + // override that may not exist, which is harmless. + if !self.is_routed(pid) { + let exe = crate::app_groups::process_path(pid).unwrap_or_default(); + self.journal.push(Entry { pid, exe }); + save_journal(&self.journal); + } + self.set(pid, Some(&path)) + } + + /// Clear overrides left over from an earlier run (crash, kill) for programs + /// that are playing right now and that Slidr is not routing on purpose. + /// `sessions` = (pid, image path) of everything with an audio session. + pub fn recover(&mut self, sessions: &HashMap, keep: &dyn Fn(u32) -> bool) { + let mut changed = false; + for entry in std::mem::take(&mut self.journal) { + let live = sessions.contains_key(&entry.pid); + if live && keep(entry.pid) { + self.journal.push(entry); + continue; + } + // Same pid still playing, or the same program under a new pid — + // the override belongs to the executable, so any of its pids works. + let pid = if live { + Some(entry.pid) + } else { + sessions + .iter() + .find(|(_, exe)| !entry.exe.is_empty() && exe.eq_ignore_ascii_case(&entry.exe)) + .map(|(pid, _)| *pid) + }; + match pid { + // Not playing right now; try again when it is. + None => self.journal.push(entry), + // Routed on purpose under its new pid, which has its own entry. + Some(p) if keep(p) => changed = true, + Some(p) => { + log::info!("mixer: restoring output of {} (pid {p})", entry.exe); + if self.set(p, None).is_ok() { + changed = true; + } else { + self.journal.push(entry); + } + } + } + } + if changed { + save_journal(&self.journal); + } + } + + /// Restore everything Slidr routed whose program is still running. + pub fn clear_all(&mut self) { + for pid in self.journal.iter().map(|e| e.pid).collect::>() { + if self.set(pid, None).is_ok() { + self.journal.retain(|e| e.pid != pid); + } + } + save_journal(&self.journal); + } + + pub fn pending(&self) -> bool { + !self.journal.is_empty() + } + + fn set(&self, pid: u32, path: Option<&HSTRING>) -> windows::core::Result<()> { + for role in [eConsole, eMultimedia] { + unsafe { + let raw: *mut c_void = match path { + Some(h) => std::mem::transmute_copy(h), + None => std::ptr::null_mut(), + }; + self.factory.SetPersistedDefaultAudioEndpoint(pid, eRender, role, raw).ok()?; + } + } + Ok(()) + } +} + +fn interface_path(dev: &IMMDevice) -> windows::core::Result { + unsafe { + let id = dev.GetId()?; + let s = id.to_string(); + CoTaskMemFree(Some(id.0 as _)); + Ok(format!(r"\\?\SWD#MMDEVAPI#{}#{RENDER_INTERFACE}", s?)) + } +} + +fn journal_path() -> std::path::PathBuf { + crate::storage::config_root().join(super::JOURNAL_FILE) +} + +fn load_journal() -> Vec { + std::fs::read(journal_path()) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()) + .unwrap_or_default() +} + +fn save_journal(entries: &[Entry]) { + let path = journal_path(); + let result = if entries.is_empty() { + match std::fs::remove_file(&path) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e), + _ => Ok(()), + } + } else { + serde_json::to_vec_pretty(entries) + .map_err(std::io::Error::other) + .and_then(|json| std::fs::write(&path, json)) + }; + if let Err(e) = result { + log::warn!("mixer: could not write {}: {e}", path.display()); + } +} diff --git a/src/mixer/sfx.rs b/src/mixer/sfx.rs new file mode 100644 index 0000000..65aa33c --- /dev/null +++ b/src/mixer/sfx.rs @@ -0,0 +1,453 @@ +//! Feedback sounds for the mixer's switches, and how long the switched thing +//! fades: a switch takes as long as its sound, so you hear the click and the +//! change together. +//! +//! Sounds are `_on.wav` / `_off.wav` (16-bit PCM, 48 kHz, mono or +//! stereo), built into Slidr from `assets/sounds//`. To tune them while +//! Slidr runs, point `SLIDR_SOUND_DIR` at a folder with the same layout +//! (`/sets//`): its files are used instead and re-read when one +//! changes. They play on the monitor only, never into the Slidr Mic. + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::SystemTime; + +use crate::model::{MixTarget, MixerConfig}; + +use super::dsp::{BLOCK_FRAMES, RATE}; + +/// Mono samples at [`RATE`]. +pub type Sound = Arc>; +pub type Sounds = HashMap; + +/// Fade for a switch whose sound file is missing. +const SILENT_FADE: u32 = RATE / 4; +/// Default level of the feedback sounds. +pub const SFX_GAIN: f32 = 0.8; + +/// The sound sets to choose from in Settings: (folder under `sets/`, name). +pub const SETS: [(&str, &str); 3] = [("tide", "Tide"), ("grid", "Grid"), ("grain", "Grain")]; +pub const DEFAULT_SET: &str = "tide"; + +/// A folder to read the sounds from instead of the built-in ones, for +/// tuning them: `SLIDR_SOUND_DIR`. Each set is a folder `sets/` in there. +pub fn sound_dir() -> Option { + std::env::var_os("SLIDR_SOUND_DIR").map(PathBuf::from) +} + +/// The sounds Slidr plays, per set: power, mute, the "Replace original" and +/// "Into Slidr Mic" switches, and the EQ sound used by "Effects & EQ". +macro_rules! built_in { + ($($set:literal),*) => { + &[$(($set, &[ + built_in!(@ $set, "power_on"), built_in!(@ $set, "power_off"), + built_in!(@ $set, "mute_on"), built_in!(@ $set, "mute_off"), + built_in!(@ $set, "replace_on"), built_in!(@ $set, "replace_off"), + built_in!(@ $set, "mic_on"), built_in!(@ $set, "mic_off"), + built_in!(@ $set, "eq_on"), built_in!(@ $set, "eq_off"), + ])),*] + }; + (@ $set:literal, $name:literal) => { + ($name, include_bytes!(concat!("../../assets/sounds/", $set, "/", $name, ".wav")) as &[u8]) + }; +} + +type BuiltInSet = (&'static str, &'static [(&'static str, &'static [u8])]); +const BUILT_IN: &[BuiltInSet] = built_in!("tide", "grid", "grain"); + +/// The built-in sounds of `set`, decoded. +fn built_in_set(set: &str) -> HashMap { + let Some((_, files)) = BUILT_IN.iter().find(|(s, _)| *s == set) else { return HashMap::new() }; + files + .iter() + .filter_map(|(name, bytes)| match parse_wav(bytes) { + Some(s) => Some((name.to_string(), Arc::new(s))), + None => { + log::warn!("mixer: built-in sound {set}/{name} is not 16-bit PCM at 48 kHz"); + None + } + }) + .collect() +} + +/// 16-bit PCM WAV at 48 kHz → mono samples. Anything else is refused. +pub fn parse_wav(bytes: &[u8]) -> Option> { + if bytes.len() < 12 || &bytes[0..4] != b"RIFF" || &bytes[8..12] != b"WAVE" { + return None; + } + let (mut channels, mut rate, mut bits, mut data) = (0u16, 0u32, 0u16, None); + let mut at = 12; + while at + 8 <= bytes.len() { + let id = &bytes[at..at + 4]; + let len = u32::from_le_bytes(bytes[at + 4..at + 8].try_into().ok()?) as usize; + let body = bytes.get(at + 8..at + 8 + len)?; + match id { + b"fmt " if len >= 16 => { + if u16::from_le_bytes([body[0], body[1]]) != 1 { + return None; // not PCM + } + channels = u16::from_le_bytes([body[2], body[3]]); + rate = u32::from_le_bytes(body[4..8].try_into().ok()?); + bits = u16::from_le_bytes([body[14], body[15]]); + } + b"data" => data = Some(body), + _ => {} + } + at += 8 + len + (len & 1); + } + let data = data?; + if bits != 16 || rate != RATE || !(1..=2).contains(&channels) { + return None; + } + let samples = data.chunks_exact(2).map(|b| i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0); + Some(if channels == 1 { + samples.collect() + } else { + let s: Vec = samples.collect(); + s.chunks_exact(2).map(|f| (f[0] + f[1]) * 0.5).collect() + }) +} + +/// The chosen set's sounds: the built-in ones, or — while tuning — its +/// folder, re-read file by file when something changed. +pub struct Library { + set: String, + files: HashMap, +} + +impl Default for Library { + fn default() -> Self { + Self { set: DEFAULT_SET.into(), files: HashMap::new() } + } +} + +impl Library { + /// Switch to another set (an unknown one falls back to the default); + /// the next [`Library::refresh`] loads it. `true` if it changed. + pub fn select(&mut self, set: &str) -> bool { + let set = SETS.iter().find(|s| s.0 == set).map_or(DEFAULT_SET, |s| s.0); + if self.set == set { + return false; + } + self.set = set.into(); + self.files.clear(); + true + } + + /// Load the set, or pick up added, changed and removed files when tuning + /// from a folder. `true` if anything changed. + pub fn refresh(&mut self) -> bool { + let Some(dir) = sound_dir().map(|d| d.join("sets").join(&self.set)) else { + if !self.files.is_empty() { + return false; + } + self.files = built_in_set(&self.set) + .into_iter() + .map(|(n, s)| (n, (SystemTime::UNIX_EPOCH, s))) + .collect(); + return !self.files.is_empty(); + }; + let Ok(entries) = std::fs::read_dir(&dir) else { + let had = !self.files.is_empty(); + self.files.clear(); + return had; + }; + let mut seen = Vec::new(); + let mut changed = false; + for e in entries.flatten() { + let path = e.path(); + if path.extension().and_then(|x| x.to_str()) != Some("wav") { + continue; + } + let Some(name) = path.file_stem().and_then(|s| s.to_str()).map(str::to_string) else { continue }; + let Ok(modified) = e.metadata().and_then(|m| m.modified()) else { continue }; + seen.push(name.clone()); + if self.files.get(&name).is_some_and(|(t, _)| *t == modified) { + continue; + } + match std::fs::read(&path).ok().and_then(|b| parse_wav(&b)) { + Some(s) => { + self.files.insert(name, (modified, Arc::new(s))); + changed = true; + } + None => log::warn!("mixer: {} is not 16-bit PCM at 48 kHz; skipped", path.display()), + } + } + let before = self.files.len(); + self.files.retain(|n, _| seen.contains(n)); + changed || self.files.len() != before + } + + pub fn sounds(&self) -> Sounds { + self.files.iter().map(|(n, (_, s))| (n.clone(), s.clone())).collect() + } +} + +/// Fade lengths (in frames) for what a layout change switched. Everything +/// not listed changes within one block. +#[derive(Default)] +pub struct Fades { + /// A strip's gains (mute, hear-yourself), keyed by `Voice` / `Channel`. + pub gain: HashMap, + /// One effect slot: (strip, slot id). + pub slots: HashMap<(MixTarget, u32), u32>, +} + +impl Fades { + pub fn gain(&self, strip: MixTarget) -> u32 { + self.gain.get(&strip).copied().unwrap_or(BLOCK_FRAMES as u32) + } + + pub fn slot(&self, strip: MixTarget, id: u32) -> u32 { + self.slots.get(&(strip, id)).copied().unwrap_or(BLOCK_FRAMES as u32) + } +} + +pub fn switch_sound(base: &str, on: bool) -> String { + format!("{base}_{}", if on { "on" } else { "off" }) +} + +/// What a layout change switched: the sounds to play and how long each +/// switched thing fades. `muted_*` are the effective mutes (board toggles +/// included) before and after. +pub fn diff( + old: &MixerConfig, + new: &MixerConfig, + muted_before: &dyn Fn(MixTarget) -> Option, + muted_after: &dyn Fn(MixTarget) -> Option, + sounds: &Sounds, +) -> (Vec, Fades) { + let mut play = Vec::new(); + let mut fades = Fades::default(); + let mut switched = |name: String, fallback: u32| -> u32 { + match sounds.get(&name) { + Some(s) => { + play.push(s.clone()); + s.len() as u32 + } + None => fallback, + } + }; + + let mut strips = vec![(MixTarget::Voice, &old.voice.fx, &new.voice.fx)]; + for c in &new.channels { + if let Some(o) = old.channels.iter().find(|o| o.id == c.id) { + strips.push((MixTarget::Channel(c.id), &o.fx, &c.fx)); + } + } + for (strip, _, _) in &strips { + if let (Some(a), Some(b)) = (muted_before(*strip), muted_after(*strip)) { + if a != b { + let len = switched(switch_sound("mute", b), BLOCK_FRAMES as u32); + fades.gain.insert(*strip, len); + } + } + } + for c in &new.channels { + let Some(o) = old.channels.iter().find(|o| o.id == c.id) else { continue }; + if o.reroute != c.reroute { + switched(switch_sound("replace", c.reroute), 0); + } + // Into the Slidr Mic: the send fades in / out with its sound. + if o.mic_send.is_some() != c.mic_send.is_some() { + let len = switched(switch_sound("mic", c.mic_send.is_some()), BLOCK_FRAMES as u32); + let e = fades.gain.entry(MixTarget::Channel(c.id)).or_insert(0); + *e = (*e).max(len); + } + } + if old.voice.to_monitor != new.voice.to_monitor { + let len = switched(switch_sound("monitor", new.voice.to_monitor), BLOCK_FRAMES as u32); + let e = fades.gain.entry(MixTarget::Voice).or_insert(0); + *e = (*e).max(len); + } + for (strip, a, b) in strips { + let master = a.on != b.on; + // The master switch clicks once for the whole chain: `effects_on/off`, + // or the EQ's sound while there is no file of its own. + let master_len = if master { + let own = switch_sound("effects", b.on); + let name = if sounds.contains_key(&own) { own } else { switch_sound("eq", b.on) }; + switched(name, SILENT_FADE) + } else { + 0 + }; + for slot in &b.chain { + let Some(was) = a.chain.iter().find(|s| s.id == slot.id) else { continue }; + let (before, after) = (was.on && a.on, slot.on && b.on); + if before == after { + continue; + } + // Switches inside the effects menu stay quiet; only the master clicks. + let len = if master { master_len } else { SILENT_FADE }; + fades.slots.insert((strip, slot.id), len); + } + } + (play, fades) +} + +/// Sounds currently playing, mixed onto the monitor. +pub struct Player { + playing: Vec<(Sound, usize)>, + pub gain: f32, +} + +impl Default for Player { + fn default() -> Self { + Self { playing: Vec::new(), gain: SFX_GAIN } + } +} + +impl Player { + pub fn play(&mut self, s: Sound) { + self.playing.push((s, 0)); + } + + /// Add the next stretch of every playing sound to a stereo block. + pub fn mix_into(&mut self, out: &mut [f32]) { + let gain = self.gain; + for (s, pos) in &mut self.playing { + for frame in out.chunks_exact_mut(2) { + let Some(v) = s.get(*pos) else { break }; + frame[0] += v * gain; + frame[1] += v * gain; + *pos += 1; + } + } + self.playing.retain(|(s, pos)| *pos < s.len()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::model::{Effects, Fx, FxSlot, Limiter, MixChannel}; + + fn wav(channels: u16, rate: u32, samples: &[i16]) -> Vec { + let data: Vec = samples.iter().flat_map(|s| s.to_le_bytes()).collect(); + let mut b = b"RIFF".to_vec(); + b.extend((36 + data.len() as u32).to_le_bytes()); + b.extend(b"WAVEfmt "); + b.extend(16u32.to_le_bytes()); + b.extend(1u16.to_le_bytes()); + b.extend(channels.to_le_bytes()); + b.extend(rate.to_le_bytes()); + b.extend((rate * channels as u32 * 2).to_le_bytes()); + b.extend((channels * 2).to_le_bytes()); + b.extend(16u16.to_le_bytes()); + b.extend(b"data"); + b.extend((data.len() as u32).to_le_bytes()); + b.extend(data); + b + } + + #[test] + fn reads_mono_and_folds_stereo() { + assert_eq!(parse_wav(&wav(1, 48000, &[16384, -16384])).unwrap(), vec![0.5, -0.5]); + assert_eq!(parse_wav(&wav(2, 48000, &[16384, 0])).unwrap(), vec![0.25]); + assert!(parse_wav(&wav(1, 44100, &[0])).is_none(), "other rates are refused"); + assert!(parse_wav(b"not a wav").is_none()); + } + + fn cfg(muted: bool, slot_on: bool, fx_on: bool) -> MixerConfig { + let fx = Effects { on: fx_on, chain: vec![FxSlot { id: 7, on: slot_on, fx: Fx::Limiter(Limiter::default()) }] }; + MixerConfig { + enabled: true, + channels: vec![MixChannel { + id: 1, + name: "c".into(), + programs: vec![], + level: 1.0, + muted, + fx, + reroute: true, + mic_send: None, + collapsed: false, + }], + ..Default::default() + } + } + + #[test] + fn a_switch_plays_its_sound_and_fades_for_as_long() { + let sounds: Sounds = [("mute_on", 300usize), ("limiter_off", 500)] + .into_iter() + .map(|(n, l)| (n.to_string(), Arc::new(vec![0.0; l]))) + .collect(); + let (a, b) = (cfg(false, true, true), cfg(true, false, true)); + let muted = |c: &MixerConfig| { + let m = c.channels[0].muted; + move |t| (t == MixTarget::Channel(1)).then_some(m) + }; + let (play, fades) = diff(&a, &b, &muted(&a), &muted(&b), &sounds); + assert_eq!(play.len(), 1, "effects inside the menu switch quietly"); + assert_eq!(fades.gain(MixTarget::Channel(1)), 300); + assert_eq!(fades.slot(MixTarget::Channel(1), 7), SILENT_FADE); + // Nothing switched: nothing plays, quick changes. + let (play, fades) = diff(&a, &a, &muted(&a), &muted(&a), &sounds); + assert!(play.is_empty()); + assert_eq!(fades.slot(MixTarget::Channel(1), 7), BLOCK_FRAMES as u32); + } + + #[test] + fn replace_and_mic_switches_have_their_sounds() { + let sounds: Sounds = [("replace_off", 100usize), ("mic_on", 700)] + .into_iter() + .map(|(n, l)| (n.to_string(), Arc::new(vec![0.0; l]))) + .collect(); + let a = cfg(false, true, true); + let mut b = a.clone(); + b.channels[0].reroute = false; + b.channels[0].mic_send = Some(0.5); + let (play, fades) = diff(&a, &b, &|_| None, &|_| None, &sounds); + assert_eq!(play.len(), 2); + assert_eq!(fades.gain(MixTarget::Channel(1)), 700, "the send fades with its sound"); + } + + #[test] + fn master_switch_clicks_once_and_fades_every_effect() { + let (a, b) = (cfg(false, true, true), cfg(false, true, false)); + let (play, fades) = diff(&a, &b, &|_| None, &|_| None, &Sounds::new()); + assert!(play.is_empty(), "no file, no sound"); + assert_eq!(fades.slot(MixTarget::Channel(1), 7), SILENT_FADE); + let eq: Sounds = [("eq_off".to_string(), Arc::new(vec![0.0; 900]))].into(); + let (play, fades) = diff(&a, &b, &|_| None, &|_| None, &eq); + assert_eq!(play.len(), 1, "falls back to the EQ sound"); + assert_eq!(fades.slot(MixTarget::Channel(1), 7), 900); + let own: Sounds = [("effects_off".to_string(), Arc::new(vec![0.0; 400])), ("eq_off".to_string(), Arc::new(vec![0.0; 900]))].into(); + assert_eq!(diff(&a, &b, &|_| None, &|_| None, &own).1.slot(MixTarget::Channel(1), 7), 400); + } + + #[test] + fn every_set_is_built_in_and_decodes() { + for (set, _) in SETS { + let sounds = built_in_set(set); + assert_eq!(sounds.len(), 10, "{set}"); + for name in ["power_on", "power_off", "mute_on", "mute_off", "replace_on", "replace_off", "mic_on", "mic_off", "eq_on", "eq_off"] { + assert!(sounds.get(name).is_some_and(|s| !s.is_empty()), "{set}/{name}"); + } + } + } + + #[test] + fn selecting_a_set_drops_the_old_sounds_and_unknown_sets_fall_back() { + let mut lib = Library::default(); + lib.files.insert("mute_on".into(), (SystemTime::UNIX_EPOCH, Arc::new(vec![0.0]))); + assert!(!lib.select("tide"), "already the default"); + assert!(lib.select("grid")); + assert!(lib.sounds().is_empty()); + assert!(lib.select("no-such-set")); + assert_eq!(lib.set, DEFAULT_SET); + } + + #[test] + fn player_mixes_to_both_sides_and_finishes() { + let mut p = Player::default(); + p.play(Arc::new(vec![1.0; 3])); + let mut out = vec![0.0; 8]; + p.mix_into(&mut out); + assert_eq!(&out[..6], &[SFX_GAIN; 6]); + assert_eq!(&out[6..], &[0.0, 0.0]); + assert!(p.playing.is_empty()); + } +} diff --git a/src/mixer/windows_io.rs b/src/mixer/windows_io.rs new file mode 100644 index 0000000..8216ba9 --- /dev/null +++ b/src/mixer/windows_io.rs @@ -0,0 +1,1200 @@ +//! Windows side of the mixer: device I/O, per-program capture, routing, and +//! the thread that clocks the engine off the monitor device. +//! +//! Every device runs 16-bit stereo at 48 kHz with Windows converting +//! (`AUTOCONVERTPCM`) — the format the process-loopback proof of concept was +//! verified with; the engine works in `f32` in between. + +use std::collections::{HashMap, HashSet}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{mpsc, Arc}; +use std::time::{Duration, Instant}; + +use anyhow::{anyhow, Context}; +use crossbeam_channel::{Receiver, RecvTimeoutError}; +use windows::core::{implement, Interface, IUnknown, HRESULT, PCWSTR}; +use windows::Win32::Devices::FunctionDiscovery::PKEY_Device_FriendlyName; +use windows::Win32::Foundation::{CloseHandle, HANDLE, WAIT_OBJECT_0}; +use windows::Win32::Media::Audio::{ + eCapture, eConsole, eRender, ActivateAudioInterfaceAsync, EDataFlow, + IActivateAudioInterfaceAsyncOperation, IActivateAudioInterfaceCompletionHandler, + IActivateAudioInterfaceCompletionHandler_Impl, IAudioCaptureClient, IAudioClient, + IAudioRenderClient, IAudioSessionControl2, IAudioSessionManager2, IMMDevice, + IMMDeviceEnumerator, MMDeviceEnumerator, AUDCLNT_SHAREMODE_SHARED, + AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM, AUDCLNT_STREAMFLAGS_EVENTCALLBACK, + AUDCLNT_STREAMFLAGS_LOOPBACK, AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, + AUDIOCLIENT_ACTIVATION_PARAMS, AUDIOCLIENT_ACTIVATION_PARAMS_0, + AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK, AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS, + DEVICE_STATE_ACTIVE, PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE, + VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, WAVEFORMATEX, +}; +use windows::Win32::System::Com::{ + CoCreateInstance, CoInitializeEx, CoTaskMemFree, CLSCTX_ALL, COINIT_MULTITHREADED, STGM_READ, +}; +use windows::Win32::System::Diagnostics::ToolHelp::{ + CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS, +}; +use windows::Win32::System::Threading::{CreateEventW, GetCurrentProcessId, WaitForSingleObject}; + +use crate::model::{MixTarget, MixerConfig, MixerDevices}; + +use super::dsp::{Fade, BLOCK_FRAMES, BLOCK_SAMPLES, RATE}; +use super::engine::Engine; +use super::ring::Ring; +use super::route::Router; +use super::sfx::{self, Library, Player, Sounds}; +use super::Cmd; + +const CHANNELS: u16 = 2; +const BYTES_PER_FRAME: usize = 4; +/// Device buffer: 100 ms in REFERENCE_TIME (100 ns) units. +const BUFFER_100MS: i64 = 1_000_000; +const SCAN_EVERY: Duration = Duration::from_secs(1); +/// Audio the engine keeps queued on the monitor: 30 ms. +const MONITOR_QUEUE: u32 = (BLOCK_FRAMES * 3) as u32; +/// Above this the Slidr Mic skips a block instead of growing latency: 60 ms. +const MIC_QUEUE_MAX: u32 = (BLOCK_FRAMES * 6) as u32; + +fn pcm_format() -> WAVEFORMATEX { + WAVEFORMATEX { + wFormatTag: 1, // WAVE_FORMAT_PCM + nChannels: CHANNELS, + nSamplesPerSec: RATE, + nAvgBytesPerSec: RATE * BYTES_PER_FRAME as u32, + nBlockAlign: BYTES_PER_FRAME as u16, + wBitsPerSample: 16, + cbSize: 0, + } +} + +// ── Devices ────────────────────────────────────────────────────────────────── + +fn friendly_name(dev: &IMMDevice) -> String { + unsafe { + dev.OpenPropertyStore(STGM_READ) + .and_then(|s| s.GetValue(&PKEY_Device_FriendlyName)) + .map(|v| v.to_string()) + .unwrap_or_default() + } +} + +fn device_id(dev: &IMMDevice) -> Option { + unsafe { + let id = dev.GetId().ok()?; + let s = id.to_string().ok(); + CoTaskMemFree(Some(id.0 as _)); + s + } +} + +/// Endpoint by friendly name (exact, then substring, case-insensitive), or +/// the default for `flow` when `name` is `None`. +fn find_device(en: &IMMDeviceEnumerator, flow: EDataFlow, name: Option<&str>) -> Option { + unsafe { + let Some(name) = name else { + return en.GetDefaultAudioEndpoint(flow, eConsole).ok(); + }; + let needle = name.to_lowercase(); + let coll = en.EnumAudioEndpoints(flow, DEVICE_STATE_ACTIVE).ok()?; + let devs: Vec = (0..coll.GetCount().ok()?).filter_map(|i| coll.Item(i).ok()).collect(); + let named: Vec<(IMMDevice, String)> = + devs.into_iter().map(|d| { let n = friendly_name(&d).to_lowercase(); (d, n) }).collect(); + named + .iter() + .find(|(_, n)| *n == needle) + .or_else(|| named.iter().find(|(_, n)| n.contains(&needle))) + .map(|(d, _)| d.clone()) + } +} + +/// pid → image name of every program with an audio session on any output. +fn session_programs(en: &IMMDeviceEnumerator) -> HashMap { + let own = unsafe { GetCurrentProcessId() }; + let mut out = HashMap::new(); + unsafe { + let Ok(coll) = en.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE) else { return out }; + for i in 0..coll.GetCount().unwrap_or(0) { + let Ok(dev) = coll.Item(i) else { continue }; + let Ok(mgr) = dev.Activate::(CLSCTX_ALL, None) else { continue }; + let Ok(list) = mgr.GetSessionEnumerator() else { continue }; + for j in 0..list.GetCount().unwrap_or(0) { + let Ok(ctl) = list.GetSession(j) else { continue }; + let Ok(ctl2) = ctl.cast::() else { continue }; + let pid = ctl2.GetProcessId().unwrap_or(0); + if pid == 0 || pid == own || out.contains_key(&pid) { + continue; + } + if let Some(name) = crate::audio::wasapi::process_name(pid) { + out.insert(pid, name); + } + } + } + } + out +} + +/// pid → parent pid of every running process. +fn parent_pids() -> HashMap { + let mut out = HashMap::new(); + unsafe { + let Ok(snap) = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) else { return out }; + let mut e = PROCESSENTRY32W { dwSize: std::mem::size_of::() as u32, ..Default::default() }; + if Process32FirstW(snap, &mut e).is_ok() { + loop { + out.insert(e.th32ProcessID, e.th32ParentProcessID); + if Process32NextW(snap, &mut e).is_err() { + break; + } + } + } + let _ = CloseHandle(snap); + } + out +} + +/// Whether one of `pid`'s ancestors is in `set`. Process loopback captures a +/// whole process tree, so capturing both would play the child twice. +fn has_ancestor_in(pid: u32, set: &HashSet, parents: &HashMap) -> bool { + let mut cur = pid; + for _ in 0..32 { + match parents.get(&cur) { + Some(&p) if p != 0 && p != cur => { + if set.contains(&p) { + return true; + } + cur = p; + } + _ => return false, + } + } + false +} + +// ── Render ─────────────────────────────────────────────────────────────────── + +struct Output { + client: IAudioClient, + rc: IAudioRenderClient, + frames: u32, + event: HANDLE, + name: String, +} + +impl Output { + fn open(dev: &IMMDevice) -> anyhow::Result { + let name = friendly_name(dev); + unsafe { + let client: IAudioClient = dev.Activate(CLSCTX_ALL, None)?; + client + .Initialize( + AUDCLNT_SHAREMODE_SHARED, + AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM + | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY + | AUDCLNT_STREAMFLAGS_EVENTCALLBACK, + BUFFER_100MS, + 0, + &pcm_format(), + None, + ) + .with_context(|| format!("open output {name:?}"))?; + let event = CreateEventW(None, false, false, None)?; + client.SetEventHandle(event)?; + let frames = client.GetBufferSize()?; + let rc: IAudioRenderClient = client.GetService()?; + client.Start()?; + Ok(Self { client, rc, frames, event, name }) + } + } + + fn queued(&self) -> u32 { + unsafe { self.client.GetCurrentPadding().unwrap_or(self.frames) } + } + + /// Queue one block if it fits; a full device buffer drops it. + fn write(&self, block: &[f32]) -> windows::core::Result<()> { + let n = (block.len() / 2) as u32; + unsafe { + if self.frames - self.client.GetCurrentPadding()? < n { + return Ok(()); + } + let buf = self.rc.GetBuffer(n)? as *mut i16; + let out = std::slice::from_raw_parts_mut(buf, block.len()); + for (o, s) in out.iter_mut().zip(block) { + *o = (s.clamp(-1.0, 1.0) * i16::MAX as f32) as i16; + } + self.rc.ReleaseBuffer(n, 0) + } + } +} + +impl Drop for Output { + fn drop(&mut self) { + unsafe { + let _ = self.client.Stop(); + let _ = CloseHandle(self.event); + } + } +} + +// ── Capture ────────────────────────────────────────────────────────────────── + +enum Source { + /// A program and its child processes. + Process(u32), + /// A capture endpoint, by endpoint id. + Device(String), +} + +/// A capture running on its own thread, feeding `ring`. Dropping stops it. +struct Capture { + ring: Arc, + stop: Arc, +} + +impl Capture { + fn spawn(source: Source) -> Self { + let ring = Arc::new(Ring::default()); + let stop = Arc::new(AtomicBool::new(false)); + let (r, s) = (ring.clone(), stop.clone()); + let _ = std::thread::Builder::new() + .name("mixer-capture".into()) + .spawn(move || { + if let Err(e) = capture_loop(&source, &r, &s) { + match source { + Source::Process(pid) => log::warn!("mixer: capture of pid {pid} stopped: {e:#}"), + Source::Device(_) => log::warn!("mixer: microphone capture stopped: {e:#}"), + } + } + }); + Self { ring, stop } + } +} + +impl Drop for Capture { + fn drop(&mut self) { + // Not joined: the thread notices within its 100 ms wait and exits on + // its own, and the audio thread must not sit here meanwhile. + self.stop.store(true, Ordering::Relaxed); + } +} + +fn capture_loop(source: &Source, ring: &Ring, stop: &AtomicBool) -> anyhow::Result<()> { + const SILENT: u32 = 0x2; // AUDCLNT_BUFFERFLAGS_SILENT + unsafe { + let _ = CoInitializeEx(None, COINIT_MULTITHREADED); + let (client, flags) = match source { + Source::Process(pid) => ( + activate_process_loopback(*pid)?, + AUDCLNT_STREAMFLAGS_LOOPBACK + | AUDCLNT_STREAMFLAGS_EVENTCALLBACK + | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM, + ), + Source::Device(id) => { + let en: IMMDeviceEnumerator = CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL)?; + let wide: Vec = id.encode_utf16().chain(Some(0)).collect(); + let dev = en.GetDevice(PCWSTR(wide.as_ptr()))?; + ( + dev.Activate::(CLSCTX_ALL, None)?, + AUDCLNT_STREAMFLAGS_EVENTCALLBACK + | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM + | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, + ) + } + }; + client.Initialize(AUDCLNT_SHAREMODE_SHARED, flags, BUFFER_100MS, 0, &pcm_format(), None)?; + let event = CreateEventW(None, false, false, None)?; + client.SetEventHandle(event)?; + let cap: IAudioCaptureClient = client.GetService()?; + client.Start()?; + let mut samples: Vec = Vec::new(); + let result = (|| -> anyhow::Result<()> { + while !stop.load(Ordering::Relaxed) { + if WaitForSingleObject(event, 100) != WAIT_OBJECT_0 { + continue; + } + loop { + let mut data = std::ptr::null_mut(); + let (mut n, mut flags) = (0u32, 0u32); + cap.GetBuffer(&mut data, &mut n, &mut flags, None, None)?; + if n == 0 { + cap.ReleaseBuffer(0)?; + break; + } + let count = n as usize * CHANNELS as usize; + samples.clear(); + if flags & SILENT != 0 { + samples.resize(count, 0.0); + } else { + let pcm = std::slice::from_raw_parts(data as *const i16, count); + samples.extend(pcm.iter().map(|&s| s as f32 / 32768.0)); + } + cap.ReleaseBuffer(n)?; + ring.push(&samples); + } + } + Ok(()) + })(); + let _ = client.Stop(); + let _ = CloseHandle(event); + result + } +} + +#[implement(IActivateAudioInterfaceCompletionHandler)] +struct Activated(mpsc::Sender<()>); + +impl IActivateAudioInterfaceCompletionHandler_Impl for Activated_Impl { + fn ActivateCompleted(&self, _op: Option<&IActivateAudioInterfaceAsyncOperation>) -> windows::core::Result<()> { + let _ = self.0.send(()); + Ok(()) + } +} + +/// Raw PROPVARIANT holding a VT_BLOB (x64 layout: 8-byte header + 16-byte union). +#[repr(C)] +struct BlobVariant { + vt: u16, + _reserved: [u16; 3], + size: u32, + data: *const u8, + _pad: u64, +} +const VT_BLOB: u16 = 65; + +/// Audio client capturing `pid` and its children (Windows 10 2004+). +fn activate_process_loopback(pid: u32) -> anyhow::Result { + let params = AUDIOCLIENT_ACTIVATION_PARAMS { + ActivationType: AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK, + Anonymous: AUDIOCLIENT_ACTIVATION_PARAMS_0 { + ProcessLoopbackParams: AUDIOCLIENT_PROCESS_LOOPBACK_PARAMS { + TargetProcessId: pid, + ProcessLoopbackMode: PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE, + }, + }, + }; + let var = BlobVariant { + vt: VT_BLOB, + _reserved: [0; 3], + size: std::mem::size_of::() as u32, + data: ¶ms as *const _ as *const u8, + _pad: 0, + }; + let (tx, rx) = mpsc::channel(); + let handler: IActivateAudioInterfaceCompletionHandler = Activated(tx).into(); + unsafe { + let op = ActivateAudioInterfaceAsync( + VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, + &IAudioClient::IID, + Some(&var as *const _ as *const windows::core::PROPVARIANT), + &handler, + )?; + rx.recv_timeout(Duration::from_secs(5)).map_err(|_| anyhow!("activation timed out"))?; + let mut hr = HRESULT(0); + let mut unk: Option = None; + op.GetActivateResult(&mut hr, &mut unk)?; + hr.ok()?; + Ok(unk.ok_or_else(|| anyhow!("no interface"))?.cast()?) + } +} + +// ── Threads ────────────────────────────────────────────────────────────────── +// +// Two threads, so nothing slow ever sits between the monitor's buffer and the +// next block: the audio thread only mixes and writes, the control thread does +// everything that may take a while (enumerating sessions and processes — +// ~100 ms — and routing) and tells the audio thread what to capture. + +/// Control thread → audio thread. +enum AudioCmd { + Mixer(Cmd), + /// Programs to capture: pid → channel. + Capture(HashMap), + /// Channels whose programs are rerouted (and so belong on the monitor). + Monitored(Vec<(u32, bool)>), + /// Microphones to capture: (channel, device name; "" = system default). + Mics(HashSet<(u32, String)>), + /// The system's default output changed: the monitor follows it. + DefaultOutputChanged, + /// The feedback sounds, after the folder changed. + Sounds(Sounds), + Stop(crossbeam_channel::Sender<()>), +} + +struct Audio { + en: IMMDeviceEnumerator, + engine: Engine, + cfg: MixerConfig, + devices: MixerDevices, + /// Levels and mutes set from the board since the last layout; re-applied + /// over a new layout so a profile edit doesn't snap faders back. + live_levels: HashMap, + live_mutes: HashMap, + monitor: Option, + mic_out: Option, + /// pid → (channel, capture). + captures: HashMap, + /// (channel, microphone) → capture; `None` while the microphone isn't + /// there (tried again when the set of microphones changes). + mics: HashMap<(u32, String), Option>, + /// Whether devices are open and mixing — the control thread only starts + /// captures and reroutes while this is set. + running_flag: Arc, + sounds: Sounds, + sfx_on: bool, + /// The set the sounds come from, once the settings named it. + sound_set: Option, + /// The next layout is the one Slidr starts with: power on without a sound. + quiet_start: bool, + player: Player, + /// Master gain of both buses: fades in on power-on, out on power-off. + bus: Fade, + /// Switched off and fading out; devices close at this moment. + closing: Option, + stats: Stats, + tmp: Vec, + voice_buf: Vec, + monitor_buf: Vec, + mic_buf: Vec, +} + +impl Audio { + fn new(en: IMMDeviceEnumerator, running_flag: Arc) -> Self { + Self { + en, + engine: Engine::default(), + cfg: MixerConfig::default(), + devices: MixerDevices::default(), + live_levels: HashMap::new(), + live_mutes: HashMap::new(), + monitor: None, + mic_out: None, + captures: HashMap::new(), + mics: HashMap::new(), + running_flag, + sounds: Sounds::new(), + sfx_on: true, + sound_set: None, + quiet_start: false, + player: Player::default(), + bus: Fade::new(1.0), + closing: None, + stats: Stats::new(), + tmp: vec![0.0; BLOCK_SAMPLES], + voice_buf: vec![0.0; BLOCK_SAMPLES], + monitor_buf: vec![0.0; BLOCK_SAMPLES], + mic_buf: vec![0.0; BLOCK_SAMPLES], + } + } + + fn running(&self) -> bool { + (self.cfg.enabled || self.closing.is_some()) && (self.monitor.is_some() || self.mic_out.is_some()) + } + + /// Start the named feedback sound (if sounds are on); its length in + /// frames either way, since the switch fades that long (0 if missing). + fn play(&mut self, name: &str) -> u32 { + match self.sounds.get(name) { + Some(s) => { + if self.sfx_on { + self.player.play(s.clone()); + } + s.len() as u32 + } + None => 0, + } + } + + fn handle(&mut self, cmd: Cmd) { + match cmd { + Cmd::Configure(cfg, devices) => self.configure(cfg, devices), + Cmd::QuietStart => self.quiet_start = true, + Cmd::SetLevel(t, v) => { + self.live_levels.insert(t, v); + self.engine.set_level(t, v); + } + Cmd::SetMute(t, m) => { + self.live_mutes.insert(t, m); + self.engine.set_mute(t, m, BLOCK_FRAMES as u32); + } + Cmd::ToggleMute(t) => { + if let Some(m) = self.engine.muted(t) { + self.live_mutes.insert(t, !m); + let len = self.play(&sfx::switch_sound("mute", !m)).max(BLOCK_FRAMES as u32); + self.engine.set_mute(t, !m, len); + } + } + Cmd::SoundPrefs(on, volume, set) => { + self.sfx_on = on; + self.player.gain = volume.clamp(0.0, 1.0); + // Picking another set lets you hear it (not on the first + // prefs after start, which only restore the saved one). + let changed = self.sound_set.as_ref().is_some_and(|s| *s != set); + self.sound_set = Some(set); + if changed && self.running() && self.closing.is_none() { + self.play("mute_on"); + } + } + Cmd::Shutdown(_) => unreachable!("the control thread handles shutdown"), + } + } + + fn configure(&mut self, cfg: MixerConfig, devices: MixerDevices) { + let was_running = self.running(); + let old = std::mem::take(&mut self.cfg); + // A board move stays until the page changes that same level (the + // page then shows the truth); editing anything else keeps it. + self.live_levels.retain(|t, _| level_of(&old, *t) == level_of(&cfg, *t)); + self.live_mutes.retain(|t, _| muted_of(&old, *t) == muted_of(&cfg, *t)); + + // What got switched: its sound plays, and it fades as long. + let live = was_running && cfg.enabled; + let none = Sounds::new(); + let engine = &self.engine; + let live_mutes = &self.live_mutes; + let (sounds, fades) = sfx::diff( + &old, + &cfg, + &|t| engine.muted(t), + &|t| live_mutes.get(&t).copied().or_else(|| muted_of(&cfg, t)), + if live { &self.sounds } else { &none }, + ); + if self.sfx_on { + for s in sounds { + self.player.play(s); + } + } + self.engine.configure(&cfg, &fades); + for (t, v) in &self.live_levels { + self.engine.set_level(*t, *v); + } + for (t, m) in &self.live_mutes { + self.engine.set_mute(*t, *m, fades.gain(*t)); + } + + let reopen = devices != self.devices; + let (was_on, now_on) = (old.enabled, cfg.enabled); + self.cfg = cfg; + self.devices = devices; + let starting = std::mem::take(&mut self.quiet_start); + if now_on && !was_on { + // Power-on: the mix rises with its sound — but not when Slidr + // itself starts with the mixer on; nothing was switched then. + self.closing = None; + self.close_devices(); + self.open_devices(); + self.bus = Fade::new(0.0); + let len = if starting { 0 } else { self.play("power_on") }; + self.bus.set_over(1.0, len.max(BLOCK_FRAMES as u32)); + } else if reopen && now_on { + // Another device: reopen quietly. Program captures don't depend + // on the devices and keep running (the control thread only sends + // changes, so dropping them here would leave the mix silent). + self.close_outputs(); + self.open_devices(); + } else if was_on && !now_on && was_running { + // Power-off: keep playing while the mix sinks with its sound. + let len = self.play("power_off").max(BLOCK_FRAMES as u32); + self.bus.set_over(0.0, len); + self.closing = Some(Instant::now() + Duration::from_secs_f32(len as f32 / RATE as f32)); + } else if now_on { + self.closing = None; + self.bus.set_over(1.0, BLOCK_FRAMES as u32); + } + self.running_flag.store(self.running(), Ordering::Relaxed); + } + + /// Finish a power-off once its fade is over. + fn finish_closing(&mut self) { + if self.closing.is_some_and(|t| Instant::now() >= t) { + self.closing = None; + self.close_devices(); + self.bus = Fade::new(1.0); + self.running_flag.store(self.running(), Ordering::Relaxed); + } + } + + fn open_devices(&mut self) { + let d = self.devices.clone(); + // The mix plays on the system's default output and follows it (so + // switching outputs in Windows or from the board just works). Tests + // name a silent one instead. + let monitor = if cfg!(test) { d.monitor.as_deref() } else { None }; + self.monitor = match find_device(&self.en, eRender, monitor).as_ref().map(Output::open) { + Some(Ok(o)) => Some(o), + Some(Err(e)) => { + log::warn!("mixer: {e:#}"); + None + } + None => { + log::warn!("mixer: no output device to play on"); + None + } + }; + self.mic_out = d.mic_out.as_deref().and_then(|n| match find_device(&self.en, eRender, Some(n)) { + Some(dev) => Output::open(&dev).map_err(|e| log::warn!("mixer: {e:#}")).ok(), + None => { + log::warn!("mixer: Slidr Mic output {n:?} not found"); + None + } + }); + log::info!( + "mixer: monitor {:?}, Slidr Mic {:?}", + self.monitor.as_ref().map(|o| &o.name), + self.mic_out.as_ref().map(|o| &o.name), + ); + } + + fn close_devices(&mut self) { + self.captures.clear(); + self.mics.clear(); + self.close_outputs(); + } + + /// Close the outputs, keeping program and microphone captures. + fn close_outputs(&mut self) { + self.monitor = None; + self.mic_out = None; + } + + /// Start captures for microphones added to channels, drop removed ones. + fn set_mics(&mut self, wanted: HashSet<(u32, String)>) { + self.mics.retain(|k, cap| wanted.contains(k) && cap.is_some()); + let cable = self.devices.mic_out.as_deref().and_then(super::adapter).map(str::to_string); + for key in wanted { + if self.mics.contains_key(&key) { + continue; + } + let name = (!key.1.is_empty()).then_some(key.1.as_str()); + let dev = find_device(&self.en, eCapture, name); + // Recording the Slidr Mic's own cable (say, as the default + // microphone) would feed the mix back into itself. + if dev.as_ref().map(friendly_name).as_deref().and_then(super::adapter).is_some_and(|a| Some(a) == cable.as_deref()) { + log::error!("mixer: microphone {:?} is the Slidr Mic itself; not captured", key.1); + self.mics.insert(key, None); + continue; + } + let cap = dev.as_ref().and_then(device_id).map(|id| Capture::spawn(Source::Device(id))); + if cap.is_none() { + log::warn!("mixer: microphone {:?} not found", key.1); + } + self.mics.insert(key, cap); + } + } + + /// Start captures for new programs, drop those that left. Spawning only + /// starts a thread, so this is cheap enough for the audio thread. + fn set_captures(&mut self, wanted: HashMap) { + self.captures.retain(|pid, (ch, _)| wanted.get(pid) == Some(ch)); + for (pid, ch) in wanted { + self.captures.entry(pid).or_insert_with(|| (ch, Capture::spawn(Source::Process(pid)))); + } + } + + /// Wait for the monitor to want audio, then mix until its queue is topped up. + fn pump(&mut self) { + let clock = self.monitor.as_ref().or(self.mic_out.as_ref()).map(|o| o.event); + match clock { + Some(ev) => unsafe { + WaitForSingleObject(ev, 20); + }, + None => std::thread::sleep(Duration::from_millis(10)), + } + for _ in 0..8 { + if self.monitor.as_ref().is_some_and(|o| o.queued() == 0) { + self.stats.monitor_dry += 1; + } + let wants = self.monitor.as_ref().or(self.mic_out.as_ref()).is_some_and(|o| o.queued() < MONITOR_QUEUE); + if !wants { + break; + } + self.mix_block(); + } + } + + fn mix_block(&mut self) { + let mut channels: HashMap> = HashMap::new(); + for (ch, cap) in self.captures.values() { + if !cap.ring.pop(&mut self.tmp) { + self.stats.underruns += 1; + } + let sum = channels.entry(*ch).or_insert_with(|| vec![0.0; BLOCK_SAMPLES]); + for (s, t) in sum.iter_mut().zip(&self.tmp) { + *s += t; + } + } + for ((ch, _), cap) in &self.mics { + let Some(cap) = cap else { continue }; + // A microphone starting up has nothing yet: silence, not an underrun. + cap.ring.pop(&mut self.voice_buf); + let sum = channels.entry(*ch).or_insert_with(|| vec![0.0; BLOCK_SAMPLES]); + for (s, t) in sum.iter_mut().zip(&self.voice_buf) { + *s += t; + } + } + self.engine.process(&mut channels, None, &mut self.monitor_buf, &mut self.mic_buf); + if !self.bus.is_at(1.0) { + for (m, k) in self.monitor_buf.chunks_exact_mut(2).zip(self.mic_buf.chunks_exact_mut(2)) { + let g = self.bus.next(); + m[0] *= g; + m[1] *= g; + k[0] *= g; + k[1] *= g; + } + } + self.player.mix_into(&mut self.monitor_buf); + if let Some(m) = &self.monitor { + if let Err(e) = m.write(&self.monitor_buf) { + log::warn!("mixer: monitor write failed ({e}); reopening devices"); + self.reopen(); + return; + } + } + if let Some(o) = &self.mic_out { + if o.queued() < MIC_QUEUE_MAX { + if let Err(e) = o.write(&self.mic_buf) { + log::warn!("mixer: Slidr Mic write failed ({e}); reopening devices"); + self.reopen(); + } + } + } + } + + /// A device went away (unplugged, format change): start over. + fn reopen(&mut self) { + self.close_outputs(); + self.open_devices(); + self.running_flag.store(self.running(), Ordering::Relaxed); + } +} + +fn level_of(cfg: &MixerConfig, t: MixTarget) -> Option { + cfg.level(t) +} + +fn muted_of(cfg: &MixerConfig, t: MixTarget) -> Option { + match t { + MixTarget::Voice => Some(cfg.voice.muted), + MixTarget::Channel(id) | MixTarget::Send(id) => cfg.channels.iter().find(|c| c.id == id).map(|c| c.muted), + } +} + +fn audio_thread(rx: Receiver, running_flag: Arc) { + unsafe { + let _ = CoInitializeEx(None, COINIT_MULTITHREADED); + } + let en: IMMDeviceEnumerator = match unsafe { CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL) } { + Ok(en) => en, + Err(e) => { + log::error!("mixer: no audio device enumerator ({e}); mixer disabled"); + return; + } + }; + let mut audio = Audio::new(en, running_flag); + loop { + // While mixing, commands must not stall the audio; otherwise block. + let first = if audio.running() { + rx.try_recv().ok() + } else { + match rx.recv() { + Ok(c) => Some(c), + Err(_) => break, + } + }; + for cmd in first.into_iter().chain(rx.try_iter()) { + match cmd { + AudioCmd::Mixer(c) => audio.handle(c), + // While powering off, keep what plays until the fade is over. + AudioCmd::Capture(_) | AudioCmd::Monitored(_) | AudioCmd::Mics(_) if audio.closing.is_some() => {} + AudioCmd::Capture(wanted) => audio.set_captures(wanted), + AudioCmd::Mics(wanted) => audio.set_mics(wanted), + AudioCmd::DefaultOutputChanged => { + if audio.running() && audio.closing.is_none() { + log::info!("mixer: default output changed; following it"); + audio.reopen(); + } + } + AudioCmd::Monitored(list) => { + for (ch, on) in list { + audio.engine.set_monitored(ch, on); + } + } + AudioCmd::Sounds(s) => audio.sounds = s, + AudioCmd::Stop(ack) => { + audio.close_devices(); + audio.running_flag.store(false, Ordering::Relaxed); + let _ = ack.send(()); + return; + } + } + } + if audio.running() { + audio.pump(); + audio.stats.report(); + } + audio.finish_closing(); + } + audio.close_devices(); +} + +/// Control side: matches programs to channels, routes them, and feeds the +/// audio thread. +struct Control { + en: IMMDeviceEnumerator, + audio: crossbeam_channel::Sender, + audio_running: Arc, + router: Option, + cfg: MixerConfig, + devices: MixerDevices, + sink: Option, + /// What the audio thread was last told to capture (to log changes only). + capturing: HashMap, + /// Microphones the audio thread was last told to capture. + mics: HashSet<(u32, String)>, + /// The system's default output, as last seen (the monitor follows it). + default_output: Option, + library: Library, + /// Switched off: rerouted programs go back when the power-off sound ends. + restore_at: Option, +} + +impl Control { + fn configure(&mut self, cfg: MixerConfig, devices: MixerDevices) { + if devices != self.devices { + self.sink = devices.sink.as_deref().and_then(|n| { + let dev = find_device(&self.en, eRender, Some(n)); + if dev.is_none() { + log::warn!("mixer: silent output {n:?} not found; programs are not rerouted"); + } + dev + }); + // Rerouting onto the device the mix plays on would feed it back into itself. + let monitor = find_device(&self.en, eRender, None).as_ref().and_then(device_id); + if self.sink.as_ref().and_then(device_id).is_some_and(|s| Some(s) == monitor) { + log::error!("mixer: silent output and headphones are the same device; programs are not rerouted"); + self.sink = None; + } + // Nor onto the Slidr Mic's cable: the programs would end up in Discord. + let sink_adapter = self.sink.as_ref().map(friendly_name); + if sink_adapter.as_deref().and_then(super::adapter).is_some_and(|a| Some(a) == devices.mic_out.as_deref().and_then(super::adapter)) { + log::error!("mixer: silent output is the Slidr Mic's cable; programs are not rerouted"); + self.sink = None; + } + log::info!("mixer: silent output {:?}", self.sink.as_ref().map(friendly_name)); + } + self.refresh_sounds(); + if self.cfg.enabled && !cfg.enabled { + let fade = self.library.sounds().get("power_off").map_or(0, |s| s.len()); + self.restore_at = Some(Instant::now() + Duration::from_secs_f32(fade as f32 / RATE as f32)); + } else if cfg.enabled { + self.restore_at = None; + } + self.cfg = cfg.clone(); + self.devices = devices.clone(); + let _ = self.audio.send(AudioCmd::Mixer(Cmd::Configure(cfg, devices))); + } + + /// Hand the audio thread the sounds again if a file changed. + fn refresh_sounds(&mut self) { + if self.library.refresh() { + let _ = self.audio.send(AudioCmd::Sounds(self.library.sounds())); + } + } + + fn restore_if_due(&mut self) { + if self.restore_at.is_some_and(|t| Instant::now() >= t) { + self.restore_at = None; + if let Some(r) = &mut self.router { + r.clear_all(); + } + } + } + + /// Match running programs to channels; update captures and routes. + fn scan(&mut self) { + let sessions = session_programs(&self.en); + let mut wanted: HashMap = HashMap::new(); + if self.cfg.enabled && self.audio_running.load(Ordering::Relaxed) { + for (pid, exe) in &sessions { + let hit = self.cfg.channels.iter().find(|c| { + c.programs.iter().any(|p| crate::audio::wasapi::program_matches(p, *pid, exe)) + }); + if let Some(c) = hit { + wanted.insert(*pid, c.id); + } + } + let parents = parent_pids(); + let set: HashSet = wanted.keys().copied().collect(); + wanted.retain(|pid, _| !has_ancestor_in(*pid, &set, &parents)); + } + if wanted != self.capturing { + for (pid, ch) in &wanted { + if self.capturing.get(pid) != Some(ch) { + log::info!("mixer: capturing {} (pid {pid}) into channel {ch}", sessions[pid]); + } + } + self.capturing = wanted.clone(); + let _ = self.audio.send(AudioCmd::Capture(wanted.clone())); + } + + let reroute: HashSet = wanted + .iter() + .filter(|(_, ch)| self.cfg.channels.iter().any(|c| c.id == **ch && c.reroute)) + .map(|(pid, _)| *pid) + .collect(); + let can_route = self.router.is_some() && self.sink.is_some(); + if let (Some(router), Some(sink)) = (&mut self.router, &self.sink) { + for pid in &reroute { + if !router.is_routed(*pid) { + if let Err(e) = router.route(*pid, sink) { + log::debug!("mixer: rerouting pid {pid} failed ({e}); retrying"); + } + } + } + } + if let Some(router) = &mut self.router { + if router.pending() { + let paths: HashMap = sessions + .keys() + .map(|pid| (*pid, crate::app_groups::process_path(*pid).unwrap_or_default())) + .collect(); + router.recover(&paths, &|pid| can_route && reroute.contains(&pid)); + } + } + // Microphones play nowhere else: a channel of only microphones is + // always heard at its fader (that's "hear yourself"). + let monitored = + self.cfg.channels.iter().map(|c| (c.id, (can_route && c.reroute) || c.mics_only())).collect(); + let _ = self.audio.send(AudioCmd::Monitored(monitored)); + + let mics: HashSet<(u32, String)> = if self.cfg.enabled && self.audio_running.load(Ordering::Relaxed) { + self.cfg.channels.iter().flat_map(|c| c.mics().map(move |m| (c.id, m.to_string()))).collect() + } else { + HashSet::new() + }; + if mics != self.mics { + self.mics = mics.clone(); + let _ = self.audio.send(AudioCmd::Mics(mics)); + } + + let default_output = find_device(&self.en, eRender, None).as_ref().and_then(device_id); + if default_output != self.default_output { + if self.default_output.is_some() { + let _ = self.audio.send(AudioCmd::DefaultOutputChanged); + } + self.default_output = default_output; + } + } + + fn shutdown(&mut self) { + if let Some(r) = &mut self.router { + r.clear_all(); + } + let (tx, rx) = crossbeam_channel::bounded(1); + if self.audio.send(AudioCmd::Stop(tx)).is_ok() { + let _ = rx.recv_timeout(Duration::from_secs(2)); + } + } +} + +pub fn run(rx: Receiver) { + unsafe { + let _ = CoInitializeEx(None, COINIT_MULTITHREADED); + } + let en: IMMDeviceEnumerator = match unsafe { CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL) } { + Ok(en) => en, + Err(e) => { + log::error!("mixer: no audio device enumerator ({e}); mixer disabled"); + return; + } + }; + let (audio_tx, audio_rx) = crossbeam_channel::unbounded(); + let audio_running = Arc::new(AtomicBool::new(false)); + { + let flag = audio_running.clone(); + if let Err(e) = std::thread::Builder::new().name("mixer-audio".into()).spawn(move || audio_thread(audio_rx, flag)) { + log::error!("mixer: could not start the audio thread: {e}"); + return; + } + } + let mut control = Control { + en, + audio: audio_tx, + audio_running, + router: Router::new(), + cfg: MixerConfig::default(), + devices: MixerDevices::default(), + sink: None, + capturing: HashMap::new(), + mics: HashSet::new(), + default_output: None, + library: Library::default(), + restore_at: None, + }; + if let Some(dir) = sfx::sound_dir() { + log::info!("mixer: feedback sounds from {}", dir.display()); + } + control.refresh_sounds(); + let mut next_scan = Instant::now(); + loop { + let due = control.restore_at.map_or(next_scan, |t| t.min(next_scan)); + let wait = due.saturating_duration_since(Instant::now()); + match rx.recv_timeout(wait) { + Ok(Cmd::Shutdown(ack)) => { + control.shutdown(); + let _ = ack.send(()); + return; + } + Ok(Cmd::Configure(cfg, devices)) => { + control.configure(cfg, devices); + // Let the audio thread open its devices before matching programs. + next_scan = Instant::now() + Duration::from_millis(200); + } + Ok(Cmd::SoundPrefs(on, volume, set)) => { + // The new set's sounds reach the audio thread first, so its + // preview plays from that set. + let switched = control.library.select(&set); + if control.library.refresh() || switched { + let _ = control.audio.send(AudioCmd::Sounds(control.library.sounds())); + } + let _ = control.audio.send(AudioCmd::Mixer(Cmd::SoundPrefs(on, volume, set))); + } + Ok(cmd) => { + let _ = control.audio.send(AudioCmd::Mixer(cmd)); + } + Err(RecvTimeoutError::Timeout) if Instant::now() < next_scan => control.restore_if_due(), + Err(RecvTimeoutError::Timeout) => { + control.restore_if_due(); + control.refresh_sounds(); + let t = Instant::now(); + control.scan(); + if t.elapsed() > Duration::from_millis(250) { + log::debug!("mixer: scan took {} ms", t.elapsed().as_millis()); + } + next_scan = Instant::now() + SCAN_EVERY; + } + Err(RecvTimeoutError::Disconnected) => break, + } + } + control.shutdown(); +} + +/// Glitch counters, logged every few seconds while something went wrong — +/// the way to tell a starving device from a starving capture. +struct Stats { + since: Instant, + /// Program capture had less than a block when the mix needed one. + underruns: u32, + /// The monitor had played everything queued before we refilled it. + monitor_dry: u32, +} + +impl Stats { + fn new() -> Self { + Self { since: Instant::now(), underruns: 0, monitor_dry: 0 } + } + + fn report(&mut self) { + if self.since.elapsed() < Duration::from_secs(5) { + return; + } + if self.underruns > 0 || self.monitor_dry > 0 { + log::info!( + "mixer: last 5 s: {} capture underruns, monitor ran dry {} times", + self.underruns, self.monitor_dry + ); + } + *self = Self::new(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// End-to-end on real devices, run by hand: + /// + /// SLIDR_TEST_MONITOR="Voicemeeter In 2" SLIDR_TEST_MIC_OUT="Voicemeeter In 1" \ + /// cargo test --bin slidr windows_io -- --ignored --nocapture + /// + /// Both outputs should be ones nobody listens to. `SLIDR_TEST_MIC` picks + /// the microphone (default: the system default). Measures what this + /// process plays (its own process loopback) at two voice fader positions. + #[test] + #[ignore] + fn microphone_channel_reaches_the_slidr_mic_and_follows_its_fader() { + let env = |k: &str| std::env::var(k).ok(); + unsafe { + let _ = CoInitializeEx(None, COINIT_MULTITHREADED); + } + let en: IMMDeviceEnumerator = unsafe { CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).unwrap() }; + let mut io = Audio::new(en, Default::default()); + let devices = MixerDevices { + monitor: env("SLIDR_TEST_MONITOR"), + mic_input: env("SLIDR_TEST_MIC"), + sink: None, + mic_out: env("SLIDR_TEST_MIC_OUT"), + }; + assert!(devices.monitor.is_some() && devices.mic_out.is_some(), "set SLIDR_TEST_MONITOR / SLIDR_TEST_MIC_OUT"); + let mic = env("SLIDR_TEST_MIC").unwrap_or_default(); + let channel = crate::model::MixChannel { + id: 1, + name: "Microphone".into(), + programs: vec![format!("{}{mic}", crate::model::MIC_PREFIX)], + level: 0.0, + muted: false, + fx: Default::default(), + reroute: false, + mic_send: Some(1.0), + collapsed: false, + }; + io.configure(MixerConfig { enabled: true, channels: vec![channel], ..Default::default() }, devices); + io.set_mics([(1, mic)].into_iter().collect()); + assert!(io.monitor.is_some() && io.mic_out.is_some() && io.mics.values().all(Option::is_some)); + let tap = Capture::spawn(Source::Process(unsafe { GetCurrentProcessId() })); + + let energy = |b: &[f32]| b.iter().map(|s| (*s as f64).powi(2)).sum::(); + let db = |e: f64, n: usize| 10.0 * (e / n.max(1) as f64).log10(); + // (what this process played, mic input, Slidr Mic bus) over `secs`. + // Input and bus are compared block by block, so the room changing + // between two measurements doesn't matter. + let measure = |io: &mut Audio, secs: u64| -> (f64, f64, f64) { + let (mut tapped, mut n_tap, mut input, mut bus, mut n) = (0f64, 0usize, 0f64, 0f64, 0usize); + let mut block = vec![0.0; BLOCK_SAMPLES]; + let end = Instant::now() + Duration::from_secs(secs); + while Instant::now() < end { + unsafe { WaitForSingleObject(io.monitor.as_ref().unwrap().event, 20) }; + while io.monitor.as_ref().unwrap().queued() < MONITOR_QUEUE { + io.mix_block(); + input += energy(&io.voice_buf); + bus += energy(&io.mic_buf); + n += BLOCK_SAMPLES; + } + while tap.ring.pop(&mut block) { + tapped += energy(&block); + n_tap += block.len(); + } + } + (db(tapped, n_tap), db(input, n), db(bus, n)) + }; + measure(&mut io, 1); + let (full, in_full, bus_full) = measure(&mut io, 2); + io.handle(Cmd::SetLevel(MixTarget::Send(1), 0.25)); + measure(&mut io, 1); + let (_, in_q, bus_q) = measure(&mut io, 2); + io.handle(Cmd::SetMute(MixTarget::Channel(1), true)); + measure(&mut io, 1); + let (muted, _, _) = measure(&mut io, 2); + println!( + "microphone {in_full:.1} dBFS, played {full:.1} dBFS; bus minus input at 100 %: {:.1} dB, at 25 %: {:.1} dB; muted played {muted:.1} dBFS", + bus_full - in_full, + bus_q - in_q + ); + io.close_devices(); + + assert!(full > -90.0, "nothing from the microphone was played"); + assert!((bus_full - in_full).abs() < 0.5, "voice at 100 % must pass unchanged"); + // Fader 0.25 → gain 0.0625 → −24 dB. + assert!((bus_q - in_q + 24.1).abs() < 1.0, "fader did not scale the voice"); + assert!(muted < -90.0, "mute did not silence the voice"); + } +} diff --git a/src/mixer_ui.rs b/src/mixer_ui.rs new file mode 100644 index 0000000..00fa591 --- /dev/null +++ b/src/mixer_ui.rs @@ -0,0 +1,999 @@ +//! Mixer page ↔ profile/settings. The page edits the active profile's +//! `MixerConfig` and the global `MixerDevices`; every edit is saved and handed +//! to the mixer right away. + +use std::sync::Arc; + +use parking_lot::Mutex; +use slint::{ComponentHandle, Model, ModelRc, SharedString, VecModel}; + +use crate::glue::Shared; +use crate::model::{ + Effects, EqBand, EqBandKind, EqPreset, Fx, FxPreset, FxSlot, MixChannel, MixTarget, MixerConfig, MIC_PREFIX, +}; +use crate::{AppWindow, EqBandUI, FxSlotUI, MixChannelUI, PickerEntry}; + +/// Device names behind the page's selectors, as last shown — an index from +/// the UI is resolved against exactly the list it was picked from. +#[derive(Default)] +struct Lists { + /// Real microphones (virtual cables left out: recording one would loop). + mics: Vec, + /// Silent-output candidates (never the Slidr Mic's own cable). + sinks: Vec, + /// Slidr Mic choices: (name Discord shows, output Slidr plays into). + cables: Vec<(String, String)>, +} + +static LISTS: Mutex = Mutex::new(Lists { mics: Vec::new(), sinks: Vec::new(), cables: Vec::new() }); + +/// What the picker lists: programs (0) or microphones (1). +static PICKER_TAB: Mutex = Mutex::new(0); + +/// Where VB-CABLE comes from; opened by the hint's download button. +const CABLE_DOWNLOAD: &str = "https://vb-audio.com/Cable/"; + +/// Strip whose effects the effects popup is editing. +static EDITING_FX: Mutex> = Mutex::new(None); + +/// Strip and EQ slot the EQ editor is editing. +static EDITING_EQ: Mutex> = Mutex::new(None); + +/// Channel whose programs the picker popup is editing. +static PICKING: Mutex> = Mutex::new(None); + +use crate::mixer::adapter; + +fn is_virtual(adapter: &str) -> bool { + let a = adapter.to_lowercase(); + ["virtual", "cable", "vb-audio", "voicemeeter"].iter().any(|k| a.contains(k)) +} + +/// Virtual cables as (recording end, playback end): what Discord lists as a +/// microphone, and the output whose audio comes out there. Prefers the plain +/// input over multi-channel variants ("CABLE In 16ch"). +fn cables(mics: &[String], outputs: &[String]) -> Vec<(String, String)> { + mics.iter() + .filter_map(|mic| { + let a = adapter(mic).filter(|a| is_virtual(a))?; + let mut ends: Vec<&String> = outputs.iter().filter(|o| adapter(o) == Some(a)).collect(); + ends.sort_by_key(|o| o.to_lowercase().contains("16ch")); + Some((mic.clone(), ends.first()?.to_string())) + }) + .collect() +} + +/// Fill the Mixer page from the profile and settings (and refresh device lists). +pub fn push(ui: &AppWindow, s: &mut Shared) { + // Profiles that never went through a load (a brand-new one) still carry + // the old voice strip. + let mic = s.settings.mixer_devices.mic_input.clone(); + if s.preset.move_voice_to_channel(mic.as_deref()) { + commit(s); + } + let mut outputs = s.audio.list_outputs(); + let all_mics = s.audio.list_mics(); + let d = &s.settings.mixer_devices; + // A configured device that is unplugged right now stays selectable, so + // opening the page doesn't silently drop the choice. + for name in [&d.sink, &d.mic_out].into_iter().flatten() { + if !outputs.contains(name) { + outputs.push(name.clone()); + } + } + let mut cables = cables(&all_mics, &outputs); + // Sensible defaults the first time: the only cable is the Slidr Mic, and + // Steam's streaming speakers (installed with Steam, silent unless + // streaming) are the silent output. + let mut picked = false; + { + let d = &mut s.settings.mixer_devices; + if d.mic_out.is_none() && cables.len() == 1 { + d.mic_out = Some(cables[0].1.clone()); + picked = true; + } + if d.sink.is_none() { + if let Some(steam) = outputs.iter().find(|o| o.to_lowercase().contains("steam streaming speakers")) { + d.sink = Some(steam.clone()); + picked = true; + } + } + } + if picked { + commit(s); + } + let d = &s.settings.mixer_devices; + if let Some(out) = &d.mic_out { + if !cables.iter().any(|(_, o)| o == out) { + cables.push((out.clone(), out.clone())); + } + } + let mics: Vec = all_mics.into_iter().filter(|m| !cables.iter().any(|(c, _)| c == m)).collect(); + let cable_adapter = d.mic_out.as_deref().and_then(adapter); + let sinks: Vec = outputs + .iter() + .filter(|o| cable_adapter.is_none() || adapter(o) != cable_adapter || Some(o.as_str()) == d.sink.as_deref()) + .cloned() + .collect(); + + // No "Not used" here: a silent output is what lets the mix replace + // programs, and there is always one to offer. + ui.set_mixer_spare_names(ModelRc::new(VecModel::from( + sinks.iter().map(|n| SharedString::from(n.as_str())).collect::>(), + ))); + ui.set_mixer_cable_names(names("Not used", cables.iter().map(|(c, _)| c.as_str()))); + ui.set_mixer_sink_idx(d.sink.as_ref().and_then(|v| sinks.iter().position(|n| n == v)).map_or(-1, |i| i as i32)); + ui.set_mixer_cable_idx( + d.mic_out.as_ref().and_then(|o| cables.iter().position(|(_, c)| c == o)).map_or(0, |i| i as i32 + 1), + ); + + let m = &s.preset.mixer; + ui.set_mixer_enabled(m.enabled); + push_channels(ui, m); + + let no_cable = cables.is_empty(); + ui.set_mixer_hint_download(no_cable); + ui.set_mixer_hint( + if no_cable { + "No virtual cable found. Slidr needs one for the Slidr Mic: install VB-CABLE by VB-Audio (donationware, all participations are welcome), restart Windows, then choose \"CABLE Output\" as the microphone in your voice chat app." + } else if m.enabled && d.mic_out.is_none() { + "Choose the Slidr Mic under Settings → Mixer devices so other apps can hear your voice through Slidr." + } else { + "" + } + .into(), + ); + *LISTS.lock() = Lists { mics, sinks, cables }; +} + +/// Selector model: a leading "none" entry, then the names. +fn names<'a>(first: &'a str, list: impl Iterator) -> ModelRc { + ModelRc::new(VecModel::from(std::iter::once(first).chain(list).map(SharedString::from).collect::>())) +} + +/// What a channel source is called on the page: a program (or program +/// category), or a microphone. +fn source_label(p: &str) -> String { + match p.strip_prefix(MIC_PREFIX) { + Some("") => "Default microphone".into(), + Some(mic) => mic.to_string(), + None => crate::app_groups::label_for(p), + } +} + +fn channel_ui(m: &MixerConfig, c: &MixChannel) -> MixChannelUI { + let shown = |t: MixTarget, saved: f32| crate::mixer::shown_level(m, t).unwrap_or(saved) * 100.0; + MixChannelUI { + id: c.id as i32, + name: c.name.as_str().into(), + programs: c.programs.iter().map(|p| source_label(p)).collect::>().join(", ").into(), + mics_only: c.mics_only(), + level: shown(MixTarget::Channel(c.id), c.level), + muted: c.muted, + fx_count: c.fx.active() as i32, + fx_on: c.fx.on, + reroute: c.reroute, + send_on: c.mic_send.is_some(), + send: shown(MixTarget::Send(c.id), c.mic_send.unwrap_or(1.0)), + collapsed: c.collapsed, + } +} + +fn push_channels(ui: &AppWindow, m: &MixerConfig) { + let rows: Vec = m.channels.iter().map(|c| channel_ui(m, c)).collect(); + ui.set_mixer_channels(ModelRc::new(VecModel::from(rows))); +} + +/// Let the page's faders follow the board. Rows are updated in place, and +/// only where a level actually moved, so nothing on the page is rebuilt. +pub fn follow_levels(ui: &AppWindow, s: &Shared) { + let m = &s.preset.mixer; + let model = ui.get_mixer_channels(); + for (i, c) in m.channels.iter().enumerate() { + let Some(row) = model.row_data(i).filter(|r| r.id == c.id as i32) else { return }; + let new = channel_ui(m, c); + if (row.level - new.level).abs() > 0.5 || (row.send - new.send).abs() > 0.5 { + model.set_row_data(i, MixChannelUI { level: new.level, send: new.send, ..row }); + } + } +} + +// ── Effects rack ──────────────────────────────────────────────────────────── + +/// Position of an effect in [`Fx::catalog`] — the UI's `kind`. +fn fx_kind(fx: &Fx) -> i32 { + match fx { + Fx::Gate(_) => 0, + Fx::Eq { .. } => 1, + Fx::Compressor(_) => 2, + Fx::Normalizer(_) => 3, + Fx::Limiter(_) => 4, + } +} + +fn slot_ui(slot: &FxSlot) -> FxSlotUI { + let mut p = [0f32; 5]; + let mut summary = String::new(); + match &slot.fx { + Fx::Gate(g) => p[..2].copy_from_slice(&[g.threshold_db, g.release_ms]), + Fx::Eq { bands } => { + summary = match bands.len() { + 0 => "No bands yet".into(), + 1 => "1 band".into(), + n => format!("{n} bands"), + } + } + Fx::Compressor(c) => p.copy_from_slice(&[c.threshold_db, c.ratio, c.attack_ms, c.release_ms, c.makeup_db]), + Fx::Normalizer(n) => p[..2].copy_from_slice(&[n.target_db, n.max_gain_db]), + Fx::Limiter(l) => p[0] = l.ceiling_db, + } + FxSlotUI { + id: slot.id as i32, + kind: fx_kind(&slot.fx), + name: slot.fx.name().into(), + on: slot.on, + p1: p[0], + p2: p[1], + p3: p[2], + p4: p[3], + p5: p[4], + summary: summary.into(), + } +} + +/// Parameter `n` (1-based, in the rack's row order) of an effect. +fn set_param(fx: &mut Fx, n: i32, v: f32) { + match fx { + Fx::Gate(g) => match n { + 1 => g.threshold_db = v, + 2 => g.release_ms = v, + _ => {} + }, + Fx::Compressor(c) => match n { + 1 => c.threshold_db = v, + 2 => c.ratio = v.max(1.0), + 3 => c.attack_ms = v, + 4 => c.release_ms = v, + 5 => c.makeup_db = v, + _ => {} + }, + Fx::Normalizer(x) => match n { + 1 => x.target_db = v, + 2 => x.max_gain_db = v, + _ => {} + }, + Fx::Limiter(l) => { + if n == 1 { + l.ceiling_db = v.min(0.0); + } + } + Fx::Eq { .. } => {} + } +} + +/// Built-in chains first, then the user's. +fn fx_presets(s: &Shared) -> Vec { + FxPreset::built_in().into_iter().chain(s.settings.fx_presets.iter().cloned()).collect() +} + +/// Built-in EQ curves first, then the user's. +fn eq_presets(s: &Shared) -> Vec { + MixerConfig::default_eq_presets().into_iter().chain(s.settings.eq_presets.iter().cloned()).collect() +} + +/// Names for a preset selector: the user's own marked "(mine)". +fn preset_names(all: Vec, builtin: usize) -> ModelRc { + let names: Vec = + all.into_iter().enumerate().map(|(i, n)| if i < builtin { n } else { format!("{n} (mine)") }).collect(); + self::names("Load preset…", names.iter().map(String::as_str)) +} + +fn push_eq_presets(ui: &AppWindow, s: &Shared) { + let builtin = MixerConfig::default_eq_presets().len(); + ui.set_mixer_eq_preset_names(preset_names(eq_presets(s).into_iter().map(|p| p.name).collect(), builtin)); +} + +fn push_rack(ui: &AppWindow, s: &Shared, fx: &Effects) { + ui.set_mixer_fx_slots(ModelRc::new(VecModel::from(fx.chain.iter().map(slot_ui).collect::>()))); + let builtin = FxPreset::built_in().len(); + ui.set_mixer_fx_preset_names(preset_names(fx_presets(s).into_iter().map(|p| p.name).collect(), builtin)); + let catalog: Vec<&str> = Fx::catalog().iter().map(Fx::name).collect(); + ui.set_mixer_fx_add_names(names("Add effect…", catalog.into_iter())); +} + +/// Change the chain being edited, save, and (for structural changes) redraw +/// the rack. Parameter moves don't redraw: the slider shows its own value. +fn edit_rack(ui: &AppWindow, s: &mut Shared, redraw: bool, edit: impl FnOnce(&mut Effects)) { + let Some(target) = *EDITING_FX.lock() else { return }; + let Some(fx) = effects_mut(&mut s.preset.mixer, target) else { return }; + edit(fx); + let fx = fx.clone(); + commit(s); + if redraw { + push_rack(ui, s, &fx); + } +} + +// ── EQ editor ────────────────────────────────────────────────────────────── + +const EQ_KINDS: [EqBandKind; 5] = + [EqBandKind::Peak, EqBandKind::LowShelf, EqBandKind::HighShelf, EqBandKind::HighPass, EqBandKind::LowPass]; +/// Editor range: ±15 dB, 20 Hz – 20 kHz, Q 0.3 – 10 (frequency and Q on a log scale). +const EQ_RANGE_DB: f32 = 15.0; +const F_MIN: f32 = 20.0; +const F_SPAN: f32 = 1000.0; +const Q_MIN: f32 = 0.3; +const Q_SPAN: f32 = 10.0 / 0.3; + +fn freq_from_pos(p: f32) -> f32 { + F_MIN * F_SPAN.powf(p.clamp(0.0, 1.0)) +} +fn pos_from_freq(f: f32) -> f32 { + ((f / F_MIN).ln() / F_SPAN.ln()).clamp(0.0, 1.0) +} +fn q_from_pos(p: f32) -> f32 { + Q_MIN * Q_SPAN.powf(p.clamp(0.0, 1.0)) +} +fn pos_from_q(q: f32) -> f32 { + ((q / Q_MIN).ln() / Q_SPAN.ln()).clamp(0.0, 1.0) +} +fn has_gain(kind: EqBandKind) -> bool { + !matches!(kind, EqBandKind::HighPass | EqBandKind::LowPass) +} + +fn freq_text(f: f32) -> String { + if f >= 1000.0 { + format!("{:.1} kHz", f / 1000.0) + } else { + format!("{f:.0} Hz") + } +} + +fn band_ui(b: &EqBand) -> EqBandUI { + let gain = if has_gain(b.kind) { b.gain_db } else { 0.0 }; + EqBandUI { + kind: EQ_KINDS.iter().position(|k| *k == b.kind).unwrap_or(0) as i32, + freq_pos: pos_from_freq(b.freq), + gain, + q_pos: pos_from_q(b.q), + freq_text: freq_text(b.freq).into(), + gain_text: if has_gain(b.kind) { format!("{gain:+.1} dB") } else { "—".into() }.into(), + q_text: format!("{:.2}", b.q).into(), + x: pos_from_freq(b.freq), + y: (EQ_RANGE_DB - gain) / (2.0 * EQ_RANGE_DB), + } +} + +/// SVG path of the combined response in the editor's 1000 × 300 box. +fn curve(bands: &[EqBand]) -> String { + let eq = crate::mixer::dsp::Eq::new(bands); + let mut path = String::new(); + for i in 0..=200 { + let x = i as f32 / 200.0; + let db = eq.response_db(freq_from_pos(x)).clamp(-EQ_RANGE_DB - 1.0, EQ_RANGE_DB + 1.0); + let y = 150.0 - db * 150.0 / EQ_RANGE_DB; + path.push_str(&format!("{}{:.1} {:.1} ", if i == 0 { "M " } else { "L " }, x * 1000.0, y)); + } + path +} + +fn effects_mut(m: &mut MixerConfig, target: MixTarget) -> Option<&mut Effects> { + match target { + MixTarget::Voice => Some(&mut m.voice.fx), + MixTarget::Channel(id) | MixTarget::Send(id) => m.channels.iter_mut().find(|c| c.id == id).map(|c| &mut c.fx), + } +} + +/// Fill the editor. `rebuild` replaces the band rows (after add/delete/load, +/// and when a drag ends so sliders pick up the new values); otherwise rows +/// are updated in place, which keeps a handle that is being dragged alive. +fn push_eq(ui: &AppWindow, on: bool, bands: &[EqBand], rebuild: bool) { + let rows: Vec = bands.iter().map(band_ui).collect(); + let model = ui.get_mixer_eq_bands(); + if !rebuild && model.row_count() == rows.len() { + for (i, r) in rows.into_iter().enumerate() { + model.set_row_data(i, r); + } + } else { + ui.set_mixer_eq_bands(ModelRc::new(VecModel::from(rows))); + } + ui.set_mixer_eq_on(on); + ui.set_mixer_eq_curve(curve(bands).into()); +} + +/// Apply `edit` to the EQ slot being edited (its on switch and bands), then +/// refresh the editor and the mixer. `save` is false while a handle is +/// dragged (saved when it's let go). +fn edit_eq(ui: &AppWindow, s: &mut Shared, rebuild: bool, save: bool, edit: impl FnOnce(&mut bool, &mut Vec)) { + let Some((target, id)) = *EDITING_EQ.lock() else { return }; + let Some(slot) = effects_mut(&mut s.preset.mixer, target).and_then(|fx| fx.slot_mut(id)) else { return }; + let Fx::Eq { bands } = &mut slot.fx else { return }; + edit(&mut slot.on, bands); + let (on, bands) = (slot.on, bands.clone()); + push_eq(ui, on, &bands, rebuild); + if save { + commit(s); + } else { + s.push_mixer(); + } +} + +/// Picker rows for `channel`: program categories, then programs playing audio +/// right now, then everything else that runs — plus whatever the channel +/// already holds, so a program that isn't running can still be removed. +fn push_picker(ui: &AppWindow, s: &Shared, channel: u32) { + let Some(c) = s.preset.mixer.channels.iter().find(|c| c.id == channel) else { return }; + let filter = ui.get_mixer_picker_filter().to_lowercase(); + let filter = filter.trim(); + let chosen = |v: &str| c.programs.iter().any(|p| p.eq_ignore_ascii_case(v)); + let mut seen: Vec = Vec::new(); + let mut rows: Vec = Vec::new(); + let mut add = |name: String, value: String, icon: i32, group: bool| { + if seen.iter().any(|v| v.eq_ignore_ascii_case(&value)) { + return; + } + if !filter.is_empty() && !name.to_lowercase().contains(filter) { + return; + } + rows.push(PickerEntry { + selected: chosen(&value), + name: name.into(), + value: value.as_str().into(), + icon_kind: icon, + is_group: group, + }); + seen.push(value); + }; + // Microphones tab: the default one, every real microphone, and whatever + // the channel already holds (unplugged ones stay removable). + if *PICKER_TAB.lock() == 1 { + add(source_label(MIC_PREFIX), MIC_PREFIX.into(), 1, false); + for m in c.mics() { + add(source_label(&format!("{MIC_PREFIX}{m}")), format!("{MIC_PREFIX}{m}"), 1, false); + } + for m in &LISTS.lock().mics { + add(m.clone(), format!("{MIC_PREFIX}{m}"), 1, false); + } + rows.sort_by_key(|r| !r.selected); + ui.set_mixer_picker_options(ModelRc::new(VecModel::from(rows))); + return; + } + for g in crate::app_groups::groups() { + add(g.name.clone(), crate::app_groups::make_ref(&g.id), g.icon, true); + } + for p in c.programs.iter().filter(|p| !p.starts_with(MIC_PREFIX)) { + add(crate::app_groups::label_for(p), p.clone(), 0, crate::app_groups::parse_ref(p).is_some()); + } + let mut playing = s.audio.list_processes(); + playing.sort_by_key(|p| p.to_lowercase()); + for p in playing { + add(p.clone(), p, 5, false); + } + let mut running = crate::glue::merged_processes(s.audio.as_ref()); + running.sort_by_key(|p| p.to_lowercase()); + for p in running { + add(p.clone(), p, 0, false); + } + // Selected rows first, so the current choice is visible without scrolling. + rows.sort_by_key(|r| !r.selected); + ui.set_mixer_picker_options(ModelRc::new(VecModel::from(rows))); +} + +fn commit(s: &mut Shared) { + s.save_preset(); + if let Err(e) = crate::storage::save_settings(&s.settings) { + log::warn!("could not save settings: {e:#}"); + } + s.push_mixer(); +} + +pub fn wire(ui: &AppWindow, shared: Arc>) { + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_page_changed(move |page| { + let Some(ui) = weak.upgrade() else { return }; + match page { + 4 => push(&ui, &mut shared.lock()), + // Settings holds the mixer's devices, and the presets saved in the mixer. + 3 => { + let mut s = shared.lock(); + push(&ui, &mut s); + crate::presets_ui::push(&ui, &s.settings); + } + _ => {} + } + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_changed(move || { + let Some(ui) = weak.upgrade() else { return }; + let mut s = shared.lock(); + let lists = LISTS.lock(); + let d = &mut s.settings.mixer_devices; + // The mix follows the default output; an old pick would only confuse. + d.monitor = None; + if let Some(sink) = usize::try_from(ui.get_mixer_sink_idx()).ok().and_then(|i| lists.sinks.get(i)) { + d.sink = Some(sink.clone()); + } + d.mic_out = usize::try_from(ui.get_mixer_cable_idx() - 1) + .ok() + .and_then(|i| lists.cables.get(i)) + .map(|(_, out)| out.clone()); + // The silent output must never be the Slidr Mic's own cable, or + // the rerouted programs would end up in Discord. + if d.sink.as_deref().and_then(adapter).is_some_and(|a| Some(a) == d.mic_out.as_deref().and_then(adapter)) { + d.sink = None; + } + drop(lists); + s.preset.mixer.enabled = ui.get_mixer_enabled(); + commit(&mut s); + // Lists depend on each other (the silent output excludes the + // Slidr Mic's cable) and the hint on the switch. + push(&ui, &mut s); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_move_channel(move |id, to| { + let mut s = shared.lock(); + let channels = &mut s.preset.mixer.channels; + let Some(from) = channels.iter().position(|c| c.id as i32 == id) else { return }; + // `to` is an insertion slot in the list as it was, before this channel left it. + let to = (to.max(0) as usize).min(channels.len()); + let to = if to > from { to - 1 } else { to }; + if to == from { + return; + } + let c = channels.remove(from); + channels.insert(to, c); + commit(&mut s); + if let Some(ui) = weak.upgrade() { + push_channels(&ui, &s.preset.mixer); + } + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_channel_set(move |id, field, num, text| { + let mut s = shared.lock(); + let Some(c) = s.preset.mixer.channels.iter_mut().find(|c| c.id as i32 == id) else { return }; + match field.as_str() { + "collapsed" => { + c.collapsed = num > 0.5; + commit(&mut s); + if let Some(ui) = weak.upgrade() { + push_channels(&ui, &s.preset.mixer); + } + return; + } + "name" => c.name = text.to_string(), + "level" => c.level = (num / 100.0).clamp(0.0, 1.0), + "muted" => c.muted = num > 0.5, + "reroute" => c.reroute = num > 0.5, + "fx-on" => c.fx.on = num > 0.5, + "send-on" => c.mic_send = (num > 0.5).then_some(c.mic_send.unwrap_or(1.0)), + "send" => c.mic_send = Some((num / 100.0).clamp(0.0, 1.0)), + other => log::warn!("mixer page: unknown channel field {other:?}"), + } + commit(&mut s); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_add_channel(move || { + let mut s = shared.lock(); + let m = &mut s.preset.mixer; + let id = m.channels.iter().map(|c| c.id).max().unwrap_or(0) + 1; + m.channels.push(MixChannel { + id, + name: format!("Channel {id}"), + programs: Vec::new(), + level: 1.0, + muted: false, + fx: Effects::default(), + reroute: true, + mic_send: None, + collapsed: false, + }); + commit(&mut s); + if let Some(ui) = weak.upgrade() { + push_channels(&ui, &s.preset.mixer); + } + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_choose_programs(move |id| { + let Some(ui) = weak.upgrade() else { return }; + let s = shared.lock(); + let Some(c) = s.preset.mixer.channels.iter().find(|c| c.id as i32 == id) else { return }; + *PICKING.lock() = Some(c.id); + // Opens on microphones for a channel that only has those. + let tab = i32::from(c.mics_only()); + *PICKER_TAB.lock() = tab; + ui.set_mixer_picker_tab_index(tab); + ui.set_mixer_picker_title(format!("Sources for {}", c.name).into()); + ui.set_mixer_picker_filter("".into()); + push_picker(&ui, &s, c.id); + ui.set_mixer_picker_open(true); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_toggle_program(move |value| { + let Some(ui) = weak.upgrade() else { return }; + let Some(id) = *PICKING.lock() else { return }; + let mut s = shared.lock(); + let Some(c) = s.preset.mixer.channels.iter_mut().find(|c| c.id == id) else { return }; + match c.programs.iter().position(|p| p.eq_ignore_ascii_case(&value)) { + Some(i) => { + c.programs.remove(i); + } + None => c.programs.push(value.to_string()), + } + commit(&mut s); + push_picker(&ui, &s, id); + push_channels(&ui, &s.preset.mixer); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_picker_filter_changed(move |_| { + let Some(ui) = weak.upgrade() else { return }; + let Some(id) = *PICKING.lock() else { return }; + push_picker(&ui, &shared.lock(), id); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_picker_tab(move |tab| { + let Some(ui) = weak.upgrade() else { return }; + let Some(id) = *PICKING.lock() else { return }; + *PICKER_TAB.lock() = tab; + push_picker(&ui, &shared.lock(), id); + }); + } + ui.on_mixer_picker_closed(|| *PICKING.lock() = None); + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_edit_fx(move |which| { + let Some(ui) = weak.upgrade() else { return }; + let Some(target) = MixTarget::from_value(&which) else { return }; + let mut s = shared.lock(); + let title = format!("Effects – {}", s.preset.mixer.label(target)); + let Some(fx) = effects_mut(&mut s.preset.mixer, target).cloned() else { return }; + *EDITING_FX.lock() = Some(target); + ui.set_mixer_fx_title(title.into()); + ui.set_mixer_fx_preset_name("".into()); + push_rack(&ui, &s, &fx); + ui.set_mixer_fx_open(true); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_param(move |id, n, v| { + let Some(ui) = weak.upgrade() else { return }; + edit_rack(&ui, &mut shared.lock(), false, |fx| { + if let Some(slot) = fx.slot_mut(id as u32) { + set_param(&mut slot.fx, n, v); + } + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_toggle(move |id, on| { + let Some(ui) = weak.upgrade() else { return }; + edit_rack(&ui, &mut shared.lock(), true, |fx| { + if let Some(slot) = fx.slot_mut(id as u32) { + slot.on = on; + } + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_move(move |id, dir| { + let Some(ui) = weak.upgrade() else { return }; + edit_rack(&ui, &mut shared.lock(), true, |fx| { + let Some(i) = fx.chain.iter().position(|s| s.id == id as u32) else { return }; + let j = i as i32 + dir; + if j >= 0 && (j as usize) < fx.chain.len() { + fx.chain.swap(i, j as usize); + } + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_remove(move |id| { + let Some(ui) = weak.upgrade() else { return }; + edit_rack(&ui, &mut shared.lock(), true, |fx| fx.chain.retain(|s| s.id != id as u32)); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_add(move |kind| { + let Some(ui) = weak.upgrade() else { return }; + let Some(new) = Fx::catalog().into_iter().nth(kind as usize) else { return }; + edit_rack(&ui, &mut shared.lock(), true, |fx| { + let id = fx.next_id(); + fx.chain.push(FxSlot { id, on: true, fx: new }); + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_load_preset(move |i| { + let Some(ui) = weak.upgrade() else { return }; + let mut s = shared.lock(); + let Some(preset) = fx_presets(&s).into_iter().nth(i as usize) else { return }; + edit_rack(&ui, &mut s, true, |fx| fx.chain = preset.chain); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_save_preset(move |name| { + let Some(ui) = weak.upgrade() else { return }; + let name = name.trim().to_string(); + let Some(target) = *EDITING_FX.lock() else { return }; + let mut s = shared.lock(); + let Some(fx) = effects_mut(&mut s.preset.mixer, target).cloned() else { return }; + if name.is_empty() { + return; + } + // Same name again replaces the old one. + let presets = &mut s.settings.fx_presets; + presets.retain(|p| p.name != name); + presets.push(FxPreset { name: name.clone(), chain: fx.chain.clone() }); + commit(&mut s); + push_rack(&ui, &s, &fx); + crate::glue::toast(&ui, &format!("Saved preset \"{name}\"")); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_closed(move || { + *EDITING_FX.lock() = None; + // The strip buttons show how many effects are on. + if let Some(ui) = weak.upgrade() { + let s = shared.lock(); + push_channels(&ui, &s.preset.mixer); + } + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_fx_edit_eq(move |id| { + let Some(ui) = weak.upgrade() else { return }; + let Some(target) = *EDITING_FX.lock() else { return }; + let mut s = shared.lock(); + push_eq_presets(&ui, &s); + ui.set_mixer_eq_preset_name("".into()); + let m = &mut s.preset.mixer; + ui.set_mixer_eq_title(format!("EQ – {}", m.label(target)).into()); + let Some(slot) = effects_mut(m, target).and_then(|fx| fx.slot_mut(id as u32)) else { return }; + let Fx::Eq { bands } = &slot.fx else { return }; + *EDITING_EQ.lock() = Some((target, slot.id)); + push_eq(&ui, slot.on, &bands.clone(), true); + ui.set_mixer_eq_open(true); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_toggled(move |on| { + let Some(ui) = weak.upgrade() else { return }; + edit_eq(&ui, &mut shared.lock(), false, true, |slot_on, _| *slot_on = on); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_set(move |i, field, v| { + let Some(ui) = weak.upgrade() else { return }; + edit_eq(&ui, &mut shared.lock(), false, true, |on, bands| { + let Some(b) = bands.get_mut(i as usize) else { return }; + match field.as_str() { + "kind" => b.kind = EQ_KINDS.get(v as usize).copied().unwrap_or(EqBandKind::Peak), + "freq" => b.freq = freq_from_pos(v), + "gain" => b.gain_db = v.clamp(-EQ_RANGE_DB, EQ_RANGE_DB), + "q" => b.q = q_from_pos(v), + other => log::warn!("EQ editor: unknown field {other:?}"), + } + // Editing a band means wanting to hear it. + *on = true; + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_drag(move |i, x, y| { + let Some(ui) = weak.upgrade() else { return }; + edit_eq(&ui, &mut shared.lock(), false, false, |on, bands| { + let Some(b) = bands.get_mut(i as usize) else { return }; + b.freq = freq_from_pos(x); + if has_gain(b.kind) { + b.gain_db = ((0.5 - y.clamp(0.0, 1.0)) * 2.0 * EQ_RANGE_DB).clamp(-EQ_RANGE_DB, EQ_RANGE_DB); + } + *on = true; + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_drag_end(move || { + let Some(ui) = weak.upgrade() else { return }; + edit_eq(&ui, &mut shared.lock(), true, true, |_, _| {}); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_add(move || { + let Some(ui) = weak.upgrade() else { return }; + edit_eq(&ui, &mut shared.lock(), true, true, |on, bands| { + bands.push(EqBand { kind: EqBandKind::Peak, freq: 1000.0, gain_db: 0.0, q: 1.0 }); + *on = true; + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_delete(move |i| { + let Some(ui) = weak.upgrade() else { return }; + edit_eq(&ui, &mut shared.lock(), true, true, |_, bands| { + if (i as usize) < bands.len() { + bands.remove(i as usize); + } + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_load_preset(move |i| { + let Some(ui) = weak.upgrade() else { return }; + let mut s = shared.lock(); + let Some(preset) = eq_presets(&s).into_iter().nth(i as usize).map(|p| p.bands) else { return }; + edit_eq(&ui, &mut s, true, true, |on, bands| { + *bands = preset; + *on = true; + }); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_save_preset(move |name| { + let Some(ui) = weak.upgrade() else { return }; + let name = name.trim().to_string(); + let Some((target, id)) = *EDITING_EQ.lock() else { return }; + if name.is_empty() { + return; + } + let mut s = shared.lock(); + let Some(slot) = effects_mut(&mut s.preset.mixer, target).and_then(|fx| fx.slot_mut(id)) else { return }; + let Fx::Eq { bands } = &slot.fx else { return }; + let bands = bands.clone(); + // Same name again replaces the old one. + let presets = &mut s.settings.eq_presets; + presets.retain(|p| p.name != name); + let id = presets.iter().map(|p| p.id).max().unwrap_or(1000) + 1; + presets.push(EqPreset { id, name: name.clone(), bands }); + commit(&mut s); + push_eq_presets(&ui, &s); + crate::glue::toast(&ui, &format!("Saved preset \"{name}\"")); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_mixer_eq_closed(move || { + *EDITING_EQ.lock() = None; + // The rack shows the EQ's switch and band count. + let Some(ui) = weak.upgrade() else { return }; + let Some(target) = *EDITING_FX.lock() else { return }; + let mut s = shared.lock(); + if let Some(fx) = effects_mut(&mut s.preset.mixer, target).cloned() { + push_rack(&ui, &s, &fx); + } + }); + } + ui.on_mixer_open_download(|| { + if let Err(e) = open::that(CABLE_DOWNLOAD) { + log::warn!("could not open {CABLE_DOWNLOAD}: {e}"); + } + }); + { + let weak = ui.as_weak(); + ui.on_mixer_delete_channel(move |id| { + let mut s = shared.lock(); + s.preset.mixer.channels.retain(|c| c.id as i32 != id); + commit(&mut s); + if let Some(ui) = weak.upgrade() { + push_channels(&ui, &s.preset.mixer); + } + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn v(names: &[&str]) -> Vec { + names.iter().map(|s| s.to_string()).collect() + } + + #[test] + fn cable_pairs_its_recording_and_playback_ends() { + let mics = v(&["Mikrofon (Wireless Microphone RX)", "CABLE Output (VB-Audio Virtual Cable)"]); + let outs = v(&[ + "CABLE In 16ch (VB-Audio Virtual Cable)", + "CABLE Input (VB-Audio Virtual Cable)", + "LG ULTRAGEAR (NVIDIA High Definition Audio)", + ]); + assert_eq!( + cables(&mics, &outs), + vec![("CABLE Output (VB-Audio Virtual Cable)".to_string(), "CABLE Input (VB-Audio Virtual Cable)".to_string())] + ); + } + + #[test] + fn real_devices_are_not_cables() { + let mics = v(&["Mikrofon (USB Audio CODEC )"]); + let outs = v(&["Lautsprecher (USB Audio CODEC )"]); + assert!(cables(&mics, &outs).is_empty()); + assert_eq!(adapter("Mikrofon (2- USB Audio CODEC )"), Some("2- USB Audio CODEC")); + assert_eq!(adapter("no parens"), None); + } + + #[test] + fn eq_scales_round_trip() { + for f in [20.0, 100.0, 1000.0, 12345.0, 20000.0] { + assert!((freq_from_pos(pos_from_freq(f)) - f).abs() / f < 1e-3); + } + for q in [0.3, 0.707, 1.0, 10.0] { + assert!((q_from_pos(pos_from_q(q)) - q).abs() / q < 1e-3); + } + } + + #[test] + fn handle_sits_on_its_band() { + let b = band_ui(&EqBand { kind: EqBandKind::Peak, freq: 1000.0, gain_db: 7.5, q: 1.0 }); + assert!((b.y - 0.25).abs() < 1e-6); + let hp = band_ui(&EqBand { kind: EqBandKind::HighPass, freq: 80.0, gain_db: 9.0, q: 0.7 }); + assert_eq!(hp.y, 0.5, "pass filters have no gain"); + } + + #[test] + fn curve_covers_the_box() { + let c = curve(&[]); + assert!(c.starts_with("M 0.0 150.0")); + assert!(c.trim_end().ends_with("1000.0 150.0")); + } +} diff --git a/src/model.rs b/src/model.rs index c7c0976..e6a69c1 100644 --- a/src/model.rs +++ b/src/model.rs @@ -22,12 +22,17 @@ pub struct AudioStream { /// percent substituted for `{value}`) instead of controlling audio volume. #[serde(default, skip_serializing_if = "Option::is_none")] pub api: Option, + /// When set, the slider drives a level inside Slidr's own mixer. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mixer: Option, } impl AudioStream { pub fn label(&self) -> String { if let Some(api) = &self.api { api.label() + } else if let Some(m) = &self.mixer { + format!("mixer: {}", m.key()) } else if let Some(p) = &self.process { crate::app_groups::label_for(p) } else if let Some(m) = &self.mic_name { @@ -137,6 +142,8 @@ pub enum ActionKind { KeyPress, CycleOutput, ApiCall, + /// Toggle mute of a mixer strip (`ButtonAction::mixer`). + MuteMixer, } impl ActionKind { @@ -150,6 +157,7 @@ impl ActionKind { Self::KeyPress => "Simulate key", Self::CycleOutput => "Cycle output device", Self::ApiCall => "API call", + Self::MuteMixer => "Mute mixer channel", } } } @@ -172,6 +180,9 @@ pub struct ButtonAction { /// HTTP request config, used when `kind == ApiCall`. #[serde(default, skip_serializing_if = "Option::is_none")] pub api: Option, + /// Mixer strip, used when `kind == MuteMixer`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mixer: Option, } // ── LED control (experimental: buttons 5/6/7 → board LEDs A0/A1/D13) ───────── @@ -503,6 +514,49 @@ pub struct Settings { /// Last loaded profile name (sticky across launches). #[serde(default)] pub active_preset: String, + + /// Devices the software mixer plays to and records from. Machine-specific, + /// so global rather than per profile. + #[serde(default)] + pub mixer_devices: MixerDevices, + + /// Effect chains saved by the user, offered on every profile's strips. + #[serde(default)] + pub fx_presets: Vec, + + /// Short sounds when mixer things are switched (see `mixer::sfx`). + #[serde(default = "default_true")] + pub mixer_sounds: bool, + /// Their volume, 0..1. + #[serde(default = "default_sound_volume")] + pub mixer_sound_volume: f32, + /// Which set they come from (`crate::mixer::SOUND_SETS`). + #[serde(default = "default_sound_set")] + pub mixer_sound_set: String, + /// EQ curves saved by the user, offered on every profile's strips. + #[serde(default)] + pub eq_presets: Vec, + + /// Show the Sliders / Buttons lists as a grid of cards. + #[serde(default)] + pub grid_sliders: bool, + #[serde(default)] + pub grid_buttons: bool, + + /// Sidebar tabs in display order, by name (see `crate::sidebar`). + #[serde(default)] + pub nav_order: Vec, + /// Tabs hidden from the sidebar. Settings can't be hidden. + #[serde(default)] + pub nav_hidden: Vec, +} + +fn default_sound_volume() -> f32 { + 0.8 +} + +fn default_sound_set() -> String { + crate::mixer::DEFAULT_SOUND_SET.into() } impl Default for Settings { @@ -517,6 +571,16 @@ impl Default for Settings { discord_client_secret: String::new(), update_check: true, active_preset: "Default".into(), + mixer_devices: MixerDevices::default(), + fx_presets: Vec::new(), + mixer_sounds: true, + mixer_sound_volume: default_sound_volume(), + mixer_sound_set: default_sound_set(), + eq_presets: Vec::new(), + grid_sliders: false, + grid_buttons: false, + nav_order: Vec::new(), + nav_hidden: Vec::new(), } } } @@ -599,6 +663,9 @@ pub struct Preset { /// See [`crate::version`] for why the comparison only matters downwards. #[serde(default)] pub app_version: String, + /// Software mixer channels of this profile (see `docs/mixer-plan.md`). + #[serde(default)] + pub mixer: MixerConfig, } impl Preset { @@ -629,6 +696,49 @@ pub fn next_id u32>(existing: &[T], id_of: F) -> u32 { mod tests { use super::*; + #[test] + fn voice_strip_becomes_a_microphone_channel_once() { + let mut p: Preset = serde_json::from_str(r#"{"id":1,"name":"P"}"#).unwrap(); + p.mixer.channels.push(MixChannel { + id: 4, + name: "Music".into(), + programs: vec!["spotify".into()], + level: 1.0, + muted: false, + fx: Effects::default(), + reroute: true, + mic_send: None, + collapsed: false, + }); + p.mixer.voice.level = 0.25; + p.slider_categories.push(SliderCategory { + id: 1, + name: "Mic".into(), + streams: vec![AudioStream { mixer: Some(MixTarget::Voice), ..Default::default() }], + collapsed: false, + }); + assert!(p.move_voice_to_channel(Some("Mikrofon (RX)"))); + let mic = &p.mixer.channels[0]; + assert_eq!((mic.id, mic.mics().collect::>()), (5, vec!["Mikrofon (RX)"])); + assert_eq!((mic.level, mic.mic_send), (0.0, Some(0.25)), "not heard, sent as before"); + assert!(mic.mics_only() && !p.mixer.channels[1].mics_only()); + assert_eq!(p.slider_categories[0].streams[0].mixer, Some(MixTarget::Send(5))); + assert!(!p.move_voice_to_channel(None), "only once"); + assert_eq!(p.mixer.channels.len(), 2); + } + + #[test] + fn preset_files_say_what_they_hold() { + let eq = PresetFile::Eq(MixerConfig::default_eq_presets().remove(0)); + let json = serde_json::to_string(&eq).unwrap(); + assert!(json.contains("\"slidr_preset\":\"eq\"")); + assert_eq!(serde_json::from_str::(&json).unwrap(), eq); + let fx = PresetFile::Effects(FxPreset::built_in().remove(0)); + let back: PresetFile = serde_json::from_str(&serde_json::to_string(&fx).unwrap()).unwrap(); + assert_eq!(back, fx); + assert!(serde_json::from_str::("{\"name\":\"x\"}").is_err()); + } + #[test] fn next_id_grows() { let v: Vec = vec![ @@ -665,4 +775,579 @@ mod tests { ); assert_eq!(AudioStream::default().label(), "default output"); } + + #[test] + fn effects_from_before_chains_become_a_chain() { + let old = r#"{"eq_on": true, "eq": [{"kind": "peak", "freq": 1000.0, "gain_db": 3.0}], + "gate": {"on": true, "threshold_db": -50.0, "release_ms": 150.0}, + "comp": {"on": false, "threshold_db": -20.0}, + "limit": {"on": true, "ceiling_db": -2.0}}"#; + let fx: Effects = serde_json::from_str(old).unwrap(); + let kinds: Vec<&str> = fx.chain.iter().map(|s| s.fx.name()).collect(); + assert_eq!(kinds, ["Noise gate", "EQ", "Limiter"], "only what was on, in the old order"); + assert!(matches!(fx.chain[0].fx, Fx::Gate(Gate { threshold_db, .. }) if threshold_db == -50.0)); + assert!(matches!(fx.chain[2].fx, Fx::Limiter(Limiter { ceiling_db }) if ceiling_db == -2.0)); + } + + #[test] + fn chain_round_trips() { + let fx = Effects { on: false, chain: FxPreset::built_in().remove(1).chain }; + let json = serde_json::to_string(&fx).unwrap(); + assert!(json.contains("\"type\":\"compressor\""), "{json}"); + assert_eq!(serde_json::from_str::(&json).unwrap(), fx); + } +} + +// ── Software mixer (see docs/mixer-plan.md) ───────────────────────────────── + +/// One strip of the mixer a slider or button can address. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum MixTarget { + /// The real microphone's strip (what goes into the Slidr Mic). + Voice, + /// A program channel's level (by `MixChannel::id`). + Channel(u32), + /// How much of a program channel goes into the Slidr Mic. + Send(u32), +} + +impl MixTarget { + /// Stable short form, also used as a map key. + pub fn key(&self) -> String { + match self { + Self::Voice => "voice".into(), + Self::Channel(id) => format!("channel {id}"), + Self::Send(id) => format!("send {id}"), + } + } + + /// Compact text form for the UI's string-valued pickers: `voice`, + /// `channel:`, `send:`. + pub fn to_value(&self) -> String { + match self { + Self::Voice => "voice".into(), + Self::Channel(id) => format!("channel:{id}"), + Self::Send(id) => format!("send:{id}"), + } + } + + pub fn from_value(v: &str) -> Option { + if v == "voice" { + return Some(Self::Voice); + } + let (kind, id) = v.split_once(':')?; + let id = id.parse().ok()?; + match kind { + "channel" => Some(Self::Channel(id)), + "send" => Some(Self::Send(id)), + _ => None, + } + } +} + +impl MixerConfig { + /// What a strip is called in lists ("Music", "Music → Slidr Mic", …). + pub fn label(&self, target: MixTarget) -> String { + let name = |id: u32| { + self.channels + .iter() + .find(|c| c.id == id) + .map(|c| c.name.clone()) + .unwrap_or_else(|| format!("Channel {id}")) + }; + match target { + MixTarget::Voice => "Voice (microphone)".into(), + MixTarget::Channel(id) => name(id), + MixTarget::Send(id) => format!("{} → Slidr Mic", name(id)), + } + } + + /// A strip's saved fader position. + pub fn level(&self, t: MixTarget) -> Option { + match t { + MixTarget::Voice => Some(self.voice.level), + MixTarget::Channel(id) => self.channels.iter().find(|c| c.id == id).map(|c| c.level), + MixTarget::Send(id) => self.channels.iter().find(|c| c.id == id).and_then(|c| c.mic_send), + } + } + + /// Built-in EQ starting points, offered by the EQ editor. + pub fn default_eq_presets() -> Vec { + use EqBandKind::*; + let b = |kind, freq, gain_db, q| EqBand { kind, freq, gain_db, q }; + let q = default_q(); + vec![ + EqPreset { + id: 1, + name: "Voice clarity".into(), + bands: vec![b(HighPass, 80.0, 0.0, q), b(LowShelf, 200.0, -2.0, q), b(Peak, 3000.0, 3.0, 1.0), b(HighShelf, 10000.0, 2.0, q)], + }, + EqPreset { + id: 2, + name: "Warm voice".into(), + bands: vec![b(HighPass, 70.0, 0.0, q), b(Peak, 250.0, 2.0, 0.8), b(HighShelf, 8000.0, -2.0, q)], + }, + EqPreset { id: 3, name: "Bass boost".into(), bands: vec![b(LowShelf, 120.0, 6.0, q)] }, + EqPreset { id: 4, name: "Treble boost".into(), bands: vec![b(HighShelf, 6000.0, 5.0, q)] }, + EqPreset { + id: 5, + name: "Loudness".into(), + bands: vec![b(LowShelf, 100.0, 4.0, q), b(HighShelf, 8000.0, 3.0, q)], + }, + ] + } +} + +/// Mixer layout of a profile. +#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] +pub struct MixerConfig { + /// Off by default: the mixer reroutes programs, which nobody should get by + /// surprise after an update. + #[serde(default)] + pub enabled: bool, + #[serde(default)] + pub channels: Vec, + /// The old fixed microphone strip; see [`Preset::move_voice_to_channel`]. + #[serde(default)] + pub voice: VoiceStrip, + /// Set once the voice strip became a channel. + #[serde(default)] + pub voice_moved: bool, + #[serde(default)] + pub eq_presets: Vec, +} + +/// A channel source that is a microphone, not a program: `@mic:`, +/// an empty device meaning the system default. +pub const MIC_PREFIX: &str = "@mic:"; + +impl MixChannel { + /// The microphones among the sources ("" = system default). + pub fn mics(&self) -> impl Iterator { + self.programs.iter().filter_map(|p| p.strip_prefix(MIC_PREFIX)) + } + + /// Only microphones, no programs: nothing plays these anywhere else, so + /// the channel's fader is simply how loud you hear them. + pub fn mics_only(&self) -> bool { + !self.programs.is_empty() && self.programs.iter().all(|p| p.starts_with(MIC_PREFIX)) + } +} + +impl Preset { + /// Older layouts had a fixed voice strip fed by the microphone chosen on + /// the page. The microphone is now a source in a channel like any + /// program: turn the strip into a "Microphone" channel (once) and point + /// board controls that drove it at that channel. `true` if it changed. + pub fn move_voice_to_channel(&mut self, mic: Option<&str>) -> bool { + let m = &mut self.mixer; + if m.voice_moved { + return false; + } + m.voice_moved = true; + let v = std::mem::take(&mut m.voice); + let id = m.channels.iter().map(|c| c.id).max().unwrap_or(0) + 1; + m.channels.insert(0, MixChannel { + id, + name: "Microphone".into(), + programs: vec![format!("{MIC_PREFIX}{}", mic.unwrap_or(""))], + // The strip's level was what went into the Slidr Mic; you heard + // yourself only with "hear yourself" on. + level: if v.to_monitor { v.level } else { 0.0 }, + muted: v.muted, + fx: v.fx, + reroute: false, + mic_send: Some(v.level), + collapsed: false, + }); + for c in &mut self.slider_categories { + for s in &mut c.streams { + if s.mixer == Some(MixTarget::Voice) { + s.mixer = Some(MixTarget::Send(id)); + } + } + } + for c in &mut self.button_categories { + for a in &mut c.actions { + if a.mixer == Some(MixTarget::Voice) { + a.mixer = Some(MixTarget::Channel(id)); + } + } + } + true + } +} + +/// A group of programs mixed as one. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct MixChannel { + pub id: u32, + pub name: String, + /// Process names, paths or `@group:` refs, as for sliders. + #[serde(default)] + pub programs: Vec, + /// Fader position in [0, 1]. + #[serde(default = "default_level")] + pub level: f32, + #[serde(default)] + pub muted: bool, + #[serde(default)] + pub fx: Effects, + /// Move the programs to the sink so only Slidr's mix is heard. Off = the + /// program keeps playing normally and is only copied into the Slidr Mic. + #[serde(default = "default_true")] + pub reroute: bool, + /// Share of this channel in the Slidr Mic, [0, 1]; `None` = not sent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mic_send: Option, + /// Folded on the Mixer page (only the header and level show). + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub collapsed: bool, +} + +/// The real microphone's strip. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct VoiceStrip { + #[serde(default = "default_level")] + pub level: f32, + #[serde(default)] + pub muted: bool, + #[serde(default)] + pub fx: Effects, + /// Also play the voice on the monitor (hear yourself). + #[serde(default)] + pub to_monitor: bool, +} + +impl Default for VoiceStrip { + fn default() -> Self { + Self { level: default_level(), muted: false, fx: Effects::default(), to_monitor: false } + } +} + +/// A strip's effect chain, processed top to bottom. The same effect may +/// appear more than once (two compressors, an EQ on each side of one). +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(from = "EffectsFile")] +pub struct Effects { + /// Master switch: off bypasses the whole chain, keeping every setting. + #[serde(default = "default_true")] + pub on: bool, + #[serde(default)] + pub chain: Vec, +} + +impl Default for Effects { + fn default() -> Self { + Self { on: true, chain: Vec::new() } + } +} + +impl Effects { + /// How many effects are on (shown on the strip's button). + pub fn active(&self) -> usize { + self.chain.iter().filter(|s| s.on).count() + } + + /// A free slot id within this chain. + pub fn next_id(&self) -> u32 { + self.chain.iter().map(|s| s.id).max().unwrap_or(0) + 1 + } + + pub fn slot_mut(&mut self, id: u32) -> Option<&mut FxSlot> { + self.chain.iter_mut().find(|s| s.id == id) + } +} + +/// One effect in a chain. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct FxSlot { + /// Unique within its chain; lets the running effect keep its state when + /// the chain is reordered or edited. + pub id: u32, + #[serde(default = "default_true")] + pub on: bool, + #[serde(flatten)] + pub fx: Fx, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum Fx { + Gate(Gate), + Eq { bands: Vec }, + Compressor(Compressor), + Normalizer(Normalizer), + Limiter(Limiter), +} + +impl Fx { + /// Every effect the "Add effect" list offers, with default settings. + pub fn catalog() -> Vec { + vec![ + Fx::Gate(Gate::default()), + Fx::Eq { bands: Vec::new() }, + Fx::Compressor(Compressor::default()), + Fx::Normalizer(Normalizer::default()), + Fx::Limiter(Limiter::default()), + ] + } + + pub fn name(&self) -> &'static str { + match self { + Fx::Gate(_) => "Noise gate", + Fx::Eq { .. } => "EQ", + Fx::Compressor(_) => "Compressor", + Fx::Normalizer(_) => "Normalizer", + Fx::Limiter(_) => "Limiter", + } + } +} + +/// Silences the strip while it stays below `threshold_db` (room noise between words). +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] +#[serde(default)] +pub struct Gate { + pub threshold_db: f32, + pub release_ms: f32, +} + +impl Default for Gate { + fn default() -> Self { + Self { threshold_db: -45.0, release_ms: 150.0 } + } +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] +#[serde(default)] +pub struct Compressor { + pub threshold_db: f32, + pub ratio: f32, + pub attack_ms: f32, + pub release_ms: f32, + pub makeup_db: f32, +} + +impl Default for Compressor { + fn default() -> Self { + Self { threshold_db: -20.0, ratio: 3.0, attack_ms: 10.0, release_ms: 150.0, makeup_db: 0.0 } + } +} + +/// Slowly steers the average loudness to `target_db` (RMS), by at most `max_gain_db`. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] +#[serde(default)] +pub struct Normalizer { + pub target_db: f32, + pub max_gain_db: f32, +} + +impl Default for Normalizer { + fn default() -> Self { + Self { target_db: -18.0, max_gain_db: 12.0 } + } +} + +/// Keeps peaks under `ceiling_db`. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] +#[serde(default)] +pub struct Limiter { + pub ceiling_db: f32, +} + +impl Default for Limiter { + fn default() -> Self { + Self { ceiling_db: -1.0 } + } +} + +/// On-disk form of [`Effects`]: the chain, or — in profiles written before +/// chains existed — one fixed slot per effect, which is turned into a chain. +#[derive(Deserialize)] +struct EffectsFile { + #[serde(default = "default_true")] + on: bool, + #[serde(default)] + chain: Option>, + #[serde(default)] + eq_on: bool, + #[serde(default)] + eq: Vec, + #[serde(default)] + gate: Option>, + #[serde(default)] + comp: Option>, + #[serde(default)] + norm: Option>, + #[serde(default)] + limit: Option>, +} + +#[derive(Deserialize)] +struct OldSlot { + #[serde(default)] + on: bool, + #[serde(flatten)] + params: T, +} + +impl From for Effects { + fn from(f: EffectsFile) -> Self { + if let Some(chain) = f.chain { + return Self { on: f.on, chain }; + } + // Old fixed order: gate → EQ → compressor → normalizer → limiter. + // Only what was in use carries over. + let mut chain = Vec::new(); + let mut push = |on: bool, fx: Fx| chain.push(FxSlot { id: chain.len() as u32 + 1, on, fx }); + if let Some(g) = f.gate.filter(|g| g.on) { + push(true, Fx::Gate(g.params)); + } + if f.eq_on || !f.eq.is_empty() { + push(f.eq_on, Fx::Eq { bands: f.eq }); + } + if let Some(c) = f.comp.filter(|c| c.on) { + push(true, Fx::Compressor(c.params)); + } + if let Some(n) = f.norm.filter(|n| n.on) { + push(true, Fx::Normalizer(n.params)); + } + if let Some(l) = f.limit.filter(|l| l.on) { + push(true, Fx::Limiter(l.params)); + } + Self { on: true, chain } + } +} + +/// A saved effect chain. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct FxPreset { + pub name: String, + pub chain: Vec, +} + +impl FxPreset { + /// Chains that ship with Slidr. + pub fn built_in() -> Vec { + use EqBandKind::*; + let q = default_q(); + let b = |kind, freq, gain_db, q| EqBand { kind, freq, gain_db, q }; + let preset = |name: &str, fx: Vec| FxPreset { + name: name.into(), + chain: fx.into_iter().enumerate().map(|(i, fx)| FxSlot { id: i as u32 + 1, on: true, fx }).collect(), + }; + vec![ + preset("Voice – clean", vec![ + Fx::Gate(Gate { threshold_db: -45.0, release_ms: 150.0 }), + Fx::Eq { bands: vec![b(HighPass, 80.0, 0.0, q), b(LowShelf, 200.0, -2.0, q), b(Peak, 3000.0, 3.0, 1.0)] }, + Fx::Compressor(Compressor { threshold_db: -20.0, ratio: 3.0, ..Compressor::default() }), + Fx::Limiter(Limiter { ceiling_db: -1.0 }), + ]), + preset("Voice – broadcast", vec![ + Fx::Gate(Gate { threshold_db: -50.0, release_ms: 200.0 }), + Fx::Eq { bands: vec![b(HighPass, 90.0, 0.0, q), b(Peak, 180.0, 2.5, 0.9), b(Peak, 4000.0, 3.0, 1.0), b(HighShelf, 10000.0, 2.0, q)] }, + Fx::Compressor(Compressor { threshold_db: -24.0, ratio: 4.0, attack_ms: 5.0, release_ms: 120.0, makeup_db: 4.0 }), + Fx::Limiter(Limiter { ceiling_db: -1.0 }), + ]), + preset("Music – even loudness", vec![ + Fx::Normalizer(Normalizer { target_db: -18.0, max_gain_db: 12.0 }), + Fx::Limiter(Limiter { ceiling_db: -1.0 }), + ]), + preset("Game – night mode", vec![ + Fx::Compressor(Compressor { threshold_db: -30.0, ratio: 4.0, attack_ms: 5.0, release_ms: 250.0, makeup_db: 6.0 }), + Fx::Limiter(Limiter { ceiling_db: -3.0 }), + ]), + preset("Bass boost", vec![Fx::Eq { bands: vec![b(LowShelf, 120.0, 6.0, q)] }, Fx::Limiter(Limiter { ceiling_db: -1.0 })]), + // Dialogue forward, impact kept: lifts the speech range, then a + // gentle, slow compressor brings quiet lines up against loud scenes + // without pumping the score. + preset("Movies & series", vec![ + Fx::Eq { bands: vec![ + b(LowShelf, 80.0, 3.0, q), + b(Peak, 300.0, -1.5, 0.8), + b(Peak, 2500.0, 3.5, 1.0), + b(HighShelf, 10000.0, 1.5, q), + ] }, + Fx::Compressor(Compressor { threshold_db: -28.0, ratio: 2.5, attack_ms: 15.0, release_ms: 300.0, makeup_db: 4.0 }), + Fx::Limiter(Limiter { ceiling_db: -1.0 }), + ]), + // An approximation of the consumer JBL voicing (not an official + // profile): warm, forward bass, slightly recessed mids, crisp top. + preset("JBL-style", vec![ + Fx::Eq { bands: vec![ + b(LowShelf, 105.0, 6.0, q), + b(Peak, 60.0, 1.5, 1.2), + b(Peak, 1500.0, -1.5, 0.9), + b(HighShelf, 8000.0, 3.0, q), + ] }, + Fx::Limiter(Limiter { ceiling_db: -1.0 }), + ]), + ] + } +} + +/// A user preset as exported to / imported from a file. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(tag = "slidr_preset", rename_all = "snake_case")] +pub enum PresetFile { + Eq(EqPreset), + Effects(FxPreset), +} + +/// A starting point for a strip's EQ. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct EqPreset { + pub id: u32, + pub name: String, + #[serde(default)] + pub bands: Vec, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum EqBandKind { + Peak, + LowShelf, + HighShelf, + HighPass, + LowPass, +} + +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +pub struct EqBand { + pub kind: EqBandKind, + /// Centre / corner frequency in Hz. + pub freq: f32, + /// Boost or cut in dB (ignored by the pass filters). + #[serde(default)] + pub gain_db: f32, + #[serde(default = "default_q")] + pub q: f32, +} + +/// Mixer devices by friendly name. `None` = the system default for `monitor` +/// and `mic_input`; without `sink` nothing is rerouted, without `mic_out` +/// there is no Slidr Mic. +#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] +pub struct MixerDevices { + /// Where the monitor mix plays (the user's headset). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub monitor: Option, + /// The real microphone. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mic_input: Option, + /// Output nobody listens to; rerouted programs play here. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub sink: Option, + /// Render side of the virtual cable that is the Slidr Mic (e.g. "CABLE Input"). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mic_out: Option, +} + +fn default_level() -> f32 { + 1.0 +} +fn default_q() -> f32 { + std::f32::consts::FRAC_1_SQRT_2 } diff --git a/src/presets_ui.rs b/src/presets_ui.rs new file mode 100644 index 0000000..6ef6b94 --- /dev/null +++ b/src/presets_ui.rs @@ -0,0 +1,171 @@ +//! Settings → Presets: the user's own EQ curves and effect chains — rename, +//! export to a file, import from one, delete. Built-in presets aren't listed; +//! they ship with Slidr and can't be changed. + +use std::sync::Arc; + +use parking_lot::Mutex; +use slint::{ComponentHandle, ModelRc, VecModel}; + +use crate::glue::{toast, Shared}; +use crate::model::{PresetFile, Settings}; +use crate::{AppWindow, UserPresetRow}; + +pub fn push(ui: &AppWindow, s: &Settings) { + let count = |n: usize, one: &str, many: &str| if n == 1 { format!("1 {one}") } else { format!("{n} {many}") }; + let eq = s.eq_presets.iter().enumerate().map(|(i, p)| UserPresetRow { + eq: true, + index: i as i32, + name: p.name.as_str().into(), + detail: format!("EQ · {}", count(p.bands.len(), "band", "bands")).into(), + }); + let fx = s.fx_presets.iter().enumerate().map(|(i, p)| UserPresetRow { + eq: false, + index: i as i32, + name: p.name.as_str().into(), + detail: format!("Effects · {}", p.chain.iter().map(|s| s.fx.name()).collect::>().join(" → ")).into(), + }); + ui.set_user_presets(ModelRc::new(VecModel::from(eq.chain(fx).collect::>()))); +} + +/// The preset a row stands for, as it would be written to a file. +fn file_of(s: &Settings, eq: bool, i: i32) -> Option { + let i = usize::try_from(i).ok()?; + if eq { + s.eq_presets.get(i).cloned().map(PresetFile::Eq) + } else { + s.fx_presets.get(i).cloned().map(PresetFile::Effects) + } +} + +/// A name not taken yet among `taken`: "Name", else "Name (2)", "Name (3)", … +fn free_name(name: &str, taken: &[&str]) -> String { + if !taken.contains(&name) { + return name.to_string(); + } + (2..).map(|n| format!("{name} ({n})")).find(|n| !taken.contains(&n.as_str())).unwrap_or_default() +} + +fn save(ui: &AppWindow, s: &Shared) { + if let Err(e) = crate::storage::save_settings(&s.settings) { + log::warn!("could not save settings: {e:#}"); + } + push(ui, &s.settings); +} + +pub fn wire(ui: &AppWindow, shared: Arc>) { + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_user_preset_rename(move |eq, i, name| { + let Some(ui) = weak.upgrade() else { return }; + let name = name.trim(); + let mut s = shared.lock(); + let i = i as usize; + let taken: Vec = if eq { + s.settings.eq_presets.iter().enumerate().filter(|(j, _)| *j != i).map(|(_, p)| p.name.clone()).collect() + } else { + s.settings.fx_presets.iter().enumerate().filter(|(j, _)| *j != i).map(|(_, p)| p.name.clone()).collect() + }; + if name.is_empty() || taken.iter().any(|t| t == name) { + if !name.is_empty() { + toast(&ui, &format!("There already is a preset called \"{name}\"")); + } + push(&ui, &s.settings); + return; + } + let slot = if eq { + s.settings.eq_presets.get_mut(i).map(|p| &mut p.name) + } else { + s.settings.fx_presets.get_mut(i).map(|p| &mut p.name) + }; + if let Some(n) = slot { + *n = name.to_string(); + } + save(&ui, &s); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_user_preset_delete(move |eq, i| { + let Some(ui) = weak.upgrade() else { return }; + let mut s = shared.lock(); + let i = i as usize; + if eq && i < s.settings.eq_presets.len() { + s.settings.eq_presets.remove(i); + } else if !eq && i < s.settings.fx_presets.len() { + s.settings.fx_presets.remove(i); + } + save(&ui, &s); + }); + } + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_user_preset_export(move |eq, i| { + let Some(ui) = weak.upgrade() else { return }; + let Some(file) = file_of(&shared.lock().settings, eq, i) else { return }; + let name = match &file { + PresetFile::Eq(p) => p.name.clone(), + PresetFile::Effects(p) => p.name.clone(), + }; + let Some(dest) = rfd::FileDialog::new() + .set_file_name(format!("slidr-{}-{name}.json", if eq { "eq" } else { "effects" })) + .add_filter("Slidr preset", &["json"]) + .save_file() + else { + return; + }; + let written = serde_json::to_string_pretty(&file) + .map_err(std::io::Error::other) + .and_then(|json| std::fs::write(&dest, json)); + match written { + Ok(()) => toast(&ui, &format!("Exported to {}", dest.display())), + Err(e) => toast(&ui, &format!("Export failed: {e}")), + } + }); + } + { + let weak = ui.as_weak(); + ui.on_user_preset_import(move || { + let Some(ui) = weak.upgrade() else { return }; + let Some(path) = rfd::FileDialog::new().add_filter("Slidr preset", &["json"]).pick_file() else { return }; + let parsed = std::fs::read_to_string(&path).ok().and_then(|d| serde_json::from_str::(&d).ok()); + let Some(file) = parsed else { + toast(&ui, "Import failed: not an EQ or effects preset"); + return; + }; + let mut s = shared.lock(); + // An import never overwrites one of yours: a taken name gets a number. + let name = match file { + PresetFile::Eq(mut p) => { + let presets = &mut s.settings.eq_presets; + p.name = free_name(&p.name, &presets.iter().map(|p| p.name.as_str()).collect::>()); + p.id = presets.iter().map(|p| p.id).max().unwrap_or(1000) + 1; + presets.push(p.clone()); + p.name + } + PresetFile::Effects(mut p) => { + let presets = &mut s.settings.fx_presets; + p.name = free_name(&p.name, &presets.iter().map(|p| p.name.as_str()).collect::>()); + presets.push(p.clone()); + p.name + } + }; + save(&ui, &s); + toast(&ui, &format!("Imported preset \"{name}\"")); + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn imports_get_a_free_name() { + assert_eq!(free_name("Warm", &[]), "Warm"); + assert_eq!(free_name("Warm", &["Warm", "Warm (2)"]), "Warm (3)"); + } +} diff --git a/src/quit_signal.rs b/src/quit_signal.rs new file mode 100644 index 0000000..e80645d --- /dev/null +++ b/src/quit_signal.rs @@ -0,0 +1,49 @@ +//! `slidr --quit`: ask a running Slidr to exit the normal way — the same path +//! as its Exit button, so the mixer hands rerouted programs back first. +//! Killing the process skips that (the next start repairs it, but until then +//! those programs play into the silent output). + +#![cfg(target_os = "windows")] + +use windows::core::{w, PCWSTR}; +use windows::Win32::Foundation::{CloseHandle, WAIT_OBJECT_0}; +use windows::Win32::System::Threading::{ + CreateEventW, OpenEventW, SetEvent, WaitForSingleObject, EVENT_MODIFY_STATE, INFINITE, +}; + +const EVENT: PCWSTR = w!("Local\\SlidrQuit"); + +/// Wait (on a background thread) for a `--quit` request and leave the event +/// loop when one comes. +pub fn listen() { + let spawned = std::thread::Builder::new().name("quit-signal".into()).spawn(|| unsafe { + let Ok(event) = CreateEventW(None, false, false, EVENT) else { + log::warn!("--quit listener unavailable"); + return; + }; + if WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0 { + log::info!("quit requested by another process"); + let _ = slint::invoke_from_event_loop(|| { + let _ = slint::quit_event_loop(); + }); + } + let _ = CloseHandle(event); + }); + if let Err(e) = spawned { + log::warn!("--quit listener not started: {e}"); + } +} + +/// Signal a running Slidr to quit. `false` if none is listening. +pub fn signal() -> bool { + unsafe { + match OpenEventW(EVENT_MODIFY_STATE, false, EVENT) { + Ok(event) => { + let ok = SetEvent(event).is_ok(); + let _ = CloseHandle(event); + ok + } + Err(_) => false, + } + } +} diff --git a/src/sidebar.rs b/src/sidebar.rs new file mode 100644 index 0000000..24c7c7b --- /dev/null +++ b/src/sidebar.rs @@ -0,0 +1,118 @@ +//! Sidebar tabs: their order and which are shown, set in Settings. Settings +//! itself can't be hidden — otherwise there'd be no way back. + +use std::sync::Arc; + +use parking_lot::Mutex; +use slint::{ComponentHandle, ModelRc, VecModel}; + +use crate::glue::Shared; +use crate::model::Settings; +use crate::{AppWindow, NavRow}; + +/// (name stored in settings, page index in the UI, label). +const TABS: [(&str, i32, &str); 5] = [ + ("home", 0, "Home"), + ("sliders", 1, "Sliders"), + ("buttons", 2, "Buttons"), + ("mixer", 4, "Mixer"), + ("settings", 3, "Settings"), +]; +const ALWAYS: &str = "settings"; + +/// Tabs in the user's order; tabs the saved order doesn't know (new ones +/// after an update) come after the known ones, in default order. +fn ordered(s: &Settings) -> Vec<(&'static str, i32, &'static str)> { + let mut out: Vec<_> = s.nav_order.iter().filter_map(|n| TABS.iter().find(|t| t.0 == n)).copied().collect(); + for t in TABS { + if !out.iter().any(|o| o.0 == t.0) { + out.push(t); + } + } + out +} + +fn visible(s: &Settings, name: &str) -> bool { + name == ALWAYS || !s.nav_hidden.iter().any(|h| h == name) +} + +/// The pages the sidebar shows, in order. +fn pages(s: &Settings) -> Vec { + ordered(s).into_iter().filter(|t| visible(s, t.0)).map(|t| t.1).collect() +} + +pub fn push(ui: &AppWindow, s: &Settings) { + let rows: Vec = ordered(s) + .into_iter() + .map(|(name, page, label)| NavRow { page, label: label.into(), visible: visible(s, name), fixed: name == ALWAYS }) + .collect(); + ui.set_nav_rows(ModelRc::new(VecModel::from(rows))); + let pages = pages(s); + // Hiding the open tab moves to the first one still shown. + if !pages.contains(&ui.get_current_page()) { + if let Some(first) = pages.first() { + ui.set_current_page(*first); + ui.invoke_page_changed(*first); + } + } + ui.set_nav_pages(ModelRc::new(VecModel::from(pages))); +} + +fn name_of(page: i32) -> Option<&'static str> { + TABS.iter().find(|t| t.1 == page).map(|t| t.0) +} + +pub fn wire(ui: &AppWindow, shared: Arc>) { + push(ui, &shared.lock().settings); + { + let weak = ui.as_weak(); + let shared = shared.clone(); + ui.on_nav_move(move |page, dir| { + let Some(ui) = weak.upgrade() else { return }; + let mut s = shared.lock(); + let mut order: Vec<&str> = ordered(&s.settings).into_iter().map(|t| t.0).collect(); + let Some(i) = name_of(page).and_then(|n| order.iter().position(|o| *o == n)) else { return }; + let j = i as i32 + dir; + if j < 0 || j as usize >= order.len() { + return; + } + order.swap(i, j as usize); + s.settings.nav_order = order.into_iter().map(str::to_string).collect(); + let _ = crate::storage::save_settings(&s.settings); + push(&ui, &s.settings); + }); + } + { + let weak = ui.as_weak(); + ui.on_nav_visible(move |page, on| { + let Some(ui) = weak.upgrade() else { return }; + let Some(name) = name_of(page).filter(|n| *n != ALWAYS) else { return }; + let mut s = shared.lock(); + s.settings.nav_hidden.retain(|h| h != name); + if !on { + s.settings.nav_hidden.push(name.to_string()); + } + let _ = crate::storage::save_settings(&s.settings); + push(&ui, &s.settings); + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn default_order_and_settings_cannot_be_hidden() { + let mut s = Settings::default(); + assert_eq!(pages(&s), vec![0, 1, 2, 4, 3]); + s.nav_hidden = vec!["settings".into(), "home".into()]; + assert_eq!(pages(&s), vec![1, 2, 4, 3]); + } + + #[test] + fn saved_order_wins_and_unknown_tabs_are_appended() { + let s = Settings { nav_order: vec!["mixer".into(), "bogus".into(), "home".into()], ..Settings::default() }; + assert_eq!(pages(&s), vec![4, 0, 1, 2, 3]); + } +} diff --git a/src/storage.rs b/src/storage.rs index 148ebe4..47b6757 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -71,7 +71,9 @@ pub fn list_presets() -> Vec { pub fn load_preset(name: &str) -> anyhow::Result { let path = presets_dir().join(name).join("preset.json"); let data = fs::read_to_string(&path).with_context(|| format!("read {path:?}"))?; - let preset: Preset = serde_json::from_str(&data)?; + let mut preset: Preset = serde_json::from_str(&data)?; + // The microphone used to be picked on the Mixer page (a global setting). + preset.move_voice_to_channel(load_settings().mixer_devices.mic_input.as_deref()); Ok(preset) } diff --git a/ui/app.slint b/ui/app.slint index dd5f39a..0616852 100644 --- a/ui/app.slint +++ b/ui/app.slint @@ -26,6 +26,11 @@ export struct ButtonCell { pressed: bool, category: string, assigned: bool } export struct LineItem { id: int, primary: string, secondary: string, icon-kind: int } export struct CategorySummary { id: int, name: string, count: int, collapsed: bool, lines: [LineItem] } export struct AssignPick { id: int, name: string } +// A sidebar tab in Settings: `page` is its page index, `fixed` = can't be hidden. +export struct NavRow { page: int, label: string, visible: bool, fixed: bool } +// One of the user's own presets in Settings: an EQ curve (`eq`) or an effect +// chain, at `index` in its list. +export struct UserPresetRow { eq: bool, index: int, name: string, detail: string } // A row in the wizard's picker. `name` is what the user reads; `value` is what // gets stored (a process name, an executable path, or an "@group:" reference // for a curated program category — `is-group` marks the latter). @@ -110,10 +115,12 @@ component Select { in property <[string]> model; in-out property current-index: 0; in property field-width: 260px; + in property enabled: true; callback selected(int); min-width: root.field-width; max-width: root.field-width; min-height: 36px; max-height: 36px; + opacity: root.enabled ? 1.0 : 0.55; Rectangle { background: Palette.alternate-background; @@ -138,7 +145,8 @@ component Select { } } ta := TouchArea { - mouse-cursor: pointer; + enabled: root.enabled; + mouse-cursor: root.enabled ? pointer : default; clicked => { popup.show(); } } popup := PopupWindow { @@ -581,6 +589,21 @@ component SquareIconButton { } } +// Small text button for moving something up / down a list. +component MoveButton { + in property text; + in property enabled: true; + callback clicked(); + min-width: 28px; max-width: 28px; min-height: 28px; max-height: 28px; + opacity: root.enabled ? 1.0 : 0.25; + Rectangle { border-radius: 6px; + background: (ta.has-hover && root.enabled) ? Palette.alternate-background.darker(0.06) : transparent; } + ta := TouchArea { enabled: root.enabled; mouse-cursor: root.enabled ? pointer : default; + clicked => { root.clicked(); } } + Text { text: root.text; font-size: 15px; font-family: "Inter"; + horizontal-alignment: center; vertical-alignment: center; } +} + // Row used in settings: label on the far left, control on the far right. component SettingRow inherits HorizontalLayout { in property label; @@ -618,7 +641,7 @@ component HomePage inherits VerticalBox { spacing: 16px; alignment: start; - HorizontalBox { + HorizontalBox { padding: 0; Text { text: "Home"; font-size: 26px; font-weight: 700; font-family: "Inter"; vertical-alignment: center; } Rectangle { horizontal-stretch: 1; } @@ -793,6 +816,10 @@ component CategoriesPage inherits VerticalBox { in-out property draft-name: ""; in-out property renaming-id: -1; in-out property rename-draft: ""; + // Cards side by side in columns (filled left to right, row by row). + in property grid; + property content-w: sv.visible-width - 18px; + property cols: root.grid ? Math.max(1, Math.floor((root.content-w + 12px) / 372px)) : 1; // ── drag state ────────────────────────────────────────────────────────── // Global pointer position (window coords) while a drag is active. @@ -832,11 +859,13 @@ component CategoriesPage inherits VerticalBox { } } - ScrollView { + sv := ScrollView { mouse-drag-pan-enabled: false; vertical-stretch: 1; VerticalBox { padding: 0; spacing: 8px; alignment: start; + // Leave room for the scrollbar, as the other pages do. + width: root.content-w; if root.categories.length == 0: Card { VerticalLayout { padding: 24px; spacing: 6px; alignment: center; Text { text: "No categories yet"; @@ -848,8 +877,13 @@ component CategoriesPage inherits VerticalBox { horizontal-alignment: center; } } } + HorizontalLayout { spacing: 12px; + for _[col] in root.cols: VerticalLayout { + width: (root.content-w - (root.cols - 1) * 12px) / root.cols; + alignment: start; for cat[ci] in root.categories: VerticalLayout { - spacing: 8px; + if Math.mod(ci, root.cols) == col: VerticalLayout { + spacing: 8px; padding-bottom: 8px; // Drop indicator above this card (category reorder). DropMark { active: root.cat-drag && root.drop-cat-pos == ci; } card := Card { @@ -916,7 +950,7 @@ component CategoriesPage inherits VerticalBox { clicked => { root.toggle-collapse(cat.id); } } if root.renaming-id != cat.id: Text { text: cat.name; - font-weight: 700; font-size: 14px; + font-weight: 700; font-size: 14px; overflow: elide; min-width: 0; font-family: "Inter"; vertical-alignment: center; horizontal-stretch: 1; } if root.renaming-id == cat.id: LineEdit { @@ -1013,11 +1047,13 @@ component CategoriesPage inherits VerticalBox { VerticalLayout { alignment: center; KindIcon { kind: line.icon-kind; size: 20px; } } VerticalLayout { - horizontal-stretch: 1; spacing: 0px; + horizontal-stretch: 1; spacing: 0px; min-width: 0; alignment: center; - Text { text: line.primary; font-weight: 500; + // Elided, so a long device list can't push + // the buttons out of a narrow (grid) card. + Text { text: line.primary; font-weight: 500; overflow: elide; font-size: 12px; font-family: "Inter"; } - Text { text: line.secondary; font-size: 10px; + Text { text: line.secondary; font-size: 10px; overflow: elide; font-family: "Inter"; color: Palette.foreground.with-alpha(0.55); } } @@ -1051,6 +1087,9 @@ component CategoriesPage inherits VerticalBox { active: root.cat-drag && root.drop-cat-pos == root.categories.length; } } + } + } + } } } } @@ -1084,6 +1123,28 @@ component SettingsPage inherits VerticalBox { // "", "checking", "up-to-date", "available", "failed" in property update-state; in property update-latest; + in property <[NavRow]> nav-rows; + in-out property sounds-on; + in-out property sound-volume; + in property <[string]> sound-sets; + in-out property sound-set-index; + in-out property grid-sliders; + in-out property grid-buttons; + in property <[UserPresetRow]> user-presets; + in property <[string]> spare-names; + in property <[string]> cable-names; + in-out property sink-idx; + in-out property cable-idx; + callback mixer-devices-changed(); + callback nav-move(int, int); + callback nav-visible(int, bool); + callback preset-rename(bool, int, string); + callback preset-export(bool, int); + callback preset-delete(bool, int); + callback preset-import(); + // Row being renamed (position in `user-presets`), -1 for none. + property renaming: -1; + property rename-draft; callback theme-changed(int); callback check-for-updates(); @@ -1103,7 +1164,7 @@ component SettingsPage inherits VerticalBox { in property profile-write-blocked; callback reopen-version-warning(); - HorizontalBox { + HorizontalBox { padding: 0; Text { text: "Settings"; font-size: 26px; font-weight: 700; font-family: "Inter"; vertical-alignment: center; } Rectangle { horizontal-stretch: 1; } @@ -1113,7 +1174,7 @@ component SettingsPage inherits VerticalBox { mouse-drag-pan-enabled: false; vertical-stretch: 1; VerticalBox { - spacing: 12px; alignment: start; + padding: 0; spacing: 12px; alignment: start; // Pin the content to the viewport width so this page scrolls only // vertically. Left to itself the layout takes its preferred width, // and one long description then drags every card off to the right. @@ -1154,6 +1215,154 @@ component SettingsPage inherits VerticalBox { } } } + SettingRow { + label: "Sliders tab"; + description: "A grid of cards uses the width of a wide window"; + Select { field-width: root.control-w; model: ["List", "Grid"]; + current-index: root.grid-sliders ? 1 : 0; + selected(i) => { root.grid-sliders = i == 1; root.dirty(); } } + } + SettingRow { + label: "Buttons tab"; + Select { field-width: root.control-w; model: ["List", "Grid"]; + current-index: root.grid-buttons ? 1 : 0; + selected(i) => { root.grid-buttons = i == 1; root.dirty(); } } + } + } + } + + Card { + VerticalBox { padding: 18px; spacing: 6px; + Text { text: "Sidebar"; font-weight: 700; font-family: "Inter"; font-size: 14px; } + Text { text: "Order and visibility of the tabs on the left. Settings always stays."; + font-family: "Inter"; font-size: 11px; color: Palette.foreground.with-alpha(0.55); } + Rectangle { height: 6px; } + for r[i] in root.nav-rows: HorizontalLayout { + spacing: 8px; height: 36px; + Text { text: r.label; horizontal-stretch: 1; vertical-alignment: center; + font-family: "Inter"; font-size: 13px; font-weight: 500; + color: r.visible ? Palette.foreground : Palette.foreground.with-alpha(0.45); } + VerticalLayout { alignment: center; + MoveButton { text: "↑"; enabled: i > 0; clicked => { root.nav-move(r.page, -1); } } } + VerticalLayout { alignment: center; + MoveButton { text: "↓"; enabled: i < root.nav-rows.length - 1; + clicked => { root.nav-move(r.page, 1); } } } + VerticalLayout { alignment: center; + Toggle { checked: r.visible; enabled: !r.fixed; + toggled => { root.nav-visible(r.page, self.checked); } } } + } + } + } + + Card { + VerticalBox { padding: 18px; spacing: 6px; + Text { text: "Mixer devices"; font-weight: 700; font-family: "Inter"; font-size: 14px; } + Text { text: "Slidr plays the mix on your Windows default output and follows it when that changes. Add your microphone to a mixer channel to send it into the Slidr Mic."; + wrap: word-wrap; font-family: "Inter"; font-size: 11px; + color: Palette.foreground.with-alpha(0.55); } + Rectangle { height: 6px; } + SettingRow { + label: "Slidr Mic"; + description: root.cable-names.length > 2 + ? "Virtual cable your voice and mix go into. Choose it as the microphone in your voice chat app." + : "Your virtual cable. Choose it as the microphone in your voice chat app."; + Select { field-width: root.control-w; model: root.cable-names; + enabled: root.cable-names.length > 2; + current-index <=> root.cable-idx; + selected => { root.mixer-devices-changed(); } } + } + SettingRow { + label: "Silent output"; + description: "An output nobody listens to. Channel programs are moved there so you only hear Slidr's mix."; + Select { field-width: root.control-w; model: root.spare-names; + current-index <=> root.sink-idx; + selected => { root.mixer-devices-changed(); } } + } + } + } + + Card { + VerticalBox { padding: 18px; spacing: 6px; + Text { text: "Mixer sounds"; font-weight: 700; font-family: "Inter"; font-size: 14px; } + Rectangle { height: 6px; } + SettingRow { + label: "Switch sounds"; + description: "Short sounds on your output when you mute, switch effects or turn the mixer on or off. Never sent to the Slidr Mic."; + HorizontalLayout { width: root.control-w; alignment: end; + VerticalLayout { alignment: center; + Toggle { checked <=> root.sounds-on; toggled => { root.dirty(); } } } } + } + SettingRow { + label: "Sound set"; + Select { + field-width: root.control-w; + model: root.sound-sets; + current-index <=> root.sound-set-index; + selected(i) => { root.dirty(); } + } + } + SettingRow { + label: "Volume"; + HorizontalLayout { + width: root.control-w; spacing: 10px; + AccentSlider { minimum: 0; maximum: 100; value: root.sound-volume; + changed(v) => { root.sound-volume = v; root.dirty(); } + horizontal-stretch: 1; } + Text { text: Math.round(root.sound-volume) + " %"; min-width: 40px; + horizontal-alignment: right; font-family: "Inter"; vertical-alignment: center; } + } + } + } + } + + Card { + VerticalBox { padding: 18px; spacing: 6px; + HorizontalLayout { spacing: 8px; + VerticalLayout { horizontal-stretch: 1; spacing: 2px; + Text { text: "My presets"; font-weight: 700; font-family: "Inter"; font-size: 14px; } + Text { text: "EQ curves and effect chains you saved in the mixer."; + font-family: "Inter"; font-size: 11px; wrap: word-wrap; + color: Palette.foreground.with-alpha(0.55); } + } + VerticalLayout { alignment: center; + IconButton { label: "Import…"; icon: @image-url("../assets/icons/plus.svg"); + clicked => { root.preset-import(); } } } + } + Rectangle { height: 6px; } + if root.user-presets.length == 0: Text { + text: "None yet. Save one with \"Save as preset\" in the EQ or effects editor."; + font-family: "Inter"; font-size: 12px; wrap: word-wrap; + color: Palette.foreground.with-alpha(0.55); } + for p[i] in root.user-presets: HorizontalLayout { + spacing: 8px; min-height: 40px; + if root.renaming != i: VerticalLayout { horizontal-stretch: 1; alignment: center; spacing: 1px; + Text { text: p.name; font-family: "Inter"; font-size: 13px; font-weight: 500; + overflow: elide; } + Text { text: p.detail; font-family: "Inter"; font-size: 11px; overflow: elide; + color: Palette.foreground.with-alpha(0.55); } + } + if root.renaming == i: VerticalLayout { horizontal-stretch: 1; alignment: center; + LineEdit { height: 32px; text <=> root.rename-draft; + accepted => { root.preset-rename(p.eq, p.index, root.rename-draft); root.renaming = -1; } } } + VerticalLayout { alignment: center; + SquareIconButton { + icon: root.renaming == i ? @image-url("../assets/icons/check.svg") + : @image-url("../assets/icons/edit.svg"); + clicked => { + if (root.renaming == i) { + root.preset-rename(p.eq, p.index, root.rename-draft); + root.renaming = -1; + } else { + root.renaming = i; root.rename-draft = p.name; + } + } } } + VerticalLayout { alignment: center; + IconButton { label: "Export…"; clicked => { root.preset-export(p.eq, p.index); } } } + VerticalLayout { alignment: center; + SquareIconButton { icon: @image-url("../assets/icons/trash.svg"); + tint: #d32f2f.with-alpha(0.8); + clicked => { root.renaming = -1; root.preset-delete(p.eq, p.index); } } } + } } } @@ -1638,8 +1847,10 @@ component WizardPopup inherits Rectangle { property <[string]> button-kinds: ["Mute process", "Mute main audio", "Mute microphone", "Open application", "Open website", "Simulate key", - "Cycle output device", "API call"]; - property <[string]> slider-kinds: ["Process volume", "Microphone", "Output device", "API call"]; + "Cycle output device", "API call", "Mute mixer channel"]; + // Mixer entries go last so the indices above (the API call's included) stay put. + property <[string]> slider-kinds: ["Process volume", "Microphone", "Output device", "API call", + "Mixer channel"]; property <[string]> kinds: root.target-kind == 1 ? root.button-kinds : root.slider-kinds; property kind-label: root.kinds[root.kind]; property <[string]> method-names: ["GET", "POST", "PUT", "PATCH", "DELETE"]; @@ -2300,6 +2511,570 @@ component LedConfigPopup inherits Rectangle { // Window // ────────────────────────────────────────────────────────────────────────── +// One mixer channel as the Mixer page shows it. `level` / `send` are percent; +// `fx-count` is how many effects of its chain are on; `fx-on` is the chain's master switch. +export struct MixChannelUI { + id: int, name: string, programs: string, level: float, muted: bool, + fx-count: int, fx-on: bool, reroute: bool, send-on: bool, send: float, + collapsed: bool, + // Only microphones in it: "Replace original" means nothing there. + mics-only: bool, +} + +// Fader + percent readout, the row control of the mixer. The readout shows +// the fader itself (dragging takes it off `value`), and a new `value` — the +// board moved it — puts the fader there again. +component LevelRow inherits HorizontalLayout { + in property value; + callback changed(float); + spacing: 10px; + changed value => { fader.value = root.value; } + fader := AccentSlider { minimum: 0; maximum: 100; value: root.value; + horizontal-stretch: 1; + changed(v) => { root.changed(v); } } + Text { text: Math.round(fader.value) + " %"; min-width: 42px; + horizontal-alignment: right; vertical-alignment: center; + font-family: "Inter"; font-size: 12px; } +} + +// Caption over a switch, the switch at its natural width. +component LabeledToggle inherits VerticalLayout { + in property label; + in-out property checked; + callback toggled(bool); + spacing: 4px; + Text { text: root.label; font-family: "Inter"; font-size: 11px; + color: Palette.foreground.with-alpha(0.6); } + // As tall as a button and centred, so a row of these lines up with a + // switch that has an "Edit" button beside it. + HorizontalLayout { alignment: start; min-height: 36px; + VerticalLayout { alignment: center; + Toggle { checked <=> root.checked; toggled => { root.toggled(self.checked); } } } } +} + +// Small caption + control, stacked. +component Labeled inherits VerticalLayout { + in property label; + spacing: 4px; + Text { text: root.label; font-family: "Inter"; font-size: 11px; + color: Palette.foreground.with-alpha(0.6); } + @children +} + +component MixerPage inherits VerticalBox { + in-out property enabled; + in property <[MixChannelUI]> channels; + in property hint; + in property hint-download; + callback open-download(); + + // The on/off switch changed. + callback changed(); + // One field of one channel: `field` names it, the value comes in `num` or `text`. + callback channel-set(int, string, float, string); + callback add-channel(); + callback delete-channel(int); + callback choose-programs(int); + // Open the effects editor for "voice" or "channel:". + callback edit-fx(string); + // Channel id, insertion slot in the list (before it moves). + callback move-channel(int, int); + + spacing: 14px; + property control-w: 260px; + property content-w: sv.visible-width - 18px; + // Channel drag (reorder). + property ptr-x; + property ptr-y; + property ch-drag: false; + property drag-id: -1; + property drop-pos: -1; + + HorizontalBox { padding: 0; + Text { text: "Mixer"; font-size: 26px; font-weight: 700; font-family: "Inter"; + vertical-alignment: center; } + Rectangle { horizontal-stretch: 1; } + Text { text: root.enabled ? "On" : "Off"; font-family: "Inter"; + vertical-alignment: center; color: Palette.foreground.with-alpha(0.7); } + Toggle { checked <=> root.enabled; toggled => { root.changed(); } } + } + + sv := ScrollView { + mouse-drag-pan-enabled: false; + vertical-stretch: 1; + VerticalBox { + padding: 0; spacing: 12px; alignment: start; + width: root.content-w; + + if root.hint != "": Card { + border-color: Theme.accent-bg; + HorizontalBox { padding: 14px; spacing: 12px; + Text { text: root.hint; wrap: word-wrap; font-family: "Inter"; + font-size: 12px; horizontal-stretch: 1; vertical-alignment: center; } + if root.hint-download: IconButton { label: "Download VB-CABLE"; primary: true; + clicked => { root.open-download(); } } + } + } + + for ch[ci] in root.channels: VerticalLayout { + spacing: 6px; padding-bottom: 6px; + // Drop indicator above this card (channel reorder). + DropMark { active: root.ch-drag && root.drop-pos == ci; } + card := Card { + // Which insertion slot the drag pointer means over this card. + // Ternary-guarded: `absolute-position` is only read mid-drag + // (see the note in CategoriesPage). + property slot: + root.ch-drag + ? ((root.ptr-x >= card.absolute-position.x + && root.ptr-x < card.absolute-position.x + card.width + && root.ptr-y >= card.absolute-position.y + && root.ptr-y < card.absolute-position.y + card.height) + ? (root.ptr-y < card.absolute-position.y + card.height / 2 ? ci : ci + 1) + : -999) + : -999; + changed slot => { if (card.slot != -999) { root.drop-pos = card.slot; } } + opacity: (root.ch-drag && root.drag-id == ch.id) ? 0.4 : 1.0; + VerticalBox { padding: 18px; spacing: 10px; + HorizontalLayout { spacing: 8px; + // Drag handle — reorder channels. + grip := TouchArea { + width: 22px; mouse-cursor: grab; + pointer-event(ev) => { + if (ev.button == PointerEventButton.left && ev.kind == PointerEventKind.down) { + root.ch-drag = true; root.drag-id = ch.id; root.drop-pos = ci; + root.ptr-x = grip.absolute-position.x + grip.mouse-x; + root.ptr-y = grip.absolute-position.y + grip.mouse-y; + } else if (ev.button == PointerEventButton.left && ev.kind == PointerEventKind.up) { + if (root.ch-drag && root.drop-pos != -1) { + root.move-channel(root.drag-id, root.drop-pos); + } + root.ch-drag = false; root.drop-pos = -1; + } + } + moved => { + root.ptr-x = grip.absolute-position.x + grip.mouse-x; + root.ptr-y = grip.absolute-position.y + grip.mouse-y; + } + Image { source: @image-url("../assets/icons/grid-dots.svg"); + width: 16px; height: 16px; + colorize: Palette.foreground.with-alpha(0.45); + x: (parent.width - self.width) / 2; y: (parent.height - self.height) / 2; } + } + VerticalLayout { alignment: center; + SquareIconButton { + icon: ch.collapsed ? @image-url("../assets/icons/chevron-right.svg") + : @image-url("../assets/icons/chevron-down.svg"); + clicked => { root.channel-set(ch.id, "collapsed", ch.collapsed ? 0 : 1, ""); } } } + LineEdit { min-width: 120px; preferred-width: 180px; height: 34px; text: ch.name; + placeholder-text: "Channel name"; + edited(t) => { root.channel-set(ch.id, "name", 0, t); } } + Text { text: ch.programs != "" ? ch.programs : "Nothing added yet"; + horizontal-stretch: 1; min-width: 0; vertical-alignment: center; overflow: elide; + font-family: "Inter"; font-size: 13px; + color: ch.programs != "" ? Palette.foreground : Palette.foreground.with-alpha(0.5); } + IconButton { icon: @image-url("../assets/icons/app-window.svg"); label: "Sources"; + clicked => { root.choose-programs(ch.id); } } + VerticalLayout { alignment: center; + SquareIconButton { icon: @image-url("../assets/icons/trash.svg"); + clicked => { root.delete-channel(ch.id); } } } + } + Labeled { label: ch.mics-only ? "Level you hear yourself at" : "Level"; + LevelRow { value: ch.level; + changed(v) => { root.channel-set(ch.id, "level", v, ""); } } + } + if !ch.collapsed: VerticalLayout { spacing: 10px; + HorizontalLayout { spacing: 20px; alignment: start; + Labeled { label: "Effects & EQ"; + HorizontalLayout { spacing: 8px; + VerticalLayout { alignment: center; + Toggle { checked: ch.fx-on; + toggled => { root.channel-set(ch.id, "fx-on", self.checked ? 1 : 0, ""); } } } + IconButton { label: ch.fx-count > 0 ? "Edit (" + ch.fx-count + " on)" : "Edit"; + clicked => { root.edit-fx("channel:" + ch.id); } } + } + } + LabeledToggle { label: "Muted"; checked: ch.muted; + toggled(on) => { root.channel-set(ch.id, "muted", on ? 1 : 0, ""); } } + if !ch.mics-only: LabeledToggle { label: "Replace original"; checked: ch.reroute; + toggled(on) => { root.channel-set(ch.id, "reroute", on ? 1 : 0, ""); } } + send := LabeledToggle { label: "Into Slidr Mic"; checked: ch.send-on; + toggled(on) => { root.channel-set(ch.id, "send-on", on ? 1 : 0, ""); } } + } + // Follows the switch itself, so flipping it doesn't rebuild the card. + if send.checked: Labeled { label: "Level in the Slidr Mic"; + LevelRow { value: ch.send; + changed(v) => { root.channel-set(ch.id, "send", v, ""); } } + } + } + } + } + // Trailing drop indicator after the last card (move to the end). + if ci == root.channels.length - 1: DropMark { + active: root.ch-drag && root.drop-pos == root.channels.length; + } + } + + HorizontalLayout { alignment: start; + IconButton { icon: @image-url("../assets/icons/plus.svg"); label: "Add channel"; + clicked => { root.add-channel(); } } + } + } + } +} + +// Multi-select list of programs for one mixer channel. +component ProgramPickerPopup inherits Rectangle { + in property title; + in property <[PickerEntry]> options; + in-out property filter; + // 0 = programs, 1 = microphones. + in-out property tab; + callback tab-changed(int); + callback toggle(string); + callback filter-changed(string); + callback close(); + + background: #00000099; + TouchArea { clicked => { root.close(); } } + + Rectangle { + x: parent.width / 2 - self.width / 2; + y: parent.height / 2 - self.height / 2; + width: 480px; height: 560px; + background: Palette.background; + border-radius: 14px; border-width: 1px; border-color: Palette.border; + TouchArea {} + VerticalBox { + padding: 22px; spacing: 12px; + HorizontalLayout { spacing: 10px; + Text { text: root.title; font-size: 20px; font-weight: 700; font-family: "Inter"; + horizontal-stretch: 1; vertical-alignment: center; overflow: elide; } + Select { field-width: 150px; model: ["Programs", "Microphones"]; + current-index <=> root.tab; + selected(i) => { root.tab-changed(i); } } + } + LineEdit { height: 36px; placeholder-text: "Search…"; text <=> root.filter; + edited(t) => { root.filter-changed(t); } } + ScrollView { + mouse-drag-pan-enabled: false; + vertical-stretch: 1; + VerticalBox { + spacing: 6px; alignment: start; + if root.options.length == 0: Text { text: "Nothing found"; + font-family: "Inter"; color: Palette.foreground.with-alpha(0.6); } + for e in root.options: Rectangle { + border-radius: 8px; height: 40px; + background: e.selected ? Theme.accent-bg.with-alpha(0.22) : Palette.alternate-background; + border-width: 1px; + border-color: e.selected ? Theme.accent-bg : Palette.border; + TouchArea { + mouse-cursor: pointer; + clicked => { root.toggle(e.value); } + HorizontalLayout { padding-left: 12px; padding-right: 12px; spacing: 10px; + VerticalLayout { alignment: center; KindIcon { kind: e.icon-kind; size: 18px; } } + Text { text: e.name; vertical-alignment: center; overflow: elide; + font-family: "Inter"; horizontal-stretch: 1; } + VerticalLayout { alignment: center; + if e.selected: Icon { source: @image-url("../assets/icons/check.svg"); + tint: Theme.accent-bg; size: 16px; } } + } + } + } + } + } + HorizontalLayout { alignment: end; + IconButton { label: "Done"; primary: true; clicked => { root.close(); } } + } + } + } +} + +// One EQ band as the editor shows it. `freq-pos` / `q-pos` are 0..1 on a log +// scale, `gain` is dB; `x` / `y` place the band's handle on the curve (0..1). +export struct EqBandUI { + kind: int, freq-pos: float, gain: float, q-pos: float, + freq-text: string, gain-text: string, q-text: string, + x: float, y: float, +} + +// Parametric EQ for one mixer strip: the response curve with draggable band +// handles, and one row of controls per band. +component EqEditorPopup inherits Rectangle { + in property title; + in-out property on; + in property <[EqBandUI]> bands; + // SVG path of the response, in a 1000 × 300 box (±15 dB). + in property curve; + in property <[string]> preset-names; + callback toggled(bool); + callback set(int, string, float); + callback drag(int, float, float); + callback drag-end(); + callback add(); + callback delete(int); + callback load-preset(int); + callback save-preset(string); + in-out property preset-name; + callback close(); + + background: #00000099; + TouchArea { clicked => { root.close(); } } + + Rectangle { + x: parent.width / 2 - self.width / 2; + y: parent.height / 2 - self.height / 2; + width: min(parent.width - 48px, 820px); height: min(parent.height - 48px, 700px); + background: Palette.background; + border-radius: 14px; border-width: 1px; border-color: Palette.border; + TouchArea {} + VerticalBox { + padding: 22px; spacing: 12px; + HorizontalLayout { spacing: 10px; + Text { text: root.title; font-size: 20px; font-weight: 700; font-family: "Inter"; + vertical-alignment: center; horizontal-stretch: 1; } + Text { text: root.on ? "On" : "Off"; font-family: "Inter"; vertical-alignment: center; + color: Palette.foreground.with-alpha(0.7); } + Toggle { checked <=> root.on; toggled => { root.toggled(self.checked); } } + } + + graph := Rectangle { + height: 220px; + background: Palette.alternate-background; + border-radius: 10px; border-width: 1px; border-color: Palette.border; + clip: true; + opacity: root.on ? 1.0 : 0.5; + for db in [12, 6, 0, -6, -12]: Rectangle { + x: 0; width: parent.width; height: 1px; + y: (15 - db) / 30 * parent.height; + background: db == 0 ? Palette.foreground.with-alpha(0.35) : Palette.border; + } + for m in [{ p: 0.233, t: "100 Hz" }, { p: 0.566, t: "1 kHz" }, { p: 0.9, t: "10 kHz" }]: Rectangle { + x: m.p * parent.width; y: 0; width: 1px; height: parent.height; + background: Palette.border; + Text { x: 4px; y: parent.height - 18px; text: m.t; font-size: 10px; + font-family: "Inter"; color: Palette.foreground.with-alpha(0.5); } + } + Path { + x: 0; y: 0; width: parent.width; height: parent.height; + viewbox-width: 1000; viewbox-height: 300; + commands: root.curve; + stroke: Theme.accent-bg; stroke-width: 2px; + } + for b[i] in root.bands: dot := Rectangle { + x: b.x * graph.width - 9px; y: b.y * graph.height - 9px; + width: 18px; height: 18px; border-radius: 9px; + background: Theme.accent-bg; border-width: 2px; border-color: white; + Text { text: i + 1; color: white; font-size: 10px; font-weight: 700; + horizontal-alignment: center; vertical-alignment: center; } + TouchArea { + mouse-cursor: move; + moved => { + if (self.pressed) { + root.drag(i, (dot.x + self.mouse-x) / graph.width, (dot.y + self.mouse-y) / graph.height); + } + } + pointer-event(e) => { + if (e.kind == PointerEventKind.up) { root.drag-end(); } + } + } + } + } + + ScrollView { + mouse-drag-pan-enabled: false; + vertical-stretch: 1; + VerticalBox { + spacing: 8px; alignment: start; + width: parent.width - 18px; + if root.bands.length == 0: Text { text: "No bands yet — add one or load a preset."; + font-family: "Inter"; color: Palette.foreground.with-alpha(0.6); } + for b[i] in root.bands: Card { + HorizontalBox { padding: 10px; spacing: 14px; + Text { text: i + 1; min-width: 14px; font-weight: 700; font-family: "Inter"; + vertical-alignment: center; } + VerticalLayout { alignment: center; + Select { field-width: 130px; + model: ["Peak", "Low shelf", "High shelf", "High pass", "Low pass"]; + current-index: b.kind; + selected(k) => { root.set(i, "kind", k); } } + } + Labeled { label: "Frequency " + b.freq-text; horizontal-stretch: 1; + AccentSlider { minimum: 0; maximum: 1; value: b.freq-pos; + changed(v) => { root.set(i, "freq", v); } } } + Labeled { label: "Gain " + b.gain-text; horizontal-stretch: 1; + AccentSlider { minimum: -15; maximum: 15; value: b.gain; + changed(v) => { root.set(i, "gain", v); } } } + Labeled { label: "Q " + b.q-text; horizontal-stretch: 1; + AccentSlider { minimum: 0; maximum: 1; value: b.q-pos; + changed(v) => { root.set(i, "q", v); } } } + VerticalLayout { alignment: center; + SquareIconButton { icon: @image-url("../assets/icons/trash.svg"); + clicked => { root.delete(i); } } } + } + } + } + } + + HorizontalLayout { spacing: 10px; + IconButton { icon: @image-url("../assets/icons/plus.svg"); label: "Add band"; + clicked => { root.add(); } } + Select { field-width: 200px; model: root.preset-names; current-index: 0; + selected(i) => { if (i > 0) { root.load-preset(i - 1); } self.current-index = 0; } } + Rectangle { horizontal-stretch: 1; } + } + HorizontalLayout { spacing: 10px; + LineEdit { height: 36px; horizontal-stretch: 1; placeholder-text: "Name for this curve…"; + text <=> root.preset-name; } + IconButton { label: "Save as preset"; enabled: root.preset-name != ""; + clicked => { root.save-preset(root.preset-name); root.preset-name = ""; } } + IconButton { label: "Done"; primary: true; clicked => { root.close(); } } + } + } + } +} + +// One effect in a strip's chain. `kind`: 0 gate · 1 EQ · 2 compressor · +// 3 normalizer · 4 limiter; `p1`…`p5` are its parameters in the order the +// rack shows them; `summary` describes an EQ. +export struct FxSlotUI { + id: int, kind: int, name: string, on: bool, + p1: float, p2: float, p3: float, p4: float, p5: float, + summary: string, +} + +// Label · slider · value with unit. +component ParamRow inherits HorizontalLayout { + in property label; + in-out property value; + in property minimum; + in property maximum; + in property unit; + in property fine: false; // one decimal instead of none + callback changed(float); + spacing: 12px; + Text { text: root.label; min-width: 90px; vertical-alignment: center; + font-family: "Inter"; font-size: 12px; } + AccentSlider { minimum: root.minimum; maximum: root.maximum; value: root.value; + horizontal-stretch: 1; + changed(v) => { root.value = v; root.changed(v); } } + Text { text: (root.fine ? Math.round(root.value * 10) / 10 : Math.round(root.value)) + " " + root.unit; + min-width: 70px; horizontal-alignment: right; vertical-alignment: center; + font-family: "Inter"; font-size: 12px; } +} + +// A strip's effect chain: effects run top to bottom; add from a list, stack, +// reorder, switch off, or load / save a whole chain as a preset. +component FxRackPopup inherits Rectangle { + in property title; + in property <[FxSlotUI]> slots; + in property <[string]> add-names; + in property <[string]> preset-names; + in-out property preset-name; + callback param(int, int, float); + callback toggle(int, bool); + callback move(int, int); + callback remove(int); + callback add(int); + callback load-preset(int); + callback save-preset(string); + callback edit-eq(int); + callback close(); + + background: #00000099; + TouchArea { clicked => { root.close(); } } + + Rectangle { + x: parent.width / 2 - self.width / 2; + y: parent.height / 2 - self.height / 2; + width: min(parent.width - 48px, 740px); height: min(parent.height - 48px, 720px); + background: Palette.background; + border-radius: 14px; border-width: 1px; border-color: Palette.border; + TouchArea {} + VerticalBox { + padding: 22px; spacing: 12px; + Text { text: root.title; font-size: 20px; font-weight: 700; font-family: "Inter"; } + Text { text: "Effects run top to bottom. Add the same one twice to stack it."; + font-family: "Inter"; font-size: 11px; color: Palette.foreground.with-alpha(0.55); } + ScrollView { + mouse-drag-pan-enabled: false; + vertical-stretch: 1; + VerticalBox { + spacing: 10px; alignment: start; + width: parent.width - 18px; + if root.slots.length == 0: Text { + text: "No effects yet — add one below or load a preset."; + font-family: "Inter"; color: Palette.foreground.with-alpha(0.6); } + for sl[i] in root.slots: Card { + VerticalBox { padding: 12px; spacing: 8px; + HorizontalLayout { spacing: 8px; + Text { text: (i + 1) + "."; min-width: 20px; vertical-alignment: center; + font-family: "Inter"; color: Palette.foreground.with-alpha(0.6); } + Text { text: sl.name; font-weight: 700; font-family: "Inter"; font-size: 14px; + vertical-alignment: center; horizontal-stretch: 1; } + MoveButton { text: "↑"; enabled: i > 0; clicked => { root.move(sl.id, -1); } } + MoveButton { text: "↓"; enabled: i < root.slots.length - 1; + clicked => { root.move(sl.id, 1); } } + VerticalLayout { alignment: center; + Toggle { checked: sl.on; toggled => { root.toggle(sl.id, self.checked); } } } + SquareIconButton { icon: @image-url("../assets/icons/trash.svg"); + clicked => { root.remove(sl.id); } } + } + VerticalLayout { spacing: 6px; opacity: sl.on ? 1.0 : 0.45; + if sl.kind == 0: VerticalLayout { spacing: 6px; + ParamRow { label: "Threshold"; minimum: -80; maximum: 0; unit: "dB"; + value: sl.p1; changed(v) => { root.param(sl.id, 1, v); } } + ParamRow { label: "Release"; minimum: 20; maximum: 1000; unit: "ms"; + value: sl.p2; changed(v) => { root.param(sl.id, 2, v); } } + } + if sl.kind == 1: HorizontalLayout { spacing: 10px; + Text { text: sl.summary; horizontal-stretch: 1; vertical-alignment: center; + font-family: "Inter"; font-size: 12px; } + IconButton { label: "Edit curve"; clicked => { root.edit-eq(sl.id); } } + } + if sl.kind == 2: VerticalLayout { spacing: 6px; + ParamRow { label: "Threshold"; minimum: -60; maximum: 0; unit: "dB"; + value: sl.p1; changed(v) => { root.param(sl.id, 1, v); } } + ParamRow { label: "Ratio"; minimum: 1; maximum: 20; unit: ": 1"; fine: true; + value: sl.p2; changed(v) => { root.param(sl.id, 2, v); } } + ParamRow { label: "Attack"; minimum: 0.5; maximum: 100; unit: "ms"; fine: true; + value: sl.p3; changed(v) => { root.param(sl.id, 3, v); } } + ParamRow { label: "Release"; minimum: 20; maximum: 1000; unit: "ms"; + value: sl.p4; changed(v) => { root.param(sl.id, 4, v); } } + ParamRow { label: "Makeup"; minimum: 0; maximum: 24; unit: "dB"; fine: true; + value: sl.p5; changed(v) => { root.param(sl.id, 5, v); } } + } + if sl.kind == 3: VerticalLayout { spacing: 6px; + ParamRow { label: "Target"; minimum: -40; maximum: -6; unit: "dB"; + value: sl.p1; changed(v) => { root.param(sl.id, 1, v); } } + ParamRow { label: "Max boost"; minimum: 0; maximum: 30; unit: "dB"; + value: sl.p2; changed(v) => { root.param(sl.id, 2, v); } } + } + if sl.kind == 4: ParamRow { label: "Ceiling"; minimum: -12; maximum: 0; unit: "dB"; + fine: true; value: sl.p1; changed(v) => { root.param(sl.id, 1, v); } } + } + } + } + } + } + HorizontalLayout { spacing: 10px; + Select { field-width: 170px; model: root.add-names; current-index: 0; + selected(i) => { if (i > 0) { root.add(i - 1); } self.current-index = 0; } } + Select { field-width: 210px; model: root.preset-names; current-index: 0; + selected(i) => { if (i > 0) { root.load-preset(i - 1); } self.current-index = 0; } } + Rectangle { horizontal-stretch: 1; } + } + HorizontalLayout { spacing: 10px; + LineEdit { height: 36px; horizontal-stretch: 1; placeholder-text: "Name for this chain…"; + text <=> root.preset-name; } + IconButton { label: "Save as preset"; enabled: root.preset-name != ""; + clicked => { root.save-preset(root.preset-name); root.preset-name = ""; } } + IconButton { label: "Done"; primary: true; clicked => { root.close(); } } + } + } + } +} + export component AppWindow inherits Window { title: "Slidr"; icon: @image-url("../assets/logo-128.png"); @@ -2358,6 +3133,79 @@ export component AppWindow inherits Window { in-out property current-page: 0; in-out property edit-mode: false; + in property <[int]> nav-pages: [0, 1, 2, 4, 3]; + in property <[NavRow]> nav-rows; + callback nav-move(int, int); + callback nav-visible(int, bool); + in-out property sounds-on: true; + in-out property sound-volume: 80; + in property <[string]> sound-sets; + in-out property sound-set-index; + + // ── Mixer page ── + in-out property mixer-enabled; + in property <[MixChannelUI]> mixer-channels; + in-out property mixer-fx-open: false; + in property mixer-fx-title; + in property <[FxSlotUI]> mixer-fx-slots; + in property <[string]> mixer-fx-add-names; + in property <[string]> mixer-fx-preset-names; + in-out property mixer-fx-preset-name; + callback mixer-edit-fx(string); + callback mixer-fx-param(int, int, float); + callback mixer-fx-toggle(int, bool); + callback mixer-fx-move(int, int); + callback mixer-fx-remove(int); + callback mixer-fx-add(int); + callback mixer-fx-load-preset(int); + callback mixer-fx-save-preset(string); + callback mixer-fx-edit-eq(int); + callback mixer-fx-closed(); + in-out property mixer-eq-open: false; + in property mixer-eq-title; + in-out property mixer-eq-on; + in property <[EqBandUI]> mixer-eq-bands; + in property mixer-eq-curve; + in property <[string]> mixer-eq-preset-names; + callback mixer-eq-toggled(bool); + callback mixer-eq-set(int, string, float); + callback mixer-eq-drag(int, float, float); + callback mixer-eq-drag-end(); + callback mixer-eq-add(); + callback mixer-eq-delete(int); + callback mixer-eq-load-preset(int); + callback mixer-eq-save-preset(string); + in-out property mixer-eq-preset-name; + callback mixer-eq-closed(); + callback mixer-move-channel(int, int); + in-out property grid-sliders; + in-out property grid-buttons; + in property <[UserPresetRow]> user-presets; + callback user-preset-rename(bool, int, string); + callback user-preset-export(bool, int); + callback user-preset-delete(bool, int); + callback user-preset-import(); + in property <[string]> mixer-spare-names; + in property <[string]> mixer-cable-names; + in property mixer-hint-download; + callback mixer-open-download(); + in-out property mixer-sink-idx; + in-out property mixer-cable-idx; + in property mixer-hint; + callback mixer-changed(); + callback mixer-channel-set(int, string, float, string); + callback mixer-add-channel(); + callback mixer-delete-channel(int); + in-out property mixer-picker-open: false; + in property mixer-picker-title; + in property <[PickerEntry]> mixer-picker-options; + in-out property mixer-picker-filter; + callback mixer-choose-programs(int); + callback mixer-toggle-program(string); + callback mixer-picker-filter-changed(string); + callback mixer-picker-closed(); + in-out property mixer-picker-tab-index; + callback mixer-picker-tab(int); in-out property assign-popup-open: false; in-out property assign-target-kind: 0; @@ -2554,22 +3402,23 @@ export component AppWindow inherits Window { } } Rectangle { height: 8px; } - NavItem { label: "Home"; compact: root.sidebar-collapsed; - icon: @image-url("../assets/icons/home.svg"); - active: root.current-page == 0; - clicked => { root.current-page = 0; root.page-changed(0); } } - NavItem { label: "Sliders"; compact: root.sidebar-collapsed; - icon: @image-url("../assets/icons/adjustments-horizontal.svg"); - active: root.current-page == 1; - clicked => { root.current-page = 1; root.edit-mode = false; root.page-changed(1); } } - NavItem { label: "Buttons"; compact: root.sidebar-collapsed; - icon: @image-url("../assets/icons/grid-dots.svg"); - active: root.current-page == 2; - clicked => { root.current-page = 2; root.edit-mode = false; root.page-changed(2); } } - NavItem { label: "Settings"; compact: root.sidebar-collapsed; - icon: @image-url("../assets/icons/settings.svg"); - active: root.current-page == 3; - clicked => { root.current-page = 3; root.edit-mode = false; root.page-changed(3); } } + // Order and visibility come from Settings (see src/sidebar.rs). + for pg in root.nav-pages: NavItem { + compact: root.sidebar-collapsed; + label: pg == 0 ? "Home" : pg == 1 ? "Sliders" : pg == 2 ? "Buttons" + : pg == 4 ? "Mixer" : "Settings"; + icon: pg == 0 ? @image-url("../assets/icons/home.svg") + : pg == 1 ? @image-url("../assets/icons/adjustments-horizontal.svg") + : pg == 2 ? @image-url("../assets/icons/grid-dots.svg") + : pg == 4 ? @image-url("../assets/icons/volume.svg") + : @image-url("../assets/icons/settings.svg"); + active: root.current-page == pg; + clicked => { + root.current-page = pg; + if (pg != 0) { root.edit-mode = false; } + root.page-changed(pg); + } + } Rectangle { vertical-stretch: 1; } @@ -2699,7 +3548,7 @@ export component AppWindow inherits Window { request-assign(k, i) => { root.open-assign(k, i); } } if root.current-page == 1: CategoriesPage { - target-kind: 0; categories: root.slider-categories; + target-kind: 0; categories: root.slider-categories; grid: root.grid-sliders; add-category(n) => { root.add-category(0, n); } rename(id, n) => { root.rename-category(0, id, n); } delete(id) => { root.delete-category(0, id); } @@ -2711,7 +3560,7 @@ export component AppWindow inherits Window { move-line(fc, fi, tc, ti) => { root.move-line(0, fc, fi, tc, ti); } } if root.current-page == 2: CategoriesPage { - target-kind: 1; categories: root.button-categories; + target-kind: 1; categories: root.button-categories; grid: root.grid-buttons; add-category(n) => { root.add-category(1, n); } rename(id, n) => { root.rename-category(1, id, n); } delete(id) => { root.delete-category(1, id); } @@ -2722,6 +3571,20 @@ export component AppWindow inherits Window { reorder-category(fid, to) => { root.reorder-category(1, fid, to); } move-line(fc, fi, tc, ti) => { root.move-line(1, fc, fi, tc, ti); } } + if root.current-page == 4: MixerPage { + enabled <=> root.mixer-enabled; + channels: root.mixer-channels; + edit-fx(t) => { root.mixer-edit-fx(t); } + hint-download: root.mixer-hint-download; + open-download => { root.mixer-open-download(); } + hint: root.mixer-hint; + changed => { root.mixer-changed(); } + channel-set(id, f, n, t) => { root.mixer-channel-set(id, f, n, t); } + move-channel(id, to) => { root.mixer-move-channel(id, to); } + add-channel => { root.mixer-add-channel(); } + delete-channel(id) => { root.mixer-delete-channel(id); } + choose-programs(id) => { root.mixer-choose-programs(id); } + } if root.current-page == 3: SettingsPage { theme-index <=> root.theme-index; accent-preset <=> root.accent-preset; @@ -2749,6 +3612,25 @@ export component AppWindow inherits Window { update-check <=> root.update-check; update-state: root.update-state; update-latest: root.update-latest; + nav-rows: root.nav-rows; + nav-move(p, d) => { root.nav-move(p, d); } + nav-visible(p, v) => { root.nav-visible(p, v); } + sounds-on <=> root.sounds-on; + sound-volume <=> root.sound-volume; + sound-sets: root.sound-sets; + sound-set-index <=> root.sound-set-index; + grid-sliders <=> root.grid-sliders; + grid-buttons <=> root.grid-buttons; + user-presets: root.user-presets; + spare-names: root.mixer-spare-names; + cable-names: root.mixer-cable-names; + sink-idx <=> root.mixer-sink-idx; + cable-idx <=> root.mixer-cable-idx; + mixer-devices-changed => { root.mixer-changed(); } + preset-rename(e, i, n) => { root.user-preset-rename(e, i, n); } + preset-export(e, i) => { root.user-preset-export(e, i); } + preset-delete(e, i) => { root.user-preset-delete(e, i); } + preset-import => { root.user-preset-import(); } check-for-updates => { root.check-for-updates(); } open-releases => { root.open-releases(); } theme-changed(i) => { root.theme-changed(i); } @@ -2920,6 +3802,49 @@ export component AppWindow inherits Window { } } + if root.mixer-fx-open: FxRackPopup { + title: root.mixer-fx-title; + slots: root.mixer-fx-slots; + add-names: root.mixer-fx-add-names; + preset-names: root.mixer-fx-preset-names; + preset-name <=> root.mixer-fx-preset-name; + param(id, n, v) => { root.mixer-fx-param(id, n, v); } + toggle(id, on) => { root.mixer-fx-toggle(id, on); } + move(id, d) => { root.mixer-fx-move(id, d); } + remove(id) => { root.mixer-fx-remove(id); } + add(k) => { root.mixer-fx-add(k); } + load-preset(i) => { root.mixer-fx-load-preset(i); } + save-preset(n) => { root.mixer-fx-save-preset(n); } + edit-eq(id) => { root.mixer-fx-edit-eq(id); } + close => { root.mixer-fx-open = false; root.mixer-fx-closed(); } + } + if root.mixer-eq-open: EqEditorPopup { + title: root.mixer-eq-title; + on <=> root.mixer-eq-on; + bands: root.mixer-eq-bands; + curve: root.mixer-eq-curve; + preset-names: root.mixer-eq-preset-names; + toggled(v) => { root.mixer-eq-toggled(v); } + set(i, f, v) => { root.mixer-eq-set(i, f, v); } + drag(i, x, y) => { root.mixer-eq-drag(i, x, y); } + drag-end => { root.mixer-eq-drag-end(); } + add => { root.mixer-eq-add(); } + delete(i) => { root.mixer-eq-delete(i); } + load-preset(i) => { root.mixer-eq-load-preset(i); } + preset-name <=> root.mixer-eq-preset-name; + save-preset(n) => { root.mixer-eq-save-preset(n); } + close => { root.mixer-eq-open = false; root.mixer-eq-closed(); } + } + if root.mixer-picker-open: ProgramPickerPopup { + title: root.mixer-picker-title; + options: root.mixer-picker-options; + filter <=> root.mixer-picker-filter; + tab <=> root.mixer-picker-tab-index; + tab-changed(t) => { root.mixer-picker-tab(t); } + toggle(v) => { root.mixer-toggle-program(v); } + filter-changed(t) => { root.mixer-picker-filter-changed(t); } + close => { root.mixer-picker-open = false; root.mixer-picker-closed(); } + } if root.preset-popup-open: PresetPopup { active-name: root.active-preset-name; presets: root.preset-names;