Skip to content

Commit

Permalink
Fix passing C# string to C++ (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jun 25, 2024
1 parent a3bac19 commit 5cce159
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux-jni.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,7 +153,7 @@ jobs:
git push https://csukuangfj:[email protected]/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
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion dart-api-examples/non-streaming-asr/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion dart-api-examples/streaming-asr/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion dart-api-examples/tts/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion dart-api-examples/vad/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion dotnet-examples/Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.10.1" />
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
</ItemGroup>

</Project>
Empty file modified dotnet-examples/offline-punctuation/run.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion nodejs-addon-examples/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"sherpa-onnx-node": "^1.10.1"
"sherpa-onnx-node": "^1.10.2"
}
}
3 changes: 2 additions & 1 deletion scripts/dotnet/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
all
macos
macos-arm64
macos-x64
linux
windows
windows-x64
Expand Down
6 changes: 4 additions & 2 deletions scripts/dotnet/OfflinePunctuation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions scripts/dotnet/OfflineTts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Copyright (c) 2024.5 by 东风破
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace SherpaOnnx
{
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
6 changes: 4 additions & 2 deletions scripts/dotnet/OfflineTtsGeneratedAudio.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Copyright (c) 2024.5 by 东风破
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace SherpaOnnx
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
25 changes: 15 additions & 10 deletions scripts/dotnet/SpeakerEmbeddingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<float[]> v_list)
Expand All @@ -31,12 +32,14 @@ public bool Add(string name, ICollection<float[]> 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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion scripts/dotnet/examples/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.10.1" />
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions sherpa-onnx/flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.10.2

* Fix passing C# string to C++

## 1.10.1

* Enable to stop TTS generation
Expand Down

0 comments on commit 5cce159

Please sign in to comment.