-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_speech.py
38 lines (33 loc) · 1.24 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
import torch
from kokoro import KModel, KPipeline
import sounddevice as sd
import numpy as np
class TTSGenerator:
def __init__(self, default_voice='af_heart'):
print("Initializing TTS...")
self.model = KModel().to('cpu').eval()
self.pipeline = KPipeline(lang_code='a', model=False)
self.voice_pack = self.pipeline.load_voice(default_voice)
print("TTS ready!")
def generate_speech(self, text):
try:
for _, ps, _ in self.pipeline(text, 'af_heart', 1):
ref_s = self.voice_pack[len(ps)-1]
audio = self.model(ps, ref_s, 1)
audio_data = audio.numpy().astype(np.float32)
sd.play(audio_data, samplerate=24000, blocking=True)
sd.wait() # Wait until audio is finished playing
except KeyboardInterrupt:
sd.stop()
def cleanup(self):
pass # No cleanup needed for sounddevice
def create_tts_generator():
"""Create and return a TTSGenerator instance."""
return TTSGenerator()
if __name__ == "__main__":
# Example usage
tts = TTSGenerator()
try:
tts.generate_speech("Hello, this is a test of the text to speech system.")
finally:
tts.cleanup()