-
Notifications
You must be signed in to change notification settings - Fork 1
/
playback_module.py
39 lines (28 loc) · 946 Bytes
/
playback_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from openai import OpenAI
import pygame
from pathlib import Path
from dotenv import load_dotenv
import os
import subprocess
load_dotenv()
def text_to_speech_and_play(text):
openai = OpenAI(api_key=os.getenv('OPENAI.API_KEY'))
speech_file_path = Path(__file__).parent / "speech.mp3"
response = openai.audio.speech.create(
model="tts-1",
voice="onyx",
input=text
)
response.stream_to_file(speech_file_path)
pygame.mixer.init()
pygame.mixer.music.load(str(speech_file_path))
pygame.mixer.music.play()
# Start the motor script as a subprocess
motor_process = subprocess.Popen(['python', 'motormouth.py'])
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
# Terminate the motor script after audio ends
motor_process.terminate()
if __name__ == "__main__":
test_text = "I am doing well, thank you!"
text_to_speech_and_play(test_text)