-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Refactor Kokoro TTS to use the new Generate API #3363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,7 +26,7 @@ rm kokoro-multi-lang-v1_0.tar.bz2 | |||||||||||
| #include "sherpa-onnx/c-api/c-api.h" | ||||||||||||
|
|
||||||||||||
| static int32_t ProgressCallback(const float *samples, int32_t num_samples, | ||||||||||||
| float progress) { | ||||||||||||
| float progress, void *arg) { | ||||||||||||
| fprintf(stderr, "Progress: %.3f%%\n", progress * 100); | ||||||||||||
| // return 1 to continue generating | ||||||||||||
| // return 0 to stop generating | ||||||||||||
|
|
@@ -58,15 +58,19 @@ int32_t main(int32_t argc, char *argv[]) { | |||||||||||
| const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&config); | ||||||||||||
| int32_t sid = 0; // there are 53 speakers | ||||||||||||
| float speed = 1.0; // larger -> faster in speech speed | ||||||||||||
| SherpaOnnxGenerationConfig cfg = {0}; | ||||||||||||
| cfg.silence_scale = 0.2f; | ||||||||||||
| cfg.sid = sid; | ||||||||||||
| cfg.speed = speed; | ||||||||||||
|
Comment on lines
+61
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of
Suggested change
|
||||||||||||
|
|
||||||||||||
| #if 0 | ||||||||||||
| // If you don't want to use a callback, then please enable this branch | ||||||||||||
| const SherpaOnnxGeneratedAudio *audio = | ||||||||||||
| SherpaOnnxOfflineTtsGenerate(tts, text, sid, speed); | ||||||||||||
| SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, NULL, NULL); | ||||||||||||
| #else | ||||||||||||
| const SherpaOnnxGeneratedAudio *audio = | ||||||||||||
| SherpaOnnxOfflineTtsGenerateWithProgressCallback(tts, text, sid, speed, | ||||||||||||
| ProgressCallback); | ||||||||||||
| SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, ProgressCallback, | ||||||||||||
| NULL); | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename); | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,12 +57,16 @@ int32_t main(int32_t argc, char *argv[]) { | |||||||||||||||||||||
| auto tts = OfflineTts::Create(config); | ||||||||||||||||||||||
| int32_t sid = 0; | ||||||||||||||||||||||
| float speed = 1.0; // larger -> faster in speech speed | ||||||||||||||||||||||
| GenerationConfig gen_config; | ||||||||||||||||||||||
| gen_config.sid = sid; | ||||||||||||||||||||||
| gen_config.speed = speed; | ||||||||||||||||||||||
| gen_config.silence_scale = 0.2f; | ||||||||||||||||||||||
|
Comment on lines
58
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #if 0 | ||||||||||||||||||||||
| // If you don't want to use a callback, then please enable this branch | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, sid, speed); | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, gen_config); | ||||||||||||||||||||||
| #else | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback); | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, gen_config, ProgressCallback); | ||||||||||||||||||||||
| #endif | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| WriteWave(filename, {audio.samples, audio.sample_rate}); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,12 +58,16 @@ int32_t main(int32_t argc, char *argv[]) { | |||||||||||||||||||||
| auto tts = OfflineTts::Create(config); | ||||||||||||||||||||||
| int32_t sid = 50; | ||||||||||||||||||||||
| float speed = 1.0; // larger -> faster in speech speed | ||||||||||||||||||||||
| GenerationConfig gen_config; | ||||||||||||||||||||||
| gen_config.sid = sid; | ||||||||||||||||||||||
| gen_config.speed = speed; | ||||||||||||||||||||||
| gen_config.silence_scale = 0.2f; | ||||||||||||||||||||||
|
Comment on lines
59
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #if 0 | ||||||||||||||||||||||
| // If you don't want to use a callback, then please enable this branch | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, sid, speed); | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, gen_config); | ||||||||||||||||||||||
| #else | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback); | ||||||||||||||||||||||
| GeneratedAudio audio = tts.Generate(text, gen_config, ProgressCallback); | ||||||||||||||||||||||
| #endif | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| WriteWave(filename, {audio.samples, audio.sample_rate}); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,9 +35,13 @@ static void Main(string[] args) | |
| "thing in the world was to lose touch with someone."; | ||
|
|
||
| // mapping of sid to voice name | ||
| // 0->af, 1->af_bella, 2->af_nicole, 3->af_sarah, 4->af_sky, 5->am_adam | ||
| // 6->am_michael, 7->bf_emma, 8->bf_isabella, 9->bm_george, 10->bm_lewis | ||
| var sid = 0; | ||
| // 0->af, 1->af_bella, 2->af_nicole, 3->af_sarah, 4->af_sky, 5->am_adam | ||
| // 6->am_michael, 7->bf_emma, 8->bf_isabella, 9->bm_george, 10->bm_lewis | ||
| var sid = 0; | ||
| OfflineTtsGenerationConfig genConfig = new OfflineTtsGenerationConfig(); | ||
| genConfig.Sid = sid; | ||
| genConfig.Speed = speed; | ||
|
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| genConfig.SilenceScale = 0.2f; | ||
|
|
||
|
|
||
| Console.WriteLine(PortAudio.VersionInfo.versionText); | ||
|
|
@@ -73,7 +77,7 @@ static void Main(string[] args) | |
| // https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview | ||
| var dataItems = new BlockingCollection<float[]>(); | ||
|
|
||
| var MyCallback = (IntPtr samples, int n, float progress) => | ||
| var MyCallback = (IntPtr samples, int n, float progress, IntPtr arg) => | ||
| { | ||
| Console.WriteLine($"Progress {progress*100}%"); | ||
|
|
||
|
|
@@ -165,9 +169,9 @@ IntPtr userData | |
|
|
||
| stream.Start(); | ||
|
|
||
| var callback = new OfflineTtsCallbackProgress(MyCallback); | ||
| var audio = tts.GenerateWithCallbackProgress(text, speed, sid, callback); | ||
| var callback = new OfflineTtsCallbackProgressWithArg(MyCallback); | ||
|
|
||
| var audio = tts.GenerateWithConfig(text, genConfig, callback); | ||
| var outputFilename = "./generated-kokoro-0.wav"; | ||
| var ok = audio.SaveToWaveFile(outputFilename); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,12 @@ static void TestZhEn() | |
|
|
||
| var sid = 50; | ||
|
|
||
| var MyCallback = (IntPtr samples, int n, float progress) => | ||
| OfflineTtsGenerationConfig genConfig = new OfflineTtsGenerationConfig(); | ||
| genConfig.Sid = sid; | ||
| genConfig.Speed = speed; | ||
|
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| genConfig.SilenceScale = 0.2f; | ||
|
|
||
| var MyCallback = (IntPtr samples, int n, float progress, IntPtr arg) => | ||
| { | ||
| float[] data = new float[n]; | ||
| Marshal.Copy(samples, data, 0, n); | ||
|
|
@@ -51,9 +56,9 @@ static void TestZhEn() | |
| return 1; | ||
| }; | ||
|
|
||
| var callback = new OfflineTtsCallbackProgress(MyCallback); | ||
| var audio = tts.GenerateWithCallbackProgress(text, speed, sid, callback); | ||
| var callback = new OfflineTtsCallbackProgressWithArg(MyCallback); | ||
|
|
||
| var audio = tts.GenerateWithConfig(text, genConfig, callback); | ||
|
|
||
| var outputFilename = "./generated-kokoro-zh-en.wav"; | ||
| var ok = audio.SaveToWaveFile(outputFilename); | ||
|
|
@@ -93,7 +98,12 @@ static void TestEn() | |
| // 6->am_michael, 7->bf_emma, 8->bf_isabella, 9->bm_george, 10->bm_lewis | ||
| var sid = 0; | ||
|
|
||
| var MyCallback = (IntPtr samples, int n, float progress) => | ||
| OfflineTtsGenerationConfig genConfig = new OfflineTtsGenerationConfig(); | ||
| genConfig.Sid = sid; | ||
| genConfig.Speed = speed; | ||
|
Comment on lines
+101
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| genConfig.SilenceScale = 0.2f; | ||
|
|
||
| var MyCallback = (IntPtr samples, int n, float progress, IntPtr arg) => | ||
| { | ||
| float[] data = new float[n]; | ||
| Marshal.Copy(samples, data, 0, n); | ||
|
|
@@ -106,9 +116,9 @@ static void TestEn() | |
| return 1; | ||
| }; | ||
|
|
||
| var callback = new OfflineTtsCallbackProgress(MyCallback); | ||
| var audio = tts.GenerateWithCallbackProgress(text, speed, sid, callback); | ||
| var callback = new OfflineTtsCallbackProgressWithArg(MyCallback); | ||
|
|
||
| var audio = tts.GenerateWithConfig(text, genConfig, callback); | ||
|
|
||
| var outputFilename = "./generated-kokoro-en.wav"; | ||
| var ok = audio.SaveToWaveFile(outputFilename); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,8 +38,12 @@ public static void main(String[] args) { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| int sid = 0; | ||||||||||||||||||||||
| float speed = 1.0f; | ||||||||||||||||||||||
| GenerationConfig genConfig = new GenerationConfig(); | ||||||||||||||||||||||
| genConfig.setSid(sid); | ||||||||||||||||||||||
| genConfig.setSpeed(speed); | ||||||||||||||||||||||
| genConfig.setSilenceScale(0.2f); | ||||||||||||||||||||||
|
Comment on lines
39
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||
| long start = System.currentTimeMillis(); | ||||||||||||||||||||||
| GeneratedAudio audio = tts.generate(text, sid, speed); | ||||||||||||||||||||||
| GeneratedAudio audio = tts.generateWithConfigAndCallback(text, genConfig, samples -> {}); | ||||||||||||||||||||||
| long stop = System.currentTimeMillis(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| float timeElapsedSeconds = (stop - start) / 1000.0f; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,8 +40,12 @@ public static void main(String[] args) { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| int sid = 0; // this model has 53 speakers. You can use sid in the range 0-52 | ||||||||||||||||||||||||
| float speed = 1.0f; | ||||||||||||||||||||||||
| GenerationConfig genConfig = new GenerationConfig(); | ||||||||||||||||||||||||
| genConfig.setSid(sid); | ||||||||||||||||||||||||
| genConfig.setSpeed(speed); | ||||||||||||||||||||||||
| genConfig.setSilenceScale(0.2f); | ||||||||||||||||||||||||
|
Comment on lines
41
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||
| long start = System.currentTimeMillis(); | ||||||||||||||||||||||||
| GeneratedAudio audio = tts.generate(text, sid, speed); | ||||||||||||||||||||||||
| GeneratedAudio audio = tts.generateWithConfigAndCallback(text, genConfig, samples -> {}); | ||||||||||||||||||||||||
| long stop = System.currentTimeMillis(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| float timeElapsedSeconds = (stop - start) / 1000.0f; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,10 +28,15 @@ function createOfflineTts() { | |
| const tts = createOfflineTts(); | ||
| const speakerId = 0; | ||
| const speed = 1.0; | ||
| const generationConfig = { | ||
| sid: speakerId, | ||
| speed: speed, | ||
| silenceScale: 0.2, | ||
| }; | ||
|
Comment on lines
+31
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const text = | ||
| '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.'; | ||
|
|
||
| const audio = tts.generate({text: text, sid: speakerId, speed: speed}); | ||
| const audio = tts.generateWithConfig(text, generationConfig); | ||
| tts.save('./test-kokoro-en.wav', audio); | ||
| console.log('Saved to test-kokoro-en.wav successfully.'); | ||
| tts.free(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,10 +30,15 @@ function createOfflineTts() { | |
| const tts = createOfflineTts(); | ||
| const speakerId = 49; | ||
| const speed = 1.0; | ||
| const generationConfig = { | ||
| sid: speakerId, | ||
| speed: speed, | ||
| silenceScale: 0.2, | ||
| }; | ||
|
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const text = | ||
| '中英文语音合成测试。This is generated by next generation Kaldi using Kokoro without Misaki. 你觉得中英文说的如何呢?'; | ||
|
|
||
| const audio = tts.generate({text: text, sid: speakerId, speed: speed}); | ||
| const audio = tts.generateWithConfig(text, generationConfig); | ||
| tts.save('./test-kokoro-zh-en-49.wav', audio); | ||
| console.log('Saved to test-kokoro-zh-en-49.wav successfully.'); | ||
| tts.free(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialization of
SherpaOnnxGenerationConfigcan be made more concise and readable by using C99 designated initializers. This avoids initializing the struct to zero and then assigning members individually.