feat: add voice cloning and speaking rate to GoogleTTSService

This commit is contained in:
Manish Kumar
2025-08-30 22:59:10 +05:30
parent c1b8d2acab
commit 2ee481d541

View File

@@ -500,9 +500,11 @@ class GoogleTTSService(TTSService):
Parameters: Parameters:
language: Language for synthesis. Defaults to English. language: Language for synthesis. Defaults to English.
speaking_rate: The speaking rate, in the range [0.25, 4.0].
""" """
language: Optional[Language] = Language.EN language: Optional[Language] = Language.EN
speaking_rate: Optional[float] = None
def __init__( def __init__(
self, self,
@@ -510,6 +512,7 @@ class GoogleTTSService(TTSService):
credentials: Optional[str] = None, credentials: Optional[str] = None,
credentials_path: Optional[str] = None, credentials_path: Optional[str] = None,
voice_id: str = "en-US-Chirp3-HD-Charon", voice_id: str = "en-US-Chirp3-HD-Charon",
voice_cloning_key: Optional[str] = None,
sample_rate: Optional[int] = None, sample_rate: Optional[int] = None,
params: InputParams = InputParams(), params: InputParams = InputParams(),
**kwargs, **kwargs,
@@ -532,8 +535,10 @@ class GoogleTTSService(TTSService):
"language": self.language_to_service_language(params.language) "language": self.language_to_service_language(params.language)
if params.language if params.language
else "en-US", else "en-US",
"speaking_rate": params.speaking_rate,
} }
self.set_voice(voice_id) self.set_voice(voice_id)
self._voice_cloning_key = voice_cloning_key
self._client: texttospeech_v1.TextToSpeechAsyncClient = self._create_client( self._client: texttospeech_v1.TextToSpeechAsyncClient = self._create_client(
credentials, credentials_path credentials, credentials_path
) )
@@ -600,15 +605,24 @@ class GoogleTTSService(TTSService):
try: try:
await self.start_ttfb_metrics() await self.start_ttfb_metrics()
voice = texttospeech_v1.VoiceSelectionParams( if self._voice_cloning_key:
language_code=self._settings["language"], name=self._voice_id voice_clone_params = texttospeech_v1.VoiceCloneParams(
) voice_cloning_key=self._voice_cloning_key
)
voice = texttospeech_v1.VoiceSelectionParams(
language_code=self._settings["language"], voice_clone=voice_clone_params
)
else:
voice = texttospeech_v1.VoiceSelectionParams(
language_code=self._settings["language"], name=self._voice_id
)
streaming_config = texttospeech_v1.StreamingSynthesizeConfig( streaming_config = texttospeech_v1.StreamingSynthesizeConfig(
voice=voice, voice=voice,
streaming_audio_config=texttospeech_v1.StreamingAudioConfig( streaming_audio_config=texttospeech_v1.StreamingAudioConfig(
audio_encoding=texttospeech_v1.AudioEncoding.PCM, audio_encoding=texttospeech_v1.AudioEncoding.PCM,
sample_rate_hertz=self.sample_rate, sample_rate_hertz=self.sample_rate,
speaking_rate=self._settings["speaking_rate"],
), ),
) )
config_request = texttospeech_v1.StreamingSynthesizeRequest( config_request = texttospeech_v1.StreamingSynthesizeRequest(