Add timestamps and tokens for .Net's online models. (#690)

This commit is contained in:
Fangjun Kuang
2024-03-23 18:51:56 +08:00
committed by GitHub
parent e6da2c5556
commit 1952772654
26 changed files with 135 additions and 73 deletions

View File

@@ -185,23 +185,82 @@ namespace SherpaOnnx
while (*buffer != 0)
{
++buffer;
length += 1;
}
length = (int)(buffer - (byte*)impl.Text);
}
byte[] stringBuffer = new byte[length];
Marshal.Copy(impl.Text, stringBuffer, 0, length);
_text = Encoding.UTF8.GetString(stringBuffer);
_tokens = new String[impl.Count];
unsafe
{
byte* buf = (byte*)impl.Tokens;
for (int i = 0; i < impl.Count; i++)
{
length = 0;
byte* start = buf;
while (*buf != 0)
{
++buf;
length += 1;
}
++buf;
stringBuffer = new byte[length];
fixed (byte* pTarget = stringBuffer)
{
for (int k = 0; k < length; k++)
{
pTarget[k] = start[k];
}
}
_tokens[i] = Encoding.UTF8.GetString(stringBuffer);
}
}
unsafe
{
float* t = (float*)impl.Timestamps;
if (t != null)
{
_timestamps = new float[impl.Count];
fixed (float* pTarget = _timestamps)
{
for (int i = 0; i < impl.Count; i++)
{
pTarget[i] = t[i];
}
}
}
else
{
_timestamps = Array.Empty<float>();
}
}
}
[StructLayout(LayoutKind.Sequential)]
struct Impl
{
public IntPtr Text;
public IntPtr Tokens;
public IntPtr TokensArr;
public IntPtr Timestamps;
public int Count;
}
private String _text;
public String Text => _text;
private String[] _tokens;
public String[] Tokens => _tokens;
private float[] _timestamps;
public float[] Timestamps => _timestamps;
}
public class OnlineStream : IDisposable