-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts.py
More file actions
70 lines (53 loc) · 2.03 KB
/
tts.py
File metadata and controls
70 lines (53 loc) · 2.03 KB
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
64
65
66
67
68
69
from google.cloud import texttospeech
import vlc
import asyncio
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "llamachoptest-90319ad5895d.json"
class TTS:
def __init__(self):
self.client = texttospeech.TextToSpeechClient()
self.media = vlc.MediaPlayer()
self.queue = asyncio.Queue()
self.processing = False
print("TTS initialized")
async def add_text(self, text):
await self.queue.put(text)
print(f"Added to queue: {text}")
print(self.queue)
if not self.processing:
print("Processing queue")
self.processing = True
asyncio.create_task(self.process_queue())
async def process_queue(self):
print("que not while")
while not self.queue.empty():
print("que while")
text = await self.queue.get()
print(f"Processing: {text}")
synthesis_input = texttospeech.SynthesisInput(text=text)
voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
name="en-US-Neural2-F"
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = self.client.synthesize_speech(
input=synthesis_input,
voice=voice,
audio_config=audio_config
)
with open("output.mp3", "wb") as out:
out.write(response.audio_content)
print("Audio content written to file 'output.mp3'")
self.media.stop()
self.media.set_media(vlc.Media("output.mp3"))
self.media.play()
while self.media.get_state() == vlc.State.Playing:
await asyncio.sleep(0.1)
# media_length = self.media.get_length() / 1000
# await asyncio.sleep(media_length)
self.queue.task_done()
print(self.processing)
self.processing = False
print(self.processing)