Add Alt key support for toggling transcription language dynamically
This commit is contained in:
@@ -76,6 +76,9 @@ class SpeechApp(Gtk.Window):
|
||||
self.audio = []
|
||||
self.hotkey_pressed = set()
|
||||
self.hotkey_active = False
|
||||
self.lang_modifier_active = False
|
||||
self.default_language = "en"
|
||||
self.alt_language = "nl"
|
||||
|
||||
# Global hotkey listener for Right-Shift + Right-Ctrl
|
||||
self.listener = keyboard.Listener(
|
||||
@@ -115,7 +118,8 @@ class SpeechApp(Gtk.Window):
|
||||
self.record_thread.join()
|
||||
# Update status bar
|
||||
self.update_status("Transcribing...")
|
||||
threading.Thread(target=self.transcribe_and_type).start()
|
||||
language = self.alt_language if self.lang_modifier_active else self.default_language
|
||||
threading.Thread(target=self.transcribe_and_type, args=(language,)).start()
|
||||
|
||||
def record_audio(self):
|
||||
def callback(indata, frames, time_info, status):
|
||||
@@ -141,7 +145,7 @@ class SpeechApp(Gtk.Window):
|
||||
self.status_bar.set_text(f"Status: {status}")
|
||||
return False # Return False to prevent being called again
|
||||
|
||||
def transcribe_and_type(self):
|
||||
def transcribe_and_type(self, language):
|
||||
if not self.audio:
|
||||
# Update status back to Ready if no audio was recorded
|
||||
GLib.idle_add(self.update_status, "Ready")
|
||||
@@ -157,7 +161,7 @@ class SpeechApp(Gtk.Window):
|
||||
|
||||
# In the transcribe_and_type method, modify the transcribe call:
|
||||
# Replace "en" with your desired language code
|
||||
result = model.transcribe(tmpfile.name, language="en")
|
||||
result = model.transcribe(tmpfile.name, language=language)
|
||||
text = result["text"].strip()
|
||||
|
||||
# Display the text in the text area
|
||||
@@ -208,8 +212,14 @@ class SpeechApp(Gtk.Window):
|
||||
self.hotkey_pressed.add("shift_r")
|
||||
elif key == keyboard.Key.ctrl_r:
|
||||
self.hotkey_pressed.add("ctrl_r")
|
||||
elif key == keyboard.Key.alt_r:
|
||||
self.hotkey_pressed.add("alt_r")
|
||||
|
||||
if not self.hotkey_active and self.hotkey_pressed == {"shift_r", "ctrl_r"}:
|
||||
self.lang_modifier_active = (
|
||||
"alt_r" in self.hotkey_pressed
|
||||
)
|
||||
|
||||
if not self.hotkey_active and (self.hotkey_pressed == {"shift_r", "ctrl_r"} or self.hotkey_pressed == {"shift_r", "alt_r"}):
|
||||
self.hotkey_active = True
|
||||
GLib.idle_add(self.start_recording)
|
||||
|
||||
@@ -218,11 +228,23 @@ class SpeechApp(Gtk.Window):
|
||||
self.hotkey_pressed.discard("shift_r")
|
||||
elif key == keyboard.Key.ctrl_r:
|
||||
self.hotkey_pressed.discard("ctrl_r")
|
||||
elif key == keyboard.Key.alt_r:
|
||||
self.hotkey_pressed.discard("alt_r")
|
||||
|
||||
if self.hotkey_active and self.hotkey_pressed != {"shift_r", "ctrl_r"}:
|
||||
self.lang_modifier_active = (
|
||||
"alt_r" in self.hotkey_pressed
|
||||
)
|
||||
|
||||
if self.hotkey_active and (self.hotkey_pressed != {"shift_r", "ctrl_r"} and self.hotkey_pressed != {"shift_r", "alt_r"}):
|
||||
self.hotkey_active = False
|
||||
GLib.idle_add(self.stop_recording)
|
||||
|
||||
def is_shift_key(self, key):
|
||||
return key in (keyboard.Key.shift, keyboard.Key.shift_l, keyboard.Key.shift_r)
|
||||
|
||||
def is_alt_key(self, key):
|
||||
return key in (keyboard.Key.alt, keyboard.Key.alt_l, keyboard.Key.alt_r, keyboard.Key.alt_gr)
|
||||
|
||||
def on_destroy(self, widget):
|
||||
try:
|
||||
self.listener.stop()
|
||||
|
||||
Reference in New Issue
Block a user