rhasspy / piper

A fast, local neural text to speech system
https://rhasspy.github.io/piper-samples/
MIT License
4.38k stars 297 forks source link

Can piper-tts work without using the terminal? #467

Closed brainiakk closed 3 weeks ago

brainiakk commented 1 month ago

I can't find documentation on implementing piper tts in a python function with using terminal or subprocess

robert-elles commented 1 month ago

I am also interested in using piper directly from python.

From the main.py I came up with a basic example on how to use the python api directly. Here for french:

import piper
from pathlib import Path
import wave

cm_path = '/path/to/models/'
model = 'fr_FR-upmc-medium'
synthesize_args = {
    "speaker_id": 0,
    "length_scale": 1.0,
    "noise_scale": 1.0,
    "noise_w": 1.0,
    "sentence_silence": 0.0,
}
output_file = Path(cm_path) / 'output.wav'

voice = piper.PiperVoice.load(
    model_path=cm_path + model +".onnx",
    config_path=cm_path + model + ".onnx.json",
    use_cuda=False)

line = "Salut tout le monde"

with wave.open(str(output_file), "wb") as wav_file:
    voice.synthesize(line, wav_file, **synthesize_args)
brainiakk commented 1 month ago

Thanks a million @robert-elles it worked perfectly. You can add the ".onnx" to the model variable value so the config_path can only have ".json" appendage, because I think that's how all the voice models are named.