mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +08:00
Fixes #3731 espeak-ng and piper-phonemize are no longer built into sherpa-onnx. Text frontends are now: - **lexicon** — built in, no extra dependency - **phonemize** — phonemize outside sherpa-onnx (e.g. [piper-phonemize](https://github.com/csukuangfj/piper-phonemize)) and pass the result in. That library is GPL, so it stays optional: sherpa-onnx is GPL-free after this PR. ## API - `SherpaOnnxGenerationConfig` gains `phoneme_codepoints` and `tokens` (mutually exclusive alternatives to `text`) - `data_dir` on the TTS model configs is deprecated and ignored; the ABI slot is kept - all bindings updated: C/C++, Python, Java, Kotlin, Go, Rust, C#, Swift, Pascal, Dart/Flutter, node-addon, WASM, HarmonyOS
77 lines
2.5 KiB
Rust
77 lines
2.5 KiB
Rust
// Copyright (c) 2026 Xiaomi Corporation
|
|
//
|
|
// This file demonstrates how to use Kokoro TTS with sherpa-onnx's Rust API
|
|
// for offline Chinese + English text-to-speech.
|
|
|
|
use sherpa_onnx::{
|
|
GenerationConfig, OfflineTts, OfflineTtsConfig, OfflineTtsKokoroModelConfig,
|
|
};
|
|
use std::time::Instant;
|
|
|
|
fn main() {
|
|
let config = OfflineTtsConfig {
|
|
model: sherpa_onnx::OfflineTtsModelConfig {
|
|
kokoro: OfflineTtsKokoroModelConfig {
|
|
model: Some("./kokoro-multi-lang-v1_0/model.onnx".into()),
|
|
voices: Some("./kokoro-multi-lang-v1_0/voices.bin".into()),
|
|
tokens: Some("./kokoro-multi-lang-v1_0/tokens.txt".into()),
|
|
lexicon: Some(
|
|
"./kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/lexicon-zh.txt".into(),
|
|
),
|
|
length_scale: 1.0,
|
|
..Default::default()
|
|
},
|
|
num_threads: 2,
|
|
debug: false,
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
|
|
let tts = OfflineTts::create(&config).expect("Failed to create OfflineTts");
|
|
|
|
println!("Sample rate: {}", tts.sample_rate());
|
|
println!("Num speakers: {}", tts.num_speakers());
|
|
|
|
let text = "中英文语音合成测试。This is generated by next generation Kaldi using \
|
|
Kokoro without Misaki. 你觉得中英文说的如何呢?";
|
|
|
|
let gen_config = GenerationConfig {
|
|
sid: 0,
|
|
speed: 1.0,
|
|
..Default::default()
|
|
};
|
|
|
|
let start = Instant::now();
|
|
|
|
let audio = tts
|
|
.generate_with_config(
|
|
text,
|
|
&gen_config,
|
|
Some(|_samples: &[f32], progress: f32| -> bool {
|
|
println!("Progress: {:.1}%", progress * 100.0);
|
|
true
|
|
}),
|
|
)
|
|
.expect("Generation failed");
|
|
|
|
let elapsed_seconds = start.elapsed().as_secs_f32();
|
|
let duration = audio.samples().len() as f32 / audio.sample_rate() as f32;
|
|
let rtf = elapsed_seconds / duration;
|
|
|
|
println!("Number of threads: {}", config.model.num_threads);
|
|
println!("Elapsed seconds: {:.3} s", elapsed_seconds);
|
|
println!("Audio duration: {:.3} s", duration);
|
|
println!(
|
|
"Real-time factor (RTF): {:.3}/{:.3} = {:.3}",
|
|
elapsed_seconds, duration, rtf
|
|
);
|
|
|
|
let filename = "./generated-kokoro-zh-en-lexicon-rust.wav";
|
|
if audio.save(filename) {
|
|
println!("Saved to: {}", filename);
|
|
} else {
|
|
eprintln!("Failed to save {}", filename);
|
|
}
|
|
}
|