Repository navigation
Expand file tree
/
Copy pathlinux_dictation.rs
More file actions
1218 lines (1172 loc) · 47 KB
/
Copy pathlinux_dictation.rs
File metadata and controls
1218 lines (1172 loc) · 47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, SyncSender, TrySendError};
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::{Duration, Instant};
use color_eyre::Result;
use color_eyre::eyre::eyre;
use crate::app_settings::AppSettings;
use crate::audio::{CaptureInstant, RecoveringAudioInput, RecoveringAudioInputEvent};
use crate::control::{PrintReply, PrintedTranscript};
use crate::dictation::{DictationCapture, Finish, POST_RELEASE_TAIL};
use crate::events::{DictationPhase, EventLog, TranscriptPhase, VoiceEvent, VoiceState, now_ms};
use crate::feedback::{self, Tone};
use crate::linux_input::{
CommandQueue, HotkeyEvent, HotkeyOptions, HotkeyStatus, LinuxHotkey, LinuxHotkeyMonitor,
};
use crate::linux_paste::LinuxPaster;
use crate::linux_rune_hud::LinuxIndicator;
use crate::local_transcriber::LocalTranscriber;
use crate::transcription_engine::{PendingTranscript, TranscriptionEngine};
const UPDATE_INTERVAL: Duration = Duration::from_millis(20);
/// How long past the tail a release waits for audio that never arrives.
const TAIL_STALL_GRACE: Duration = Duration::from_millis(100);
struct Job {
transcript: PendingTranscript,
audio_ms: u64,
/// `hex dictation stop --print`: the text goes here, never pasted.
print: Option<PrintReply>,
/// Dictate without processing: paste the transcript as heard.
raw: bool,
}
enum OutputJob {
Dictation(Job),
/// Pastes the newest completed dictation again.
PasteLast,
}
#[derive(Clone, Copy, PartialEq)]
enum OutputKind {
Dictation,
/// A dictation printed by `hex dictation stop --print`.
Printed,
PasteLast,
}
/// A release whose capture is still collecting its post-release tail.
struct Finishing {
ended_at: CaptureInstant,
print: Option<PrintReply>,
raw: bool,
released: Instant,
deadline: Instant,
}
struct OutputWorker {
stop: Arc<AtomicBool>,
worker: Option<thread::JoinHandle<()>>,
}
impl Drop for OutputWorker {
fn drop(&mut self) {
self.stop.store(true, Ordering::Release);
if let Some(worker) = self.worker.take() {
let _ = worker.join();
}
}
}
/// What the host hands a running listener. Settings apply in place while no
/// dictation records, so changing one never stops listening.
pub(crate) enum ListenerUpdate {
Settings(Box<AppSettings>),
/// A model the host prepared, swapped in once no transcription is pending.
Transcriber {
selection: crate::transcription_models::TranscriptionSelection,
transcriber: Box<LocalTranscriber>,
},
/// True while the window records a shortcut: global matching stops.
SuspendShortcuts(bool),
}
/// The shortcuts a listener registers for `settings`.
fn shortcuts(settings: &AppSettings) -> (LinuxHotkey, HotkeyOptions) {
let options = HotkeyOptions {
double_tap: settings.double_tap_lock,
// Double-tap-only needs the double-tap window and a non-modifier key.
double_tap_only: settings.double_tap_lock
&& settings.double_tap_only
&& settings.dictation_hotkey.key.is_some(),
toggle: settings.shortcut_mode == crate::dictation::ShortcutMode::Toggle,
paste_last: settings.paste_last_hotkey.as_ref().map(LinuxHotkey::from),
raw_dictation: settings
.raw_dictation_hotkey
.as_ref()
.map(LinuxHotkey::from),
capture: settings
.capture
.enabled
.then(|| LinuxHotkey::from(&settings.capture.hotkey)),
};
(LinuxHotkey::from(&settings.dictation_hotkey), options)
}
/// Opens `--device`, else the microphone `settings` select, else the system
/// default, or keeps it released until a dictation starts.
fn microphone_input(settings: &AppSettings, device: Option<&str>) -> RecoveringAudioInput {
let microphone = settings.microphone.as_deref();
if settings.release_microphone_while_idle {
RecoveringAudioInput::closed(device, 0, microphone)
} else {
RecoveringAudioInput::open(device, 0, microphone)
}
}
/// What happens to a transcript before and after it is pasted: the modes
/// that process it and the history that keeps it. The host shares it
/// with the listener so saved settings apply without a restart.
#[derive(Clone)]
pub(crate) struct DictationOutput {
pub(crate) history: Option<crate::history::History>,
pub(crate) modes: Arc<RwLock<crate::dictation_modes::ModeRuntime>>,
pub(crate) transformations: Arc<crate::personal_commands::TransformationClient>,
}
impl DictationOutput {
pub(crate) fn from_settings(
settings: &crate::app_settings::AppSettings,
history: Option<crate::history::History>,
) -> Self {
Self {
history,
modes: Arc::new(RwLock::new(
crate::dictation_modes::ModeRuntime::from_settings(settings),
)),
transformations: Arc::new(crate::personal_commands::TransformationClient::default()),
}
}
/// Processes (unless `raw`) and pastes one transcript, or only
/// processes it when there is no paster (a printing stop), then records
/// it.
fn deliver(
&self,
raw_text: String,
raw: bool,
paster: Option<&mut LinuxPaster>,
audio_ms: u64,
inference_ms: u64,
started: Instant,
) -> Result<String> {
let context = crate::linux_context::capture();
let processed = if raw {
crate::dictation_processor::Processed::unchanged(raw_text.trim())
} else {
let modes = self
.modes
.read()
.unwrap_or_else(|error| error.into_inner())
.clone();
modes.process(&raw_text, &context, &self.transformations)
};
if processed.text.trim().is_empty() {
return Err(eyre!("text replacements produced empty output"));
}
let printed = paster.is_none();
if let Some(paster) = paster {
paster.paste(&processed.text)?;
}
if let Some(history) = &self.history {
let mut draft = crate::dictation_modes::history_draft(
raw_text,
&processed,
context.application,
audio_ms,
inference_ms,
started.elapsed().as_millis() as u64,
);
if printed {
draft.kind = crate::history::HistoryKind::Printed;
} else if raw {
draft.kind = crate::history::HistoryKind::Raw;
}
if let Err(error) = history.record(draft) {
tracing::warn!(%error, "could not retain Linux dictation history");
}
}
Ok(processed.text)
}
}
/// Runs hotkey dictation until `shutdown`. A prepared transcriber skips the
/// model load; otherwise the supplied transcription selection is loaded here.
/// `updates` change settings, the model, and shortcut registration in place.
#[allow(clippy::too_many_arguments)]
pub(crate) fn run(
event_path: &Path,
device: Option<&str>,
shutdown: &AtomicBool,
settings: &AppSettings,
output_settings: DictationOutput,
transcriber: Option<LocalTranscriber>,
hotkey_status: HotkeyStatus,
commands: CommandQueue,
updates: mpsc::Receiver<ListenerUpdate>,
) -> Result<()> {
let mut settings = settings.clone();
let transcriber =
transcriber.map_or_else(|| LocalTranscriber::load(&settings.transcription), Ok)?;
crate::speech_detector::prewarm();
// The engine outlives the output worker, which waits on its transcripts.
// Captures transcribe on it too, holding it weakly.
let mut engine = Arc::new(TranscriptionEngine::start(transcriber)?);
// The local API transcribes host audio on this engine's model.
engine.share();
// Drop audio/hotkeys and close the job queue before joining slow output on
// every exit, including failures during startup or recording.
let mut output = OutputWorker {
stop: Arc::new(AtomicBool::new(false)),
worker: None,
};
// A missing microphone falls back to the default one. A released
// microphone opens only while a dictation records.
let mut input = microphone_input(&settings, device);
let mut release_while_idle = settings.release_microphone_while_idle;
let (binding, options) = shortcuts(&settings);
let hotkey_label = binding.label();
let mut toggle = options.toggle;
let start_shortcuts = |(binding, options): (LinuxHotkey, HotkeyOptions)| {
LinuxHotkeyMonitor::start(binding, options, hotkey_status.clone(), commands.clone())
};
let mut hotkey = Some(start_shortcuts((binding, options))?);
let mut shortcuts_suspended = false;
let mut indicator = LinuxIndicator::new(settings.indicator_position);
let mut suppressor =
crate::linux_audio_control::AudioSuppressor::dictation(settings.recording_audio_behavior);
let paste_with_shift = Arc::new(AtomicBool::new(settings.paste_with_shift));
let worker_paste_with_shift = paste_with_shift.clone();
let paste_keys = hotkey_status.keys();
let (jobs, job_receiver) = mpsc::sync_channel::<OutputJob>(2);
let (result_sender, results) = mpsc::channel();
let worker_stop = output.stop.clone();
output.worker = Some(thread::spawn(move || {
let mut paster = LinuxPaster::new(worker_stop.clone(), worker_paste_with_shift, paste_keys);
let mut last_dictation: Option<String> = None;
while let Ok(job) = job_receiver.recv() {
if worker_stop.load(Ordering::Acquire) {
break;
}
let (mut job, kind) = match job {
OutputJob::Dictation(job) if job.print.is_some() => (job, OutputKind::Printed),
OutputJob::Dictation(job) => (job, OutputKind::Dictation),
OutputJob::PasteLast => {
let result = last_dictation
.clone()
.ok_or_else(|| eyre!("there is no completed dictation to paste"))
.and_then(|text| {
paster
.as_mut()
.map_err(|error| eyre!("{error:#}"))?
.paste(&text)
})
.map(|()| String::new())
.map_err(|error| format!("{error:#}"));
if result_sender.send((OutputKind::PasteLast, result)).is_err() {
break;
}
continue;
}
};
let started = Instant::now();
let result = (|| -> Result<String> {
let transcribed = job.transcript.wait().map_err(|error| eyre!(error))?;
let inference_ms = started.elapsed().as_millis() as u64;
tracing::info!(
source = transcribed.source.label(),
finish_ms = transcribed.finish_ms,
audio_ms = job.audio_ms,
"transcribed Linux dictation"
);
let text = transcribed.text;
if text.is_empty() {
if let Some(print) = job.print.take() {
print.send(PrintedTranscript::NoSpeech);
}
return Err(eyre!("transcription was empty"));
}
if kind == OutputKind::Printed {
return output_settings.deliver(
text,
job.raw,
None,
job.audio_ms,
inference_ms,
started,
);
}
let paster = paster.as_mut().map_err(|error| eyre!("{error:#}"))?;
output_settings.deliver(
text,
job.raw,
Some(paster),
job.audio_ms,
inference_ms,
started,
)
})()
.map_err(|error| format!("{error:#}"));
if let Some(print) = job.print.take() {
print.send(match &result {
Ok(text) => PrintedTranscript::Text(text.clone()),
Err(error) => PrintedTranscript::Failed(error.clone()),
});
}
tracing::info!(
audio_ms = job.audio_ms,
elapsed_ms = started.elapsed().as_millis(),
"completed Linux dictation job"
);
if let Ok(text) = &result
&& kind == OutputKind::Dictation
{
last_dictation = Some(text.clone());
}
if result_sender.send((kind, result)).is_err() {
break;
}
}
}));
// The `capture` experiment: its host, the audio it copies from this
// listener's microphone, and the capsule's commands.
let timeline = crate::capture_host::ListenerTimeline::default();
let environment = crate::capture_host::Environment;
let mut capture_host =
crate::capture_host::CaptureHost::start(Some(engine.clone()), &environment);
let capture_controls = crate::capture_host::listen_for_controls();
// The microphone stays open for a capture even while released when idle.
let mut capture_holds_microphone = false;
// The draw shortcut started a recording: drawing turns on once its
// engine takes input.
let mut draw_pending = false;
let mut events = EventLog::create(event_path)?;
let mut audio_ready = input.is_open();
// A released microphone's sample rate is unknown until it opens; the
// first chunk replaces this placeholder capture.
let mut capture = DictationCapture::new(if audio_ready {
input.sample_rate()
} else {
16_000
});
let released_label = |settings: &AppSettings| {
device
.or(settings.microphone.as_deref())
.unwrap_or("System default")
.to_string()
};
let mut device_label = if audio_ready {
input.device_name().to_string()
} else {
released_label(&settings)
};
// Updates wait for a moment without a recording.
let mut next_settings: Option<AppSettings> = None;
let mut model = crate::local_transcriber::ModelSwap::new(&settings.transcription);
let mut next_suspended: Option<bool> = None;
// A press that arrived while the released microphone was opening;
// recording starts at its first chunk.
let mut start_pending = false;
let mut captured_through = CaptureInstant::ZERO;
let mut finishing: Option<Finishing> = None;
// The recording skips processing (Dictate without processing).
let mut recording_raw = false;
let mut pending = 0_usize;
events.emit(&VoiceEvent::SessionStarted {
timestamp_ms: now_ms(),
})?;
emit_state(&mut events, VoiceState::Listening, &device_label)?;
println!(
"Hex dictation is ready on {device_label}. Hold {hotkey_label} to dictate; Ctrl-C stops."
);
while !shutdown.load(Ordering::Relaxed) {
for update in updates.try_iter() {
match update {
ListenerUpdate::Settings(next) => next_settings = Some(*next),
ListenerUpdate::Transcriber {
selection,
transcriber,
} => model.offer(&selection, *transcriber),
ListenerUpdate::SuspendShortcuts(suspended) => next_suspended = Some(suspended),
}
}
if let Some(failed) = model.poll() {
fail(&events, failed.error)?;
}
if !capture.is_recording() && !start_pending && finishing.is_none() {
let mut register = next_suspended.take().is_some_and(|suspended| {
std::mem::replace(&mut shortcuts_suspended, suspended) != suspended
});
if let Some(next) = next_settings.take() {
register |= shortcuts(&next) != shortcuts(&settings);
model.want(&next.transcription);
toggle = next.shortcut_mode == crate::dictation::ShortcutMode::Toggle;
paste_with_shift.store(next.paste_with_shift, Ordering::Relaxed);
if next.indicator_position != settings.indicator_position {
indicator = LinuxIndicator::new(next.indicator_position);
}
if next.recording_audio_behavior != settings.recording_audio_behavior {
suppressor = crate::linux_audio_control::AudioSuppressor::dictation(
next.recording_audio_behavior,
);
}
if device.is_none() && next.microphone != settings.microphone
|| next.release_microphone_while_idle != release_while_idle
{
drop(input);
input = microphone_input(&next, device);
release_while_idle = next.release_microphone_while_idle;
audio_ready = input.is_open();
if audio_ready {
capture = DictationCapture::new(input.sample_rate());
device_label = input.device_name().to_string();
} else {
device_label = released_label(&next);
}
emit_state(&mut events, active_state(false, pending), &device_label)?;
}
settings = next;
}
if register {
// The old registration goes first: X11 grabs the same keys again.
drop(hotkey.take());
if !shortcuts_suspended {
hotkey = Some(start_shortcuts(shortcuts(&settings))?);
}
}
if pending == 0
&& let Some(next) = model.take_ready()
{
let old =
std::mem::replace(&mut engine, Arc::new(TranscriptionEngine::start(next)?));
engine.share();
crate::capture_host::use_engine(&engine);
// A capture still transcribing holds the old engine a moment.
if let Ok(old) = Arc::try_unwrap(old) {
old.retire();
}
}
}
crate::capture_host::CaptureHost::reconcile(
&mut capture_host,
Some(&engine),
&environment,
&timeline,
);
if draw_pending && crate::capture_host::wants_input() {
draw_pending = false;
crate::capture_host::set_pen(true);
} else if draw_pending
&& capture_host
.as_mut()
.is_none_or(|host| host.recording_since(&timeline).is_none())
{
// The recording never started.
draw_pending = false;
}
while let Ok(control) = capture_controls.try_recv() {
let reply = match (&mut capture_host, control) {
(Some(host), crate::capture_host::CaptureControl::Draw) => {
draw_pending = host.draw_shortcut(&timeline);
crate::control::ControlReply::done("drawing")
}
(Some(host), crate::capture_host::CaptureControl::Scrawl(action)) => {
host.control(action, &timeline)
}
(Some(host), crate::capture_host::CaptureControl::Command(command)) => {
host.command(command, &timeline)
}
(host, crate::capture_host::CaptureControl::Reply(action, answer)) => {
let report = match host {
Some(host) => host.control(action, &timeline),
None => crate::control::ControlReply::error(
"feature-off",
"Annotate is off; turn it on with `hex settings set capture.enabled on`",
),
};
let _ = answer.send(report);
continue;
}
(None, _) => crate::control::ControlReply::error("feature-off", "captures are off"),
};
if let crate::control::ControlReply::Error { message, .. } = reply {
tracing::warn!(message, "a capture command did nothing");
}
}
// A capture copies this microphone: a released one opens for it,
// and closes again once nothing records.
if release_while_idle {
let wants = timeline.wants_audio();
if wants && !input.is_active() {
input.request_open();
}
if wants {
capture_holds_microphone = true;
} else if capture_holds_microphone
&& !capture.is_recording()
&& !start_pending
&& finishing.is_none()
{
capture_holds_microphone = false;
input.close();
audio_ready = false;
}
}
if let Some(hotkey) = &hotkey
&& let Ok(error) = hotkey.errors.try_recv()
{
return Err(eyre!("Linux hotkey monitor stopped: {error}"));
}
// A release keeps recording through its tail; shortcut events wait,
// so a press inside the tail starts after this clip is submitted.
if let Some(release) = &finishing
&& (captured_through >= release.ended_at + POST_RELEASE_TAIL
|| Instant::now() >= release.deadline)
{
tracing::debug!(
tail_ms = release.released.elapsed().as_millis(),
"post-release tail captured"
);
let Finishing {
ended_at,
print,
raw,
..
} = finishing.take().expect("a release is finishing");
submit_capture(
&mut capture,
ended_at,
&engine,
&jobs,
&mut events,
&mut pending,
print,
raw,
)?;
suppressor.restore();
if release_while_idle && !timeline.wants_audio() {
input.close();
audio_ready = false;
}
emit_state(
&mut events,
active_state(capture.is_recording(), pending),
&device_label,
)?;
}
while finishing.is_none()
&& let Some(action) = hotkey
.as_ref()
.and_then(|hotkey| hotkey.events.try_recv().ok())
{
// A printing stop finishes as a stop does; its reply follows.
let (action, mut print) = match action {
HotkeyEvent::FinishPrinting(reply) => (HotkeyEvent::Finish, Some(reply)),
action => (action, None),
};
match action {
// Annotate records the narration itself: while it records,
// dictation starts nothing. The desktop already took the keys,
// so nothing reaches the app.
HotkeyEvent::Start { .. }
if capture_host
.as_mut()
.is_some_and(|host| host.recording_since(&timeline).is_some()) => {}
HotkeyEvent::Start { raw } if !capture.is_recording() && !start_pending => {
recording_raw = raw;
if audio_ready {
start_capture(&mut capture, captured_through, || {
feedback::play_provisional_start();
});
stream_when_due(&mut capture, &engine, toggle);
} else if release_while_idle {
start_pending = true;
input.request_open();
feedback::prepare();
} else {
continue;
}
events.dictation(DictationPhase::Started, "")?;
emit_state(&mut events, VoiceState::Dictating, &device_label)?;
}
HotkeyEvent::Finish | HotkeyEvent::Cancel if start_pending => {
// Released before the opening microphone produced audio:
// nothing was captured.
start_pending = false;
if let Some(print) = print.take() {
print.send(PrintedTranscript::NoSpeech);
}
suppressor.restore();
if !timeline.wants_audio() {
input.close();
audio_ready = false;
}
events.dictation(
if action == HotkeyEvent::Cancel {
DictationPhase::Cancelled
} else {
DictationPhase::Discarded
},
"",
)?;
emit_state(&mut events, active_state(false, pending), &device_label)?;
}
HotkeyEvent::Finish if capture.is_recording() => {
finishing = Some(Finishing {
raw: recording_raw,
..begin_finishing(&capture, captured_through, print.take(), || {
suppressor.restore_then(|| feedback::play(Tone::DictationStop))
})
});
}
// One key: a tap starts and stops; held while recording, it
// draws until it comes up.
HotkeyEvent::Capture => {
if let Some(host) = &mut capture_host {
host.annotate_down(&timeline);
}
}
HotkeyEvent::CaptureUp => {
if let Some(host) = &mut capture_host {
host.annotate_up(&timeline);
}
}
HotkeyEvent::PasteLast if !capture.is_recording() => {
match jobs.try_send(OutputJob::PasteLast) {
Ok(()) | Err(TrySendError::Full(_)) => {}
Err(TrySendError::Disconnected(_)) => {
return Err(eyre!("dictation worker is unavailable"));
}
}
}
HotkeyEvent::Cancel if capture.is_recording() => {
capture.cancel();
feedback::retract_start();
feedback::play(Tone::Cancel);
suppressor.restore();
if release_while_idle && !timeline.wants_audio() {
input.close();
audio_ready = false;
}
events.dictation(DictationPhase::Cancelled, "")?;
emit_state(
&mut events,
active_state(capture.is_recording(), pending),
&device_label,
)?;
}
_ => {}
}
}
while let Ok((kind, result)) = results.try_recv() {
let phase = if kind == OutputKind::Printed {
DictationPhase::Printed
} else {
DictationPhase::Pasted
};
// A repeated paste has no transcript and is not pending work.
if kind == OutputKind::PasteLast {
if let Err(error) = result {
fail(&events, error)?;
}
continue;
}
pending = pending.saturating_sub(1);
indicator.finished(result.is_ok());
match result {
Ok(text) => {
feedback::play(Tone::DictationDone);
events.emit(&VoiceEvent::Transcript {
timestamp_ms: now_ms(),
phase: TranscriptPhase::Completed,
latency_ms: 0,
text: text.clone(),
})?;
events.dictation(phase, text)?;
}
Err(error) => fail(&events, error)?,
}
emit_state(
&mut events,
active_state(capture.is_recording(), pending),
&device_label,
)?;
}
indicator.update(capture.is_recording() || start_pending, pending);
match input.recv_timeout(
UPDATE_INTERVAL,
crate::audio::capture_idle(
capture.is_recording() || timeline.wants_audio(),
start_pending,
finishing.is_some(),
),
) {
RecoveringAudioInputEvent::Chunk {
samples,
captured_through: chunk_captured_through,
} => {
captured_through = chunk_captured_through;
if !audio_ready {
capture = DictationCapture::new(input.sample_rate());
audio_ready = true;
device_label = input.device_name().to_string();
if start_pending {
// The on-demand open finished while the shortcut is
// held: recording starts here, without pre-roll.
start_pending = false;
start_capture(&mut capture, captured_through, || {
feedback::play_provisional_start();
});
}
}
crate::input_level::record(&samples);
timeline.feed(&samples, input.sample_rate(), captured_through);
capture.ingest(&samples, captured_through);
if capture.is_recording() {
indicator.meter(&samples);
}
if capture.become_intentional(captured_through) && finishing.is_none() {
// Ordinary shortcut chords and short taps never mute or pause.
suppressor.suppress();
}
stream_when_due(&mut capture, &engine, toggle);
capture.report_live_text();
}
RecoveringAudioInputEvent::Timeout => {}
RecoveringAudioInputEvent::Interrupted => {
timeline.restart();
// A release still in its tail finishes with the audio it has.
if let Some(release) = &mut finishing {
release.deadline = Instant::now();
}
audio_ready = false;
if capture.is_recording() && finishing.is_none() {
capture.cancel();
feedback::retract_start();
feedback::play(Tone::Cancel);
suppressor.restore();
events.dictation(DictationPhase::Cancelled, "")?;
}
events.dictation(
DictationPhase::Failed("microphone stream interrupted; reopening".into()),
"",
)?;
emit_state(
&mut events,
active_state(capture.is_recording(), pending),
&device_label,
)?;
}
RecoveringAudioInputEvent::Reopened => {
timeline.restart();
audio_ready = false;
device_label = input.device_name().to_string();
// A press waiting on the released microphone is already
// Dictating; its first chunk starts the capture.
emit_state(
&mut events,
active_state(capture.is_recording() || start_pending, pending),
&device_label,
)?;
}
RecoveringAudioInputEvent::OpenFailed(error) => {
if !release_while_idle {
return Err(eyre!("could not open the microphone: {error}"));
}
// An on-demand open failed (device busy or unplugged): report
// it and stay released rather than stopping the listener.
start_pending = false;
suppressor.restore();
fail(&events, format!("could not open microphone: {error}"))?;
emit_state(&mut events, active_state(false, pending), &device_label)?;
}
}
}
if capture.is_recording() {
capture.cancel();
events.dictation(DictationPhase::Cancelled, "")?;
}
if let Some(mut host) = capture_host.take() {
host.shutdown(&timeline);
}
indicator.update(false, pending);
suppressor.restore();
emit_state(&mut events, VoiceState::Stopping, &device_label)?;
output.stop.store(true, Ordering::Release);
drop(hotkey);
drop(input);
drop(jobs);
if output
.worker
.take()
.expect("dictation output worker exists")
.join()
.is_err()
{
return Err(eyre!("Linux dictation worker panicked"));
}
println!("Stopped.");
Ok(())
}
/// Applies the saved sound settings and preloads the tones.
pub(crate) fn initialize_feedback(settings: &crate::app_settings::AppSettings) {
feedback::set_enabled(settings.sound_effects);
feedback::set_volume(settings.sound_effect_volume.clamp(0.0, 1.0));
feedback::set_sound_set(settings.sound_theme);
if let Err(error) = feedback::preload() {
tracing::warn!(%error, "recording sounds are unavailable; continuing without feedback");
}
}
fn start_capture(capture: &mut DictationCapture, started_at: CaptureInstant, play: impl FnOnce()) {
capture.start(started_at);
play();
}
/// Streams once the hold counts, so short taps and chords never reach the
/// model; a Toggle press is deliberate and streams at once.
fn stream_when_due(capture: &mut DictationCapture, engine: &TranscriptionEngine, toggle: bool) {
if capture.is_recording() && (toggle || capture.is_intentional()) {
capture.begin_stream(engine);
}
}
/// Starts collecting a release's tail. A release that will be transcribed
/// is announced at once rather than after its tail; a short tap is discarded
/// on its release alone, needs no tail, and stays silent.
///
/// The release restores any muted output before playing the stop tone so it is
/// heard while the tail is still captured. The silence gate ignores the tail,
/// so the tone itself never makes a silent clip transcribe.
fn begin_finishing(
capture: &DictationCapture,
ended_at: CaptureInstant,
print: Option<PrintReply>,
announce: impl FnOnce(),
) -> Finishing {
let now = Instant::now();
let transcribes = capture.would_transcribe(ended_at);
if transcribes {
announce();
}
Finishing {
ended_at,
print,
raw: false,
released: now,
deadline: if transcribes {
now + POST_RELEASE_TAIL + TAIL_STALL_GRACE
} else {
now
},
}
}
#[allow(clippy::too_many_arguments)]
fn submit_capture(
capture: &mut DictationCapture,
ended_at: CaptureInstant,
engine: &TranscriptionEngine,
jobs: &SyncSender<OutputJob>,
events: &mut EventLog,
pending: &mut usize,
print: Option<PrintReply>,
raw: bool,
) -> Result<()> {
let no_speech = |print: Option<PrintReply>| {
if let Some(print) = print {
print.send(PrintedTranscript::NoSpeech);
}
};
match capture.finish_with_tail(ended_at, POST_RELEASE_TAIL) {
Finish::Discard => {
// A short tap or chord withdraws the start tone of its press.
feedback::retract_start();
no_speech(print);
events.dictation(DictationPhase::Discarded, "")?
}
Finish::Transcribe(clip) if clip.is_near_silent() => {
no_speech(print);
events.dictation(DictationPhase::Discarded, "")?
}
Finish::Transcribe(mut clip) => {
let audio_ms = clip.duration_ms();
let job = OutputJob::Dictation(Job {
transcript: engine.finish(clip.take_stream(), clip.into_transcription_samples())?,
audio_ms,
print,
raw,
});
match jobs.try_send(job) {
Ok(()) => {
*pending += 1;
events.dictation(DictationPhase::Transcribing, "")?;
}
Err(TrySendError::Full(_)) => fail(events, "dictation queue is full")?,
Err(TrySendError::Disconnected(_)) => {
return Err(eyre!("dictation worker is unavailable"));
}
}
}
}
Ok(())
}
fn active_state(recording: bool, pending: usize) -> VoiceState {
if recording {
VoiceState::Dictating
} else if pending > 0 {
VoiceState::Transcribing
} else {
VoiceState::Listening
}
}
/// Logs a failed dictation or paste with the sound set's error sound.
fn fail(events: &EventLog, error: impl Into<String>) -> Result<()> {
feedback::play(Tone::Error);
events.dictation(DictationPhase::Failed(error.into()), "")?;
Ok(())
}
fn emit_state(events: &mut EventLog, state: VoiceState, device: &str) -> Result<()> {
events.emit(&VoiceEvent::State {
timestamp_ms: now_ms(),
state,
device: device.into(),
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transcription_engine::Command;
fn capture_time() -> CaptureInstant {
CaptureInstant::from_nanos(60_000_000_000)
}
fn event_log() -> (EventLog, std::path::PathBuf) {
let path = std::env::temp_dir().join(format!(
"hex-linux-dictation-{}-{}.ndjson",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
(EventLog::create(&path).unwrap(), path)
}
/// Records `seconds` of speech-level audio in 100 ms chunks, streaming
/// as the live loop would.
fn record(
capture: &mut DictationCapture,
engine: &TranscriptionEngine,
toggle: bool,
seconds: u64,
) -> CaptureInstant {
let start = capture_time();
start_capture(capture, start, || {});
stream_when_due(capture, engine, toggle);
let mut through = start;
for _ in 0..seconds * 10 {
through = through + Duration::from_millis(100);
let tone = (0..1_600)
.map(|index| 0.1 * (index as f32 * 0.05).sin())
.collect::<Vec<_>>();
capture.ingest(&tone, through);
capture.become_intentional(through);
stream_when_due(capture, engine, toggle);
}
through
}
#[test]
fn start_sound_precedes_audio_and_does_not_change_short_tap_discard() {
let mut capture = DictationCapture::new(16_000);
let started_at = capture_time();
let mut sounds = Vec::new();