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
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);
}
... ...
... ... @@ -17,8 +17,11 @@ namespace SherpaOnnx
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;
... ...
... ... @@ -19,14 +19,20 @@ namespace SherpaOnnx
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);
}
... ...
... ... @@ -16,7 +16,10 @@ namespace SherpaOnnx
{
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;
}
... ...
... ... @@ -18,7 +18,10 @@ namespace SherpaOnnx
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<float[]> v_list)
... ... @@ -33,13 +36,19 @@ namespace SherpaOnnx
}
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 @@ namespace SherpaOnnx
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()
... ...