Created
April 18, 2022 22:19
-
-
Save dat-boris/b8a36d22c1bc0827f28493dc3ba6b0e1 to your computer and use it in GitHub Desktop.
Script for making sure I read out loud while I am typing text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Ensure that you write out loud when you write your text. | |
brew install portaudio | |
pip installl pynput speechrecognition pyaudio pyttsx3 | |
./out_loud.py | |
WARNING: you might need to hack your security setting following: | |
https://stackoverflow.com/questions/53088995/pynput-keyboard-listener-does-not-detect-keys-on-mac-os-x | |
""" | |
import time | |
from pynput.keyboard import Listener, KeyCode, Key | |
import speech_recognition as sr | |
import pyttsx3 | |
BACKOFF_SECONDS = 10 | |
# create a speech recognition object | |
r = sr.Recognizer() | |
buffer = "" | |
def resemble_writing(duration=5, min_words=2): | |
global buffer | |
buffer = "" | |
def on_press(key): | |
global buffer | |
if isinstance(key, Key): | |
if key == Key.space: | |
buffer += ' ' | |
elif isinstance(key, KeyCode): | |
buffer += key.char | |
else: | |
print(f"Unknown key: {key}") | |
listener = Listener(on_press=on_press) | |
listener.start() | |
listener.wait() | |
time.sleep(duration) | |
listener.stop() | |
print(f"Got typing: {buffer}") | |
return len(buffer.split()) >= min_words | |
def record_speech(duration=5): | |
text = None | |
with sr.Microphone() as source: | |
# read the audio data from the default microphone | |
audio_data = r.record(source, duration=duration) | |
try: | |
print("Recognizing...") | |
# convert speech to text | |
text = r.recognize_google(audio_data) | |
print(f"Recognized speech: {text}") | |
except sr.UnknownValueError: | |
print("No speech seen!") | |
text = None | |
return text | |
if __name__ == "__main__": | |
while True: | |
if resemble_writing(duration=5): | |
speaking = record_speech(duration=5) | |
if not speaking: | |
pyttsx3.speak("outloud.") | |
print( | |
f"Screamed at ya! Backing off for {BACKOFF_SECONDS} secs") | |
time.sleep(BACKOFF_SECONDS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment