Skip to content

Commit e09f359

Browse files
authored
Merge pull request #51 from devitools/fix/whisper-recording-restart
fix(whisper): fix recording restart and stuck transcribing
2 parents 21cff37 + 3ac0ba9 commit e09f359

3 files changed

Lines changed: 44 additions & 25 deletions

File tree

apps/tauri/src-tauri/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct InitialFile(Mutex<Option<String>>);
114114

115115
pub struct ExplicitQuit(pub Arc<AtomicBool>);
116116
pub struct IsRecording(pub Arc<AtomicBool>);
117+
pub struct IsTranscribing(pub Arc<AtomicBool>);
117118
pub struct RecordingMode(pub Mutex<Option<String>>);
118119

119120
fn create_file_watcher(app: tauri::AppHandle) -> Result<notify::RecommendedWatcher, String> {
@@ -755,6 +756,11 @@ pub fn handle_recording_toggle(handle: &tauri::AppHandle) {
755756
if currently_recording {
756757
let _ = handle.emit("stop-recording", ());
757758
} else {
759+
let is_transcribing = handle.state::<IsTranscribing>();
760+
if is_transcribing.0.load(Ordering::Relaxed) {
761+
return;
762+
}
763+
758764
if let Ok(mut guard) = handle.state::<RecordingMode>().0.lock() {
759765
*guard = Some("shortcut".to_string());
760766
}
@@ -922,6 +928,7 @@ pub fn run() {
922928
.manage(InitialFile(Mutex::new(None)))
923929
.manage(ExplicitQuit(Arc::new(AtomicBool::new(false))))
924930
.manage(IsRecording(Arc::new(AtomicBool::new(false))))
931+
.manage(IsTranscribing(Arc::new(AtomicBool::new(false))))
925932
.manage(RecordingMode(Mutex::new(None)))
926933
.manage(whisper::commands::RecorderState(Mutex::new(None)))
927934
.manage(whisper::commands::TranscriberState(Mutex::new(None)))

apps/tauri/src-tauri/src/whisper/commands.rs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ pub fn cancel_recording(
8888
app: tauri::AppHandle,
8989
) -> Result<(), String> {
9090
let is_recording = app.state::<crate::IsRecording>();
91+
let is_transcribing = app.state::<crate::IsTranscribing>();
9192
is_recording.0.store(false, Ordering::Relaxed);
93+
is_transcribing.0.store(false, Ordering::Relaxed);
9294

9395
let mut guard = state.0.lock().map_err(|e| e.to_string())?;
9496
*guard = None;
@@ -102,35 +104,42 @@ pub fn stop_and_transcribe(
102104
app: tauri::AppHandle,
103105
) -> Result<String, String> {
104106
let is_recording = app.state::<crate::IsRecording>();
107+
let is_transcribing = app.state::<crate::IsTranscribing>();
105108
is_recording.0.store(false, Ordering::Relaxed);
109+
is_transcribing.0.store(true, Ordering::Relaxed);
110+
111+
let result = (|| {
112+
let mut recorder = {
113+
let mut guard = recorder_state.0.lock().map_err(|e| e.to_string())?;
114+
guard
115+
.take()
116+
.ok_or_else(|| "No active recording".to_string())?
117+
};
118+
let audio = recorder.stop()?;
119+
120+
if audio.is_empty() {
121+
return Err("No audio captured".to_string());
122+
}
106123

107-
let mut recorder = {
108-
let mut guard = recorder_state.0.lock().map_err(|e| e.to_string())?;
109-
guard
110-
.take()
111-
.ok_or_else(|| "No active recording".to_string())?
112-
};
113-
let audio = recorder.stop()?;
114-
115-
if audio.is_empty() {
116-
return Err("No audio captured".to_string());
117-
}
118-
119-
let guard = transcriber_state.0.lock().map_err(|e| e.to_string())?;
120-
let transcriber = guard.as_ref().ok_or("No whisper model loaded")?;
124+
let guard = transcriber_state.0.lock().map_err(|e| e.to_string())?;
125+
let transcriber = guard.as_ref().ok_or("No whisper model loaded")?;
121126

122-
let _ = app.emit("transcription-started", ());
127+
let _ = app.emit("transcription-started", ());
123128

124-
match transcriber.transcribe(&audio) {
125-
Ok(text) => {
126-
let _ = app.emit("transcription-complete", text.clone());
127-
Ok(text)
128-
}
129-
Err(e) => {
130-
let _ = app.emit("transcription-error", e.clone());
131-
Err(e)
129+
match transcriber.transcribe(&audio) {
130+
Ok(text) => {
131+
let _ = app.emit("transcription-complete", text.clone());
132+
Ok(text)
133+
}
134+
Err(e) => {
135+
let _ = app.emit("transcription-error", e.clone());
136+
Err(e)
137+
}
132138
}
133-
}
139+
})();
140+
141+
is_transcribing.0.store(false, Ordering::Relaxed);
142+
result
134143
}
135144

136145
#[tauri::command]

apps/tauri/src/WhisperApp.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ function WhisperContent() {
7777
try {
7878
await invoke("stop_and_transcribe");
7979
} catch (e) {
80-
console.error("stop_and_transcribe failed:", e);
80+
setErrorMsg(
81+
String(e).length > 50 ? String(e).substring(0, 50) + "\u2026" : String(e)
82+
);
83+
setViewState("error");
8184
}
8285
}, []);
8386

0 commit comments

Comments
 (0)