-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts.py
50 lines (36 loc) · 1.12 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
import wave
from decouple import config
from pyaudio import PyAudio as pyaudio
import google.cloud.texttospeech as tts
def speak_file(path):
audio_file = wave.open(path, "rb")
audio = pyaudio()
stream = audio.open(
format = audio.get_format_from_width(audio_file.getsampwidth()),
channels = audio_file.getnchannels(),
rate = audio_file.getframerate(),
output = True
)
data = audio_file.readframes(1024)
while data:
stream.write(data)
data = audio_file.readframes(1024)
stream.stop_stream()
stream.close()
audio.terminate()
def generate_speech(text, path):
text_input = tts.SynthesisInput(text=text)
voice_params = tts.VoiceSelectionParams(
language_code='en-GB', name="en-GB-Neural2-B"
)
audio_config = tts.AudioConfig(audio_encoding=tts.AudioEncoding.LINEAR16)
client = tts.TextToSpeechClient(
client_options={"api_key": config('GOOGLE_API_KEY'), "quota_project_id": "snidegpt"}
)
response = client.synthesize_speech(
input=text_input,
voice=voice_params,
audio_config=audio_config,
)
with open(path, "wb") as audio_file:
audio_file.write(response.audio_content)