-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext2speech.py
36 lines (27 loc) · 1.12 KB
/
text2speech.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
import google.cloud.texttospeech as tts
from gtts import gTTS as g_tts
class GoogleCloudTTS:
def __init__(self,model: str):
self.model = model
language_code = "-".join(self.model.split("-")[:2])
self.voice_params = tts.VoiceSelectionParams(language_code=language_code, name=self.model)
self.audio_config = tts.AudioConfig(audio_encoding=tts.AudioEncoding.OGG_OPUS)
def synth(self,message: str):
text_input = tts.SynthesisInput(text=message)
client = tts.TextToSpeechClient()
response = client.synthesize_speech(
input=text_input,
voice=self.voice_params,
audio_config=self.audio_config,
)
filename = "tts.wav"
with open(filename, "wb") as out:
out.write(response.audio_content)
class Gtts:
def __init__(self,language: str,tld: str):
self.language = language
self.tld = tld
def synth(self,message: str):
response = g_tts(text=message,lang=self.language,tld=self.tld)
return response.stream()
sound.save("tts.wav")