Skip to content

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

feat: improve dictation audio cue audibility (#219)

Rutam Bhagat30e91d30234e
diff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.js
index 3438b244c..0541a501d 100644
--- a/src/hooks/useAudioRecording.js
+++ b/src/hooks/useAudioRecording.js
@@ -1,6 +1,7 @@
-import { useState, useEffect, useRef } from "react";
+import { useState, useEffect, useRef, useCallback } from "react";
import AudioManager from "../helpers/audioManager";
import logger from "../utils/logger";
+import { playStartCue, playStopCue } from "../utils/dictationCues";
export const useAudioRecording = (toast, options = {}) => {
const [isRecording, setIsRecording] = useState(false);
@@ -11,6 +12,51 @@ export const useAudioRecording = (toast, options = {}) => {
const audioManagerRef = useRef(null);
const { onToggle } = options;
+ const performStartRecording = useCallback(async () => {
+ if (!audioManagerRef.current) {
+ return false;
+ }
+
+ const currentState = audioManagerRef.current.getState();
+ if (currentState.isRecording || currentState.isProcessing) {
+ return false;
+ }
+
+ const didStart = audioManagerRef.current.shouldUseStreaming()
+ ? await audioManagerRef.current.startStreamingRecording()
+ : await audioManagerRef.current.startRecording();
+
+ if (didStart) {
+ void playStartCue();
+ }
+
+ return didStart;
+ }, []);
+
+ const performStopRecording = useCallback(async () => {
+ if (!audioManagerRef.current) {
+ return false;
+ }
+
+ const currentState = audioManagerRef.current.getState();
+ if (!currentState.isRecording) {
+ return false;
+ }
+
+ if (currentState.isStreaming) {
+ void playStopCue(); // streaming stop finalization is async, play cue immediately on stop action
+ return await audioManagerRef.current.stopStreamingRecording();
+ }
+
+ const didStop = audioManagerRef.current.stopRecording();
+
+ if (didStop) {
+ void playStopCue();
+ }
+
+ return didStop;
+ }, []);
+
useEffect(() => {
audioManagerRef.current = new AudioManager();
@@ -94,43 +140,22 @@ export const useAudioRecording = (toast, options = {}) => {
audioManagerRef.current.warmupStreamingConnection();
const handleToggle = async () => {
+ if (!audioManagerRef.current) return;
const currentState = audioManagerRef.current.getState();
if (!currentState.isRecording && !currentState.isProcessing) {
- if (audioManagerRef.current.shouldUseStreaming()) {
- await audioManagerRef.current.startStreamingRecording();
- } else {
- await audioManagerRef.current.startRecording();
- }
+ await performStartRecording();
} else if (currentState.isRecording) {
- if (currentState.isStreaming) {
- await audioManagerRef.current.stopStreamingRecording();
- } else {
- audioManagerRef.current.stopRecording();
- }
+ await performStopRecording();
}
};
const handleStart = async () => {
- const currentState = audioManagerRef.current.getState();
- if (!currentState.isRecording && !currentState.isProcessing) {
- if (audioManagerRef.current.shouldUseStreaming()) {
- await audioManagerRef.current.startStreamingRecording();
- } else {
- await audioManagerRef.current.startRecording();
- }
- }
+ await performStartRecording();
};
const handleStop = async () => {
- const currentState = audioManagerRef.current.getState();
- if (currentState.isRecording) {
- if (currentState.isStreaming) {
- await audioManagerRef.current.stopStreamingRecording();
- } else {
- audioManagerRef.current.stopRecording();
- }
- }
+ await performStopRecording();
};
const disposeToggle = window.electronAPI.onToggleDictation(() => {
@@ -168,27 +193,14 @@ export const useAudioRecording = (toast, options = {}) => {
audioManagerRef.current.cleanup();
}
};
- }, [toast, onToggle]);
+ }, [toast, onToggle, performStartRecording, performStopRecording]);
const startRecording = async () => {
- if (audioManagerRef.current) {
- if (audioManagerRef.current.shouldUseStreaming()) {
- return await audioManagerRef.current.startStreamingRecording();
- }
- return await audioManagerRef.current.startRecording();
- }
- return false;
+ return performStartRecording();
};
const stopRecording = async () => {
- if (audioManagerRef.current) {
- const state = audioManagerRef.current.getState();
- if (state.isStreaming) {
- return await audioManagerRef.current.stopStreamingRecording();
- }
- return audioManagerRef.current.stopRecording();
- }
- return false;
+ return performStopRecording();
};
const cancelRecording = async () => {
diff --git a/src/utils/dictationCues.js b/src/utils/dictationCues.js
new file mode 100644
index 000000000..5fb71c2d1
--- /dev/null
+++ b/src/utils/dictationCues.js
@@ -0,0 +1,94 @@
+import logger from "./logger";
+
+const START_NOTES = [880, 1174.66];
+const STOP_NOTES = [1174.66, 880];
+const NOTE_DURATION_SECONDS = 0.06;
+const NOTE_GAP_SECONDS = 0.02;
+const NOTE_ATTACK_SECONDS = 0.008;
+const MAX_GAIN = 0.2;
+const MIN_GAIN = 0.0001;
+
+let audioContext = null;
+
+const getAudioContext = () => {
+ if (typeof window === "undefined") {
+ return null;
+ }
+
+ const AudioContextCtor = window.AudioContext || window.webkitAudioContext;
+ if (!AudioContextCtor) {
+ return null;
+ }
+
+ if (!audioContext || audioContext.state === "closed") {
+ audioContext = new AudioContextCtor();
+ }
+
+ return audioContext;
+};
+
+export const resumeContextIfNeeded = async () => {
+ try {
+ const context = getAudioContext();
+ if (!context) {
+ return null;
+ }
+
+ if (context.state === "suspended") {
+ await context.resume();
+ }
+
+ return context.state === "running" ? context : null;
+ } catch (error) {
+ logger.debug(
+ "Failed to initialize dictation cue audio context",
+ { error: error instanceof Error ? error.message : String(error) },
+ "audio"
+ );
+ return null;
+ }
+};
+
+const scheduleTone = (context, frequency, startTime) => {
+ const oscillator = context.createOscillator();
+ const gainNode = context.createGain();
+ const stopTime = startTime + NOTE_DURATION_SECONDS;
+
+ oscillator.type = "sine";
+ oscillator.frequency.setValueAtTime(frequency, startTime);
+
+ gainNode.gain.setValueAtTime(MIN_GAIN, startTime);
+ gainNode.gain.linearRampToValueAtTime(MAX_GAIN, startTime + NOTE_ATTACK_SECONDS);
+ gainNode.gain.exponentialRampToValueAtTime(MIN_GAIN, stopTime);
+
+ oscillator.connect(gainNode);
+ gainNode.connect(context.destination);
+
+ oscillator.start(startTime);
+ oscillator.stop(stopTime + 0.01);
+};
+
+const playCue = async (notes) => {
+ try {
+ const context = await resumeContextIfNeeded();
+ if (!context) {
+ return;
+ }
+
+ const baseTime = context.currentTime + 0.005;
+ notes.forEach((frequency, index) => {
+ const noteStart = baseTime + index * (NOTE_DURATION_SECONDS + NOTE_GAP_SECONDS);
+ scheduleTone(context, frequency, noteStart);
+ });
+ } catch (error) {
+ logger.debug(
+ "Failed to play dictation cue",
+ { error: error instanceof Error ? error.message : String(error) },
+ "audio"
+ );
+ }
+};
+
+export const playStartCue = () => playCue(START_NOTES);
+
+export const playStopCue = () => playCue(STOP_NOTES);