From af222d2869a6b748439bd834a2f6645315bd7b8d Mon Sep 17 00:00:00 2001 From: jli-software <123122329+jli-software@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:45:46 +0100 Subject: [PATCH] feat: hold the microphone level (1.2.0) Settings -> Recording -> Level keeps the microphone at a chosen level while Utterform runs and sets it back every two seconds when Teams or another app changed it. Windows uses IAudioEndpointVolume on the chosen microphone, Linux wpctl or pactl on the default source, macOS osascript. Switched off, the level is never touched. App version, installers, README, changelog and release notes move to v1.2.0. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 + README.md | 3 +- docs/ARCHITECTURE.md | 27 +++ docs/DEVELOPMENT.md | 14 +- docs/plans/v1.0-roadmap.md | 1 + docs/releases/v1.2.0.md | 32 +++ package-lock.json | 4 +- package.json | 2 +- scripts/install-linux.sh | 2 +- scripts/install-windows.ps1 | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 4 +- src-tauri/src/commands.rs | 12 + src-tauri/src/domain.rs | 4 + src-tauri/src/lib.rs | 4 + src-tauri/src/mic_level.rs | 364 ++++++++++++++++++++++++++++++ src-tauri/src/tray_microphones.rs | 19 +- src-tauri/tauri.conf.json | 2 +- src/App.svelte | 33 +++ src/app.css | 4 + src/lib/api.ts | 2 + src/lib/settings-catalog.test.ts | 2 +- src/lib/settings-catalog.ts | 3 +- src/lib/types.ts | 3 + tests/ui.e2e.ts | 34 +++ 25 files changed, 570 insertions(+), 19 deletions(-) create mode 100644 docs/releases/v1.2.0.md create mode 100644 src-tauri/src/mic_level.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d07020..c1c51d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 1.2.0 — v1.2.0 · The microphone stays where you put it + +- Add **Level** to Settings → Recording. Switched on, it starts at the level the microphone has now; the slider sets it from 0 to 100 %. While Utterform runs it looks every two seconds and sets the level back when Teams, Zoom, Chrome or anything else has changed it. Switched off, Utterform never touches the level. +- Windows: the microphone chosen for recordings, else the default one — the same slider as in Sound settings. Linux: the default input of PipeWire (`wpctl`) or PulseAudio (`pactl`). macOS: the default input. +- Choosing another microphone in the tray moves the held level to it. +- Where the level cannot be read, the switch stays off and says why. +- App version 1.2.0; the installers install v1.2.0 by default. + +See [1.2.0 release notes](docs/releases/v1.2.0.md). + ## 1.1.0 — v1.1.0 · The right microphone, one click away - Add **Microphone** to the tray menu: **System default (name)** and every microphone, the one in use checked. A click makes it the microphone for the next recordings — the laptop's own while the headset for calls stays plugged in. It is the same setting as Settings → Recording → Microphone, saved at once, and the window follows. diff --git a/README.md b/README.md index e38d17c..068677f 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Utterform records a short voice clip, transcribes it with **OpenAI GPT Transcrib the cursor. The window can stay out of the way while a global shortcut controls the whole flow. -> **1.1.0 — The right microphone, one click away** switches the microphone from the tray menu and brings everything since 0.8.0: a first-run assistant, Settings with search, ChatGPT text actions, Context, an action per app and audio files. The Apple-Silicon build is Developer ID signed and notarized; Windows remains unsigned. See the [release notes](docs/releases/v1.1.0.md) and [downloads](https://github.com/jli-software/utterform/releases/tag/v1.1.0). +> **1.2.0 — The microphone stays where you put it** holds the microphone level you choose, even when Teams or another app turns it up or down. 1.1.0 brought the microphone in the tray menu and everything since 0.8.0: a first-run assistant, Settings with search, ChatGPT text actions, Context, an action per app and audio files. The Apple-Silicon build is Developer ID signed and notarized; Windows remains unsigned. See the [release notes](docs/releases/v1.2.0.md) and [downloads](https://github.com/jli-software/utterform/releases/tag/v1.2.0). ## How it works @@ -333,6 +333,7 @@ Push/PR CI runs Linux validation without release compilation. For a test binary - A vocabulary of your own terms, and a reasoning effort for the text step - Clipboard, TXT, Markdown, or combined output - Selectable microphone with a system-default fallback, in Settings and in the tray menu +- Hold the microphone level, so other apps cannot change how loud you record - Global dictation with a reserved key combination on Windows, macOS and X11, or a compositor binding on Wayland: either way without raising the window - Optional typing of the finished text into the focused window, next to clipboard and file, as one paste or as keystrokes (Linux and Windows) - A dictation key recorded by pressing it, read by physical key so the layout cannot change it diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c2d2e00..a30e9b9 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -294,6 +294,33 @@ Linux has no such hover event; there the list follows the window. **Diagnostics.** `tray.microphone_chosen` (`default` or `device`) and `tray.microphones_failed` (step). Device names are not logged. +## Microphone level (1.2.0) + +**What it holds.** `settings.microphone_level`: a percentage, or `None` to leave the +level to the operating system (the default, and what an older settings file reads as). +`mic_level.rs` runs one thread, started in `setup`, that receives the target over a +channel — from `init` (the settings file), `save_settings`, `complete_onboarding` and the +tray's microphone choice — and, while a level is set, reads it every two seconds and +writes it back when it is more than one point away (rounding in the system moves it by +one). Nothing is read or written while no level is set. + +**Per system.** Windows: `IMMDeviceEnumerator::GetDevice` with the endpoint id from the +cpal id `wasapi:`, else the default capture endpoint, then +`IAudioEndpointVolume::{Get,Set}MasterVolumeLevelScalar` — the Sound settings slider; +COM is initialised once on the thread. Linux: `wpctl get-volume/set-volume +@DEFAULT_AUDIO_SOURCE@`, else `pactl get-source-volume/set-source-volume +@DEFAULT_SOURCE@`; the ALSA ids Utterform records from do not map to PipeWire nodes, so it +is always the default source. macOS: `osascript` `input volume of (get volume settings)` / +`set volume input volume`, the default input. + +**Settings.** Recording → Level: the switch reads the current level +(`read_microphone_level`) and starts there; if that fails the switch stays off and the +reason is shown. The slider saves after 300 ms. + +**Diagnostics.** `mic_level.target` (level or `off`), `mic_level.restored` (from, to; at +most once a minute), `mic_level.failed` once until it works again, +`mic_level.read_failed`. No device names. + ## First-run assistant (0.14.0) **Who sees it.** `settings.onboarding_completed`. `AppSettings::default()` — what `load` diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index fdfec1b..718ab79 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -19,7 +19,19 @@ proposals are in [plans/v1.0-roadmap.md](plans/v1.0-roadmap.md). The plan of 202 (reliability, assistant, finish) is superseded; 0.7.13 shipped, 0.7.14 (`feat/simple-settings`) was rejected and will not be merged. -## Current handoff — 1.1.0 Microphone in the tray +## Current handoff — 1.2.0 Hold the microphone level + +Branch `work/utterform-1.2.0-hold-mic-level`. Jonas asked on 2026-11-13 for the +microphone to stay set (a RØDE NT-USB on Windows; its knobs only change the headphone +mix) without a separate tool, on Windows and Linux alike. Settings → Recording → Level +holds a level while Utterform runs. Design in +[ARCHITECTURE.md → Microphone level](ARCHITECTURE.md#microphone-level-120). Released +directly as 1.2.0 like 1.1.0. CBZIT007 has no Visual C++ Build Tools: the Windows code was +compiled by `Desktop builds`, the frontend and its Playwright tests ran locally (Edge via +`PLAYWRIGHT_CHROMIUM_EXECUTABLE`). Open: a real check with the NT-USB and Teams running, +and Linux on a PipeWire desktop. + +## Previous handoff — 1.1.0 Microphone in the tray Branch `work/utterform-1.1.0-tray-microphone`. Jonas asked on 2026-11-13 for a quick microphone switch in the tray (headset plugged in for calls, but dictating into the diff --git a/docs/plans/v1.0-roadmap.md b/docs/plans/v1.0-roadmap.md index 4ac30e6..95a3fe1 100644 --- a/docs/plans/v1.0-roadmap.md +++ b/docs/plans/v1.0-roadmap.md @@ -79,6 +79,7 @@ Proposals not yet confirmed by Jonas are marked **Proposal**. | 0.14.0 | First-run assistant | merged, ships with 1.0 | | 1.0.0 | Acceptance on Linux, Windows, macOS; release; WinGet | rc.1 published; not released on its own — ships as 1.1.0 | | 1.1.0 | Microphone in the tray; first stable release since 0.8.0 | released (2026-11-13) | +| 1.2.0 | Hold the microphone level | released (2026-11-13) | **Proposal (order):** the Omarchy backend first because it is small and the CLI already exists; Settings next so every later feature gets a fixed place; Codex before context diff --git a/docs/releases/v1.2.0.md b/docs/releases/v1.2.0.md new file mode 100644 index 0000000..aab99c9 --- /dev/null +++ b/docs/releases/v1.2.0.md @@ -0,0 +1,32 @@ +# Utterform 1.2.0 — The microphone stays where you put it + +**Hold the microphone level.** How loud a microphone records is set by the operating +system, and other programs change it without asking: Teams, Zoom and Chrome turn it up +and down on their own. Open Settings → Recording and switch on **Level**. It starts at the +level your microphone has now; move the slider to where it sounds right. From then on +Utterform keeps it there while it runs and sets it back within two seconds when something +else changed it — for dictation, for a call, for a video you record. + +Switch it off and Utterform leaves the level alone again. + +- **Windows:** the microphone chosen for recordings, otherwise the default one. It is the + same level as in Sound settings → Input. +- **Linux:** the default input of PipeWire or PulseAudio. +- **macOS:** the default input. + +A tip for USB microphones with knobs, such as the RØDE NT-USB: the knobs set what you +hear in the headphones. What is recorded is set here. + +## Install + +Quit Utterform first. + +- **Linux (Omarchy / Arch):** + `curl -fsSL https://github.com/jli-software/utterform/releases/download/v1.2.0/install-linux.sh | sh` +- **Windows (PowerShell):** + `irm https://github.com/jli-software/utterform/releases/download/v1.2.0/install-windows.ps1 | iex` +- **macOS:** the [DMG](https://github.com/jli-software/utterform/releases/download/v1.2.0/utterform-macos-aarch64.dmg), + Developer ID signed and notarized. + +Windows builds remain unsigned and may trigger SmartScreen. Verify downloads against +`SHA256SUMS.txt`. diff --git a/package-lock.json b/package-lock.json index b7b282e..1aa1e5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "utterform", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "utterform", - "version": "1.1.0", + "version": "1.2.0", "dependencies": { "@tauri-apps/api": "^2.11.1", "@tauri-apps/plugin-dialog": "^2.4.0", diff --git a/package.json b/package.json index 89af544..87806f4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "utterform", "private": true, - "version": "1.1.0", + "version": "1.2.0", "type": "module", "scripts": { "dev": "vite", diff --git a/scripts/install-linux.sh b/scripts/install-linux.sh index 6891cf6..259e1c1 100755 --- a/scripts/install-linux.sh +++ b/scripts/install-linux.sh @@ -2,7 +2,7 @@ set -eu -version="${UTTERFORM_VERSION:-v1.1.0}" +version="${UTTERFORM_VERSION:-v1.2.0}" release_base="${UTTERFORM_RELEASE_BASE_URL:-https://github.com/jli-software/utterform/releases/download/${version}}" asset="utterform-linux-x86_64-system.tar.gz" prefix="${UTTERFORM_PREFIX:-${HOME}/.local}" diff --git a/scripts/install-windows.ps1 b/scripts/install-windows.ps1 index 3ccabcd..26e66bd 100644 --- a/scripts/install-windows.ps1 +++ b/scripts/install-windows.ps1 @@ -1,6 +1,6 @@ # Verify the NSIS setup before opening it; no elevation or forced app shutdown. param( - [string]$Version = 'v1.1.0', + [string]$Version = 'v1.2.0', [switch]$VerifyOnly ) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index e8270af..1d2afbd 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -5904,7 +5904,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] name = "utterform" -version = "1.1.0" +version = "1.2.0" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 69c56e0..f17a50e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "utterform" -version = "1.1.0" +version = "1.2.0" description = "Fast, lightweight voice-to-text transformation for the desktop" authors = ["Jonas"] edition = "2024" @@ -66,6 +66,8 @@ zbus = "5.9" [target.'cfg(target_os = "windows")'.dependencies] windows-sys = { version = "0.61", features = ["Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging", "Win32_UI_Accessibility", "Win32_System_Threading", "Win32_Security", "Win32_Foundation"] } winreg = "0.55" +# The microphone level (`mic_level`). Already in the build through cpal. +windows = { version = "0.62", features = ["Win32_Media_Audio", "Win32_Media_Audio_Endpoints", "Win32_System_Com", "Win32_System_Com_StructuredStorage"] } [target.'cfg(target_os = "macos")'.dependencies] # Typing at the cursor posts keyboard events through CoreGraphics; the frontmost diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index a445553..83041bb 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -114,6 +114,7 @@ pub fn save_settings(app: AppHandle, value: AppSettings) -> Result<(), String> { app.state::() .set(!value.onboarding_completed); crate::tray_microphones::set_selected(&app, value.input_device.clone()); + crate::mic_level::set(&app, value.microphone_level, value.input_device.clone()); diagnostics::info!( "settings.saved", engine = value.engine.as_str(), @@ -152,6 +153,7 @@ pub fn complete_onboarding( } app.state::().set(false); crate::tray_microphones::set_selected(&app, value.input_device.clone()); + crate::mic_level::set(&app, value.microphone_level, value.input_device.clone()); if skipped { diagnostics::info!("onboarding.skipped"); } else { @@ -178,6 +180,16 @@ pub fn list_input_devices(app: AppHandle) -> Result, String Ok(devices) } +/// The microphone's level now, in percent, for Settings → Recording → Level +/// to start from. An error says why this system cannot hold the level. +#[tauri::command] +pub fn read_microphone_level(device: Option) -> Result { + crate::mic_level::read(device.as_deref()).map_err(|error| { + diagnostics::warning!("mic_level.read_failed", error = error); + error + }) +} + /// Open the microphone and play the start cue. /// /// Asynchronous so the work leaves the main thread: a synchronous command runs diff --git a/src-tauri/src/domain.rs b/src-tauri/src/domain.rs index caf2993..6e923cb 100644 --- a/src-tauri/src/domain.rs +++ b/src-tauri/src/domain.rs @@ -143,6 +143,9 @@ pub struct AppSettings { pub engine: TranscriptionEngine, pub cloud_model: CloudModel, pub input_device: Option, + /// The microphone level to hold, in percent; `None` leaves it to the + /// operating system. See `mic_level`. + pub microphone_level: Option, pub output_directory: Option, pub copy_to_clipboard: bool, pub save_to_file: bool, @@ -227,6 +230,7 @@ impl Default for AppSettings { engine: TranscriptionEngine::OpenAi, cloud_model: CloudModel::GptTranscribe, input_device: None, + microphone_level: None, output_directory: None, copy_to_clipboard: true, save_to_file: false, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 5c34184..d929961 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -15,6 +15,7 @@ mod hotkey; mod live; #[cfg(target_os = "macos")] mod macos; +mod mic_level; mod models; mod output; mod platform; @@ -271,6 +272,7 @@ fn setup(app: &mut tauri::App, startup_intent: Intent) -> Result<(), Box, + pub device: Option, +} + +/// The way to the watcher thread; `None` until [`init`] started it. +#[derive(Default)] +pub struct MicLevel(Mutex>>); + +/// Start watching with the level in the settings file. +pub fn init(app: &AppHandle) { + let (sender, receiver) = mpsc::channel(); + let started = thread::Builder::new() + .name("mic-level".into()) + .spawn(move || watch(receiver)); + if let Err(error) = started { + diagnostics::warning!("mic_level.thread_failed", error = error); + return; + } + if let Ok(mut slot) = app.state::().0.lock() { + *slot = Some(sender); + } + if let Ok(value) = settings::load(app) { + set(app, value.microphone_level, value.input_device); + } +} + +/// Hold `level` (or stop holding with `None`) on `device`. Applied at once. +pub fn set(app: &AppHandle, level: Option, device: Option) { + let target = Target { + level: level.map(|level| level.min(100)), + device, + }; + if let Ok(slot) = app.state::().0.lock() + && let Some(sender) = slot.as_ref() + { + let _ = sender.send(target); + } +} + +fn watch(receiver: mpsc::Receiver) { + platform::init_thread(); + let mut target = Target::default(); + let mut last_log: Option = None; + let mut failing = false; + loop { + let wait = if target.level.is_some() { + INTERVAL + } else { + Duration::from_secs(3600) + }; + match receiver.recv_timeout(wait) { + Ok(next) => { + if next.level != target.level { + diagnostics::info!( + "mic_level.target", + level = next + .level + .map_or_else(|| "off".to_string(), |level| level.to_string()), + ); + } + target = next; + failing = false; + } + Err(mpsc::RecvTimeoutError::Timeout) => {} + Err(mpsc::RecvTimeoutError::Disconnected) => return, + } + let Some(level) = target.level else { continue }; + match hold(level, target.device.as_deref()) { + Ok(Some(previous)) => { + failing = false; + if last_log.is_none_or(|at| at.elapsed() >= LOG_INTERVAL) { + last_log = Some(Instant::now()); + diagnostics::info!("mic_level.restored", from = previous, to = level); + } + } + Ok(None) => failing = false, + Err(error) => { + if !failing { + failing = true; + diagnostics::warning!("mic_level.failed", error = error); + } + } + } + } +} + +/// Set the level back if it moved; `Some(previous)` when it had to. +fn hold(level: u8, device: Option<&str>) -> Result, String> { + let current = platform::read(device)?; + if !needs_correction(current, level) { + return Ok(None); + } + platform::write(device, level)?; + Ok(Some(current)) +} + +/// Rounding in the operating system moves a level by a point; that is no change. +fn needs_correction(current: u8, target: u8) -> bool { + current.abs_diff(target) > 1 +} + +/// The level now, for Settings to start the slider at. +pub fn read(device: Option<&str>) -> Result { + platform::read(device) +} + +fn percent(fraction: f32) -> u8 { + (fraction.clamp(0.0, 1.0) * 100.0).round() as u8 +} + +/// `wpctl get-volume`: "Volume: 0.40" or "Volume: 0.40 [MUTED]". +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +fn parse_wpctl(output: &str) -> Option { + let value = output + .trim() + .strip_prefix("Volume:")? + .split_whitespace() + .next()?; + value.parse::().ok().map(percent) +} + +/// `pactl get-source-volume`: "Volume: front-left: 26214 / 40% / -23.88 dB, …". +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +fn parse_pactl(output: &str) -> Option { + output + .split_whitespace() + .find_map(|word| word.strip_suffix('%')?.parse::().ok()) + .map(|value| value.min(100) as u8) +} + +/// `osascript -e "input volume of (get volume settings)"`: "75", or +/// "missing value" for a microphone without a level. +#[cfg_attr(not(target_os = "macos"), allow(dead_code))] +fn parse_osascript(output: &str) -> Option { + output + .trim() + .parse::() + .ok() + .map(|value| value.min(100) as u8) +} + +#[cfg(target_os = "windows")] +mod platform { + use windows::{ + Win32::{ + Media::Audio::{ + Endpoints::IAudioEndpointVolume, IMMDevice, IMMDeviceEnumerator, + MMDeviceEnumerator, eCapture, eConsole, + }, + System::Com::{CLSCTX_ALL, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx}, + }, + core::HSTRING, + }; + + pub fn init_thread() { + // The watcher thread is Utterform's own; nothing else initialised COM on it. + let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) }; + } + + /// The chosen microphone (cpal names it `wasapi:`), else + /// the default input — the same fallback a recording takes. + fn endpoint(device: Option<&str>) -> Result { + unsafe { + let enumerator: IMMDeviceEnumerator = + CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL) + .map_err(|error| format!("enumerator: {error}"))?; + let chosen: Option = device + .and_then(|id| id.strip_prefix("wasapi:")) + .and_then(|id| enumerator.GetDevice(&HSTRING::from(id)).ok()); + let device = match chosen { + Some(device) => device, + None => enumerator + .GetDefaultAudioEndpoint(eCapture, eConsole) + .map_err(|error| format!("no microphone: {error}"))?, + }; + device + .Activate::(CLSCTX_ALL, None) + .map_err(|error| format!("volume: {error}")) + } + } + + pub fn read(device: Option<&str>) -> Result { + let volume = endpoint(device)?; + let level = unsafe { volume.GetMasterVolumeLevelScalar() } + .map_err(|error| format!("read: {error}"))?; + Ok(super::percent(level)) + } + + pub fn write(device: Option<&str>, level: u8) -> Result<(), String> { + let volume = endpoint(device)?; + unsafe { volume.SetMasterVolumeLevelScalar(f32::from(level) / 100.0, std::ptr::null()) } + .map_err(|error| format!("write: {error}")) + } +} + +#[cfg(target_os = "linux")] +mod platform { + use std::process::Command; + + pub fn init_thread() {} + + fn run(program: &str, arguments: &[&str]) -> Option { + let output = Command::new(program).args(arguments).output().ok()?; + output + .status + .success() + .then(|| String::from_utf8_lossy(&output.stdout).into_owned()) + } + + pub fn read(_device: Option<&str>) -> Result { + if let Some(output) = run("wpctl", &["get-volume", "@DEFAULT_AUDIO_SOURCE@"]) { + return super::parse_wpctl(&output).ok_or_else(|| "wpctl: unreadable".into()); + } + if let Some(output) = run("pactl", &["get-source-volume", "@DEFAULT_SOURCE@"]) { + return super::parse_pactl(&output).ok_or_else(|| "pactl: unreadable".into()); + } + Err("neither wpctl (PipeWire) nor pactl (PulseAudio) answered".into()) + } + + pub fn write(_device: Option<&str>, level: u8) -> Result<(), String> { + let fraction = format!("{:.2}", f32::from(level) / 100.0); + if run( + "wpctl", + &["set-volume", "@DEFAULT_AUDIO_SOURCE@", &fraction], + ) + .is_some() + { + return Ok(()); + } + let percent = format!("{level}%"); + if run( + "pactl", + &["set-source-volume", "@DEFAULT_SOURCE@", &percent], + ) + .is_some() + { + return Ok(()); + } + Err("neither wpctl (PipeWire) nor pactl (PulseAudio) set the level".into()) + } +} + +#[cfg(target_os = "macos")] +mod platform { + use std::process::Command; + + pub fn init_thread() {} + + fn osascript(script: &str) -> Result { + let output = Command::new("osascript") + .args(["-e", script]) + .output() + .map_err(|error| format!("osascript: {error}"))?; + if !output.status.success() { + return Err("osascript failed".into()); + } + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) + } + + pub fn read(_device: Option<&str>) -> Result { + let output = osascript("input volume of (get volume settings)")?; + super::parse_osascript(&output).ok_or_else(|| "this microphone has no level".into()) + } + + pub fn write(_device: Option<&str>, level: u8) -> Result<(), String> { + osascript(&format!("set volume input volume {level}")).map(|_| ()) + } +} + +#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))] +mod platform { + pub fn init_thread() {} + + pub fn read(_device: Option<&str>) -> Result { + Err("not available on this system".into()) + } + + pub fn write(_device: Option<&str>, _level: u8) -> Result<(), String> { + Err("not available on this system".into()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_point_of_rounding_is_no_change() { + assert!(!needs_correction(70, 70)); + assert!(!needs_correction(69, 70)); + assert!(!needs_correction(71, 70)); + assert!(needs_correction(68, 70)); + assert!(needs_correction(100, 70)); + assert!(needs_correction(0, 70)); + } + + #[test] + fn wpctl_volume_is_read_muted_or_not() { + assert_eq!(parse_wpctl("Volume: 0.40\n"), Some(40)); + assert_eq!(parse_wpctl("Volume: 0.73 [MUTED]\n"), Some(73)); + assert_eq!(parse_wpctl("Volume: 1.50\n"), Some(100)); + assert_eq!(parse_wpctl("nothing\n"), None); + } + + #[test] + fn pactl_volume_takes_the_first_channel() { + let output = "Volume: front-left: 26214 / 40% / -23.88 dB, front-right: 26214 / 40% / -23.88 dB\n balance 0.00\n"; + assert_eq!(parse_pactl(output), Some(40)); + assert_eq!( + parse_pactl("Volume: mono: 98304 / 150% / 10.57 dB"), + Some(100) + ); + assert_eq!(parse_pactl("No such entity"), None); + } + + #[test] + fn osascript_level_or_none() { + assert_eq!(parse_osascript("75\n"), Some(75)); + assert_eq!(parse_osascript("missing value\n"), None); + } + + #[test] + fn fractions_round_to_whole_percent() { + assert_eq!(percent(0.0), 0); + assert_eq!(percent(0.704), 70); + assert_eq!(percent(1.2), 100); + assert_eq!(percent(-0.1), 0); + } +} diff --git a/src-tauri/src/tray_microphones.rs b/src-tauri/src/tray_microphones.rs index 05f09a6..9a27852 100644 --- a/src-tauri/src/tray_microphones.rs +++ b/src-tauri/src/tray_microphones.rs @@ -182,14 +182,19 @@ pub fn refresh(app: &AppHandle, throttled: bool) { pub fn choose(app: &AppHandle, selected: Option) { let saved = settings::load(app).and_then(|mut value| { value.input_device = selected.clone(); - settings::save(app, &value) + settings::save(app, &value).map(|()| value.microphone_level) }); - if let Err(failure) = saved { - diagnostics::warning!("tray.microphone_choice_failed", class = failure.class()); - // Show the choice that is still in force. - update(app, |_| {}); - return; - } + let level = match saved { + Ok(level) => level, + Err(failure) => { + diagnostics::warning!("tray.microphone_choice_failed", class = failure.class()); + // Show the choice that is still in force. + update(app, |_| {}); + return; + } + }; + // A held level follows the microphone to the one now chosen. + crate::mic_level::set(app, level, selected.clone()); diagnostics::info!( "tray.microphone_chosen", microphone = if selected.is_some() { diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index d323c24..a045f41 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Utterform", - "version": "1.1.0", + "version": "1.2.0", "identifier": "software.jli.utterform", "build": { "beforeDevCommand": "npm run dev", diff --git a/src/App.svelte b/src/App.svelte index 2e2cae5..8323b84 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -64,6 +64,7 @@ /// How long a text field waits after the last keystroke before it is written. const TYPING_SAVE_DELAY_MS = 500; + const LEVEL_SAVE_DELAY_MS = 300; const ENGINES: Array<{ mode: TranscriptionMode; label: string; title: string }> = [ { mode: "gpt_transcribe", label: "Cloud", title: "GPT Transcribe: finished text, audio is sent to OpenAI" }, { mode: "gpt_live_transcribe", label: "Live", title: "GPT Live Transcribe: text appears at the cursor while you speak" }, @@ -119,6 +120,7 @@ // appears and then turns out to be dead is worse than one that arrives late. let hotkeySupport: HotkeySupport = { supported: false, default: "Ctrl+Alt+D", explanation: "", failure: null }; let hotkeyError = ""; + let levelMessage = ""; /// While the recorder is listening, the key it is listening for must not be /// held by the operating system on Utterform's behalf, or pressing it would /// start a recording instead of being read. Released for that moment only, @@ -1007,6 +1009,23 @@ /// Put a change into effect and write it. `delay` is for text fields: the /// write waits for a pause in typing, and the next change restarts the wait. + /// Holding starts at the level the microphone has now, so switching it on + /// changes nothing you hear until the slider moves. + async function toggleMicrophoneLevel(toggle: HTMLInputElement) { + levelMessage = ""; + if (!toggle.checked) { + change({ microphone_level: null }); + return; + } + try { + const level = await api.readMicrophoneLevel(settings.input_device); + change({ microphone_level: Math.max(0, Math.min(100, Math.round(level))) }); + } catch (error) { + levelMessage = `Utterform cannot set the microphone level here: ${String(error)}`; + toggle.checked = false; + } + } + function change(patch: Partial, delay = 0) { settings = { ...settings, ...patch }; scheduleSave(delay); @@ -1658,6 +1677,20 @@
{@render rowHead("microphone", "Microphone")}
change({ input_device: value || null })} />
{/if} + {#if shownRows.has("mic-level")} +
{@render rowHead("mic-level", "Level", settings.microphone_level !== null ? "mic-level" : null)} +
+ {#if settings.microphone_level !== null} + change({ microphone_level: Number(event.currentTarget.value) }, LEVEL_SAVE_DELAY_MS)} /> + {settings.microphone_level}% + {/if} + toggleMicrophoneLevel(event.currentTarget)} /> +
+ {#if levelMessage}{/if} +
+ {/if} {#if shownRows.has("shortcut")}
{@render rowHead("shortcut", "Shortcut")}
diff --git a/src/app.css b/src/app.css index 2bcf5c7..35973ef 100644 --- a/src/app.css +++ b/src/app.css @@ -118,6 +118,10 @@ main:not(.has-result) footer { margin-top:20px; } .row-control.wide { flex:1; max-width:400px; } .row-message { flex-basis:100%; margin:0; text-align:right; } .row-note { color:var(--muted); font-size:11px; line-height:1.5; } +.level-slider { width:140px; height:20px; min-height:0; padding:0; border:none; background:transparent; accent-color:var(--accent); cursor:pointer; } +.level-slider:focus { border:none; outline:none; } +.level-slider:focus-visible { outline:2px solid var(--accent); outline-offset:2px; } +.level-value { min-width:32px; text-align:right; font-variant-numeric:tabular-nums; } .row-input { width:min(300px,100%); min-height:34px; height:34px; font-size:12px; } .row-input.narrow { width:76px; } .row-button { min-height:30px; padding:0 10px; border:1px solid var(--line); border-radius:5px; background:transparent; color:var(--text); font-size:12px; white-space:nowrap; } diff --git a/src/lib/api.ts b/src/lib/api.ts index 1df6462..7a21e25 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -61,6 +61,8 @@ export const api = { copyText: (text: string) => invoke("copy_text", { text }), saveSettings: (value: AppSettings) => invoke("save_settings", { value }), listInputDevices: () => invoke("list_input_devices"), + // The microphone's level now, in percent; rejects where it cannot be held. + readMicrophoneLevel: (device: string | null) => invoke("read_microphone_level", { device }), startRecording: ( inputDevice: string | null, engine: Engine, diff --git a/src/lib/settings-catalog.test.ts b/src/lib/settings-catalog.test.ts index 605e03f..da642bd 100644 --- a/src/lib/settings-catalog.test.ts +++ b/src/lib/settings-catalog.test.ts @@ -9,7 +9,7 @@ describe("the settings catalog", () => { }); it("shows a category's own rows when nothing is searched", () => { - expect([...visibleRows("recording", " ")]).toEqual(["microphone", "shortcut", "sounds"]); + expect([...visibleRows("recording", " ")]).toEqual(["microphone", "mic-level", "shortcut", "sounds"]); }); it("finds rows across categories by label, keyword or category, ignoring case", () => { diff --git a/src/lib/settings-catalog.ts b/src/lib/settings-catalog.ts index 86dccc0..c09931f 100644 --- a/src/lib/settings-catalog.ts +++ b/src/lib/settings-catalog.ts @@ -14,7 +14,7 @@ export const SETTINGS_CATEGORIES: Array<{ id: SettingsCategory; label: string }> ]; export type SettingsRowId = - | "microphone" | "shortcut" | "sounds" + | "microphone" | "mic-level" | "shortcut" | "sounds" | "engine" | "api-key" | "language" | "vocabulary" | "context" | "local-model" | "actions" | "text-provider" | "text-model" | "context-selection" | "context-clipboard" | "app-actions" @@ -31,6 +31,7 @@ export interface SettingsRow { export const SETTINGS_ROWS: SettingsRow[] = [ { id: "microphone", category: "recording", label: "Microphone", keywords: ["mic", "input", "device", "audio"] }, + { id: "mic-level", category: "recording", label: "Level", keywords: ["volume", "gain", "loud", "quiet", "input level", "hold", "lock", "teams", "pegel"] }, { id: "shortcut", category: "recording", label: "Shortcut", keywords: ["hotkey", "key", "keyboard", "dictation", "global"] }, { id: "sounds", category: "recording", label: "Sounds", keywords: ["sound", "cue", "beep", "chime", "click", "test"] }, { id: "engine", category: "transcription", label: "Engine", keywords: ["cloud", "live", "local", "whisper", "gpt", "transcribe", "mode", "offline"] }, diff --git a/src/lib/types.ts b/src/lib/types.ts index 3e96ed3..0b387ff 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -33,6 +33,8 @@ export interface AppSettings { engine: Engine; cloud_model: CloudModel; input_device: string | null; + /// The microphone level Utterform holds, in percent; `null` leaves it alone. + microphone_level: number | null; output_directory: string | null; copy_to_clipboard: boolean; save_to_file: boolean; @@ -181,6 +183,7 @@ export const DEFAULT_SETTINGS: AppSettings = { engine: "open_ai", cloud_model: "gpt_transcribe", input_device: null, + microphone_level: null, output_directory: null, copy_to_clipboard: true, save_to_file: false, diff --git a/tests/ui.e2e.ts b/tests/ui.e2e.ts index bf3d9ff..c24702b 100644 --- a/tests/ui.e2e.ts +++ b/tests/ui.e2e.ts @@ -81,6 +81,7 @@ test.beforeEach(async ({ page }) => { case "codex_status": return { signedIn: !Reflect.get(window, "__codexSignedOut"), models: [{ id: "gpt-6.1-sol", name: "GPT-6.1-Sol" }, { id: "gpt-6-luna", name: "GPT-6-Luna" }], defaultModel: "gpt-6-luna" }; case "open_codex_setup": (Reflect.get(window, "__supportCalls") as string[]).push("codex-setup"); return; case "list_input_devices": return [{ id: "test-mic", name: "Studio microphone", isDefault: true }]; + case "read_microphone_level": if (Reflect.get(window, "__levelUnavailable")) throw "neither wpctl (PipeWire) nor pactl (PulseAudio) answered"; return 63; case "list_local_models": return models; case "download_local_model": case "delete_local_model": { const model = models.find((model) => model.id === args.modelId); @@ -927,6 +928,39 @@ test("text actions can run on the ChatGPT subscription, marked experimental", as await page.screenshot({ path: testInfo.outputPath("ai-chatgpt-model.png") }); }); +test("the microphone level is held from where it is, and moved with the slider", async ({ page }, testInfo) => { + await page.goto("/"); + await page.getByRole("button", { name: "Open settings" }).click(); + const hold = page.getByRole("switch", { name: "Hold the microphone level" }); + await expect(hold).not.toBeChecked(); + await expect(page.getByRole("slider")).toHaveCount(0); + await hold.click(); + const level = () => page.evaluate(() => (Reflect.get(window, "__savedSettings") as { microphone_level?: number | null } | null)?.microphone_level); + await expect.poll(level).toBe(63); + const slider = page.getByRole("slider", { name: "Level" }); + await expect(slider).toHaveValue("63"); + await slider.fill("80"); + await expect.poll(level).toBe(80); + await expect(page.getByText("80%")).toBeVisible(); + await page.mouse.move(0, 0); + await page.screenshot({ path: testInfo.outputPath("microphone-level.png") }); + await hold.click(); + await expect.poll(level).toBeNull(); + await expect(page.getByRole("slider")).toHaveCount(0); + expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBe(true); +}); + +test("where the level cannot be set, the switch stays off and says why", async ({ page }) => { + await page.addInitScript(() => Object.assign(window, { __levelUnavailable: true })); + await page.goto("/"); + await page.getByRole("button", { name: "Open settings" }).click(); + const hold = page.getByRole("switch", { name: "Hold the microphone level" }); + await hold.click(); + await expect(page.getByRole("alert")).toContainText("cannot set the microphone level"); + await expect(hold).not.toBeChecked(); + expect(await page.evaluate(() => (Reflect.get(window, "__savedSettings") as { microphone_level?: number | null } | null)?.microphone_level ?? null)).toBeNull(); +}); + test("the Context action's sources are switched in their own category, at once", async ({ page }, testInfo) => { await page.goto("/"); await page.getByRole("button", { name: "Open settings" }).click();