diff --git a/scripts/dotnet/KeywordSpotter.cs b/scripts/dotnet/KeywordSpotter.cs index 9c76acefc..6f19fc945 100644 --- a/scripts/dotnet/KeywordSpotter.cs +++ b/scripts/dotnet/KeywordSpotter.cs @@ -26,7 +26,10 @@ public OnlineStream CreateStream() public OnlineStream CreateStream(string keywords) { byte[] utf8Bytes = Encoding.UTF8.GetBytes(keywords); - IntPtr p = SherpaOnnxCreateKeywordStreamWithKeywords(_handle.Handle, utf8Bytes); + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length); + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator + IntPtr p = SherpaOnnxCreateKeywordStreamWithKeywords(_handle.Handle, utf8BytesWithNull); return new OnlineStream(p); } diff --git a/scripts/dotnet/OfflinePunctuation.cs b/scripts/dotnet/OfflinePunctuation.cs index 4b39c3d32..4730bb6a0 100644 --- a/scripts/dotnet/OfflinePunctuation.cs +++ b/scripts/dotnet/OfflinePunctuation.cs @@ -17,8 +17,11 @@ public OfflinePunctuation(OfflinePunctuationConfig config) public String AddPunct(String text) { byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length); + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator - IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, utf8Bytes); + IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, utf8BytesWithNull); string s = ""; int length = 0; diff --git a/scripts/dotnet/OfflineTts.cs b/scripts/dotnet/OfflineTts.cs index 6e36d816f..e4d29733c 100644 --- a/scripts/dotnet/OfflineTts.cs +++ b/scripts/dotnet/OfflineTts.cs @@ -19,14 +19,20 @@ public OfflineTts(OfflineTtsConfig config) public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId) { byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); - IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, utf8Bytes, speakerId, speed); + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length); + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator + IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, utf8BytesWithNull, speakerId, speed); return new OfflineTtsGeneratedAudio(p); } public OfflineTtsGeneratedAudio GenerateWithCallback(String text, float speed, int speakerId, OfflineTtsCallback callback) { byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); - IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, utf8Bytes, speakerId, speed, callback); + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length); + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator + IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, utf8BytesWithNull, speakerId, speed, callback); return new OfflineTtsGeneratedAudio(p); } diff --git a/scripts/dotnet/OfflineTtsGeneratedAudio.cs b/scripts/dotnet/OfflineTtsGeneratedAudio.cs index 2b521649c..282820790 100644 --- a/scripts/dotnet/OfflineTtsGeneratedAudio.cs +++ b/scripts/dotnet/OfflineTtsGeneratedAudio.cs @@ -16,7 +16,10 @@ public bool SaveToWaveFile(String filename) { Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); byte[] utf8Filename = Encoding.UTF8.GetBytes(filename); - int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8Filename); + byte[] utf8FilenameWithNull = new byte[utf8Filename.Length + 1]; // +1 for null terminator + Array.Copy(utf8Filename, utf8FilenameWithNull, utf8Filename.Length); + utf8FilenameWithNull[utf8Filename.Length] = 0; // Null terminator + int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8FilenameWithNull); return status == 1; } diff --git a/scripts/dotnet/SpeakerEmbeddingManager.cs b/scripts/dotnet/SpeakerEmbeddingManager.cs index 85b4812a7..d9c4215c3 100644 --- a/scripts/dotnet/SpeakerEmbeddingManager.cs +++ b/scripts/dotnet/SpeakerEmbeddingManager.cs @@ -18,7 +18,10 @@ public SpeakerEmbeddingManager(int dim) public bool Add(string name, float[] v) { byte[] utf8Name = Encoding.UTF8.GetBytes(name); - return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, utf8Name, v) == 1; + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length); + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator + return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, utf8NameWithNull, v) == 1; } public bool Add(string name, ICollection v_list) @@ -33,13 +36,19 @@ public bool Add(string name, ICollection v_list) } byte[] utf8Name = Encoding.UTF8.GetBytes(name); - return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, utf8Name, v, n) == 1; + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length); + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator + return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, utf8NameWithNull, v, n) == 1; } public bool Remove(string name) { byte[] utf8Name = Encoding.UTF8.GetBytes(name); - return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, utf8Name) == 1; + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length); + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator + return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, utf8NameWithNull) == 1; } public string Search(float[] v, float threshold) @@ -77,13 +86,19 @@ public string Search(float[] v, float threshold) public bool Verify(string name, float[] v, float threshold) { byte[] utf8Name = Encoding.UTF8.GetBytes(name); - return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, utf8Name, v, threshold) == 1; + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length); + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator + return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, utf8NameWithNull, v, threshold) == 1; } public bool Contains(string name) { byte[] utf8Name = Encoding.UTF8.GetBytes(name); - return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, utf8Name) == 1; + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length); + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator + return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, utf8NameWithNull) == 1; } public string[] GetAllSpeakers()