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
//! Fluent builder for constructing an [`AudioEngine`].
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU32};
use std::sync::Arc;
use crate::{RecordingMode, TranscriptionProfile, WhisperModel};
use tokio::sync::broadcast;
use tracing::info;
use crate::transcription;
use crate::transcription::TranscribeStateCallback;
use crate::{AudioEngine, EngineConfig, EngineTranscriptionCallback};
/// Broadcast channel capacity.
const BROADCAST_CAPACITY: usize = 256;
/// [`TranscribeStateCallback`] implementation that stores the path of each
/// saved recording WAV into a shared slot on the engine.
struct LastRecordingPathCallback {
last_recording_path: Arc<std::sync::Mutex<Option<std::path::PathBuf>>>,
}
impl TranscribeStateCallback for LastRecordingPathCallback {
fn on_recording_saved(&self, path: String) {
if let Ok(mut guard) = self.last_recording_path.lock() {
*guard = Some(std::path::PathBuf::from(path));
}
}
fn on_queue_update(&self, _depth: usize) {}
}
/// Fluent builder for constructing an [`AudioEngine`].
///
/// # Example
///
/// ```rust,no_run
/// use vtx_engine::EngineBuilder;
///
/// #[tokio::main]
/// async fn main() {
/// let (engine, rx) = EngineBuilder::new()
/// .segment_max_duration_ms(10_000)
/// .without_visualization()
/// .build()
/// .await
/// .unwrap();
/// }
/// ```
pub struct EngineBuilder {
config: EngineConfig,
app_name: String,
transcription_enabled: bool,
visualization_enabled: bool,
vad_enabled: bool,
audio_streaming_enabled: bool,
raw_audio_streaming_enabled: bool,
}
impl EngineBuilder {
/// Create a new builder with all subsystems enabled and default configuration.
pub fn new() -> Self {
Self {
config: EngineConfig::default(),
app_name: "vtx-engine".to_string(),
transcription_enabled: true,
visualization_enabled: true,
vad_enabled: true,
audio_streaming_enabled: false,
raw_audio_streaming_enabled: false,
}
}
/// Create a builder pre-populated from an existing [`EngineConfig`].
pub fn from_config(config: EngineConfig) -> Self {
Self {
config,
app_name: "vtx-engine".to_string(),
transcription_enabled: true,
visualization_enabled: true,
vad_enabled: true,
audio_streaming_enabled: false,
raw_audio_streaming_enabled: false,
}
}
/// Set the application name used for resolving data directories
/// (model cache, config, history).
///
/// Defaults to `"vtx-engine"`. Host applications should set this to
/// their own name so that model files, config, and history are stored
/// under the application's own data directory rather than vtx-engine's.
pub fn app_name(mut self, name: impl Into<String>) -> Self {
self.app_name = name.into();
self
}
// -------------------------------------------------------------------------
// Config field setters
// -------------------------------------------------------------------------
/// Set the Whisper model variant to use for transcription.
///
/// Path resolution is handled by [`ModelManager`](crate::ModelManager).
pub fn model(mut self, model: WhisperModel) -> Self {
self.config.model = model;
self
}
/// Override the Whisper model file path.
///
/// **Deprecated** — use [`model`](Self::model) instead. When set, this
/// takes precedence over the `model` field and a warning is logged.
#[deprecated(since = "0.2.0", note = "Use EngineBuilder::model instead")]
pub fn model_path(mut self, path: PathBuf) -> Self {
#[allow(deprecated)]
{
self.config.model_path = Some(path);
}
self
}
/// Enable or disable word-break segmentation.
///
/// When `false`, the audio loop still detects word-break events internally
/// but does not split segments at pause boundaries. Segment boundaries are
/// determined solely by speech-end detection and `segment_max_duration_ms`.
/// Defaults to `true`.
pub fn word_break_segmentation_enabled(mut self, enabled: bool) -> Self {
self.config.word_break_segmentation_enabled = enabled;
self
}
/// Apply a [`TranscriptionProfile`] preset, seeding `EngineConfig` with
/// the profile's default values.
///
/// This method **overwrites** any previously-set fields covered by the
/// profile. Call individual setters *after* `with_profile` to override
/// specific fields.
///
/// `Custom` profile does nothing — all fields remain at their `Default`.
pub fn with_profile(mut self, profile: TranscriptionProfile) -> Self {
match profile {
TranscriptionProfile::Dictation => {
self.config.vad_voiced_threshold_db = -42.0;
self.config.vad_whisper_threshold_db = -52.0;
self.config.vad_voiced_onset_ms = 80;
self.config.vad_whisper_onset_ms = 120;
self.config.segment_max_duration_ms = 4_000;
self.config.segment_word_break_grace_ms = 750;
self.config.word_break_segmentation_enabled = true;
self.config.model = WhisperModel::BaseEn;
}
TranscriptionProfile::Transcription => {
self.config.vad_voiced_threshold_db = -42.0;
self.config.vad_whisper_threshold_db = -52.0;
self.config.vad_voiced_onset_ms = 80;
self.config.vad_whisper_onset_ms = 120;
self.config.segment_max_duration_ms = 15_000;
self.config.segment_word_break_grace_ms = 0;
self.config.word_break_segmentation_enabled = false;
self.config.model = WhisperModel::MediumEn;
}
TranscriptionProfile::Custom => {
// No presets applied; caller supplies all values via setters.
}
}
self
}
/// Set recording mode (`Mixed` or `EchoCancel`).
pub fn recording_mode(mut self, mode: RecordingMode) -> Self {
self.config.recording_mode = mode;
self
}
/// Set the voiced speech detection threshold in dB.
pub fn vad_voiced_threshold_db(mut self, db: f32) -> Self {
self.config.vad_voiced_threshold_db = db;
self
}
/// Set the whisper/soft speech detection threshold in dB.
pub fn vad_whisper_threshold_db(mut self, db: f32) -> Self {
self.config.vad_whisper_threshold_db = db;
self
}
/// Set the voiced speech onset duration in ms.
pub fn vad_voiced_onset_ms(mut self, ms: u64) -> Self {
self.config.vad_voiced_onset_ms = ms;
self
}
/// Set the whisper speech onset duration in ms.
pub fn vad_whisper_onset_ms(mut self, ms: u64) -> Self {
self.config.vad_whisper_onset_ms = ms;
self
}
/// Set maximum segment duration before seeking a word-break split in ms.
pub fn segment_max_duration_ms(mut self, ms: u64) -> Self {
self.config.segment_max_duration_ms = ms;
self
}
/// Set grace period after max duration before forcing segment submission in ms.
pub fn segment_word_break_grace_ms(mut self, ms: u64) -> Self {
self.config.segment_word_break_grace_ms = ms;
self
}
/// Set lookback buffer duration in ms.
pub fn segment_lookback_ms(mut self, ms: u64) -> Self {
self.config.segment_lookback_ms = ms;
self
}
/// Set maximum transcription queue depth.
pub fn transcription_queue_capacity(mut self, cap: usize) -> Self {
self.config.transcription_queue_capacity = cap;
self
}
/// Set visualization frame interval in ms.
pub fn viz_frame_interval_ms(mut self, ms: u64) -> Self {
self.config.viz_frame_interval_ms = ms;
self
}
// -------------------------------------------------------------------------
// Subsystem toggles
// -------------------------------------------------------------------------
/// Disable the transcription subsystem. No `TranscriptionComplete` events
/// will be emitted and whisper.cpp will not be loaded.
pub fn without_transcription(mut self) -> Self {
self.transcription_enabled = false;
self
}
/// Disable the visualization subsystem. No `VisualizationData` events
/// will be emitted.
pub fn without_visualization(mut self) -> Self {
self.visualization_enabled = false;
self
}
/// Disable the VAD subsystem. No `SpeechStarted`/`SpeechEnded` events
/// will be emitted from VAD. (Manual recording via `start_recording`/
/// `stop_recording` still works.)
pub fn without_vad(mut self) -> Self {
self.vad_enabled = false;
self
}
/// Enable streaming of processed audio data. When enabled, the engine
/// emits [`EngineEvent::AudioData`](crate::EngineEvent::AudioData) events
/// for every audio chunk during live capture. The samples are mono f32
/// PCM after mic-gain and AGC processing.
///
/// Disabled by default.
pub fn with_audio_streaming(mut self) -> Self {
self.audio_streaming_enabled = true;
self
}
/// Enable streaming of raw (unprocessed) audio data. When enabled, the
/// engine emits [`EngineEvent::RawAudioData`](crate::EngineEvent::RawAudioData)
/// events for every audio chunk during live capture. The samples are mono
/// f32 PCM immediately after channel down-mix, before mic-gain and AGC.
///
/// Disabled by default. Can be combined with [`with_audio_streaming`](Self::with_audio_streaming)
/// to receive both processed and raw streams independently.
pub fn with_raw_audio_streaming(mut self) -> Self {
self.raw_audio_streaming_enabled = true;
self
}
// -------------------------------------------------------------------------
// Build
// -------------------------------------------------------------------------
/// Build the [`AudioEngine`].
///
/// Returns `(engine, receiver)` where `receiver` is the first broadcast
/// receiver. Call [`AudioEngine::subscribe`] to obtain additional receivers.
pub async fn build(
self,
) -> Result<(AudioEngine, broadcast::Receiver<crate::EngineEvent>), String> {
info!("Initializing audio backend...");
crate::platform::init_audio_backend()?;
let (sender, receiver) = broadcast::channel(BROADCAST_CAPACITY);
let sender = Arc::new(sender);
// Resolve the model path: model_path (deprecated) takes precedence, then model enum.
#[allow(deprecated)]
let resolved_model_path = if let Some(ref explicit_path) = self.config.model_path {
tracing::warn!(
"[EngineBuilder] model_path is deprecated (since 0.2.0). \
Use EngineConfig::model instead. Falling back to explicit path: {}",
explicit_path.display()
);
explicit_path.clone()
} else {
// Use ModelManager to resolve path from the WhisperModel enum.
crate::model_manager::ModelManager::new(&self.app_name).path(self.config.model)
};
// Optionally initialize transcription worker
let transcription_queue = if self.transcription_enabled {
let callback = EngineTranscriptionCallback {
sender: sender.clone(),
};
let queue = Arc::new(transcription::TranscriptionQueue::new());
queue.set_callback(Arc::new(callback));
queue.start_worker(resolved_model_path.clone());
Some(queue)
} else {
None
};
// TranscribeState needs a queue; use a dummy no-op queue when transcription is off
let queue_for_state = transcription_queue
.clone()
.unwrap_or_else(|| Arc::new(transcription::TranscriptionQueue::new()));
// Shared slot that the TranscribeStateCallback writes the WAV path into.
let last_recording_path: Arc<std::sync::Mutex<Option<std::path::PathBuf>>> =
Arc::new(std::sync::Mutex::new(None));
let mut ts = transcription::TranscribeState::new(queue_for_state);
ts.set_callback(Arc::new(LastRecordingPathCallback {
last_recording_path: last_recording_path.clone(),
}));
let transcribe_state = Arc::new(std::sync::Mutex::new(ts));
let initial_mic_gain_bits = self.config.mic_gain_db.to_bits();
let initial_agc_config = self.config.agc.clone();
let engine = AudioEngine {
config: self.config,
app_name: self.app_name,
recording_mode_override: Arc::new(std::sync::Mutex::new(None)),
sender,
transcription_queue,
transcribe_state,
audio_loop_active: Arc::new(AtomicBool::new(false)),
transcription_enabled: Arc::new(AtomicBool::new(self.transcription_enabled)),
vad_enabled: self.vad_enabled,
visualization_enabled: self.visualization_enabled,
audio_streaming_enabled: self.audio_streaming_enabled,
raw_audio_streaming_enabled: self.raw_audio_streaming_enabled,
shutdown_flag: Arc::new(AtomicBool::new(false)),
recording_active: Arc::new(AtomicBool::new(false)),
recording_start: Arc::new(std::sync::Mutex::new(None)),
model_path: resolved_model_path,
last_recording_path,
playback_tx: Arc::new(std::sync::Mutex::new(None)),
playback_active: Arc::new(AtomicBool::new(false)),
mic_gain_db: Arc::new(AtomicU32::new(initial_mic_gain_bits)),
ptt_mode: Arc::new(AtomicBool::new(true)),
agc_config: Arc::new(std::sync::Mutex::new(initial_agc_config)),
render_tx: Arc::new(std::sync::Mutex::new(None)),
};
Ok((engine, receiver))
}
}
impl Default for EngineBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn audio_streaming_disabled_by_default() {
let builder = EngineBuilder::new();
assert!(!builder.audio_streaming_enabled);
assert!(!builder.raw_audio_streaming_enabled);
}
#[test]
fn audio_streaming_disabled_by_default_from_config() {
let builder = EngineBuilder::from_config(crate::EngineConfig::default());
assert!(!builder.audio_streaming_enabled);
assert!(!builder.raw_audio_streaming_enabled);
}
#[test]
fn with_audio_streaming_enables_processed() {
let builder = EngineBuilder::new().with_audio_streaming();
assert!(builder.audio_streaming_enabled);
assert!(!builder.raw_audio_streaming_enabled);
}
#[test]
fn with_raw_audio_streaming_enables_raw() {
let builder = EngineBuilder::new().with_raw_audio_streaming();
assert!(!builder.audio_streaming_enabled);
assert!(builder.raw_audio_streaming_enabled);
}
#[test]
fn both_streams_enabled_independently() {
let builder = EngineBuilder::new()
.with_audio_streaming()
.with_raw_audio_streaming();
assert!(builder.audio_streaming_enabled);
assert!(builder.raw_audio_streaming_enabled);
}
#[test]
fn audio_streaming_combines_with_subsystem_toggles() {
let builder = EngineBuilder::new()
.with_audio_streaming()
.with_raw_audio_streaming()
.without_transcription()
.without_visualization();
assert!(builder.audio_streaming_enabled);
assert!(builder.raw_audio_streaming_enabled);
assert!(!builder.transcription_enabled);
assert!(!builder.visualization_enabled);
}
#[test]
fn builder_method_chaining_order_independent() {
// Verify order doesn't matter
let b1 = EngineBuilder::new()
.with_raw_audio_streaming()
.with_audio_streaming();
let b2 = EngineBuilder::new()
.with_audio_streaming()
.with_raw_audio_streaming();
assert_eq!(b1.audio_streaming_enabled, b2.audio_streaming_enabled);
assert_eq!(
b1.raw_audio_streaming_enabled,
b2.raw_audio_streaming_enabled
);
}
}