Open-source State-of-the-art TTS model which runs on a CPU 😻
Python
added streaming and removed misaki
Rohan Joshiac47d9610e78
diff --git a/.gitignore b/.gitignoreindex 15631bd..c81f9f1 100644--- a/.gitignore+++ b/.gitignore@@ -25,6 +25,10 @@ Network Trash FolderTemporary Items.apdisk+.claude/+.venv*++*.wav# Byte-compiled / optimized / DLL files__pycache__/diff --git a/example_streaming.py b/example_streaming.pynew file mode 100644index 0000000..bc41518--- /dev/null+++ b/example_streaming.py@@ -0,0 +1,39 @@+import numpy as np+import soundfile as sf+from kittentts import KittenTTS++SAMPLE_RATE = 24000++# Step 1: Load the model+m = KittenTTS("KittenML/kitten-tts-mini-0.8") # 80M version (highest quality)+# m = KittenTTS("KittenML/kitten-tts-mini-0.8", backend="cuda") # GPU version+# m = KittenTTS("KittenML/kitten-tts-micro-0.8") # 40M version+# m = KittenTTS("KittenML/kitten-tts-nano-0.8") # 15M version (tiny and faster)++# Step 2: Define text and voice+text = """One day, a little girl named Lily found a needle in her room. She knew it was difficult to play with it because it was sharp. Lily wanted to use the needle to sew a button on her shirt. She asked her mom for help."""++voice = "Bruno"++# Optional: try to import sounddevice for real-time playback+try:+ import sounddevice as sd+ has_audio = True+except (ImportError, OSError):+ has_audio = False++# Step 3: Stream audio chunk by chunk+print("Streaming audio...")+chunks = []+for i, chunk in enumerate(m.generate_stream(text=text, voice=voice)):+ audio = chunk.squeeze()+ chunks.append(audio)+ print(f" Chunk {i + 1}: {len(audio)} samples ({len(audio) / SAMPLE_RATE:.2f}s)")+ if has_audio:+ sd.play(audio, samplerate=SAMPLE_RATE)+ sd.wait()++# Save the full audio+full_audio = np.concatenate(chunks)+sf.write("output_streaming.wav", full_audio, SAMPLE_RATE)+print(f"Audio saved to output_streaming.wav ({len(full_audio) / SAMPLE_RATE:.2f}s total)")diff --git a/kittentts/get_model.py b/kittentts/get_model.pyindex 12d2ef3..8af5561 100644--- a/kittentts/get_model.py+++ b/kittentts/get_model.py@@ -36,7 +36,15 @@ class KittenTTS:"""print(f"Generating audio for text: {text}")return self.model.generate(text, voice=voice, speed=speed, clean_text=clean_text)-++ def generate_stream(self, text, voice="expr-voice-5-m", speed=1.0, clean_text=False):+ """Generate audio as a stream of chunks.++ Yields:+ numpy.ndarray: Audio data for each text chunk.+ """+ yield from self.model.generate_stream(text, voice=voice, speed=speed, clean_text=clean_text)+def generate_to_file(self, text, output_path, voice="expr-voice-5-m", speed=1.0, sample_rate=24000):"""Generate audio from text and save to file.diff --git a/kittentts/onnx_model.py b/kittentts/onnx_model.pyindex 56d2027..45ca6cc 100644--- a/kittentts/onnx_model.py+++ b/kittentts/onnx_model.py@@ -1,4 +1,3 @@-from misaki import en, espeakimport numpy as npimport phonemizerimport soundfile as sf@@ -159,6 +158,17 @@ class KittenTTS_1_Onnx:out_chunks.append(self.generate_single_chunk(text_chunk, voice, speed))return np.concatenate(out_chunks, axis=-1)+ def generate_stream(self, text: str, voice: str = "expr-voice-5-m", speed: float = 1.0, clean_text: bool = True):+ """Generate audio chunk-by-chunk as a generator.++ Yields:+ numpy.ndarray: Audio data for each text chunk.+ """+ if clean_text:+ text = self.preprocessor(text)+ for text_chunk in chunk_text(text):+ yield self.generate_single_chunk(text_chunk, voice, speed)+def generate_single_chunk(self, text: str, voice: str = "expr-voice-5-m", speed: float = 1.0) -> np.ndarray:"""Synthesize speech from text.diff --git a/output.wav b/output.wavdeleted file mode 100644index 326dcd0..0000000Binary files a/output.wav and /dev/null differdiff --git a/requirements.txt b/requirements.txtindex 37bfbb3..b8b0b6a 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,7 +1,6 @@num2wordsspacyespeakng_loader-misaki[en]>=0.9.4onnxruntimesoundfilenumpydiff --git a/requirements_gpu.txt b/requirements_gpu.txtindex 9e1a7b0..7012ba7 100644--- a/requirements_gpu.txt+++ b/requirements_gpu.txt@@ -1,7 +1,6 @@num2wordsspacyespeakng_loader-misaki[en]>=0.9.4onnxruntime-gpusoundfilenumpy