Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing strings from C# to C. #1701

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading