Add tts play example for .Net. (#676)

It plays the generated audio via a speaker as it is generating.
This commit is contained in:
Fangjun Kuang
2024-03-19 17:33:15 +08:00
committed by GitHub
parent ce60100f68
commit 6571fc9552
12 changed files with 371 additions and 14 deletions

View File

@@ -159,6 +159,9 @@ namespace SherpaOnnx
private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPStr)] string filename);
}
// IntPtr is actuallly a `const float*` from C++
public delegate void OfflineTtsCallback(IntPtr samples, int n);
public class OfflineTts : IDisposable
{
public OfflineTts(OfflineTtsConfig config)
@@ -173,6 +176,12 @@ namespace SherpaOnnx
return new OfflineTtsGeneratedAudio(p);
}
public OfflineTtsGeneratedAudio GenerateWithCallback(String text, float speed, int speakerId, OfflineTtsCallback callback)
{
IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, text, speakerId, speed, callback);
return new OfflineTtsGeneratedAudio(p);
}
public void Dispose()
{
Cleanup();
@@ -215,6 +224,9 @@ namespace SherpaOnnx
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxOfflineTtsGenerate(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed);
[DllImport(Dll.Filename, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr SherpaOnnxOfflineTtsGenerateWithCallback(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed, OfflineTtsCallback callback);
}