Spaces:
Sleeping
Sleeping
Update piper_engine.py
Browse files- piper_engine.py +42 -39
piper_engine.py
CHANGED
|
@@ -1,39 +1,42 @@
|
|
| 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 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# piper_engine.py
|
| 2 |
+
|
| 3 |
+
import io
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
|
| 6 |
+
from piper.voice import PiperVoice
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class PiperEngine:
|
| 10 |
+
|
| 11 |
+
def __init__(self, model_path):
|
| 12 |
+
|
| 13 |
+
self.voice = PiperVoice.load(model_path)
|
| 14 |
+
|
| 15 |
+
def synthesize(
|
| 16 |
+
self,
|
| 17 |
+
text,
|
| 18 |
+
speed=1.0,
|
| 19 |
+
pitch=1.0,
|
| 20 |
+
sample_rate=22050,
|
| 21 |
+
):
|
| 22 |
+
|
| 23 |
+
# Generate raw audio
|
| 24 |
+
audio = self.voice.synthesize(
|
| 25 |
+
text,
|
| 26 |
+
length_scale=1 / speed,
|
| 27 |
+
pitch_scale=pitch,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Convert to WAV buffer
|
| 31 |
+
buffer = io.BytesIO()
|
| 32 |
+
|
| 33 |
+
sf.write(
|
| 34 |
+
buffer,
|
| 35 |
+
audio,
|
| 36 |
+
sample_rate,
|
| 37 |
+
format="WAV",
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
buffer.seek(0)
|
| 41 |
+
|
| 42 |
+
return buffer
|