Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

feat: auto-pause media playback during dictation (#331)

Alcahest13e6e62ba61c
diff --git a/preload.js b/preload.js
index 975d5629d..5af7ecd28 100644
--- a/preload.js
+++ b/preload.js
@@ -335,6 +335,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
openMicrophoneSettings: () => ipcRenderer.invoke("open-microphone-settings"),
openSoundInputSettings: () => ipcRenderer.invoke("open-sound-input-settings"),
openAccessibilitySettings: () => ipcRenderer.invoke("open-accessibility-settings"),
+ toggleMediaPlayback: () => ipcRenderer.invoke("toggle-media-playback"),
openWhisperModelsFolder: () => ipcRenderer.invoke("open-whisper-models-folder"),
authClearSession: () => ipcRenderer.invoke("auth-clear-session"),
diff --git a/resources/linux-fast-paste.c b/resources/linux-fast-paste.c
index 0a55d33fd..9c9fa8ada 100644
--- a/resources/linux-fast-paste.c
+++ b/resources/linux-fast-paste.c
@@ -473,10 +473,74 @@ static int paste_via_uinput(int use_shift) {
}
#endif
+static int send_media_play_pause(void) {
+#ifdef HAVE_UINPUT
+ /* KEY_PLAYPAUSE = 164 (evdev) — works without X11 display */
+ int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
+ if (fd >= 0) {
+ if (ioctl(fd, UI_SET_EVBIT, EV_KEY) >= 0 &&
+ ioctl(fd, UI_SET_KEYBIT, KEY_PLAYPAUSE) >= 0) {
+
+ struct uinput_setup usetup;
+ memset(&usetup, 0, sizeof(usetup));
+ usetup.id.bustype = BUS_USB;
+ usetup.id.vendor = 0x1234;
+ usetup.id.product = 0x5678;
+ snprintf(usetup.name, UINPUT_MAX_NAME_SIZE, "openwhispr-media");
+
+ if (ioctl(fd, UI_DEV_SETUP, &usetup) >= 0 &&
+ ioctl(fd, UI_DEV_CREATE) >= 0) {
+
+ usleep(50000);
+
+ emit(fd, EV_KEY, KEY_PLAYPAUSE, 1);
+ emit(fd, EV_SYN, SYN_REPORT, 0);
+ usleep(8000);
+ emit(fd, EV_KEY, KEY_PLAYPAUSE, 0);
+ emit(fd, EV_SYN, SYN_REPORT, 0);
+ usleep(20000);
+
+ ioctl(fd, UI_DEV_DESTROY);
+ close(fd);
+ return 0;
+ }
+ }
+ close(fd);
+ }
+#endif
+
+ /* Fallback to XTest */
+ Display *dpy = XOpenDisplay(NULL);
+ if (!dpy) return 1;
+
+ int event_base, error_base, major, minor;
+ if (!XTestQueryExtension(dpy, &event_base, &error_base, &major, &minor)) {
+ XCloseDisplay(dpy);
+ return 2;
+ }
+
+ /* XF86AudioPlay keysym = 0x1008FF14 */
+ KeyCode play = XKeysymToKeycode(dpy, 0x1008FF14);
+ if (play == 0) {
+ XCloseDisplay(dpy);
+ return 2;
+ }
+
+ XTestFakeKeyEvent(dpy, play, True, CurrentTime);
+ usleep(8000);
+ XTestFakeKeyEvent(dpy, play, False, CurrentTime);
+
+ XFlush(dpy);
+ usleep(20000);
+ XCloseDisplay(dpy);
+ return 0;
+}
+
int main(int argc, char *argv[]) {
int force_terminal = 0;
int use_uinput = 0;
int use_portal = 0;
+ int media_play_pause = 0;
const char *restore_token = NULL;
Window target_window = None;
@@ -487,6 +551,8 @@ int main(int argc, char *argv[]) {
use_uinput = 1;
} else if (strcmp(argv[i], "--portal") == 0) {
use_portal = 1;
+ } else if (strcmp(argv[i], "--media-play-pause") == 0) {
+ media_play_pause = 1;
} else if (strcmp(argv[i], "--restore-token") == 0 && i + 1 < argc) {
restore_token = argv[++i];
} else if (strcmp(argv[i], "--window") == 0 && i + 1 < argc) {
@@ -494,6 +560,10 @@ int main(int argc, char *argv[]) {
}
}
+ if (media_play_pause) {
+ return send_media_play_pause();
+ }
+
if (use_portal) {
#ifdef HAVE_GIO
return paste_via_portal(force_terminal, restore_token);
diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsx
index 49e265165..d461c281c 100644
--- a/src/components/SettingsPage.tsx
+++ b/src/components/SettingsPage.tsx
@@ -675,6 +675,8 @@ export default function SettingsPage({ activeSection = "general" }: SettingsPage
setCloudReasoningMode,
audioCuesEnabled,
setAudioCuesEnabled,
+ pauseMediaOnDictation,
+ setPauseMediaOnDictation,
floatingIconAutoHide,
setFloatingIconAutoHide,
cloudBackupEnabled,
@@ -1684,6 +1686,14 @@ export default function SettingsPage({ activeSection = "general" }: SettingsPage
<Toggle checked={audioCuesEnabled} onChange={setAudioCuesEnabled} />
</SettingsRow>
</SettingsPanelRow>
+ <SettingsPanelRow>
+ <SettingsRow
+ label={t("settingsPage.general.soundEffects.pauseMedia")}
+ description={t("settingsPage.general.soundEffects.pauseMediaDescription")}
+ >
+ <Toggle checked={pauseMediaOnDictation} onChange={setPauseMediaOnDictation} />
+ </SettingsRow>
+ </SettingsPanelRow>
</SettingsPanel>
</div>
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index d09463f26..5e713d5e2 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -1726,6 +1726,11 @@ class IPCHandlers {
ipcMain.handle("open-sound-input-settings", () => openSystemSettings("sound"));
ipcMain.handle("open-accessibility-settings", () => openSystemSettings("accessibility"));
+ ipcMain.handle("toggle-media-playback", () => {
+ const mediaPlayer = require("./mediaPlayer");
+ return mediaPlayer.toggleMedia();
+ });
+
ipcMain.handle("request-microphone-access", async () => {
if (process.platform !== "darwin") {
return { granted: true };
diff --git a/src/helpers/mediaPlayer.js b/src/helpers/mediaPlayer.js
new file mode 100644
index 000000000..5245d3b30
--- /dev/null
+++ b/src/helpers/mediaPlayer.js
@@ -0,0 +1,150 @@
+const { spawnSync } = require("child_process");
+const path = require("path");
+const fs = require("fs");
+const debugLogger = require("./debugLogger");
+
+class MediaPlayer {
+ constructor() {
+ this._linuxBinaryChecked = false;
+ this._linuxBinaryPath = null;
+ this._nircmdChecked = false;
+ this._nircmdPath = null;
+ }
+
+ _resolveLinuxFastPaste() {
+ if (this._linuxBinaryChecked) return this._linuxBinaryPath;
+ this._linuxBinaryChecked = true;
+
+ const candidates = [
+ path.join(__dirname, "..", "..", "resources", "bin", "linux-fast-paste"),
+ path.join(__dirname, "..", "..", "resources", "linux-fast-paste"),
+ ];
+
+ if (process.resourcesPath) {
+ candidates.push(path.join(process.resourcesPath, "bin", "linux-fast-paste"));
+ }
+
+ for (const candidate of candidates) {
+ try {
+ if (fs.existsSync(candidate)) {
+ fs.accessSync(candidate, fs.constants.X_OK);
+ this._linuxBinaryPath = candidate;
+ return candidate;
+ }
+ } catch {
+ continue;
+ }
+ }
+ return null;
+ }
+
+ _resolveNircmd() {
+ if (this._nircmdChecked) return this._nircmdPath;
+ this._nircmdChecked = true;
+
+ const candidates = [
+ path.join(process.resourcesPath || "", "bin", "nircmd.exe"),
+ path.join(__dirname, "..", "..", "resources", "bin", "nircmd.exe"),
+ ];
+
+ for (const candidate of candidates) {
+ try {
+ if (fs.existsSync(candidate)) {
+ this._nircmdPath = candidate;
+ return candidate;
+ }
+ } catch {
+ continue;
+ }
+ }
+ return null;
+ }
+
+ toggleMedia() {
+ try {
+ if (process.platform === "linux") {
+ return this._toggleLinux();
+ } else if (process.platform === "darwin") {
+ return this._toggleMacOS();
+ } else if (process.platform === "win32") {
+ return this._toggleWindows();
+ }
+ } catch (err) {
+ debugLogger.warn("Media toggle failed", { error: err.message }, "media");
+ }
+ return false;
+ }
+
+ _toggleLinux() {
+ const binary = this._resolveLinuxFastPaste();
+ if (binary) {
+ const result = spawnSync(binary, ["--media-play-pause"], {
+ stdio: "pipe",
+ timeout: 3000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media toggled via linux-fast-paste", {}, "media");
+ return true;
+ }
+ }
+
+ // Fallback to playerctl
+ const result = spawnSync("playerctl", ["play-pause"], {
+ stdio: "pipe",
+ timeout: 3000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media toggled via playerctl", {}, "media");
+ return true;
+ }
+
+ debugLogger.warn("No media control method available on Linux", {}, "media");
+ return false;
+ }
+
+ _toggleMacOS() {
+ const result = spawnSync("osascript", [
+ "-e",
+ 'tell application "System Events" to key code 100',
+ ], {
+ stdio: "pipe",
+ timeout: 3000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media toggled via osascript", {}, "media");
+ return true;
+ }
+ return false;
+ }
+
+ _toggleWindows() {
+ const nircmd = this._resolveNircmd();
+ if (nircmd) {
+ const result = spawnSync(nircmd, ["sendkeypress", "0xB3"], {
+ stdio: "pipe",
+ timeout: 3000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media toggled via nircmd", {}, "media");
+ return true;
+ }
+ }
+
+ // Fallback to PowerShell
+ const result = spawnSync("powershell", [
+ "-NoProfile", "-NonInteractive", "-Command",
+ "$wsh = New-Object -ComObject WScript.Shell; $wsh.SendKeys([char]0xB3)",
+ ], {
+ stdio: "pipe",
+ timeout: 5000,
+ });
+ if (result.status === 0) {
+ debugLogger.debug("Media toggled via PowerShell", {}, "media");
+ return true;
+ }
+
+ return false;
+ }
+}
+
+module.exports = new MediaPlayer();
diff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.js
index 1728c0333..dc9996b67 100644
--- a/src/hooks/useAudioRecording.js
+++ b/src/hooks/useAudioRecording.js
@@ -33,6 +33,9 @@ export const useAudioRecording = (toast, options = {}) => {
if (didStart) {
void playStartCue();
+ if (getSettings().pauseMediaOnDictation) {
+ window.electronAPI?.toggleMediaPlayback?.();
+ }
}
return didStart;
@@ -113,6 +116,10 @@ export const useAudioRecording = (toast, options = {}) => {
audioManagerRef.current.saveTranscription(result.text);
+ if (getSettings().pauseMediaOnDictation) {
+ window.electronAPI?.toggleMediaPlayback?.();
+ }
+
if (result.source === "openai" && getSettings().useLocalWhisper) {
toast({
title: t("hooks.audioRecording.fallback.title"),
@@ -217,6 +224,9 @@ export const useAudioRecording = (toast, options = {}) => {
const cancelRecording = async () => {
if (audioManagerRef.current) {
const state = audioManagerRef.current.getState();
+ if (getSettings().pauseMediaOnDictation) {
+ window.electronAPI?.toggleMediaPlayback?.();
+ }
if (state.isStreaming) {
return await audioManagerRef.current.stopStreamingRecording();
}
diff --git a/src/locales/de/translation.json b/src/locales/de/translation.json
index fe5547928..e0d647735 100644
--- a/src/locales/de/translation.json
+++ b/src/locales/de/translation.json
@@ -1074,6 +1074,8 @@
"soundEffects": {
"dictationSounds": "Diktat-Signaltöne",
"dictationSoundsDescription": "Ton abspielen, wenn die Aufnahme startet und stoppt",
+ "pauseMedia": "Medien pausieren",
+ "pauseMediaDescription": "Musik beim Starten der Diktat automatisch pausieren",
"title": "Soundeffekte"
},
"startup": {
diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json
index cd2922bc0..159490e1c 100644
--- a/src/locales/en/translation.json
+++ b/src/locales/en/translation.json
@@ -1074,6 +1074,8 @@
"soundEffects": {
"dictationSounds": "Dictation sounds",
"dictationSoundsDescription": "Play a tone when recording starts and stops",
+ "pauseMedia": "Pause media",
+ "pauseMediaDescription": "Automatically pause music when dictation starts",
"title": "Sound Effects"
},
"startup": {
diff --git a/src/locales/es/translation.json b/src/locales/es/translation.json
index c8df40722..d1c1ac235 100644
--- a/src/locales/es/translation.json
+++ b/src/locales/es/translation.json
@@ -1078,6 +1078,8 @@
"soundEffects": {
"dictationSounds": "Sonidos de dictado",
"dictationSoundsDescription": "Reproduce un tono al iniciar y detener la grabación",
+ "pauseMedia": "Pausar multimedia",
+ "pauseMediaDescription": "Pausar la música automáticamente al iniciar el dictado",
"title": "Efectos de sonido"
},
"startup": {
diff --git a/src/locales/fr/translation.json b/src/locales/fr/translation.json
index 96d2e1741..c3658f1bb 100644
--- a/src/locales/fr/translation.json
+++ b/src/locales/fr/translation.json
@@ -1078,6 +1078,8 @@
"soundEffects": {
"dictationSounds": "Sons de dictée",
"dictationSoundsDescription": "Jouer un son au début et à la fin de l'enregistrement",
+ "pauseMedia": "Mettre en pause le média",
+ "pauseMediaDescription": "Mettre automatiquement la musique en pause au début de la dictée",
"title": "Effets sonores"
},
"startup": {
diff --git a/src/locales/it/translation.json b/src/locales/it/translation.json
index c71f63dfc..40e5a4ea1 100644
--- a/src/locales/it/translation.json
+++ b/src/locales/it/translation.json
@@ -1078,6 +1078,8 @@
"soundEffects": {
"dictationSounds": "Suoni di dettatura",
"dictationSoundsDescription": "Riproduci un tono quando la registrazione inizia e finisce",
+ "pauseMedia": "Pausa multimedia",
+ "pauseMediaDescription": "Metti automaticamente in pausa la musica quando inizia la dettatura",
"title": "Effetti sonori"
},
"startup": {
diff --git a/src/locales/ja/translation.json b/src/locales/ja/translation.json
index 6a166b211..80f0ce26f 100644
--- a/src/locales/ja/translation.json
+++ b/src/locales/ja/translation.json
@@ -1074,6 +1074,8 @@
"soundEffects": {
"dictationSounds": "ディクテーション音",
"dictationSoundsDescription": "録音の開始・停止時に効果音を再生",
+ "pauseMedia": "メディアを一時停止",
+ "pauseMediaDescription": "ディクテーション開始時に音楽を自動的に一時停止",
"title": "サウンドエフェクト"
},
"startup": {
diff --git a/src/locales/pt/translation.json b/src/locales/pt/translation.json
index e62f39dba..b0c2bb642 100644
--- a/src/locales/pt/translation.json
+++ b/src/locales/pt/translation.json
@@ -1050,6 +1050,8 @@
"soundEffects": {
"dictationSounds": "Sons de ditado",
"dictationSoundsDescription": "Tocar um som ao iniciar e parar a gravação",
+ "pauseMedia": "Pausar mídia",
+ "pauseMediaDescription": "Pausar a música automaticamente ao iniciar o ditado",
"title": "Efeitos sonoros"
},
"startup": {
diff --git a/src/locales/ru/translation.json b/src/locales/ru/translation.json
index 977d90c08..9bd290c29 100644
--- a/src/locales/ru/translation.json
+++ b/src/locales/ru/translation.json
@@ -1078,6 +1078,8 @@
"soundEffects": {
"dictationSounds": "Звуки диктовки",
"dictationSoundsDescription": "Воспроизводить звуковой сигнал при начале и окончании записи",
+ "pauseMedia": "Пауза медиа",
+ "pauseMediaDescription": "Автоматически приостанавливать музыку при начале диктовки",
"title": "Звуковые эффекты"
},
"startup": {
diff --git a/src/locales/zh-CN/translation.json b/src/locales/zh-CN/translation.json
index 6011ba8b6..849a32153 100644
--- a/src/locales/zh-CN/translation.json
+++ b/src/locales/zh-CN/translation.json
@@ -1074,6 +1074,8 @@
"soundEffects": {
"dictationSounds": "听写音效",
"dictationSoundsDescription": "在录音开始和停止时播放提示音",
+ "pauseMedia": "暂停媒体",
+ "pauseMediaDescription": "开始听写时自动暂停音乐",
"title": "音效"
},
"startup": {
diff --git a/src/locales/zh-TW/translation.json b/src/locales/zh-TW/translation.json
index ed2b16179..bcf481ad1 100644
--- a/src/locales/zh-TW/translation.json
+++ b/src/locales/zh-TW/translation.json
@@ -1074,6 +1074,8 @@
"soundEffects": {
"dictationSounds": "語音輸入音效",
"dictationSoundsDescription": "錄音開始和結束時播放提示音",
+ "pauseMedia": "暫停媒體",
+ "pauseMediaDescription": "開始聽寫時自動暫停音樂",
"title": "音效"
},
"startup": {
diff --git a/src/stores/settingsStore.ts b/src/stores/settingsStore.ts
index 583487d2f..e58ded1de 100644
--- a/src/stores/settingsStore.ts
+++ b/src/stores/settingsStore.ts
@@ -54,6 +54,7 @@ const BOOLEAN_SETTINGS = new Set([
"cloudBackupEnabled",
"telemetryEnabled",
"audioCuesEnabled",
+ "pauseMediaOnDictation",
"floatingIconAutoHide",
"isSignedIn",
]);
@@ -83,6 +84,7 @@ export interface SettingsState
ThemeSettings {
isSignedIn: boolean;
audioCuesEnabled: boolean;
+ pauseMediaOnDictation: boolean;
floatingIconAutoHide: boolean;
setUseLocalWhisper: (value: boolean) => void;
@@ -235,6 +237,7 @@ export const useSettingsStore = create<SettingsState>()((set, get) => ({
cloudBackupEnabled: readBoolean("cloudBackupEnabled", false),
telemetryEnabled: readBoolean("telemetryEnabled", false),
audioCuesEnabled: readBoolean("audioCuesEnabled", true),
+ pauseMediaOnDictation: readBoolean("pauseMediaOnDictation", false),
floatingIconAutoHide: readBoolean("floatingIconAutoHide", false),
isSignedIn: readBoolean("isSignedIn", false),
@@ -359,6 +362,7 @@ export const useSettingsStore = create<SettingsState>()((set, get) => ({
setCloudBackupEnabled: createBooleanSetter("cloudBackupEnabled"),
setTelemetryEnabled: createBooleanSetter("telemetryEnabled"),
setAudioCuesEnabled: createBooleanSetter("audioCuesEnabled"),
+ setPauseMediaOnDictation: createBooleanSetter("pauseMediaOnDictation"),
setFloatingIconAutoHide: (enabled: boolean) => {
if (get().floatingIconAutoHide === enabled) return;
diff --git a/src/types/electron.ts b/src/types/electron.ts
index 51dcba4df..b75666b49 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -667,6 +667,7 @@ declare global {
openMicrophoneSettings?: () => Promise<{ success: boolean; error?: string }>;
openSoundInputSettings?: () => Promise<{ success: boolean; error?: string }>;
openAccessibilitySettings?: () => Promise<{ success: boolean; error?: string }>;
+ toggleMediaPlayback?: () => Promise<boolean>;
openWhisperModelsFolder?: () => Promise<{ success: boolean; error?: string }>;
// Windows Push-to-Talk notifications