Add TTS demo for C# API (#557)
This commit is contained in:
20
scripts/dotnet/examples/offline-tts.csproj
Normal file
20
scripts/dotnet/examples/offline-tts.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>offline_tts</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<RestoreSources>/tmp/packages;$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -10,6 +10,214 @@ using System;
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineTtsVitsModelConfig
|
||||
{
|
||||
public OfflineTtsVitsModelConfig()
|
||||
{
|
||||
Model = "";
|
||||
Lexicon = "";
|
||||
Tokens = "";
|
||||
DataDir = "";
|
||||
|
||||
NoiseScale = 0.667F;
|
||||
NoiseScaleW = 0.8F;
|
||||
LengthScale = 1.0F;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Model;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Lexicon;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Tokens;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string DataDir;
|
||||
|
||||
public float NoiseScale;
|
||||
public float NoiseScaleW;
|
||||
public float LengthScale;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineTtsModelConfig
|
||||
{
|
||||
public OfflineTtsModelConfig()
|
||||
{
|
||||
Vits = new OfflineTtsVitsModelConfig();
|
||||
NumThreads = 1;
|
||||
Debug = 0;
|
||||
Provider = "cpu";
|
||||
}
|
||||
|
||||
public OfflineTtsVitsModelConfig Vits;
|
||||
public int NumThreads;
|
||||
public int Debug;
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Provider;
|
||||
}
|
||||
|
||||
public struct OfflineTtsConfig
|
||||
{
|
||||
public OfflineTtsConfig()
|
||||
{
|
||||
Model = new OfflineTtsModelConfig();
|
||||
RuleFsts = "";
|
||||
MaxNumSentences = 1;
|
||||
}
|
||||
public OfflineTtsModelConfig Model;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string RuleFsts;
|
||||
|
||||
public int MaxNumSentences;
|
||||
}
|
||||
|
||||
public class OfflineTtsGeneratedAudio
|
||||
{
|
||||
public OfflineTtsGeneratedAudio(IntPtr p)
|
||||
{
|
||||
_handle = new HandleRef(this, p);
|
||||
}
|
||||
|
||||
public bool SaveToWaveFile(String filename)
|
||||
{
|
||||
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
|
||||
int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, filename);
|
||||
return status == 1;
|
||||
}
|
||||
|
||||
~OfflineTtsGeneratedAudio()
|
||||
{
|
||||
Cleanup();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Cleanup();
|
||||
// Prevent the object from being placed on the
|
||||
// finalization queue
|
||||
System.GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void Cleanup()
|
||||
{
|
||||
SherpaOnnxDestroyOfflineTtsGeneratedAudio(Handle);
|
||||
|
||||
// Don't permit the handle to be used again.
|
||||
_handle = new HandleRef(this, IntPtr.Zero);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct Impl
|
||||
{
|
||||
public IntPtr Samples;
|
||||
public int NumSamples;
|
||||
public int SampleRate;
|
||||
}
|
||||
|
||||
private HandleRef _handle;
|
||||
public IntPtr Handle => _handle.Handle;
|
||||
|
||||
public int NumSamples
|
||||
{
|
||||
get
|
||||
{
|
||||
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
|
||||
return impl.NumSamples;
|
||||
}
|
||||
}
|
||||
|
||||
public int SampleRate
|
||||
{
|
||||
get
|
||||
{
|
||||
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
|
||||
return impl.SampleRate;
|
||||
}
|
||||
}
|
||||
|
||||
public float[] Samples
|
||||
{
|
||||
get
|
||||
{
|
||||
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
|
||||
|
||||
float[] samples = new float[impl.NumSamples];
|
||||
Marshal.Copy(impl.Samples, samples, 0, impl.NumSamples);
|
||||
return samples;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport(Dll.Filename)]
|
||||
private static extern void SherpaOnnxDestroyOfflineTtsGeneratedAudio(IntPtr handle);
|
||||
|
||||
[DllImport(Dll.Filename)]
|
||||
private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPStr)] string filename);
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
[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 IntPtr SherpaOnnxOfflineTtsGenerate(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineTransducerModelConfig
|
||||
{
|
||||
|
||||
@@ -15,12 +15,23 @@ pushd /tmp
|
||||
|
||||
mkdir -p linux macos windows
|
||||
|
||||
# You can pre-download the required wheels to /tmp
|
||||
src_dir=/tmp
|
||||
|
||||
linux_wheel=$src_dir/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
|
||||
macos_wheel=$src_dir/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_10_14_x86_64.whl
|
||||
windows_wheel=$src_dir/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl
|
||||
|
||||
if [ ! -f /tmp/linux/libsherpa-onnx-core.so ]; then
|
||||
echo "---linux x86_64---"
|
||||
cd linux
|
||||
mkdir wheel
|
||||
mkdir -p wheel
|
||||
cd wheel
|
||||
curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
|
||||
if [ -f $linux_wheel ]; then
|
||||
cp -v $linux_wheel .
|
||||
else
|
||||
curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
|
||||
fi
|
||||
unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
|
||||
cp -v sherpa_onnx/lib/*.so* ../
|
||||
cd ..
|
||||
@@ -36,9 +47,13 @@ fi
|
||||
if [ ! -f /tmp/macos/libsherpa-onnx-core.dylib ]; then
|
||||
echo "---macOS x86_64---"
|
||||
cd macos
|
||||
mkdir wheel
|
||||
mkdir -p wheel
|
||||
cd wheel
|
||||
curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_10_14_x86_64.whl
|
||||
if [ -f $macos_wheel ]; then
|
||||
cp -v $macos_wheel .
|
||||
else
|
||||
curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_10_14_x86_64.whl
|
||||
fi
|
||||
unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_10_14_x86_64.whl
|
||||
cp -v sherpa_onnx/lib/*.dylib ../
|
||||
|
||||
@@ -57,9 +72,13 @@ fi
|
||||
if [ ! -f /tmp/windows/libsherpa-onnx-core.dll ]; then
|
||||
echo "---windows x64---"
|
||||
cd windows
|
||||
mkdir wheel
|
||||
mkdir -p wheel
|
||||
cd wheel
|
||||
curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl
|
||||
if [ -f $windows_wheel ]; then
|
||||
cp -v $windows_wheel .
|
||||
else
|
||||
curl -OL https://huggingface.co/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl
|
||||
fi
|
||||
unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl
|
||||
cp -v sherpa_onnx-${SHERPA_ONNX_VERSION}.data/data/bin/*.dll ../
|
||||
cp -v sherpa_onnx-${SHERPA_ONNX_VERSION}.data/data/bin/*.lib ../
|
||||
@@ -101,5 +120,5 @@ popd
|
||||
|
||||
ls -lh packages
|
||||
|
||||
mkdir /tmp/packages
|
||||
mkdir -p /tmp/packages
|
||||
cp -v packages/*.nupkg /tmp/packages
|
||||
|
||||
Reference in New Issue
Block a user