Spaces:
Sleeping
Sleeping
File size: 1,260 Bytes
7c18902 13508c9 7c18902 7ea4ecf 7c18902 7ea4ecf 7c18902 9e3d3a0 7ea4ecf 13508c9 7ea4ecf 7c18902 706d6b4 7c18902 f54a774 | 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 40 41 42 43 | # piper_engine.py
import io
import numpy as np
import soundfile as sf
from piper.voice import PiperVoice
class PiperEngine:
def __init__(self, model_path):
# Load the voice model
self.voice = PiperVoice.load(model_path)
def synthesize(
self,
text,
speed=1.0, # Default speed multiplier
pitch=1.0, # Default pitch multiplier
sample_rate=22050,
):
# -----------------------------
# Important: call synthesize EXACTLY as Piper expects
# -----------------------------
# Positional arguments only: text, length_scale, pitch_scale
# Do not use keywords like length_scale= or pitch_scale=
audio = self.voice.synthesize(text, speed, pitch)
# -----------------------------
# Convert to NumPy array and ensure correct shape
# -----------------------------
audio = np.array(audio, dtype=np.float32)
if audio.ndim == 1:
audio = audio[:, np.newaxis]
# -----------------------------
# Write to WAV buffer
# -----------------------------
buffer = io.BytesIO()
sf.write(buffer, audio, sample_rate, format="WAV")
buffer.seek(0)
return buffer
|