Fangjun Kuang
Committed by GitHub

Fix passing strings from C# to C. (#1701)

See also
https://github.com/k2-fsa/sherpa-onnx/issues/1695#issuecomment-2585725190

We need to place a 0 at the end of the buffer.
@@ -26,7 +26,10 @@ namespace SherpaOnnx @@ -26,7 +26,10 @@ namespace SherpaOnnx
26 public OnlineStream CreateStream(string keywords) 26 public OnlineStream CreateStream(string keywords)
27 { 27 {
28 byte[] utf8Bytes = Encoding.UTF8.GetBytes(keywords); 28 byte[] utf8Bytes = Encoding.UTF8.GetBytes(keywords);
29 - IntPtr p = SherpaOnnxCreateKeywordStreamWithKeywords(_handle.Handle, utf8Bytes); 29 + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator
  30 + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length);
  31 + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator
  32 + IntPtr p = SherpaOnnxCreateKeywordStreamWithKeywords(_handle.Handle, utf8BytesWithNull);
30 return new OnlineStream(p); 33 return new OnlineStream(p);
31 } 34 }
32 35
@@ -17,8 +17,11 @@ namespace SherpaOnnx @@ -17,8 +17,11 @@ namespace SherpaOnnx
17 public String AddPunct(String text) 17 public String AddPunct(String text)
18 { 18 {
19 byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); 19 byte[] utf8Bytes = Encoding.UTF8.GetBytes(text);
  20 + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator
  21 + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length);
  22 + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator
20 23
21 - IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, utf8Bytes); 24 + IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, utf8BytesWithNull);
22 25
23 string s = ""; 26 string s = "";
24 int length = 0; 27 int length = 0;
@@ -19,14 +19,20 @@ namespace SherpaOnnx @@ -19,14 +19,20 @@ namespace SherpaOnnx
19 public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId) 19 public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId)
20 { 20 {
21 byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); 21 byte[] utf8Bytes = Encoding.UTF8.GetBytes(text);
22 - IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, utf8Bytes, speakerId, speed); 22 + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator
  23 + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length);
  24 + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator
  25 + IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, utf8BytesWithNull, speakerId, speed);
23 return new OfflineTtsGeneratedAudio(p); 26 return new OfflineTtsGeneratedAudio(p);
24 } 27 }
25 28
26 public OfflineTtsGeneratedAudio GenerateWithCallback(String text, float speed, int speakerId, OfflineTtsCallback callback) 29 public OfflineTtsGeneratedAudio GenerateWithCallback(String text, float speed, int speakerId, OfflineTtsCallback callback)
27 { 30 {
28 byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); 31 byte[] utf8Bytes = Encoding.UTF8.GetBytes(text);
29 - IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, utf8Bytes, speakerId, speed, callback); 32 + byte[] utf8BytesWithNull = new byte[utf8Bytes.Length + 1]; // +1 for null terminator
  33 + Array.Copy(utf8Bytes, utf8BytesWithNull, utf8Bytes.Length);
  34 + utf8BytesWithNull[utf8Bytes.Length] = 0; // Null terminator
  35 + IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, utf8BytesWithNull, speakerId, speed, callback);
30 return new OfflineTtsGeneratedAudio(p); 36 return new OfflineTtsGeneratedAudio(p);
31 } 37 }
32 38
@@ -16,7 +16,10 @@ namespace SherpaOnnx @@ -16,7 +16,10 @@ namespace SherpaOnnx
16 { 16 {
17 Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); 17 Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
18 byte[] utf8Filename = Encoding.UTF8.GetBytes(filename); 18 byte[] utf8Filename = Encoding.UTF8.GetBytes(filename);
19 - int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8Filename); 19 + byte[] utf8FilenameWithNull = new byte[utf8Filename.Length + 1]; // +1 for null terminator
  20 + Array.Copy(utf8Filename, utf8FilenameWithNull, utf8Filename.Length);
  21 + utf8FilenameWithNull[utf8Filename.Length] = 0; // Null terminator
  22 + int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8FilenameWithNull);
20 return status == 1; 23 return status == 1;
21 } 24 }
22 25
@@ -18,7 +18,10 @@ namespace SherpaOnnx @@ -18,7 +18,10 @@ namespace SherpaOnnx
18 public bool Add(string name, float[] v) 18 public bool Add(string name, float[] v)
19 { 19 {
20 byte[] utf8Name = Encoding.UTF8.GetBytes(name); 20 byte[] utf8Name = Encoding.UTF8.GetBytes(name);
21 - return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, utf8Name, v) == 1; 21 + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator
  22 + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length);
  23 + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator
  24 + return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, utf8NameWithNull, v) == 1;
22 } 25 }
23 26
24 public bool Add(string name, ICollection<float[]> v_list) 27 public bool Add(string name, ICollection<float[]> v_list)
@@ -33,13 +36,19 @@ namespace SherpaOnnx @@ -33,13 +36,19 @@ namespace SherpaOnnx
33 } 36 }
34 37
35 byte[] utf8Name = Encoding.UTF8.GetBytes(name); 38 byte[] utf8Name = Encoding.UTF8.GetBytes(name);
36 - return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, utf8Name, v, n) == 1; 39 + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator
  40 + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length);
  41 + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator
  42 + return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, utf8NameWithNull, v, n) == 1;
37 } 43 }
38 44
39 public bool Remove(string name) 45 public bool Remove(string name)
40 { 46 {
41 byte[] utf8Name = Encoding.UTF8.GetBytes(name); 47 byte[] utf8Name = Encoding.UTF8.GetBytes(name);
42 - return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, utf8Name) == 1; 48 + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator
  49 + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length);
  50 + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator
  51 + return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, utf8NameWithNull) == 1;
43 } 52 }
44 53
45 public string Search(float[] v, float threshold) 54 public string Search(float[] v, float threshold)
@@ -77,13 +86,19 @@ namespace SherpaOnnx @@ -77,13 +86,19 @@ namespace SherpaOnnx
77 public bool Verify(string name, float[] v, float threshold) 86 public bool Verify(string name, float[] v, float threshold)
78 { 87 {
79 byte[] utf8Name = Encoding.UTF8.GetBytes(name); 88 byte[] utf8Name = Encoding.UTF8.GetBytes(name);
80 - return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, utf8Name, v, threshold) == 1; 89 + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator
  90 + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length);
  91 + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator
  92 + return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, utf8NameWithNull, v, threshold) == 1;
81 } 93 }
82 94
83 public bool Contains(string name) 95 public bool Contains(string name)
84 { 96 {
85 byte[] utf8Name = Encoding.UTF8.GetBytes(name); 97 byte[] utf8Name = Encoding.UTF8.GetBytes(name);
86 - return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, utf8Name) == 1; 98 + byte[] utf8NameWithNull = new byte[utf8Name.Length + 1]; // +1 for null terminator
  99 + Array.Copy(utf8Name, utf8NameWithNull, utf8Name.Length);
  100 + utf8NameWithNull[utf8Name.Length] = 0; // Null terminator
  101 + return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, utf8NameWithNull) == 1;
87 } 102 }
88 103
89 public string[] GetAllSpeakers() 104 public string[] GetAllSpeakers()