This repository has been archived on 2025-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enginex_bi_series-sherpa-onnx/scripts/dotnet/OfflineTts.cs
东风破 00de2bd00b Refactor .Net example project (#1049)
Co-authored-by: 东风破 <birdfishs@163.com>
2024-06-24 10:10:13 +08:00

88 lines
2.8 KiB
C#

/// Copyright (c) 2024.5 by 东风破
using System;
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
// IntPtr is actually a `const float*` from C++
public delegate int OfflineTtsCallback(IntPtr samples, int n);
public class OfflineTts : IDisposable
{
public OfflineTts(OfflineTtsConfig config)
{
IntPtr h = SherpaOnnxCreateOfflineTts(ref config);
_handle = new HandleRef(this, h);
}
public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId)
{
IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, text, speakerId, speed);
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();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
~OfflineTts()
{
Cleanup();
}
private void Cleanup()
{
SherpaOnnxDestroyOfflineTts(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
private HandleRef _handle;
public int SampleRate
{
get
{
return SherpaOnnxOfflineTtsSampleRate(_handle.Handle);
}
}
public int NumSpeakers
{
get
{
return SherpaOnnxOfflineTtsNumSpeakers(_handle.Handle);
}
}
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCreateOfflineTts(ref OfflineTtsConfig config);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyOfflineTts(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxOfflineTtsSampleRate(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxOfflineTtsNumSpeakers(IntPtr handle);
[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);
}
}