-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtts.py
58 lines (45 loc) · 1.77 KB
/
tts.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
import io
from google.cloud import texttospeech
import pyaudio
class TTS:
def __init__(self, config):
self.path_to_credentials = config["credentials"]
self.client = texttospeech.TextToSpeechClient()
self.tts_done_callback = None
def speak(self, text):
synthesis_input = texttospeech.SynthesisInput(text=text)
# Build the voice request
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", name="en-US-Neural2-D", ssml_gender=texttospeech.SsmlVoiceGender.MALE
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.LINEAR16
)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = self.client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# Open a stream in memory to play the audio
stream = io.BytesIO(response.audio_content)
# Create an instance of the PyAudio class
p = pyaudio.PyAudio()
# Open a stream for playing the audio
stream_player = p.open(
format=pyaudio.paInt16,
channels=1,
rate=24000,
output=True
)
# Play the audio stream
data = stream.read(1024)
while data:
stream_player.write(data)
data = stream.read(1024)
# Stop and close the audio stream player and PyAudio instance
stream_player.stop_stream()
stream_player.close()
p.terminate()
if self.tts_done_callback is not None:
self.tts_done_callback()