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

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>offline_tts_play</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="*" />
<PackageReference Include="PortAudioSharp2" Version="*" />
</ItemGroup>
</Project>

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

View File

@@ -10,6 +10,9 @@ echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR"
SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" $SHERPA_ONNX_DIR/CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
# HF_MIRROR=hf-mirror.com
HF_MIRROR=hf.co
mkdir -p /tmp/
pushd /tmp
@@ -18,7 +21,7 @@ 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_28_x86_64.whl
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_11_0_x86_64.whl
windows_wheel=$src_dir/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl
@@ -30,9 +33,9 @@ if [ ! -f /tmp/linux/libsherpa-onnx-core.so ]; then
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_28_x86_64.whl
curl -OL https://$HF_MIRROR/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_28_x86_64.whl
unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
cp -v sherpa_onnx/lib/*.so* ../
cd ..
rm -v libpiper_phonemize.so libpiper_phonemize.so.1.2.0
@@ -52,7 +55,7 @@ if [ ! -f /tmp/macos/libsherpa-onnx-core.dylib ]; then
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_11_0_x86_64.whl
curl -OL https://$HF_MIRROR/csukuangfj/sherpa-onnx-wheels/resolve/main/sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_11_0_x86_64.whl
fi
unzip sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-macosx_11_0_x86_64.whl
cp -v sherpa_onnx/lib/*.dylib ../
@@ -69,7 +72,7 @@ if [ ! -f /tmp/macos/libsherpa-onnx-core.dylib ]; then
fi
if [ ! -f /tmp/windows/libsherpa-onnx-core.dll ]; then
if [ ! -f /tmp/windows/sherpa-onnx-core.dll ]; then
echo "---windows x64---"
cd windows
mkdir -p wheel
@@ -77,7 +80,7 @@ if [ ! -f /tmp/windows/libsherpa-onnx-core.dll ]; then
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
curl -OL https://$HF_MIRROR/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 ../