Spaces:
Sleeping
Sleeping
Update piper_engine.py
Browse files- piper_engine.py +17 -13
piper_engine.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
# piper_engine.py
|
| 2 |
-
|
| 3 |
import io
|
| 4 |
import numpy as np
|
| 5 |
import soundfile as sf
|
|
@@ -8,29 +7,34 @@ from piper.voice import PiperVoice
|
|
| 8 |
class PiperEngine:
|
| 9 |
|
| 10 |
def __init__(self, model_path):
|
|
|
|
| 11 |
self.voice = PiperVoice.load(model_path)
|
| 12 |
|
| 13 |
def synthesize(
|
| 14 |
self,
|
| 15 |
text,
|
| 16 |
-
speed=1.0,
|
| 17 |
-
pitch=1.0,
|
| 18 |
sample_rate=22050,
|
| 19 |
):
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
if audio.ndim == 1:
|
| 32 |
audio = audio[:, np.newaxis]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
buffer = io.BytesIO()
|
| 35 |
sf.write(buffer, audio, sample_rate, format="WAV")
|
| 36 |
buffer.seek(0)
|
|
|
|
| 1 |
# piper_engine.py
|
|
|
|
| 2 |
import io
|
| 3 |
import numpy as np
|
| 4 |
import soundfile as sf
|
|
|
|
| 7 |
class PiperEngine:
|
| 8 |
|
| 9 |
def __init__(self, model_path):
|
| 10 |
+
# Load the voice model
|
| 11 |
self.voice = PiperVoice.load(model_path)
|
| 12 |
|
| 13 |
def synthesize(
|
| 14 |
self,
|
| 15 |
text,
|
| 16 |
+
speed=1.0, # Default speed multiplier
|
| 17 |
+
pitch=1.0, # Default pitch multiplier
|
| 18 |
sample_rate=22050,
|
| 19 |
):
|
| 20 |
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# Important: call synthesize EXACTLY as Piper expects
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# Positional arguments only: text, length_scale, pitch_scale
|
| 25 |
+
# Do not use keywords like length_scale= or pitch_scale=
|
| 26 |
+
audio = self.voice.synthesize(text, speed, pitch)
|
| 27 |
+
|
| 28 |
+
# -----------------------------
|
| 29 |
+
# Convert to NumPy array and ensure correct shape
|
| 30 |
+
# -----------------------------
|
| 31 |
+
audio = np.array(audio, dtype=np.float32)
|
| 32 |
if audio.ndim == 1:
|
| 33 |
audio = audio[:, np.newaxis]
|
| 34 |
|
| 35 |
+
# -----------------------------
|
| 36 |
+
# Write to WAV buffer
|
| 37 |
+
# -----------------------------
|
| 38 |
buffer = io.BytesIO()
|
| 39 |
sf.write(buffer, audio, sample_rate, format="WAV")
|
| 40 |
buffer.seek(0)
|