forked from advaitpaliwal/insight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_speech.py
49 lines (38 loc) · 1.27 KB
/
text_to_speech.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
from google.cloud import texttospeech
import os
import pygame
from time import sleep
import tempfile
def speak(text: str):
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text=text)
voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
ssml_gender=texttospeech.SsmlVoiceGender.MALE
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=synthesis_input,
voice=voice,
audio_config=audio_config
)
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
temp_file.write(response.audio_content)
temp_file_path = temp_file.name
print(f'Audio content written to temporary file: {temp_file_path}')
# Initialize pygame mixer
pygame.mixer.init()
# Load the temporary audio file
pygame.mixer.music.load(temp_file_path)
# Play the audio file
pygame.mixer.music.play()
# Wait for the music to play completely
while pygame.mixer.music.get_busy():
sleep(0.5)
# Remove the temporary file
os.remove(temp_file_path)
if __name__ == "__main__":
speak("Hello, how are you?")