Skip to content

Commit

Permalink
Fix passing strings from C# to C. (#1701)
Browse files Browse the repository at this point in the history
See also
#1695 (comment)

We need to place a 0 at the end of the buffer.
  • Loading branch information
csukuangfj authored Jan 13, 2025
1 parent ecc6538 commit 0d20558
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
5 changes: 4 additions & 1 deletion scripts/dotnet/KeywordSpotter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion scripts/dotnet/OfflinePunctuation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions scripts/dotnet/OfflineTts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion scripts/dotnet/OfflineTtsGeneratedAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
25 changes: 20 additions & 5 deletions scripts/dotnet/SpeakerEmbeddingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<float[]> v_list)
Expand All @@ -33,13 +36,19 @@ public bool Add(string name, ICollection<float[]> 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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0d20558

Please sign in to comment.