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);
}
}