File size: 945 Bytes
27c3220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time

import google.cloud.texttospeech as tts
import simpleaudio as sa

class TextToSpeech:
    def __init__(self):
        self.voice_params = tts.VoiceSelectionParams(
            language_code="id-ID", name="id-ID-Standard-A"
        )
        self.audio_config = tts.AudioConfig(audio_encoding=tts.AudioEncoding.LINEAR16, speaking_rate=1.25)
        self.client = tts.TextToSpeechClient()

    def text_to_speech(self, text: str):
        
        start = time.time()
        text_input = tts.SynthesisInput(text=text)
        response = self.client.synthesize_speech(
            input=text_input,
            voice=self.voice_params,
            audio_config=self.audio_config,
        )
        end = time.time()
        print(f"Time taken to synthesize speech: {end-start:.2f}s")
        
        play_obj = sa.play_buffer(response.audio_content, num_channels=1, bytes_per_sample=2, sample_rate=24000)
        play_obj.wait_done()