-
Notifications
You must be signed in to change notification settings - Fork 1
/
speak.py
58 lines (46 loc) · 1.69 KB
/
speak.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import subprocess
from pathlib import Path
from openai import OpenAI
from pydub import AudioSegment
import rospy
from speakers_controller.srv import playSound, playSoundRequest
from secret import OPENAI_API_KEY
client = OpenAI(api_key=OPENAI_API_KEY, base_url="https://openai.batalov.me/v1")
print('wait for playSound')
rospy.wait_for_service('playSound')
service_playSound = rospy.ServiceProxy('playSound', playSound, persistent=True)
def speak_ai(text: str, sync: bool = True):
print('Speak AI: ', text)
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text,
response_format="mp3",
speed="0.8"
)
response.stream_to_file(speech_file_path)
audio = AudioSegment.from_file("/home/pi/morsai/speech.mp3", format="mp3")
louder_audio = audio + 20
louder_audio.export("/home/pi/morsai/speech_louder.mp3", format="mp3")
print('Sound generated, play')
request = playSoundRequest()
request.FileName = "/home/pi/morsai/speech_louder.mp3"
request.IsBreakable = sync
service_playSound(request)
def speak(text: str):
rate = 100
pitch = 30
voice = 'ru'
# subprocess.call(['espeak', '-v', voice, text])
subprocess.call(['espeak', '-s', str(rate), '-p', str(pitch), '-v', voice, text])
def speak_start_thinking():
request = playSoundRequest()
request.FileName = "/home/pi/morsai/wait.mp3"
request.IsBreakable = 0
service_playSound(request)
if __name__ == '__main__':
rospy.init_node("speaker")
speak_ai('Раз, два, три, четыре, пять, я хочу выйти погулять')
print('done')
# speak('Гаф гаф!')