-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_speech.py
More file actions
114 lines (96 loc) · 3.82 KB
/
Copy pathtext_to_speech.py
File metadata and controls
114 lines (96 loc) · 3.82 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from google.cloud import texttospeech
from typing import Optional
import os
from dotenv import load_dotenv
load_dotenv()
class TextToSpeech:
def __init__(self):
"""Initialize Google Cloud Text-to-Speech client"""
try:
self.client = texttospeech.TextToSpeechClient()
self.voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
name="en-US-Standard-I",
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE
)
self.audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
speaking_rate=0.9, # Slightly slower for clarity
pitch=0.0
)
except Exception as e:
print(f"Warning: Could not initialize TTS client: {e}")
self.client = None
async def synthesize(self, text: str) -> Optional[bytes]:
"""Convert text to speech"""
try:
if not self.client:
return self.mock_synthesize()
synthesis_input = texttospeech.SynthesisInput(text=text)
response = self.client.synthesize_speech(
input=synthesis_input,
voice=self.voice,
audio_config=self.audio_config
)
return response.audio_content
except Exception as e:
print(f"Error in text-to-speech synthesis: {e}")
return self.mock_synthesize()
async def synthesize_ssml(self, ssml: str) -> Optional[bytes]:
"""Convert SSML to speech for more natural pronunciation"""
try:
if not self.client:
return self.mock_synthesize()
synthesis_input = texttospeech.SynthesisInput(ssml=ssml)
response = self.client.synthesize_speech(
input=synthesis_input,
voice=self.voice,
audio_config=self.audio_config
)
return response.audio_content
except Exception as e:
print(f"Error in SSML synthesis: {e}")
return self.mock_synthesize()
def mock_synthesize(self) -> bytes:
"""Return mock audio data for testing"""
# Return a minimal valid MP3 file
return b'ID3\x03\x00\x00\x00\x00\x00\x00'
def customize_voice(self,
language_code: str = "en-US",
voice_name: str = "en-US-Standard-I",
gender: str = "FEMALE",
speaking_rate: float = 0.9,
pitch: float = 0.0) -> None:
"""Customize the voice parameters"""
gender_map = {
"FEMALE": texttospeech.SsmlVoiceGender.FEMALE,
"MALE": texttospeech.SsmlVoiceGender.MALE,
"NEUTRAL": texttospeech.SsmlVoiceGender.NEUTRAL
}
self.voice = texttospeech.VoiceSelectionParams(
language_code=language_code,
name=voice_name,
ssml_gender=gender_map.get(gender, texttospeech.SsmlVoiceGender.FEMALE)
)
self.audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
speaking_rate=speaking_rate,
pitch=pitch
)
def get_available_voices(self) -> list:
"""Get list of available voices"""
try:
if not self.client:
return []
voices = self.client.list_voices().voices
return [
{
"name": voice.name,
"language_codes": voice.language_codes,
"gender": texttospeech.SsmlVoiceGender(voice.ssml_gender).name
}
for voice in voices
]
except Exception as e:
print(f"Error getting available voices: {e}")
return []