Skip to content

Commit b3624a8

Browse files
authored
Refactor MatchaTTS to use the new Generate API. (#3362)
This pull request streamlines the MatchaTTS API by introducing a consolidated GenerationConfig object for text-to-speech generation parameters. This change affects examples across various programming languages, ensuring a more consistent and extensible interface for controlling speech synthesis attributes like speaker, speed, and silence. The refactoring simplifies future enhancements and reduces parameter sprawl in API calls.
1 parent 8ffdb89 commit b3624a8

19 files changed

Lines changed: 171 additions & 68 deletions

c-api-examples/matcha-tts-en-c-api.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ wget https://github.com/k2-fsa/sherpa-onnx/releases/download/vocoder-models/voco
2727
#include "sherpa-onnx/c-api/c-api.h"
2828

2929
static int32_t ProgressCallback(const float *samples, int32_t num_samples,
30-
float progress) {
30+
float progress, void *arg) {
3131
fprintf(stderr, "Progress: %.3f%%\n", progress * 100);
3232
// return 1 to continue generating
3333
// return 0 to stop generating
@@ -61,17 +61,19 @@ int32_t main(int32_t argc, char *argv[]) {
6161
"thing in the world was to lose touch with someone.";
6262

6363
const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&config);
64-
int32_t sid = 0;
65-
float speed = 1.0; // larger -> faster in speech speed
64+
SherpaOnnxGenerationConfig cfg = {0};
65+
cfg.sid = 0;
66+
cfg.speed = 1.0f; // larger -> faster in speech speed
67+
cfg.silence_scale = config.silence_scale;
6668

6769
#if 0
6870
// If you don't want to use a callback, then please enable this branch
6971
const SherpaOnnxGeneratedAudio *audio =
70-
SherpaOnnxOfflineTtsGenerate(tts, text, sid, speed);
72+
SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, NULL, NULL);
7173
#else
7274
const SherpaOnnxGeneratedAudio *audio =
73-
SherpaOnnxOfflineTtsGenerateWithProgressCallback(tts, text, sid, speed,
74-
ProgressCallback);
75+
SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, ProgressCallback,
76+
NULL);
7577
#endif
7678

7779
SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename);
@@ -80,7 +82,7 @@ int32_t main(int32_t argc, char *argv[]) {
8082
SherpaOnnxDestroyOfflineTts(tts);
8183

8284
fprintf(stderr, "Input text is: %s\n", text);
83-
fprintf(stderr, "Speaker ID is: %d\n", sid);
85+
fprintf(stderr, "Speaker ID is: %d\n", cfg.sid);
8486
fprintf(stderr, "Saved to: %s\n", filename);
8587

8688
return 0;

c-api-examples/matcha-tts-zh-c-api.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ wget https://github.com/k2-fsa/sherpa-onnx/releases/download/vocoder-models/voco
2727
#include "sherpa-onnx/c-api/c-api.h"
2828

2929
static int32_t ProgressCallback(const float *samples, int32_t num_samples,
30-
float progress) {
30+
float progress, void *arg) {
3131
fprintf(stderr, "Progress: %.3f%%\n", progress * 100);
3232
// return 1 to continue generating
3333
// return 0 to stop generating
@@ -61,17 +61,19 @@ int32_t main(int32_t argc, char *argv[]) {
6161
"经济不断增长。2024年12月31号,拨打110或者18920240511。123456块钱。";
6262

6363
const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&config);
64-
int32_t sid = 0;
65-
float speed = 1.0; // larger -> faster in speech speed
64+
SherpaOnnxGenerationConfig cfg = {0};
65+
cfg.sid = 0;
66+
cfg.speed = 1.0f; // larger -> faster in speech speed
67+
cfg.silence_scale = config.silence_scale;
6668

6769
#if 0
6870
// If you don't want to use a callback, then please enable this branch
6971
const SherpaOnnxGeneratedAudio *audio =
70-
SherpaOnnxOfflineTtsGenerate(tts, text, sid, speed);
72+
SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, NULL, NULL);
7173
#else
7274
const SherpaOnnxGeneratedAudio *audio =
73-
SherpaOnnxOfflineTtsGenerateWithProgressCallback(tts, text, sid, speed,
74-
ProgressCallback);
75+
SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, ProgressCallback,
76+
NULL);
7577
#endif
7678

7779
SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename);
@@ -80,7 +82,7 @@ int32_t main(int32_t argc, char *argv[]) {
8082
SherpaOnnxDestroyOfflineTts(tts);
8183

8284
fprintf(stderr, "Input text is: %s\n", text);
83-
fprintf(stderr, "Speaker ID is: %d\n", sid);
85+
fprintf(stderr, "Speaker ID is: %d\n", cfg.sid);
8486
fprintf(stderr, "Saved to: %s\n", filename);
8587

8688
return 0;

cxx-api-examples/matcha-tts-en-cxx-api.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,22 @@ int32_t main(int32_t argc, char *argv[]) {
6262
"thing in the world was to lose touch with someone.";
6363

6464
auto tts = OfflineTts::Create(config);
65-
int32_t sid = 0;
66-
float speed = 1.0; // larger -> faster in speech speed
65+
GenerationConfig gen_config;
66+
gen_config.sid = 0;
67+
gen_config.speed = 1.0; // larger -> faster in speech speed
68+
gen_config.silence_scale = config.silence_scale;
6769

6870
#if 0
6971
// If you don't want to use a callback, then please enable this branch
70-
GeneratedAudio audio = tts.Generate(text, sid, speed);
72+
GeneratedAudio audio = tts.Generate(text, gen_config);
7173
#else
72-
GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback);
74+
GeneratedAudio audio = tts.Generate(text, gen_config, ProgressCallback);
7375
#endif
7476

7577
WriteWave(filename, {audio.samples, audio.sample_rate});
7678

7779
fprintf(stderr, "Input text is: %s\n", text.c_str());
78-
fprintf(stderr, "Speaker ID is: %d\n", sid);
80+
fprintf(stderr, "Speaker ID is: %d\n", gen_config.sid);
7981
fprintf(stderr, "Saved to: %s\n", filename.c_str());
8082

8183
return 0;

cxx-api-examples/matcha-tts-zh-cxx-api.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,22 @@ int32_t main(int32_t argc, char *argv[]) {
6161
"经济不断增长。2024年12月31号,拨打110或者18920240511。123456块钱。";
6262

6363
auto tts = OfflineTts::Create(config);
64-
int32_t sid = 0;
65-
float speed = 1.0; // larger -> faster in speech speed
64+
GenerationConfig gen_config;
65+
gen_config.sid = 0;
66+
gen_config.speed = 1.0; // larger -> faster in speech speed
67+
gen_config.silence_scale = config.silence_scale;
6668

6769
#if 0
6870
// If you don't want to use a callback, then please enable this branch
69-
GeneratedAudio audio = tts.Generate(text, sid, speed);
71+
GeneratedAudio audio = tts.Generate(text, gen_config);
7072
#else
71-
GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback);
73+
GeneratedAudio audio = tts.Generate(text, gen_config, ProgressCallback);
7274
#endif
7375

7476
WriteWave(filename, {audio.samples, audio.sample_rate});
7577

7678
fprintf(stderr, "Input text is: %s\n", text.c_str());
77-
fprintf(stderr, "Speaker ID is: %d\n", sid);
79+
fprintf(stderr, "Speaker ID is: %d\n", gen_config.sid);
7880
fprintf(stderr, "Saved to: %s\n", filename.c_str());
7981

8082
return 0;

dart-api-examples/tts/bin/matcha-en.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void main(List<String> arguments) async {
5858
vocoder: vocoder,
5959
tokens: tokens,
6060
dataDir: dataDir,
61-
lengthScale: 1 / speed,
6261
);
6362

6463
final modelConfig = sherpa_onnx.OfflineTtsModelConfig(
@@ -74,7 +73,12 @@ void main(List<String> arguments) async {
7473
);
7574

7675
final tts = sherpa_onnx.OfflineTts(config);
77-
final audio = tts.generate(text: text, sid: sid, speed: speed);
76+
final genConfig = sherpa_onnx.OfflineTtsGenerationConfig(
77+
sid: sid,
78+
speed: speed,
79+
silenceScale: config.silenceScale,
80+
);
81+
final audio = tts.generateWithConfig(text: text, config: genConfig);
7882
tts.free();
7983

8084
sherpa_onnx.writeWave(

dart-api-examples/tts/bin/matcha-zh.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void main(List<String> arguments) async {
5454
vocoder: vocoder,
5555
lexicon: lexicon,
5656
tokens: tokens,
57-
lengthScale: 1 / speed,
5857
);
5958

6059
final modelConfig = sherpa_onnx.OfflineTtsModelConfig(
@@ -70,7 +69,12 @@ void main(List<String> arguments) async {
7069
);
7170

7271
final tts = sherpa_onnx.OfflineTts(config);
73-
final audio = tts.generate(text: text, sid: sid, speed: speed);
72+
final genConfig = sherpa_onnx.OfflineTtsGenerationConfig(
73+
sid: sid,
74+
speed: speed,
75+
silenceScale: config.silenceScale,
76+
);
77+
final audio = tts.generateWithConfig(text: text, config: genConfig);
7478
tts.free();
7579

7680
sherpa_onnx.writeWave(

java-api-examples/NonStreamingTtsMatchaEn.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ public static void main(String[] args) {
3636
OfflineTtsConfig config = OfflineTtsConfig.builder().setModel(modelConfig).build();
3737
OfflineTts tts = new OfflineTts(config);
3838

39-
int sid = 0;
40-
float speed = 1.0f;
39+
GenerationConfig genConfig = new GenerationConfig();
40+
genConfig.setSid(0);
41+
genConfig.setSpeed(1.0f);
42+
genConfig.setSilenceScale(config.getSilenceScale());
43+
4144
long start = System.currentTimeMillis();
42-
GeneratedAudio audio = tts.generate(text, sid, speed);
45+
GeneratedAudio audio = tts.generateWithConfigAndCallback(text, genConfig, (float[] samples) -> 1);
4346
long stop = System.currentTimeMillis();
4447

4548
float timeElapsedSeconds = (stop - start) / 1000.0f;
@@ -53,6 +56,7 @@ public static void main(String[] args) {
5356
System.out.printf("-- audio duration: %.3f seconds\n", audioDuration);
5457
System.out.printf("-- real-time factor (RTF): %.3f\n", realTimeFactor);
5558
System.out.printf("-- text: %s\n", text);
59+
System.out.printf("-- speaker ID: %d\n", genConfig.getSid());
5660
System.out.printf("-- Saved to %s\n", waveFilename);
5761

5862
tts.release();

java-api-examples/NonStreamingTtsMatchaZh.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ public static void main(String[] args) {
4040
OfflineTtsConfig.builder().setModel(modelConfig).setRuleFsts(ruleFsts).build();
4141
OfflineTts tts = new OfflineTts(config);
4242

43-
int sid = 0;
44-
float speed = 1.0f;
43+
GenerationConfig genConfig = new GenerationConfig();
44+
genConfig.setSid(0);
45+
genConfig.setSpeed(1.0f);
46+
genConfig.setSilenceScale(config.getSilenceScale());
47+
4548
long start = System.currentTimeMillis();
46-
GeneratedAudio audio = tts.generate(text, sid, speed);
49+
GeneratedAudio audio = tts.generateWithConfigAndCallback(text, genConfig, (float[] samples) -> 1);
4750
long stop = System.currentTimeMillis();
4851

4952
float timeElapsedSeconds = (stop - start) / 1000.0f;
@@ -57,6 +60,7 @@ public static void main(String[] args) {
5760
System.out.printf("-- audio duration: %.3f seconds\n", audioDuration);
5861
System.out.printf("-- real-time factor (RTF): %.3f\n", realTimeFactor);
5962
System.out.printf("-- text: %s\n", text);
63+
System.out.printf("-- speaker ID: %d\n", genConfig.getSid());
6064
System.out.printf("-- Saved to %s\n", waveFilename);
6165

6266
tts.release();

nodejs-addon-examples/test_tts_non_streaming_matcha_icefall_en.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ const tts = createOfflineTts();
2727
const text =
2828
'Today as always, men fall into two groups: slaves and free men. Whoever does not have two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a businessman, an official, or a scholar.';
2929

30+
const generationConfig = new sherpa_onnx.GenerationConfig({
31+
sid: 0,
32+
speed: 1.0,
33+
silenceScale: 0.2,
34+
});
35+
3036

3137
let start = Date.now();
32-
const audio = tts.generate({text: text, sid: 0, speed: 1.0});
38+
const audio = tts.generate({text, generationConfig});
3339
let stop = Date.now();
3440
const elapsed_seconds = (stop - start) / 1000;
3541
const duration = audio.samples.length / audio.sampleRate;

nodejs-addon-examples/test_tts_non_streaming_matcha_icefall_zh.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ const tts = createOfflineTts();
2929
const text =
3030
'当夜幕降临,星光点点,伴随着微风拂面,我在静谧中感受着时光的流转,思念如涟漪荡漾,梦境如画卷展开,我与自然融为一体,沉静在这片宁静的美丽之中,感受着生命的奇迹与温柔. 某某银行的副行长和一些行政领导表示,他们去过长江和长白山; 经济不断增长。2024年12月31号,拨打110或者18920240511。123456块钱。';
3131

32+
const generationConfig = new sherpa_onnx.GenerationConfig({
33+
sid: 0,
34+
speed: 1.0,
35+
silenceScale: 0.2,
36+
});
37+
3238

3339
let start = Date.now();
34-
const audio = tts.generate({text: text, sid: 0, speed: 1.0});
40+
const audio = tts.generate({text, generationConfig});
3541
let stop = Date.now();
3642
const elapsed_seconds = (stop - start) / 1000;
3743
const duration = audio.samples.length / audio.sampleRate;

0 commit comments

Comments
 (0)