ai-mate 0.3.3

A simple audio ai conversation system
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
use clap::Parser;
use cpal::traits::DeviceTrait;
use crossbeam_channel::{bounded, unbounded};
use std::process;
use std::sync::{Arc, OnceLock, atomic::Ordering};
use std::thread::{self, Builder};
use std::time::Instant;

mod assets;
mod audio;
mod config;
mod conversation;
mod keyboard;
mod llm;
mod log;
mod playback;
mod record;
mod state;
mod stt;
mod tts;
mod ui;
mod util;

static START_INSTANT: OnceLock<Instant> = OnceLock::new();

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
  env_logger::init();
  whisper_rs::install_logging_hooks();

  if !util::terminal_supported() {
    log::log(
      "error",
      "Terminal does not support colors or emojis. Please use a different terminal. exiting...",
    );
    process::exit(1);
  }
  assets::ensure_piper_espeak_env();
  assets::ensure_assets_env();

  crossterm::execute!(
    std::io::stdout(),
    crossterm::terminal::Clear(crossterm::terminal::ClearType::All)
  )
  .unwrap();
  println!(
    r#"
   █████╗ ██╗      ███╗   ███╗ █████╗ ████████╗███████╗
  ██╔══██╗██║      ████╗ ████║██╔══██╗╚══██╔══╝██╔════╝
  ███████║██║█████╗██╔████╔██║███████║   ██║   █████╗  
  ██╔══██║██║╚════╝██║╚██╔╝██║██╔══██║   ██║   ██╔══╝  
  ██║  ██║██║      ██║ ╚═╝ ██║██║  ██║   ██║   ███████╗
  ╚═╝  ╚═╝╚═╝      ╚═╝     ╚═╝╚═╝  ╚═╝   ╚═╝   ╚══════╝"#
  );

  println!(
    "    \x1b[90mv{}\x1b[0m\n\n\n\n\n",
    env!("CARGO_PKG_VERSION")
  );

  let _ = START_INSTANT.get_or_init(Instant::now);
  let args = crate::config::Args::parse();

  if args.list_voices {
    tts::print_voices();
    process::exit(0);
  }

  // silence external whisper logs
  // unsafe {
  //   whisper_rs::set_log_callback(Some(noop_whisper_log), std::ptr::null_mut());
  // }
  // show external whisper.cpp logs

  crate::log::set_verbose(args.verbose);

  // Resolve Whisper model path and log it
  let whisper_path = args.resolved_whisper_model_path();
  crate::log::log("info", &format!("Whisper model path: {}", whisper_path));

  let vad_thresh: f32 = args.sound_threshold_peak;
  let end_silence_ms: u64 = args.end_silence_ms;

  let host = cpal::default_host();
  let (in_dev, _in_stream) = audio::pick_input_stream(&host).unwrap_or_else(|msg| {
    log::log("error", &format!("{}", msg));
    process::exit(1)
  });
  let (out_dev, _out_stream) = audio::pick_output_stream(&host).unwrap_or_else(|msg| {
    log::log("error", &format!("{}", msg));
    process::exit(1)
  });
  log::log(
    "info",
    &format!(
      "Input device:  {}",
      in_dev.name().unwrap_or("<unknown>".into())
    ),
  );
  log::log(
    "info",
    &format!(
      "Output device: {}",
      out_dev.name().unwrap_or("<unknown>".into())
    ),
  );

  let out_cfg_supported = out_dev.default_output_config()?;
  let out_cfg: cpal::StreamConfig = out_cfg_supported.clone().into();
  let out_sample_rate = out_cfg.sample_rate.0;
  let out_channels = out_cfg.channels;

  let in_cfg_supported = config::pick_input_config(&in_dev, out_sample_rate)?;
  let in_cfg: cpal::StreamConfig = in_cfg_supported.clone().into();

  log::log(
    "info",
    &format!(
      "Picked Input:  {} ch @ {} Hz ({:?})",
      in_cfg.channels,
      in_cfg.sample_rate.0,
      in_cfg_supported.sample_format()
    ),
  );
  log::log(
    "info",
    &format!(
      "Picked Output: {} ch @ {} Hz ({:?})",
      out_cfg.channels,
      out_cfg.sample_rate.0,
      out_cfg_supported.sample_format()
    ),
  );
  log::log(
    "info",
    &format!("Playback stream SR (truth): {}", out_sample_rate),
  );

  // broadcast stop signal to all threads
  let (stop_all_tx, stop_all_rx) = unbounded::<()>();
  // channel for utterance audio chunks
  let (tx_utt, rx_utt) = unbounded::<audio::AudioChunk>();
  // channel for tts phrases
  let (tx_tts, rx_tts) = bounded::<(String, u64)>(1);
  // channel for playback audio chunks
  let (tx_play, rx_play) = unbounded::<audio::AudioChunk>();

  // Clones for threads
  let rx_play_for_playback = rx_play.clone();

  let stop_all_rx_for_record = stop_all_rx.clone();
  let stop_all_rx_for_keyboard = stop_all_rx.clone();
  let stop_all_rx_for_playback = stop_all_rx.clone();
  let (stop_play_tx, stop_play_rx) = unbounded::<()>(); // stop playback signal

  let available_langs = tts::get_all_available_languages();
  if !available_langs.contains(&args.language.as_str()) {
    log::log(
      "error",
      &format!(
        "Unsupported language '{}'. Next languages are supported: {}",
        args.language,
        available_langs.join(", ")
      ),
    );
    process::exit(1);
  }

  let voice_selected = if let Some(v) = &args.voice {
    v.clone()
  } else {
    if args.tts == "opentts" {
      tts::DEFAULT_OPENTTS_VOICES_PER_LANGUAGE
        .iter()
        .find(|(lang, _)| *lang == args.language.as_str())
        .map(|(_, voice)| (*voice).to_string())
        .unwrap()
    } else {
      tts::DEFAULTKOKORO_VOICES_PER_LANGUAGE
        .iter()
        .find(|(lang, _)| *lang == args.language.as_str())
        .map(|(_, voice)| (*voice).to_string())
        .unwrap()
    }
  };

  let valid_voices: Vec<&str> = tts::get_voices_for(&args.tts, &args.language);
  if valid_voices.is_empty() {
    log::log(
      "error",
      &format!(
        "No available voices for TTS '{}' and language '{}'.",
        args.tts, args.language
      ),
    );
    process::exit(1);
  }
  if !valid_voices.contains(&voice_selected.as_str()) {
    log::log(
      "error",
      &format!(
        "Invalid voice '{}' for TTS '{}' and language '{}'. Available voices: {}",
        voice_selected,
        args.tts,
        args.language,
        valid_voices.join(", ")
      ),
    );
    process::exit(1);
  }
  log::log("info", &format!("TTS system: {}", args.tts));
  if args.tts == "kokoro" {
    tts::start_kokoro_engine()?;
  }
  log::log("info", &format!("Language: {}", args.language));
  log::log("info", &format!("TTS voice: {}", voice_selected));
  log::log("info", &format!("LLM engine: {}", args.llm));
  if args.llm == "ollama" {
    log::log("info", &format!("ollama base url: {}", args.ollama_url));
  } else {
    log::log(
      "info",
      &format!("llama-server url: {}", args.llama_server_url),
    );
  }
  // initialize state after voice_selected
  let state = Arc::new(state::AppState::new_with_voice(voice_selected.clone()));
  let recording_paused = state.recording_paused.clone();
  let recording_paused_for_record = recording_paused.clone();
  if args.ptt {
    recording_paused.store(true, Ordering::Relaxed);
  }
  state::GLOBAL_STATE.set(state.clone()).unwrap();

  let interrupt_counter = state.interrupt_counter.clone();
  let paused = state.playback.paused.clone();
  let playback_active = state.playback.playback_active.clone();
  let gate_until_ms = state.playback.gate_until_ms.clone();

  let ui = state.ui.clone();
  let volume = state.playback.volume.clone();
  let conversation_history = state.conversation_history.clone();
  let volume_play = volume.clone();
  let volume_rec = volume.clone();
  let status_line = state.status_line.clone();

  // ---- Thread: UI Thread ----
  let (tx_ui, rx_ui) = unbounded::<String>();
  let ui_handle = ui::spawn_ui_thread(
    ui.clone(),
    stop_all_rx.clone(),
    status_line.clone(),
    ui.peak.clone(),
    rx_ui,
  );

  // ---- Thread: TTS -----
  let stop_play_tx_for_tts = stop_play_tx.clone();
  let tts_handle = thread::spawn({
    let voice_state = state.voice.clone();
    let out_sample_rate = out_sample_rate.clone();
    let tx_play = tx_play.clone();
    let stop_all_rx = stop_all_rx.clone();
    let interrupt_counter = interrupt_counter.clone();
    let args = args.clone();
    move || {
      tts::tts_thread(
        voice_state,
        out_sample_rate,
        tx_play,
        stop_all_rx,
        interrupt_counter,
        args,
        rx_tts,
        stop_play_tx_for_tts,
      )
      .unwrap();
    }
  });

  // ---- Thread: Playback (persistent) ----
  let playback_active_for_play = playback_active.clone();
  let gate_until_ms_for_play = gate_until_ms.clone();
  let paused_for_play = paused.clone();
  let ui_for_play = ui.clone();
  let volume_play_for_play = volume_play.clone();
  let play_handle = thread::spawn({
    move || {
      playback::playback_thread(
        &START_INSTANT,
        out_dev.clone(),
        out_cfg_supported.clone(),
        out_cfg.clone(),
        rx_play_for_playback,
        stop_play_rx,
        stop_all_rx_for_playback.clone(),
        playback_active_for_play.clone(),
        gate_until_ms_for_play.clone(),
        paused_for_play.clone(),
        out_channels,
        ui_for_play.clone(),
        volume_play_for_play.clone(),
      )
    }
  });

  // ---- Thread: record ----
  let tx_utt_for_rec = tx_utt.clone();
  let playback_active_for_rec = playback_active.clone();
  let gate_until_ms_for_rec = gate_until_ms.clone();
  let interrupt_counter_for_rec = interrupt_counter.clone();
  let stop_all_rx_for_record_for_rec = stop_all_rx_for_record.clone();
  let ui_peak_for_rec = ui.peak.clone();
  let ui_for_rec = ui.clone();
  let volume_rec_for_rec = volume_rec.clone();
  let recording_paused_for_record_for_rec = recording_paused_for_record.clone();
  let tx_ui_for_record = tx_ui.clone();
  let rec_handle = Builder::new()
    .name("record_thread".to_string())
    .stack_size(4 * 1024 * 1024)
    .spawn({
      move || {
        record::record_thread(
          &START_INSTANT,
          in_dev.clone(),
          in_cfg_supported,
          in_cfg,
          tx_utt_for_rec.clone(),
          tx_ui_for_record,
          vad_thresh,
          end_silence_ms,
          playback_active_for_rec.clone(),
          gate_until_ms_for_rec.clone(),
          interrupt_counter_for_rec.clone(),
          stop_all_rx_for_record_for_rec.clone(),
          ui_peak_for_rec.clone(),
          ui_for_rec.clone(),
          volume_rec_for_rec.clone(),
          recording_paused_for_record_for_rec.clone(),
        )
      }
    })?;

  // ---- Thread: conversation ----
  let rx_utt_for_conv = rx_utt.clone();
  let stop_all_rx_for_conv = stop_all_rx.clone();
  let stop_all_tx_for_conv = stop_all_tx.clone();
  let interrupt_counter_for_conv = interrupt_counter.clone();
  let whisper_path_for_conv = whisper_path.clone();
  let args_for_conv = args.clone();
  let ui_for_conv = ui.clone();
  let conversation_history_for_conv = conversation_history.clone();
  let tx_tts_for_conv = tx_tts.clone();
  let conv_handle = thread::spawn({
    move || {
      conversation::conversation_thread(
        rx_utt_for_conv,
        stop_all_rx_for_conv.clone(),
        stop_all_tx_for_conv.clone(),
        interrupt_counter_for_conv.clone(),
        whisper_path_for_conv.clone(),
        args_for_conv.clone(),
        ui_for_conv.clone(),
        conversation_history_for_conv.clone(),
        tx_ui.clone(),
        tx_tts_for_conv.clone(),
      )
    }
  });

  // ---- Thread: keyboard ----
  let state_for_key = state.clone();
  let paused_for_key = paused.clone();
  let recording_paused_for_key = recording_paused.clone();
  let voice_for_key = state_for_key.voice.clone();
  let args_tts_for_key = args.tts.clone();
  let args_language_for_key = args.language.clone();
  let stop_all_tx_for_key = stop_all_tx.clone();
  let stop_play_tx_for_key = stop_play_tx.clone();
  let key_handle = thread::spawn({
    move || {
      keyboard::keyboard_thread(
        stop_all_tx_for_key.clone(),
        stop_all_rx_for_keyboard.clone(),
        paused_for_key.clone(),
        recording_paused_for_key.clone(),
        voice_for_key.clone(),
        args_tts_for_key.clone(),
        args_language_for_key.clone(),
        stop_play_tx_for_key.clone(),
        interrupt_counter.clone(),
        args.ptt,
      )
    }
  });

  // Print config knobs
  let hangover_ms = util::env_u64("HANGOVER_MS", config::HANGOVER_MS_DEFAULT);
  log::log(
    "info",
    &format!(
      "sound_threshold_peak={:.3}  end_silence_ms={}  hangover_ms={}",
      vad_thresh, end_silence_ms, hangover_ms
    ),
  );

  // Block until keyboard thread exits (Enter/Esc), then propagate stop.
  let _ = key_handle.join();
  let _ = stop_all_tx.try_send(());

  drop(stop_play_tx);
  drop(tx_tts);

  // Wait for all threads to finish
  let _ = rec_handle.join().unwrap();
  let _ = play_handle.join().unwrap();
  let _ = conv_handle.join().unwrap();
  let _ = ui_handle.join().unwrap();
  let _ = tts_handle.join().unwrap();

  Ok(())
}