Skip to content

Commit

Permalink
Further clean up main logic
Browse files Browse the repository at this point in the history
  • Loading branch information
petergtz committed Nov 26, 2023
1 parent e38d1cd commit a022786
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions examples/Python3/wakeword.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,52 +60,52 @@


def detected_callback():
# detector.terminate()
print("Wake word detected. ", end="", flush=True)
send_to_arduino(code_from("[Curiosity]"))
detector.terminate()

while True:
detector.terminate()
with no_alsa_error():
audio = pyaudio.PyAudio()
record_audio(audio, out_filename="recording.wav")
print("Sending I to Arduino.")
usb.write(b"\0I") # I for thinking

text = transcribe(in_filename="recording.wav")
messages.append({"role": "user", "content": text})
text = submit_to_chatgpt(messages)
messages.append({"role": "assistant", "content": text})

emotions = re.findall(r"\[.*?\]", text)
sentences = re.split(r"\[.*?\]", text)

print("Sending T to Arduino.")
usb.write(b"\0T") # T for talk
for i in range(0, len(emotions)):
text = (
sentences[i]
.replace(" .", ".")
.replace(" ,", ",")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" :", ":")
.replace(" ;", ";")
.replace(" ", " ")
)
text = text.lstrip(".,!?:;")
text_to_speech(text, out_filename="response.mp3")

print("Sending to Arduino:", code_from(emotions[i]))
usb.write(code_from(emotions[i]))
# with no_alsa_error():

send_to_arduino(b"\0I") # I for thinking

sentence = transcribe(in_filename="recording.wav")
messages.append({"role": "user", "content": sentence})
sentence = submit_to_chatgpt(messages)
messages.append({"role": "assistant", "content": sentence})

send_to_arduino(b"\0T") # T for talk
sentences, emotions = sentences_and_emotions_from(sentence)
for sentence, emotion in zip(sentences, emotions):
text_to_speech(sentence, out_filename="response.mp3")
send_to_arduino(code_from(emotion))
play(increased_pitch(AudioSegment.from_mp3("response.mp3")))

# playingObj = sa.WaveObject.from_wave_file("response_robotic.wav").play()
# playingObj.wait_done()
print("Sending S to Arduino.")
send_to_arduino(b"\0S") # S for silence


def sentences_and_emotions_from(text):
emotions = re.findall(r"\[.*?\]", text)
sentences = re.split(r"\[.*?\]", text)
sentences_cleaned = []
for sentence, emotion in zip(sentences, emotions):
sentences_cleaned.append(
sentence.replace(" .", ".")
.replace(" ,", ",")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" :", ":")
.replace(" ;", ";")
.replace(" ", " ")
.lstrip(".,!?:;")
)
return sentences_cleaned, emotions

def send_to_arduino(code):
print("Sending to Arduino:", code)
usb.write(code)

usb.write(b"\0S") # S for silence


def record_audio(audio: pyaudio.PyAudio, out_filename: str):
print("Recording...", end="", flush=True)
Expand Down Expand Up @@ -187,7 +187,7 @@ def increased_pitch(audio_segment):


def text_to_speech(text, out_filename):
print("Transcribing with Polly...", end="")
print("Synthesizing speech with Polly...", end="")

start_time = datetime.now()
response = polly.synthesize_speech(
Expand Down

0 comments on commit a022786

Please sign in to comment.