From 5cce159cf3647b944c55cd1452a0d883265a8427 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 25 Jun 2024 10:52:59 +0800 Subject: [PATCH] Fix passing C# string to C++ (#1055) --- .github/workflows/linux-jni.yaml | 4 +-- CMakeLists.txt | 2 +- .../non-streaming-asr/pubspec.yaml | 2 +- dart-api-examples/streaming-asr/pubspec.yaml | 2 +- dart-api-examples/tts/pubspec.yaml | 2 +- dart-api-examples/vad/pubspec.yaml | 2 +- dotnet-examples/Common/Common.csproj | 2 +- dotnet-examples/offline-punctuation/run.sh | 0 nodejs-addon-examples/package.json | 2 +- scripts/dotnet/.gitignore | 3 ++- scripts/dotnet/OfflinePunctuation.cs | 6 +++-- scripts/dotnet/OfflineTts.cs | 11 +++++--- scripts/dotnet/OfflineTtsGeneratedAudio.cs | 6 +++-- scripts/dotnet/SpeakerEmbeddingManager.cs | 25 +++++++++++-------- scripts/dotnet/examples/Common.csproj | 2 +- sherpa-onnx/flutter/CHANGELOG.md | 4 +++ 16 files changed, 46 insertions(+), 29 deletions(-) mode change 100644 => 100755 dotnet-examples/offline-punctuation/run.sh diff --git a/.github/workflows/linux-jni.yaml b/.github/workflows/linux-jni.yaml index ce9621078..0bba1a045 100644 --- a/.github/workflows/linux-jni.yaml +++ b/.github/workflows/linux-jni.yaml @@ -71,7 +71,7 @@ jobs: mkdir build cd build - cmake -DSHERPA_ONNX_ENABLE_TTS=ON -D CMAKE_BUILD_TYPE=${{ matrix.build_type }} -D BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install -DSHERPA_ONNX_ENABLE_JNI=ON .. + cmake -DSHERPA_ONNX_ENABLE_TTS=ON -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install -DSHERPA_ONNX_ENABLE_JNI=ON .. make -j2 make install @@ -153,7 +153,7 @@ jobs: git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs main - name: Release pre-compiled binaries and libs for linux x64 - if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release' + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') uses: svenstaro/upload-release-action@v2 with: file_glob: true diff --git a/CMakeLists.txt b/CMakeLists.txt index ec529bab6..3cabb0c74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(sherpa-onnx) # ./nodejs-addon-examples # ./dart-api-examples/ # ./sherpa-onnx/flutter/CHANGELOG.md -set(SHERPA_ONNX_VERSION "1.10.1") +set(SHERPA_ONNX_VERSION "1.10.2") # Disable warning about # diff --git a/dart-api-examples/non-streaming-asr/pubspec.yaml b/dart-api-examples/non-streaming-asr/pubspec.yaml index 98bbcc138..96b9f90a0 100644 --- a/dart-api-examples/non-streaming-asr/pubspec.yaml +++ b/dart-api-examples/non-streaming-asr/pubspec.yaml @@ -10,7 +10,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.1 + sherpa_onnx: ^1.10.2 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/streaming-asr/pubspec.yaml b/dart-api-examples/streaming-asr/pubspec.yaml index 78c7df842..65a1282ea 100644 --- a/dart-api-examples/streaming-asr/pubspec.yaml +++ b/dart-api-examples/streaming-asr/pubspec.yaml @@ -11,7 +11,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.1 + sherpa_onnx: ^1.10.2 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/tts/pubspec.yaml b/dart-api-examples/tts/pubspec.yaml index 24e1b0234..8732c3d04 100644 --- a/dart-api-examples/tts/pubspec.yaml +++ b/dart-api-examples/tts/pubspec.yaml @@ -8,7 +8,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.1 + sherpa_onnx: ^1.10.2 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/vad/pubspec.yaml b/dart-api-examples/vad/pubspec.yaml index 2033fff66..4c408dd6c 100644 --- a/dart-api-examples/vad/pubspec.yaml +++ b/dart-api-examples/vad/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: ^3.4.0 dependencies: - sherpa_onnx: ^1.10.1 + sherpa_onnx: ^1.10.2 path: ^1.9.0 args: ^2.5.0 diff --git a/dotnet-examples/Common/Common.csproj b/dotnet-examples/Common/Common.csproj index 40a816f44..a9630614f 100644 --- a/dotnet-examples/Common/Common.csproj +++ b/dotnet-examples/Common/Common.csproj @@ -6,7 +6,7 @@ - + diff --git a/dotnet-examples/offline-punctuation/run.sh b/dotnet-examples/offline-punctuation/run.sh old mode 100644 new mode 100755 diff --git a/nodejs-addon-examples/package.json b/nodejs-addon-examples/package.json index c51643c81..3d6746e5e 100644 --- a/nodejs-addon-examples/package.json +++ b/nodejs-addon-examples/package.json @@ -1,5 +1,5 @@ { "dependencies": { - "sherpa-onnx-node": "^1.10.1" + "sherpa-onnx-node": "^1.10.2" } } diff --git a/scripts/dotnet/.gitignore b/scripts/dotnet/.gitignore index 6e69e3c10..6d09be539 100644 --- a/scripts/dotnet/.gitignore +++ b/scripts/dotnet/.gitignore @@ -1,5 +1,6 @@ all -macos +macos-arm64 +macos-x64 linux windows windows-x64 diff --git a/scripts/dotnet/OfflinePunctuation.cs b/scripts/dotnet/OfflinePunctuation.cs index 384df045b..4b39c3d32 100644 --- a/scripts/dotnet/OfflinePunctuation.cs +++ b/scripts/dotnet/OfflinePunctuation.cs @@ -16,7 +16,9 @@ public OfflinePunctuation(OfflinePunctuationConfig config) public String AddPunct(String text) { - IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, text); + byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); + + IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, utf8Bytes); string s = ""; int length = 0; @@ -77,7 +79,7 @@ private void Cleanup() private static extern void SherpaOnnxDestroyOfflinePunctuation(IntPtr handle); [DllImport(Dll.Filename)] - private static extern IntPtr SherpaOfflinePunctuationAddPunct(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text); + private static extern IntPtr SherpaOfflinePunctuationAddPunct(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Text); [DllImport(Dll.Filename)] private static extern void SherpaOfflinePunctuationFreeText(IntPtr p); diff --git a/scripts/dotnet/OfflineTts.cs b/scripts/dotnet/OfflineTts.cs index 357a8dd27..6e36d816f 100644 --- a/scripts/dotnet/OfflineTts.cs +++ b/scripts/dotnet/OfflineTts.cs @@ -1,6 +1,7 @@ /// Copyright (c) 2024.5 by 东风破 using System; using System.Runtime.InteropServices; +using System.Text; namespace SherpaOnnx { @@ -17,13 +18,15 @@ public OfflineTts(OfflineTtsConfig config) public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId) { - IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, text, speakerId, speed); + byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); + IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, utf8Bytes, 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); + byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); + IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, utf8Bytes, speakerId, speed, callback); return new OfflineTtsGeneratedAudio(p); } @@ -79,9 +82,9 @@ public int NumSpeakers 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); + private static extern IntPtr SherpaOnnxOfflineTtsGenerate(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Text, 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); + private static extern IntPtr SherpaOnnxOfflineTtsGenerateWithCallback(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Text, int sid, float speed, OfflineTtsCallback callback); } } diff --git a/scripts/dotnet/OfflineTtsGeneratedAudio.cs b/scripts/dotnet/OfflineTtsGeneratedAudio.cs index ad7540f58..2b521649c 100644 --- a/scripts/dotnet/OfflineTtsGeneratedAudio.cs +++ b/scripts/dotnet/OfflineTtsGeneratedAudio.cs @@ -1,6 +1,7 @@ /// Copyright (c) 2024.5 by 东风破 using System; using System.Runtime.InteropServices; +using System.Text; namespace SherpaOnnx { @@ -14,7 +15,8 @@ public OfflineTtsGeneratedAudio(IntPtr p) public bool SaveToWaveFile(String filename) { Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); - int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, filename); + byte[] utf8Filename = Encoding.UTF8.GetBytes(filename); + int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8Filename); return status == 1; } @@ -84,6 +86,6 @@ public float[] Samples 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); + private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Filename); } } diff --git a/scripts/dotnet/SpeakerEmbeddingManager.cs b/scripts/dotnet/SpeakerEmbeddingManager.cs index ee3dfa3dd..85b4812a7 100644 --- a/scripts/dotnet/SpeakerEmbeddingManager.cs +++ b/scripts/dotnet/SpeakerEmbeddingManager.cs @@ -17,7 +17,8 @@ public SpeakerEmbeddingManager(int dim) public bool Add(string name, float[] v) { - return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, name, v) == 1; + byte[] utf8Name = Encoding.UTF8.GetBytes(name); + return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, utf8Name, v) == 1; } public bool Add(string name, ICollection v_list) @@ -31,12 +32,14 @@ public bool Add(string name, ICollection v_list) i += _dim; } - return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, name, v, n) == 1; + byte[] utf8Name = Encoding.UTF8.GetBytes(name); + return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, utf8Name, v, n) == 1; } public bool Remove(string name) { - return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, name) == 1; + byte[] utf8Name = Encoding.UTF8.GetBytes(name); + return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, utf8Name) == 1; } public string Search(float[] v, float threshold) @@ -73,12 +76,14 @@ public string Search(float[] v, float threshold) public bool Verify(string name, float[] v, float threshold) { - return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, name, v, threshold) == 1; + byte[] utf8Name = Encoding.UTF8.GetBytes(name); + return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, utf8Name, v, threshold) == 1; } public bool Contains(string name) { - return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, name) == 1; + byte[] utf8Name = Encoding.UTF8.GetBytes(name); + return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, utf8Name) == 1; } public string[] GetAllSpeakers() @@ -155,13 +160,13 @@ public int NumSpeakers private static extern void SherpaOnnxDestroySpeakerEmbeddingManager(IntPtr handle); [DllImport(Dll.Filename)] - private static extern int SherpaOnnxSpeakerEmbeddingManagerAdd(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v); + private static extern int SherpaOnnxSpeakerEmbeddingManagerAdd(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Name, float[] v); [DllImport(Dll.Filename)] - private static extern int SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v, int n); + private static extern int SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Name, float[] v, int n); [DllImport(Dll.Filename)] - private static extern int SherpaOnnxSpeakerEmbeddingManagerRemove(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name); + private static extern int SherpaOnnxSpeakerEmbeddingManagerRemove(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Name); [DllImport(Dll.Filename)] private static extern IntPtr SherpaOnnxSpeakerEmbeddingManagerSearch(IntPtr handle, float[] v, float threshold); @@ -170,10 +175,10 @@ public int NumSpeakers private static extern void SherpaOnnxSpeakerEmbeddingManagerFreeSearch(IntPtr p); [DllImport(Dll.Filename)] - private static extern int SherpaOnnxSpeakerEmbeddingManagerVerify(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v, float threshold); + private static extern int SherpaOnnxSpeakerEmbeddingManagerVerify(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Name, float[] v, float threshold); [DllImport(Dll.Filename)] - private static extern int SherpaOnnxSpeakerEmbeddingManagerContains(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name); + private static extern int SherpaOnnxSpeakerEmbeddingManagerContains(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Name); [DllImport(Dll.Filename)] private static extern int SherpaOnnxSpeakerEmbeddingManagerNumSpeakers(IntPtr handle); diff --git a/scripts/dotnet/examples/Common.csproj b/scripts/dotnet/examples/Common.csproj index bbbef478b..868c1470f 100644 --- a/scripts/dotnet/examples/Common.csproj +++ b/scripts/dotnet/examples/Common.csproj @@ -7,7 +7,7 @@ - + diff --git a/sherpa-onnx/flutter/CHANGELOG.md b/sherpa-onnx/flutter/CHANGELOG.md index 200b6497a..96a6040b4 100644 --- a/sherpa-onnx/flutter/CHANGELOG.md +++ b/sherpa-onnx/flutter/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.10.2 + +* Fix passing C# string to C++ + ## 1.10.1 * Enable to stop TTS generation