SpokenLanguageIdentificationResult.cs
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// Copyright (c) 2024.5 by 东风破
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SherpaOnnx
{
public class SpokenLanguageIdentificationResult
{
public SpokenLanguageIdentificationResult(IntPtr handle)
{
Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
// PtrToStringUTF8() requires .net standard 2.1
// _text = Marshal.PtrToStringUTF8(impl.Text);
int length = 0;
unsafe
{
byte* buffer = (byte*)impl.Lang;
while (*buffer != 0)
{
++buffer;
length += 1;
}
}
byte[] stringBuffer = new byte[length];
Marshal.Copy(impl.Lang, stringBuffer, 0, length);
_lang = Encoding.UTF8.GetString(stringBuffer);
}
[StructLayout(LayoutKind.Sequential)]
struct Impl
{
public IntPtr Lang;
}
private String _lang;
public String Lang => _lang;
}
}