diff --git a/README.md b/README.md index a10c8a1..8b7b3aa 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,9 @@ service externe. - **Texte** entre le client et l'API, **jamais d'audio**. - **Projets** : une conversation = une mémoire persistante, rejouée vers le modèle à chaque tour, avec plafond de taille et bouton de vérification. -- **Voix locale** : maintien du bouton micro pour parler, réponse lue à voix haute. +- **Voix locale** : un appui sur le bouton micro lance l'enregistrement, un second + appui (`Ctrl+Espace` au clavier) l'arrête et lance la transcription, réponse lue à + voix haute. - **Deux modes** : un onglet **💬 Chat** (écrire) et un onglet **🗣️ Compagnon** (parler à l'avatar centré) ; la transcription part vers le backend en **texte JSON**, jamais en audio. diff --git a/docs/architecture.md b/docs/architecture.md index 726d338..e166576 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -226,7 +226,9 @@ lancements : - le **Chat** garde la barre latérale des projets et les bulles ; - le **Compagnon** centre l'avatar (tamagotchi), affiche l'humeur et le dernier échange en -sous-titres, et met en avant un grand bouton **push-to-talk**. +sous-titres, et met en avant un grand bouton **micro à bascule** (un appui démarre +l'enregistrement, le suivant l'arrête ; `Ctrl+Espace` fait la même chose depuis les deux +onglets). Le mode ne change **pas** le contrat réseau : la transcription produite par Whisper est passée à `send_message`, donc envoyée au backend en **texte JSON** exactement comme un diff --git a/frontend/README.md b/frontend/README.md index e8a108a..7b5cef5 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -83,7 +83,7 @@ Deux onglets en haut de la fenêtre basculent l'affichage : | Onglet | Contenu | | --- | --- | | **💬 Chat** | Barre latérale des projets, bulles de conversation, saisie clavier. | -| **🗣️ Compagnon** | L'avatar (tamagotchi) au centre, l'humeur, les sous-titres du dernier échange et un grand bouton micro *push-to-talk*. | +| **🗣️ Compagnon** | L'avatar (tamagotchi) au centre, l'humeur, les sous-titres du dernier échange et un grand bouton micro à bascule. | Le **mode est mémorisé** et rouvert au lancement suivant. Les deux onglets parlent au même code : un message transcrit par la voix emprunte exactement le même chemin qu'un @@ -174,11 +174,13 @@ se replie au lieu de déborder de la fenêtre. La voix est traitée **entièrement côté client** : rien d'audio ne transite vers le backend. -- **STT** — bouton **push-to-talk** : dans le mode Chat, le petit bouton `🎤` de la barre - de saisie ; dans le mode Compagnon, le **grand bouton circulaire** sous l'avatar. Maintiens - pour parler, relâche pour transcrire. Le micro est capturé via `cpal`, rééchantillonné à - 16 kHz mono si besoin, puis transcrit par **whisper.cpp** (`whisper-rs`). Le texte obtenu - est envoyé automatiquement au backend — même requête JSON qu'un message tapé. +- **STT** — bouton micro à **bascule** : dans le mode Chat, le petit bouton `🎤` de la barre + de saisie ; dans le mode Compagnon, le **grand bouton circulaire** sous l'avatar. Un appui + démarre l'enregistrement, un second appui le stoppe et lance la transcription ; le + raccourci `Ctrl+Espace` fait exactement la même chose, depuis n'importe quel onglet. Le + micro est capturé via `cpal`, rééchantillonné à 16 kHz mono si besoin, puis transcrit par + **whisper.cpp** (`whisper-rs`). Le texte obtenu est envoyé automatiquement au backend — + même requête JSON qu'un message tapé. - **TTS** — coche « 🔊 Voix » (ou `JARVIS_SPEAK=1`) : à la fin du streaming, la réponse est synthétisée par **Piper** (`piper-rs`) et jouée sur le haut-parleur (rééchantillonnage vers la fréquence du périphérique de sortie). diff --git a/frontend/src/app.rs b/frontend/src/app.rs index 9da37d2..7ea6ecf 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -34,6 +34,15 @@ const ERROR: egui::Color32 = egui::Color32::from_rgb(0xff, 0x6b, 0x6b); /// Background of the top and bottom strips. const BAR_FILL: egui::Color32 = egui::Color32::from_rgb(0x16, 0x19, 0x21); +/// Keyboard shortcut that toggles the microphone, the exact same action as +/// clicking the mic button. It is consumed before any text widget sees it, so +/// it never types a space into the compose box. +const MIC_HOTKEY: egui::Key = egui::Key::Space; +/// Modifier held with `MIC_HOTKEY`. +const MIC_HOTKEY_MODIFIERS: egui::Modifiers = egui::Modifiers::CTRL; +/// How the shortcut is spelled in the interface. +const MIC_HOTKEY_LABEL: &str = "Ctrl+Espace"; + /// Messages sent from background tasks to the UI thread. enum UiEvent { /// Login succeeded: a token and the user profile are available. @@ -615,7 +624,7 @@ impl JarviseApp { self.login_password.clear(); } - /// Starts capturing the microphone (push-to-talk press). + /// Starts capturing the microphone (first press of the record toggle). fn start_recording(&mut self) { if self.recorder.is_some() { return; @@ -624,13 +633,37 @@ impl JarviseApp { match Recorder::start() { Ok(recorder) => { self.recorder = Some(recorder); - self.voice_status = Some("🎙️ Enregistrement… relâche pour transcrire".to_string()); + self.voice_status = Some(format!( + "🎙️ Enregistrement… appuie à nouveau ({MIC_HOTKEY_LABEL}) pour transcrire" + )); } Err(error) => self.error = Some(error.to_string()), } } - /// Stops the capture and queues the transcription (push-to-talk release). + /// Global microphone shortcut, available from both tabs. The key is + /// consumed so it cannot be typed into the focused compose box. + fn handle_mic_hotkey(&mut self, ui: &mut egui::Ui) { + if !self.voice.stt_ready() { + return; + } + if ui.input_mut(take_mic_hotkey) { + self.toggle_recording(); + } + } + + /// Flips the capture state: idle starts recording, recording stops it and + /// queues the transcription. Shared by the mic button and the shortcut so + /// both behave identically. + fn toggle_recording(&mut self) { + if self.recorder.is_some() { + self.stop_recording(); + } else { + self.start_recording(); + } + } + + /// Stops the capture and queues the transcription (second press of the toggle). fn stop_recording(&mut self) { let Some(recorder) = self.recorder.take() else { return; @@ -1079,20 +1112,21 @@ impl JarviseApp { let input_width = (ui.available_width() - button_width - mic_width - 20.0).max(120.0); - // Push-to-talk: hold the mic button to record, release to send. + // Record toggle: one click starts the capture, the next stops it. let mic = ui.add_enabled( self.voice.stt_ready(), egui::Button::new(if self.recorder.is_some() { "⏺" } else { "🎤" }) .min_size(egui::vec2(mic_width, 52.0)), ); - if mic.is_pointer_button_down_on() && self.recorder.is_none() { - self.start_recording(); - } - if !mic.is_pointer_button_down_on() && self.recorder.is_some() { - self.stop_recording(); + if mic.clicked() { + self.toggle_recording(); } if self.voice.stt_ready() { - mic.on_hover_text("Maintenir pour parler (push-to-talk)"); + mic.on_hover_text(if self.recorder.is_some() { + format!("Arrêter et transcrire ({MIC_HOTKEY_LABEL})") + } else { + format!("Démarrer l'enregistrement ({MIC_HOTKEY_LABEL})") + }); } else { mic.on_disabled_hover_text("STT non configuré : définis WHISPER_MODEL"); } @@ -1170,7 +1204,7 @@ impl JarviseApp { } /// The companion stage: the avatar in the middle, the mood underneath, the - /// last exchange as subtitles, and the large push-to-talk button. Talking + /// last exchange as subtitles, and the large record toggle. Talking /// produces a transcript, which is sent to the backend as plain text JSON /// exactly like a typed message. fn companion_screen(&mut self, ui: &mut egui::Ui) { @@ -1217,17 +1251,18 @@ impl JarviseApp { ui.add_space((available.y * 0.06).max(10.0)); - // Push-to-talk: hold to record, release to transcribe + send. - let mic = mic_button(ui, self.recorder.is_some(), self.voice.stt_ready()); - let down = mic.is_pointer_button_down_on(); + // Record toggle: press once to speak, press again to transcribe + send. + let recording = self.recorder.is_some(); + let mic = mic_button(ui, recording, self.voice.stt_ready()); + if mic.clicked() { + self.toggle_recording(); + } if self.voice.stt_ready() { - if down && self.recorder.is_none() { - self.start_recording(); - } - if !down && self.recorder.is_some() { - self.stop_recording(); - } - mic.on_hover_text("Maintenir pour parler — relâche pour transcrire et envoyer"); + mic.on_hover_text(if recording { + format!("Arrêter et envoyer ({MIC_HOTKEY_LABEL})") + } else { + format!("Appuyer pour parler à Jarvis ({MIC_HOTKEY_LABEL})") + }); } else { mic.on_hover_text("STT non configuré : définis WHISPER_MODEL"); } @@ -1438,6 +1473,7 @@ impl JarviseApp { impl eframe::App for JarviseApp { fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { self.drain_events(ui.ctx()); + self.handle_mic_hotkey(ui); // A finished transcription is placed in the input then sent as-is. if let Some(text) = self.pending_transcript.take() { @@ -1544,8 +1580,15 @@ fn subtitle(ui: &mut egui::Ui, who: &str, text: &str, width: f32, from_jarvis: b }); } -/// A large circular push-to-talk button, painted by hand so it reads as a real -/// button. Returns the response the caller senses pointer state on. +/// Consumes the microphone shortcut from the current frame's input and reports +/// whether it fired. Consuming it keeps the `Ctrl+Espace` press away from the +/// compose box, and makes a single press toggle the capture only once. +fn take_mic_hotkey(input: &mut egui::InputState) -> bool { + input.consume_key(MIC_HOTKEY_MODIFIERS, MIC_HOTKEY) +} + +/// A large circular record toggle, painted by hand so it reads as a real +/// button. Returns the response the caller reacts to (click toggles capture). fn mic_button(ui: &mut egui::Ui, active: bool, enabled: bool) -> egui::Response { let diameter = (ui.available_width() * 0.20).clamp(80.0, 124.0); let sense = if enabled { @@ -1772,6 +1815,49 @@ mod tests { output.drop_without_applying_deltas(); } + /// The mic button is a toggle, so the shortcut that mirrors it must be a + /// single press that is swallowed by the app rather than typed as a space. + #[test] + fn mic_hotkey_is_a_swallowed_keypress() { + let ctx = egui::Context::default(); + ctx.set_fonts(egui::FontDefinitions::default()); + + let key_press = |key: egui::Key, modifiers: egui::Modifiers| egui::Event::Key { + key, + physical_key: Some(key), + pressed: true, + repeat: false, + modifiers, + }; + + let fired = |events: Vec| { + let input = egui::RawInput { + screen_rect: Some(egui::Rect::from_min_size( + egui::Pos2::ZERO, + egui::vec2(900.0, 700.0), + )), + events, + ..Default::default() + }; + let (mut first, mut second) = (false, false); + let output = ctx.run_ui(input, |ui| { + first = ui.input_mut(take_mic_hotkey); + // One press must not toggle twice in the same frame. + second = ui.input_mut(take_mic_hotkey); + }); + output.drop_without_applying_deltas(); + (first, second) + }; + + // A bare space stays ordinary text for the compose box. + let (plain, _) = fired(vec![key_press(egui::Key::Space, egui::Modifiers::NONE)]); + assert!(!plain, "Espace seul doit rester du texte"); + + let (toggle, repeated) = fired(vec![key_press(MIC_HOTKEY, MIC_HOTKEY_MODIFIERS)]); + assert!(toggle, "{MIC_HOTKEY_LABEL} doit basculer le micro"); + assert!(!repeated, "un appui ne doit basculer le micro qu'une fois"); + } + #[test] fn modes_have_distinct_tabs() { assert_ne!(Mode::Chat.label(), Mode::Companion.label()); diff --git a/frontend/src/tamagotchi.rs b/frontend/src/tamagotchi.rs index eb5872d..44e0e1b 100644 --- a/frontend/src/tamagotchi.rs +++ b/frontend/src/tamagotchi.rs @@ -20,7 +20,7 @@ pub const DEFAULT_SIZE: f32 = 40.0; pub enum Mood { /// Nothing special: gentle bob and occasional blink. Idle, - /// The microphone is recording (push-to-talk held). + /// The microphone is recording (the record toggle is on). Listening, /// Jarvis is generating an answer. Thinking,