Add C# API for Moonshine models. (#1483)
* Also, return timestamps for non-streaming ASR.
This commit is contained in:
@@ -24,6 +24,7 @@ namespace SherpaOnnx
|
||||
BpeVocab = "";
|
||||
TeleSpeechCtc = "";
|
||||
SenseVoice = new OfflineSenseVoiceModelConfig();
|
||||
Moonshine = new OfflineMoonshineModelConfig();
|
||||
}
|
||||
public OfflineTransducerModelConfig Transducer;
|
||||
public OfflineParaformerModelConfig Paraformer;
|
||||
@@ -54,5 +55,6 @@ namespace SherpaOnnx
|
||||
public string TeleSpeechCtc;
|
||||
|
||||
public OfflineSenseVoiceModelConfig SenseVoice;
|
||||
public OfflineMoonshineModelConfig Moonshine;
|
||||
}
|
||||
}
|
||||
|
||||
29
scripts/dotnet/OfflineMoonshineModelConfig.cs
Normal file
29
scripts/dotnet/OfflineMoonshineModelConfig.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SherpaOnnx
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OfflineMoonshineModelConfig
|
||||
{
|
||||
public OfflineMoonshineModelConfig()
|
||||
{
|
||||
Preprocessor = "";
|
||||
Encoder = "";
|
||||
UncachedDecoder = "";
|
||||
CachedDecoder = "";
|
||||
}
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Preprocessor;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string Encoder;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string UncachedDecoder;
|
||||
|
||||
[MarshalAs(UnmanagedType.LPStr)]
|
||||
public string CachedDecoder;
|
||||
}
|
||||
}
|
||||
@@ -31,17 +31,70 @@ namespace SherpaOnnx
|
||||
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
|
||||
{
|
||||
if (impl.Timestamps != IntPtr.Zero)
|
||||
{
|
||||
float *t = (float*)impl.Timestamps;
|
||||
_timestamps = new float[impl.Count];
|
||||
fixed (float* f = _timestamps)
|
||||
{
|
||||
for (int k = 0; k < impl.Count; k++)
|
||||
{
|
||||
f[k] = t[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct Impl
|
||||
{
|
||||
public IntPtr Text;
|
||||
public IntPtr Timestamps;
|
||||
public int Count;
|
||||
public IntPtr Tokens;
|
||||
}
|
||||
|
||||
private String _text;
|
||||
public String Text => _text;
|
||||
|
||||
private String[] _tokens;
|
||||
public String[] Tokens => _tokens;
|
||||
|
||||
private float[] _timestamps;
|
||||
public float[] Timestamps => _timestamps;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user