This repository was archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_speech.py
More file actions
48 lines (36 loc) · 1.52 KB
/
text_to_speech.py
File metadata and controls
48 lines (36 loc) · 1.52 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
import os
from google.cloud import texttospeech
def text_to_speech(text_list, tts_service_account_key_path, voice_name='en-GB-Wavenet-B'):
language_code='en-GB'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = tts_service_account_key_path
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the voice
voice = texttospeech.VoiceSelectionParams(
language_code=language_code,
name=voice_name
)
# Set the audio encoding
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
speaking_rate=1.1,
pitch=0.0
)
# Create a list to store the file paths
file_names = []
print('Generating speech files...')
# Iterate through each text in the list and generate an mp3 file
for i, text in enumerate(text_list):
# Set the input text
synthesis_input = texttospeech.SynthesisInput(text=text)
# Perform the text-to-speech request on the text input with the selected voice parameters and audio file type
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
# Set the file name and path
file_name = f'tts_{i}.mp3'
file_path = os.path.join(os.getcwd(), 'assets', file_name)
# Write the binary content to an mp3 file
with open(file_path, 'wb') as out:
out.write(response.audio_content)
# Add the file path to the list
file_names.append(file_name)
return file_names