Skip to content

Instantly share code, notes, and snippets.

@baztian
Created February 19, 2024 13:04
Show Gist options
  • Save baztian/edf5d1256d59fdf523be4e873c0f5299 to your computer and use it in GitHub Desktop.
Save baztian/edf5d1256d59fdf523be4e873c0f5299 to your computer and use it in GitHub Desktop.
Speech recognition and insert at cursor position
#!/usr/bin/env python3
import subprocess
import sys
import speech_recognition as sr
def get_audio_text(language=None):
language = language or "en-US"
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Say something:")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio, language=language)
return text
except sr.UnknownValueError:
print("Could not understand audio.")
except sr.RequestError as e:
print(f"Error with the speech recognition service; {e}")
return None
def insert_text_at_cursor(text):
subprocess.run(["xdotool", "type", "--clearmodifiers", text])
if __name__ == "__main__":
# If a language code is provided, use it; otherwise, use the default
# e.g. de-DE
if len(sys.argv) > 1:
language = sys.argv[1]
else:
language = None
spoken_text = get_audio_text(language=language)
if spoken_text:
insert_text_at_cursor(spoken_text)
@baztian
Copy link
Author

baztian commented Feb 19, 2024

ChatGPT prompt:

How can I do voice typing in Linux we'll.like speech recognition. Is there a way to integrate this like a keyboard so for example that I have a button on my screen and it just inserts the spoken text at the point of the cursor

Preconditions

pip install SpeechRecognition
sudo apt install portaudio19-dev

Execute for en-US by just executing. Provide other locales such as de-DE as first parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment