Views
No views yet
| Field | Value |
|---|---|
| Architecture | VITS (end-to-end) |
| Format | ONNX |
| Language | English (US) |
| Gender | Male |
| Model Size | medium (~63 MB ONNX, ~15M params) |
| Sample Rate | 22050 Hz |
| License | CC BY-NC-SA 4.0 |
Note: Piper uses the terms "medium", "high", etc. to refer to model size, not output quality. Medium models (~63 MB, ~15M params) and high models (~114 MB, ~28M params) both produce 22.05 kHz audio.
1from piper import PiperVoice
2
3voice = PiperVoice.load("model.onnx")
4for chunk in voice.synthesize("Hello, this is a test."):
5 # chunk.audio_float_array contains float32 audio
6 passespeak-ng installed (brew install espeak-ng / apt install espeak-ng).1import json, subprocess, numpy as np, onnxruntime as ort, soundfile as sf
2from huggingface_hub import hf_hub_download
3
4model_id = "Trelis/piper-en-us-ryan-medium"
5onnx_path = hf_hub_download(model_id, "model.onnx")
6config_path = hf_hub_download(model_id, "model.onnx.json")
7
8with open(config_path) as f:
9 config = json.load(f)
10
11session = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])
12phoneme_id_map = config["phoneme_id_map"]
13espeak_voice = config["espeak"]["voice"]
14
15def phonemize(text, voice):
16 out = subprocess.run(
17 ["espeak-ng", "-v", voice, "-q", "--ipa=2", "-x", text],
18 capture_output=True, text=True,
19 ).stdout.strip()
20 return [list(line.replace("_", " ")) for line in out.split("\n") if line.strip()]
21
22def to_ids(phonemes, pmap):
23 ids = [pmap["^"][0], pmap["_"][0]]
24 for p in phonemes:
25 if p in pmap:
26 ids.extend(pmap[p])
27 ids.append(pmap["_"][0])
28 ids.append(pmap["$"][0])
29 return ids
30
31text = "Hello, this is a test."
32audio_chunks = []
33for sentence in phonemize(text, espeak_voice):
34 ids = to_ids(sentence, phoneme_id_map)
35 if len(ids) < 3:
36 continue
37 audio = session.run(None, {
38 "input": np.array([ids], dtype=np.int64),
39 "input_lengths": np.array([len(ids)], dtype=np.int64),
40 "scales": np.array([
41 config["inference"]["noise_scale"],
42 config["inference"]["length_scale"],
43 config["inference"]["noise_w"],
44 ], dtype=np.float32),
45 })[0]
46 audio_chunks.append(audio.squeeze())
47
48audio = np.concatenate(audio_chunks).astype(np.float32)
49sf.write("output.wav", audio, config["audio"]["sample_rate"])en_US-ryan-medium