This commit is contained in:
retoor 2025-01-18 10:02:13 +01:00
parent 84c83cfd70
commit a292f571cb
4 changed files with 39 additions and 15 deletions

22
play.py
View File

@ -29,12 +29,21 @@ import functools
import os
import subprocess
import sys
import pygame
@functools.cache
def get_py_audio():
return pyaudio.PyAudio()
def play_audio(filename):
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
def play_audio2(filename):
ffmpeg_cmd = [
"ffmpeg",
"-i", filename,
@ -45,9 +54,9 @@ def play_audio(filename):
]
process = subprocess.Popen(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=10**6)
py_audio = get_py_audio()
stream = py_audio.open(
format=py_audio.get_format_from_width(2),
p = pyaudio.PyAudio()
stream = p.open(
format=p.get_format_from_width(2),
channels=2,
rate=44100,
output=True
@ -62,5 +71,6 @@ def play_audio(filename):
finally:
stream.stop_stream()
stream.close()
p.terminate()
process.stdout.close()
process.wait()

View File

@ -3,3 +3,4 @@ SpeechRecognition
google-cloud-speech
google-cloud-texttospeech
google-auth
pygame

27
tts.py
View File

@ -33,23 +33,22 @@ import gcloud
molodetz = ServerProxy("https://api.molodetz.nl/rpc")
async def main():
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Adjusting to surroundings for a five seconds.")
recognizer.adjust_for_ambient_noise(source, duration=5)
#recognizer.non_speaking_duration = 60*60
while True:
print("Listening...")
try:
audio_data = recognizer.listen(source, timeout=10)
text = recognizer.recognize_google(audio_data, language="nl-NL") #en-US
print(f"You said:\n\t{text}")
response_llm = molodetz.gpt4o_mini(text)
print(f"GPT4o mini said:\n\t{response_llm}")
await gcloud.tts(response_llm)
source = None
recognizer = None
return text
except sr.WaitTimeoutError:
continue
except sr.UnknownValueError:
@ -57,6 +56,20 @@ async def main():
except sr.RequestError:
continue
async def main():
#recognizer.adjust_for_ambient_noise(source, duration=5)
while True:
text = listen()
print(f"You said:\n\t{text}")
response_llm = molodetz.gpt4o_mini(text)
print(f"GPT4o mini said:\n\t{response_llm}")
await gcloud.tts(response_llm)
if __name__ == "__main__":
import asyncio
asyncio.run(main())