Spaces:
Sleeping
Sleeping
Update piper_engine.py
Browse files- piper_engine.py +6 -20
piper_engine.py
CHANGED
|
@@ -3,14 +3,11 @@
|
|
| 3 |
import io
|
| 4 |
import numpy as np
|
| 5 |
import soundfile as sf
|
| 6 |
-
|
| 7 |
from piper.voice import PiperVoice
|
| 8 |
|
| 9 |
-
|
| 10 |
class PiperEngine:
|
| 11 |
|
| 12 |
def __init__(self, model_path):
|
| 13 |
-
# Load the voice model
|
| 14 |
self.voice = PiperVoice.load(model_path)
|
| 15 |
|
| 16 |
def synthesize(
|
|
@@ -20,33 +17,22 @@ class PiperEngine:
|
|
| 20 |
pitch=1.0,
|
| 21 |
sample_rate=22050,
|
| 22 |
):
|
| 23 |
-
"""
|
| 24 |
-
Synthesizes expressive speech from text using Piper.
|
| 25 |
-
Returns a BytesIO WAV buffer.
|
| 26 |
-
"""
|
| 27 |
|
| 28 |
-
#
|
| 29 |
audio_gen = self.voice.synthesize(
|
| 30 |
-
text,
|
| 31 |
-
1 / speed,
|
| 32 |
-
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# Consume generator into
|
| 36 |
audio = np.fromiter(audio_gen, dtype=np.float32)
|
| 37 |
|
| 38 |
-
# Ensure audio is 2D: shape (samples, channels)
|
| 39 |
if audio.ndim == 1:
|
| 40 |
audio = audio[:, np.newaxis]
|
| 41 |
|
| 42 |
-
# Write to WAV buffer
|
| 43 |
buffer = io.BytesIO()
|
| 44 |
-
sf.write(
|
| 45 |
-
buffer,
|
| 46 |
-
audio,
|
| 47 |
-
sample_rate,
|
| 48 |
-
format="WAV",
|
| 49 |
-
)
|
| 50 |
buffer.seek(0)
|
| 51 |
|
| 52 |
return buffer
|
|
|
|
| 3 |
import io
|
| 4 |
import numpy as np
|
| 5 |
import soundfile as sf
|
|
|
|
| 6 |
from piper.voice import PiperVoice
|
| 7 |
|
|
|
|
| 8 |
class PiperEngine:
|
| 9 |
|
| 10 |
def __init__(self, model_path):
|
|
|
|
| 11 |
self.voice = PiperVoice.load(model_path)
|
| 12 |
|
| 13 |
def synthesize(
|
|
|
|
| 17 |
pitch=1.0,
|
| 18 |
sample_rate=22050,
|
| 19 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Correctly call synthesize with keyword arguments
|
| 22 |
audio_gen = self.voice.synthesize(
|
| 23 |
+
text=text,
|
| 24 |
+
length_scale=1 / speed,
|
| 25 |
+
pitch_scale=pitch
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Consume generator into NumPy array
|
| 29 |
audio = np.fromiter(audio_gen, dtype=np.float32)
|
| 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)
|
| 37 |
|
| 38 |
return buffer
|