for some reason my function "transcription" executes the API before playing a sound that is supposed to notify that one is asked to speak into the microphone.
I don't understand why all of a sudden the general linear execution order is broken.
Any suggestions?
r = sr.Recognizer()
### instantiate pycharm
p = pyaudio.PyAudio()
acknowledge_wav = ["asyouwish.wav", "openingdesired.wav", "rightaway.wav"]
keywords = {"firefox": "C:/Program Files (x86)/Mozilla Firefox/firefox.exe",
"discord": "C:/Users/LucasGames/AppData/Local/Discord/Update.exe --processStart Discord.exe",
"pycharm": "C:/Program Files/JetBrains/PyCharm Community Edition 2022.1/bin/pycharm64.exe",
"chrome": "C:/Program Files/Google/Chrome/Application/chrome.exe",
"wars": "C:/Spiele/steamapps/common/Jedi Outcast/GameData/JK2MV/jk2mvmp.exe"
}
with sr.Microphone(sample_rate=20000) as mic:
r.adjust_for_ambient_noise(mic, duration=0.5)
audio = r.listen(mic)
### play some sound
def play_sound(file):
wf = wave.open(file)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
wav_data = wf.readframes(1024)
while len(wav_data) > 0:
stream.write(wav_data)
wav_data = wf.readframes(1024)
stream.stop_stream()
stream.close()
p.terminate()
### activation and transcription of recognizer
def transcription(audio_data):
## speak now indicator
play_sound("notificationtest.wav")
## call google API
try:
command = r.recognize_google(audio_data, language="en-USA").lower()
except sr.RequestError:
print("I am experiencing brain fog - please try again.")
except sr.UnknownValueError:
print("I could not understand you. Please repeat your command.")
# splits the command sentence in single words to iterate over
splt_command = command.split()
## check if keyword is in the spoken command
for word in splt_command:
if word in keywords:
print(f"Command: {command}")
print(f"Keyword: {word}")
assistant_response = random.choice(acknowledge_wav)
play_sound(assistant_response)
subprocess.call(keywords[word])
# no keyword in command
print("No keyword detected.")
print(f"Said: {command}")
transcription(audio)