Add C# API for Kokoro TTS models (#1720)

This commit is contained in:
Fangjun Kuang
2025-01-16 16:30:10 +08:00
committed by GitHub
parent 2d0869c709
commit cc812e6237
11 changed files with 381 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ namespace SherpaOnnx
{
// IntPtr is actually a `const float*` from C++
public delegate int OfflineTtsCallback(IntPtr samples, int n);
public delegate int OfflineTtsCallbackProgress(IntPtr samples, int n, float progress);
public class OfflineTts : IDisposable
{
@@ -36,6 +37,16 @@ namespace SherpaOnnx
return new OfflineTtsGeneratedAudio(p);
}
public OfflineTtsGeneratedAudio GenerateWithCallbackProgress(String text, float speed, int speakerId, OfflineTtsCallbackProgress callback)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(text);
byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator
Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length);
utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator
IntPtr p = SherpaOnnxOfflineTtsGenerateWithProgressCallback(_handle.Handle, utf8BytesWithNull, speakerId, speed, callback);
return new OfflineTtsGeneratedAudio(p);
}
public void Dispose()
{
Cleanup();
@@ -92,5 +103,8 @@ namespace SherpaOnnx
[DllImport(Dll.Filename, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr SherpaOnnxOfflineTtsGenerateWithCallback(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Text, int sid, float speed, OfflineTtsCallback callback);
[DllImport(Dll.Filename, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr SherpaOnnxOfflineTtsGenerateWithProgressCallback(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Text, int sid, float speed, OfflineTtsCallbackProgress callback);
}
}

View File

@@ -0,0 +1,33 @@
/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflineTtsKokoroModelConfig
{
public OfflineTtsKokoroModelConfig()
{
Model = "";
Voices = "";
Tokens = "";
DataDir = "";
LengthScale = 1.0F;
}
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
[MarshalAs(UnmanagedType.LPStr)]
public string Voices;
[MarshalAs(UnmanagedType.LPStr)]
public string Tokens;
[MarshalAs(UnmanagedType.LPStr)]
public string DataDir;
public float LengthScale;
}
}

View File

@@ -12,6 +12,7 @@ namespace SherpaOnnx
{
Vits = new OfflineTtsVitsModelConfig();
Matcha = new OfflineTtsMatchaModelConfig();
Kokoro = new OfflineTtsKokoroModelConfig();
NumThreads = 1;
Debug = 0;
Provider = "cpu";
@@ -24,5 +25,6 @@ namespace SherpaOnnx
public string Provider;
public OfflineTtsMatchaModelConfig Matcha;
public OfflineTtsKokoroModelConfig Kokoro;
}
}