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
//! Minimal Voice-Only Controller TUI for MicStudio.
pub mod app;
pub mod render;
pub use app::AppState;
pub use render::render_ui;
use crate::monitor::MicMonitor;
use anyhow::Result;
use crossterm::{
event::{self, Event, KeyCode, KeyModifiers},
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use std::io::stdout;
use std::time::Duration;
pub fn run_tui() -> Result<()> {
let state = AppState::new()?;
enable_raw_mode()?;
let mut out = stdout();
execute!(out, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(out);
let mut terminal = Terminal::new(backend)?;
let res = run_event_loop_with_state(&mut terminal, state);
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
terminal.show_cursor()?;
res
}
pub fn run_tui_spectrograph(waterfall: bool) -> Result<()> {
let mut state = AppState::new()?;
enable_raw_mode()?;
let mut out = stdout();
execute!(out, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(out);
let mut terminal = Terminal::new(backend)?;
state.spectrograph_fullscreen = true;
if waterfall {
state.spectrograph_mode = app::SpectrographMode::Waterfall;
}
let res = run_event_loop_with_state(&mut terminal, state);
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
terminal.show_cursor()?;
res
}
fn run_event_loop_with_state(
terminal: &mut Terminal<CrosstermBackend<std::io::Stdout>>,
mut state: AppState,
) -> Result<()> {
// If sidetone was persisted as enabled, establish PipeWire monitor links immediately on launch
if state.sidetone_enabled {
crate::config::CleanMicSettings::sync_hear_voice(true);
state.sidetone_guarded = crate::config::CleanMicSettings::is_sidetone_guarded();
}
let should_monitor =
state.meter_enabled || state.spectrograph_enabled || state.spectrograph_fullscreen;
let mut monitor = MicMonitor::new(should_monitor);
let mut tick_counter: usize = 0;
loop {
// Redraw current screen state
terminal.draw(|frame| render_ui(frame, &state))?;
// Periodically refresh output sink guard state (approx every 1s)
tick_counter = (tick_counter + 1) % 30;
if tick_counter == 0 {
state.sidetone_guarded = crate::config::CleanMicSettings::is_sidetone_guarded();
}
// 30Hz when meter or spectrograph is on (smooth 33ms animation), 500ms slow tick when idle
let active_monitoring =
state.meter_enabled || state.spectrograph_enabled || state.spectrograph_fullscreen;
let timeout = if active_monitoring {
Duration::from_millis(33)
} else {
Duration::from_millis(500)
};
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
// Ignore key release events on Windows/terminals that report them
if key.kind == crossterm::event::KeyEventKind::Release {
continue;
}
if state.show_inputs {
match key.code {
KeyCode::Esc | KeyCode::Char('q') => state.show_inputs = false,
KeyCode::Char('j') | KeyCode::Down => {
state.input_cursor = (state.input_cursor + 1).min(state.input_devices.len());
}
KeyCode::Char('k') | KeyCode::Up => {
state.input_cursor = state.input_cursor.saturating_sub(1);
}
KeyCode::Enter => {
let name = if state.input_cursor == 0 { "" } else {
&state.input_devices[state.input_cursor - 1].name
};
let result = crate::input::select(name);
state.set_status(match result {
Ok(()) => "Input selection saved; automatic fallback remains enabled.".into(),
Err(error) => format!("Input selection failed: {error}"),
});
state.show_inputs = false;
}
_ => {}
}
continue;
}
if state.show_help {
match key.code {
KeyCode::Esc | KeyCode::Char('?') | KeyCode::Char('q') | KeyCode::Enter => {
state.close_help();
}
_ => {}
}
continue;
}
if let Some(action) = state.confirm_action.clone() {
match key.code {
KeyCode::Char('y') | KeyCode::Enter => {
match action {
crate::ui::app::ConfirmAction::ResetSelected(_) => {
let _ = state.reset_selected_preset();
}
crate::ui::app::ConfirmAction::ResetAll => {
let _ = state.reset_all_to_factory();
}
crate::ui::app::ConfirmAction::QuitUnsaved => {
let _ = state.save_current_tweaks();
state.should_quit = true;
}
}
state.confirm_action = None;
}
KeyCode::Char('w') if matches!(action, crate::ui::app::ConfirmAction::QuitUnsaved) => {
let _ = state.save_current_tweaks();
state.confirm_action = None;
state.should_quit = true;
}
KeyCode::Char('q') | KeyCode::Char('n') if matches!(action, crate::ui::app::ConfirmAction::QuitUnsaved) => {
state.discard_and_restore_saved();
state.confirm_action = None;
state.should_quit = true;
}
KeyCode::Char('n') | KeyCode::Esc => {
state.confirm_action = None;
state.set_status("Cancelled.".to_string());
}
_ => {}
}
continue;
}
// If in search input mode
if state.search_mode {
match key.code {
KeyCode::Esc => {
state.exit_search(true);
}
KeyCode::Enter => {
state.exit_search(false);
}
KeyCode::Backspace => {
state.handle_search_backspace();
}
KeyCode::Char(c) => {
state.handle_search_char(c);
}
_ => {}
}
continue;
}
match (key.code, key.modifiers) {
(KeyCode::Char('i'), _) => {
match crate::input::devices() {
Ok(devices) => {
let selected = crate::input::selected().unwrap_or_default();
state.input_cursor = devices.iter().position(|d| d.name == selected)
.map(|index| index + 1).unwrap_or(0);
state.input_devices = devices;
state.show_inputs = true;
}
Err(error) => state.set_status(format!("Input discovery failed: {error}")),
}
}
(KeyCode::Char('q'), _) => {
state.request_quit();
}
(KeyCode::Char('c'), KeyModifiers::CONTROL) => {
state.request_quit();
}
// Shift+j / J: cycle down through Live Controls
(KeyCode::Char('J'), _) => {
state.cycle_control_next();
}
(KeyCode::Char('j'), m) if m.contains(KeyModifiers::SHIFT) => {
state.cycle_control_next();
}
// Shift+k / K: cycle up through Live Controls
(KeyCode::Char('K'), _) => {
state.cycle_control_prev();
}
(KeyCode::Char('k'), m) if m.contains(KeyModifiers::SHIFT) => {
state.cycle_control_prev();
}
// j / Down: move down in presets list
(KeyCode::Char('j'), _) | (KeyCode::Down, _) => {
state.move_cursor_down();
}
// k / Up: move up in presets list
(KeyCode::Char('k'), _) | (KeyCode::Up, _) => {
state.move_cursor_up();
}
(KeyCode::Enter, _) => {
let _ = state.apply_selected();
monitor.restart();
if state.show_details_modal {
state.close_details_modal();
}
}
(KeyCode::Char(' '), _) => {
state.toggle_details_modal();
}
(KeyCode::Char('c'), _) => {
let _ = state.revert_clean();
monitor.restart();
}
(KeyCode::Char('/'), _) => {
state.enter_search();
}
(KeyCode::Char('p'), _) => {
state.trigger_audition();
}
(KeyCode::F(5), _) => {
let _ = state.reload_presets();
monitor.restart();
}
(KeyCode::Char('r'), KeyModifiers::CONTROL) => {
let _ = state.reload_presets();
monitor.restart();
}
(KeyCode::Char('R'), _) => {
state.request_reset_all();
}
(KeyCode::Char('r'), m) if m.contains(KeyModifiers::SHIFT) => {
state.request_reset_all();
}
(KeyCode::Char('r'), _) => {
state.request_reset_selected();
}
(KeyCode::Char('w'), _) => {
let _ = state.save_current_tweaks();
}
(KeyCode::Char(c), _) if c.is_ascii_digit() && c != '0' => {
let num = (c as u8 - b'0') as usize;
state.jump_to_preset(num);
}
(KeyCode::Tab, KeyModifiers::NONE) => {
state.cycle_control_next();
}
(KeyCode::BackTab, _) => {
state.cycle_control_prev();
}
(KeyCode::Char('h'), _) | (KeyCode::Left, _) => {
let _ = state.adjust_selected_control(-1);
monitor.set_enabled(
state.meter_enabled
|| state.spectrograph_enabled
|| state.sidetone_enabled,
);
}
(KeyCode::Char('l'), _) | (KeyCode::Right, _) => {
let _ = state.adjust_selected_control(1);
monitor.set_enabled(
state.meter_enabled
|| state.spectrograph_enabled
|| state.sidetone_enabled,
);
}
(KeyCode::Char('m'), _) | (KeyCode::Char('M'), _) | (KeyCode::Char('s'), _) => {
state.toggle_spectrograph_fullscreen();
monitor.set_enabled(
state.meter_enabled
|| state.spectrograph_enabled
|| state.spectrograph_fullscreen
|| state.sidetone_enabled,
);
crate::config::ensure_meter_linked();
}
(KeyCode::Char('g'), _) | (KeyCode::Char('G'), _) => {
state.toggle_spectrograph_mode();
}
(KeyCode::Char('v'), _) => {
let _ = state.toggle_sidetone();
monitor.set_enabled(
state.meter_enabled
|| state.spectrograph_enabled
|| state.spectrograph_fullscreen
|| state.sidetone_enabled,
);
crate::config::ensure_meter_linked();
}
(KeyCode::Char('t'), _) => {
state.toggle_meter();
monitor.set_enabled(
state.meter_enabled
|| state.spectrograph_enabled
|| state.spectrograph_fullscreen
|| state.sidetone_enabled,
);
crate::config::ensure_meter_linked();
}
(KeyCode::Char('?'), _) => {
state.toggle_help();
}
(KeyCode::Esc, _) => {
if state.show_details_modal {
state.close_details_modal();
} else if state.spectrograph_fullscreen {
state.spectrograph_fullscreen = false;
} else if !state.search_query.is_empty() {
state.search_query.clear();
state.cursor = 0;
} else {
state.close_help();
}
}
_ => {}
}
}
}
if state.should_quit {
break;
}
// Update live spectrum and meter telemetry
if state.meter_enabled || state.spectrograph_enabled || state.spectrograph_fullscreen {
let spec = monitor.get_spectrum();
state.input_rms_db = spec.rms_db;
state.spectrum = spec;
}
}
// Ensure any sidetone monitor links are severed upon quitting MicStudio
crate::config::CleanMicSettings::sync_hear_voice(false);
Ok(())
}