-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeak_gtts.py
64 lines (53 loc) · 1.4 KB
/
speak_gtts.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
59
60
61
62
63
import threading
from gtts import gTTS
from io import BytesIO
import pygame
import time
import queue
line_queue = queue.Queue()
audio_queue = queue.Queue()
line_q_idle = True
audio_q_idle = True
def loop_audio():
global audio_q_idle
while True:
while audio_queue.empty():
if line_queue.empty():
audio_q_idle = True
time.sleep(0.001)
audio_q_idle = False
clip = audio_queue.get()
clip.seek(0)
pygame.mixer.music.load(clip)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
def loop_speak():
global line_q_idle
global audio_q_idle
while True:
while line_queue.empty():
line_q_idle = True
time.sleep(0.001)
line_q_idle = False
audio_q_idle = False
line = line_queue.get()
mp3_fp = BytesIO()
tts = gTTS(line, lang='en')
tts.write_to_fp(mp3_fp)
audio_queue.put(mp3_fp)
def speak(line):
#print ("$")
global line_q_idle
line_q_idle = False
line_queue.put(line)
def wait():
global line_q_idle
global audio_q_idle
while not line_q_idle or not audio_q_idle:
time.sleep(0.001)
pygame.mixer.init()
thread_speak = threading.Thread(target=loop_speak)
thread_audio = threading.Thread(target=loop_audio)
thread_speak.start()
thread_audio.start()