Skip to content

Instantly share code, notes, and snippets.

@moodymudskipper
Last active August 8, 2024 09:58
Show Gist options
  • Save moodymudskipper/d04a94a8c128162d067d037f85578ec6 to your computer and use it in GitHub Desktop.
Save moodymudskipper/d04a94a8c128162d067d037f85578ec6 to your computer and use it in GitHub Desktop.
speech to R
## installation (for MacOS, not sure for other systems)
brew install portaudio
pip install SpeechRecognition
pip install pyAudio
copy those scripts to your package, adapt line 60 to name your package
call capture_text()
speak, if you say "stop listening" or wait 5 sec without saying anything, the text is returned
## inst/speech_to_r.py ############################################################################################
import speech_recognition as sr
import subprocess
import time
recognizer = sr.Recognizer()
def listen_and_recognize():
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
try:
audio = recognizer.listen(source) #, timeout = 5, phrase_time_limit = 5)
command = recognizer.recognize_google(audio, language="en-US")
#print(f"Recognized: {command}")
return command
except sr.UnknownValueError:
#print("Could not understand audio")
return None
except sr.RequestError as e:
print(f"Could not request results; {e}")
return None
except sr.WaitTimeoutError:
return "stop listening"
inactivity_timeout = 5
out = ""
while True:
captured_text = listen_and_recognize()
if captured_text:
last_activity_time = time.time()
if captured_text.lower() == "stop listening":
#print("Stopping listening.")
break
else:
print(captured_text)
out = out + "\n" + captured_text
if time.time() - last_activity_time > inactivity_timeout:
#print("Stopping listening due to inactivity timeout.")
break
out
## R/capture_text ############################################################################################
capture_text <- function() {
out <- NULL # for check notes, will be defined by python script
file <- system.file("speech_to_r.py", package = "yourpackage")
writeLines(cli::col_red("Listening..."))
reticulate::source_python(file)
out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment