Skip to content

Commit

Permalink
Add C# API for Kokoro TTS 1.0 (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Feb 7, 2025
1 parent e1a88a7 commit ae32dfa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/test-dot-net.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cd dotnet-examples/

cd ./kokoro-tts
./run-kokoro-en.sh
./run-kokoro.sh
ls -lh

cd ../offline-tts
Expand Down
59 changes: 58 additions & 1 deletion dotnet-examples/kokoro-tts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,63 @@
class OfflineTtsDemo
{
static void Main(string[] args)
{

TestZhEn();
TestEn();
}

static void TestZhEn()
{
var config = new OfflineTtsConfig();
config.Model.Kokoro.Model = "./kokoro-multi-lang-v1_0/model.onnx";
config.Model.Kokoro.Voices = "./kokoro-multi-lang-v1_0/voices.bin";
config.Model.Kokoro.Tokens = "./kokoro-multi-lang-v1_0/tokens.txt";
config.Model.Kokoro.DataDir = "./kokoro-multi-lang-v1_0/espeak-ng-data";
config.Model.Kokoro.DictDir = "./kokoro-multi-lang-v1_0/dict";
config.Model.Kokoro.Lexicon = "./kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/lexicon-zh.txt";

config.Model.NumThreads = 2;
config.Model.Debug = 1;
config.Model.Provider = "cpu";

var tts = new OfflineTts(config);
var speed = 1.0f;
var text = "中英文语音合成测试。This is generated by next generation Kaldi using Kokoro without Misaki. 你觉得中英文说的如何呢?";

var sid = 50;

var MyCallback = (IntPtr samples, int n, float progress) =>
{
float[] data = new float[n];
Marshal.Copy(samples, data, 0, n);
// You can process samples here, e.g., play them.
// See ../kokoro-tts-playback for how to play them
Console.WriteLine($"Progress {progress*100}%");

// 1 means to keep generating
// 0 means to stop generating
return 1;
};

var callback = new OfflineTtsCallbackProgress(MyCallback);

var audio = tts.GenerateWithCallbackProgress(text, speed, sid, callback);

var outputFilename = "./generated-kokoro-zh-en.wav";
var ok = audio.SaveToWaveFile(outputFilename);

if (ok)
{
Console.WriteLine($"Wrote to {outputFilename} succeeded!");
}
else
{
Console.WriteLine($"Failed to write {outputFilename}");
}
}

static void TestEn()
{
var config = new OfflineTtsConfig();
config.Model.Kokoro.Model = "./kokoro-en-v0_19/model.onnx";
Expand Down Expand Up @@ -54,7 +111,7 @@ static void Main(string[] args)

var audio = tts.GenerateWithCallbackProgress(text, speed, sid, callback);

var outputFilename = "./generated-kokoro-0.wav";
var outputFilename = "./generated-kokoro-en.wav";
var ok = audio.SaveToWaveFile(outputFilename);

if (ok)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/usr/bin/env bash
set -ex

if [ ! -f ./kokoro-multi-lang-v1_0/model.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-multi-lang-v1_0.tar.bz2
tar xf kokoro-multi-lang-v1_0.tar.bz2
rm kokoro-multi-lang-v1_0.tar.bz2
fi

if [ ! -f ./kokoro-en-v0_19/model.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2
tar xf kokoro-en-v0_19.tar.bz2
Expand Down
9 changes: 9 additions & 0 deletions scripts/dotnet/OfflineTtsKokoroModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public OfflineTtsKokoroModelConfig()
DataDir = "";

LengthScale = 1.0F;

DictDir = "";
Lexicon = "";
}
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
Expand All @@ -29,5 +32,11 @@ public OfflineTtsKokoroModelConfig()
public string DataDir;

public float LengthScale;

[MarshalAs(UnmanagedType.LPStr)]
public string DictDir;

[MarshalAs(UnmanagedType.LPStr)]
public string Lexicon;
}
}

0 comments on commit ae32dfa

Please sign in to comment.