feat(frontend): transcribe the microphone while the user is still speaking
Waiting for the release before starting reduced the whole cost to the moment it is most visible: the Whisper model load and the inference over the entire recording landed together after the button press. The capture is now cut into pieces as it arrives — inside a pause when there is one, so a word is never split in two, and at four seconds at the latest — and each piece is queued to the voice thread in order. By the time the user stops, only the tail is left to transcribe; a tail too short to hold a word is not even sent. The model load happens during the first sentence instead of after it, the recognised text appears live as it is spoken, and the pieces are joined back into one question.
This commit is contained in:
3 files changed
+331
-26
No files matched your search
@@ -62,6 +62,10 @@ fn is_file(path: &Path) -> bool {
|
||||
/// Result of a voice job, delivered back to the GUI thread.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum VoiceEvent {
|
||||
/// A piece of audio transcribed while the capture is still running. The UI
|
||||
/// shows it as it arrives; it is not the question yet.
|
||||
TranscribedPart(String),
|
||||
/// The capture ended: this is the complete transcript of what was said.
|
||||
Transcribed(String),
|
||||
/// Playback of a synthesized answer finished.
|
||||
Spoken,
|
||||
@@ -70,7 +74,13 @@ pub enum VoiceEvent {
|
||||
}
|
||||
|
||||
enum VoiceCommand {
|
||||
Transcribe { samples: Vec<f32>, sample_rate: u32 },
|
||||
Transcribe {
|
||||
samples: Vec<f32>,
|
||||
sample_rate: u32,
|
||||
/// Whether this closes the capture: the last piece decides which event
|
||||
/// the app receives.
|
||||
final_chunk: bool,
|
||||
},
|
||||
Speak { text: String },
|
||||
}
|
||||
|
||||
@@ -91,11 +101,23 @@ impl VoiceHandle {
|
||||
self.tts_ready
|
||||
}
|
||||
|
||||
/// Queues a transcription job (safe to call from the GUI thread).
|
||||
/// Queues a transcription of the whole capture; the result is the question.
|
||||
pub fn transcribe(&self, samples: Vec<f32>, sample_rate: u32) {
|
||||
self.send_transcription(samples, sample_rate, true);
|
||||
}
|
||||
|
||||
/// Queues a transcription of a piece of the capture. Pieces are inferred in
|
||||
/// the order they are queued, so the app can start on the beginning of a
|
||||
/// sentence while the end is still being spoken.
|
||||
pub fn transcribe_part(&self, samples: Vec<f32>, sample_rate: u32) {
|
||||
self.send_transcription(samples, sample_rate, false);
|
||||
}
|
||||
|
||||
fn send_transcription(&self, samples: Vec<f32>, sample_rate: u32, final_chunk: bool) {
|
||||
let _ = self.tx.send(VoiceCommand::Transcribe {
|
||||
samples,
|
||||
sample_rate,
|
||||
final_chunk,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -126,6 +148,7 @@ where
|
||||
VoiceCommand::Transcribe {
|
||||
samples,
|
||||
sample_rate,
|
||||
final_chunk,
|
||||
} => {
|
||||
let Some(model) = worker_config.whisper_model.as_deref() else {
|
||||
on_event(VoiceEvent::TranscriptionError(
|
||||
@@ -147,7 +170,8 @@ where
|
||||
.expect("stt initialised above")
|
||||
.transcribe(&samples, sample_rate, &worker_config.language)
|
||||
{
|
||||
Ok(text) => on_event(VoiceEvent::Transcribed(text)),
|
||||
Ok(text) if final_chunk => on_event(VoiceEvent::Transcribed(text)),
|
||||
Ok(text) => on_event(VoiceEvent::TranscribedPart(text)),
|
||||
Err(error) => {
|
||||
on_event(VoiceEvent::TranscriptionError(error.to_string()))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user