东风破
Committed by GitHub

Split online.cs and offline.csFile (#941)

Co-authored-by: 东风破 <birdfishs@163.com>
正在显示 37 个修改的文件 包含 1813 行增加1440 行删除
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 + internal static class Dll
  14 + {
  15 + public const string Filename = "sherpa-onnx-c-api";
  16 + }
  17 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 + /// It expects 16 kHz 16-bit single channel wave format.
  14 + [StructLayout(LayoutKind.Sequential)]
  15 + public struct FeatureConfig
  16 + {
  17 + public FeatureConfig()
  18 + {
  19 + SampleRate = 16000;
  20 + FeatureDim = 80;
  21 + }
  22 + /// Sample rate of the input data. MUST match the one expected
  23 + /// by the model. For instance, it should be 16000 for models provided
  24 + /// by us.
  25 + public int SampleRate;
  26 +
  27 + /// Feature dimension of the model.
  28 + /// For instance, it should be 80 for models provided by us.
  29 + public int FeatureDim;
  30 + }
  31 +
  32 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + [StructLayout(LayoutKind.Sequential)]
  13 + public struct OfflineLMConfig
  14 + {
  15 + public OfflineLMConfig()
  16 + {
  17 + Model = "";
  18 + Scale = 0.5F;
  19 + }
  20 + [MarshalAs(UnmanagedType.LPStr)]
  21 + public string Model;
  22 +
  23 + public float Scale;
  24 + }
  25 +
  26 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + [StructLayout(LayoutKind.Sequential)]
  13 + public struct OfflineModelConfig
  14 + {
  15 + public OfflineModelConfig()
  16 + {
  17 + Transducer = new OfflineTransducerModelConfig();
  18 + Paraformer = new OfflineParaformerModelConfig();
  19 + NeMoCtc = new OfflineNemoEncDecCtcModelConfig();
  20 + Whisper = new OfflineWhisperModelConfig();
  21 + Tdnn = new OfflineTdnnModelConfig();
  22 + Tokens = "";
  23 + NumThreads = 1;
  24 + Debug = 0;
  25 + Provider = "cpu";
  26 + ModelType = "";
  27 + }
  28 + public OfflineTransducerModelConfig Transducer;
  29 + public OfflineParaformerModelConfig Paraformer;
  30 + public OfflineNemoEncDecCtcModelConfig NeMoCtc;
  31 + public OfflineWhisperModelConfig Whisper;
  32 + public OfflineTdnnModelConfig Tdnn;
  33 +
  34 + [MarshalAs(UnmanagedType.LPStr)]
  35 + public string Tokens;
  36 +
  37 + public int NumThreads;
  38 +
  39 + public int Debug;
  40 +
  41 + [MarshalAs(UnmanagedType.LPStr)]
  42 + public string Provider;
  43 +
  44 + [MarshalAs(UnmanagedType.LPStr)]
  45 + public string ModelType;
  46 + }
  47 +
  48 +
  49 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + [StructLayout(LayoutKind.Sequential)]
  13 + public struct OfflineNemoEncDecCtcModelConfig
  14 + {
  15 + public OfflineNemoEncDecCtcModelConfig()
  16 + {
  17 + Model = "";
  18 + }
  19 + [MarshalAs(UnmanagedType.LPStr)]
  20 + public string Model;
  21 + }
  22 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct OfflineParaformerModelConfig
  13 + {
  14 + public OfflineParaformerModelConfig()
  15 + {
  16 + Model = "";
  17 + }
  18 + [MarshalAs(UnmanagedType.LPStr)]
  19 + public string Model;
  20 + }
  21 +
  22 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public class OfflineRecognizer : IDisposable
  12 + {
  13 + public OfflineRecognizer(OfflineRecognizerConfig config)
  14 + {
  15 + IntPtr h = CreateOfflineRecognizer(ref config);
  16 + _handle = new HandleRef(this, h);
  17 + }
  18 +
  19 + public OfflineStream CreateStream()
  20 + {
  21 + IntPtr p = CreateOfflineStream(_handle.Handle);
  22 + return new OfflineStream(p);
  23 + }
  24 +
  25 + public void Decode(OfflineStream stream)
  26 + {
  27 + Decode(_handle.Handle, stream.Handle);
  28 + }
  29 +
  30 + // The caller should ensure all passed streams are ready for decoding.
  31 + public void Decode(IEnumerable<OfflineStream> streams)
  32 + {
  33 + IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();
  34 + Decode(_handle.Handle, ptrs, ptrs.Length);
  35 + }
  36 +
  37 + public void Dispose()
  38 + {
  39 + Cleanup();
  40 + // Prevent the object from being placed on the
  41 + // finalization queue
  42 + System.GC.SuppressFinalize(this);
  43 + }
  44 +
  45 + ~OfflineRecognizer()
  46 + {
  47 + Cleanup();
  48 + }
  49 +
  50 + private void Cleanup()
  51 + {
  52 + DestroyOfflineRecognizer(_handle.Handle);
  53 +
  54 + // Don't permit the handle to be used again.
  55 + _handle = new HandleRef(this, IntPtr.Zero);
  56 + }
  57 +
  58 + private HandleRef _handle;
  59 +
  60 + [DllImport(Dll.Filename)]
  61 + private static extern IntPtr CreateOfflineRecognizer(ref OfflineRecognizerConfig config);
  62 +
  63 + [DllImport(Dll.Filename)]
  64 + private static extern void DestroyOfflineRecognizer(IntPtr handle);
  65 +
  66 + [DllImport(Dll.Filename)]
  67 + private static extern IntPtr CreateOfflineStream(IntPtr handle);
  68 +
  69 + [DllImport(Dll.Filename, EntryPoint = "DecodeOfflineStream")]
  70 + private static extern void Decode(IntPtr handle, IntPtr stream);
  71 +
  72 + [DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOfflineStreams")]
  73 + private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
  74 + }
  75 +
  76 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + [StructLayout(LayoutKind.Sequential)]
  13 + public struct OfflineRecognizerConfig
  14 + {
  15 + public OfflineRecognizerConfig()
  16 + {
  17 + FeatConfig = new FeatureConfig();
  18 + ModelConfig = new OfflineModelConfig();
  19 + LmConfig = new OfflineLMConfig();
  20 +
  21 + DecodingMethod = "greedy_search";
  22 + MaxActivePaths = 4;
  23 + HotwordsFile = "";
  24 + HotwordsScore = 1.5F;
  25 +
  26 + }
  27 + public FeatureConfig FeatConfig;
  28 + public OfflineModelConfig ModelConfig;
  29 + public OfflineLMConfig LmConfig;
  30 +
  31 + [MarshalAs(UnmanagedType.LPStr)]
  32 + public string DecodingMethod;
  33 +
  34 + public int MaxActivePaths;
  35 +
  36 + [MarshalAs(UnmanagedType.LPStr)]
  37 + public string HotwordsFile;
  38 +
  39 + public float HotwordsScore;
  40 + }
  41 +
  42 +
  43 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + public class OfflineRecognizerResult
  13 + {
  14 + public OfflineRecognizerResult(IntPtr handle)
  15 + {
  16 + Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
  17 +
  18 + // PtrToStringUTF8() requires .net standard 2.1
  19 + // _text = Marshal.PtrToStringUTF8(impl.Text);
  20 +
  21 + int length = 0;
  22 +
  23 + unsafe
  24 + {
  25 + byte* buffer = (byte*)impl.Text;
  26 + while (*buffer != 0)
  27 + {
  28 + ++buffer;
  29 + length += 1;
  30 + }
  31 + }
  32 +
  33 + byte[] stringBuffer = new byte[length];
  34 + Marshal.Copy(impl.Text, stringBuffer, 0, length);
  35 + _text = Encoding.UTF8.GetString(stringBuffer);
  36 + }
  37 +
  38 + [StructLayout(LayoutKind.Sequential)]
  39 + struct Impl
  40 + {
  41 + public IntPtr Text;
  42 + }
  43 +
  44 + private String _text;
  45 + public String Text => _text;
  46 + }
  47 +
  48 +
  49 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + public class OfflineStream : IDisposable
  13 + {
  14 + public OfflineStream(IntPtr p)
  15 + {
  16 + _handle = new HandleRef(this, p);
  17 + }
  18 +
  19 + public void AcceptWaveform(int sampleRate, float[] samples)
  20 + {
  21 + AcceptWaveform(Handle, sampleRate, samples, samples.Length);
  22 + }
  23 +
  24 + public OfflineRecognizerResult Result
  25 + {
  26 + get
  27 + {
  28 + IntPtr h = GetResult(_handle.Handle);
  29 + OfflineRecognizerResult result = new OfflineRecognizerResult(h);
  30 + DestroyResult(h);
  31 + return result;
  32 + }
  33 + }
  34 +
  35 + ~OfflineStream()
  36 + {
  37 + Cleanup();
  38 + }
  39 +
  40 + public void Dispose()
  41 + {
  42 + Cleanup();
  43 + // Prevent the object from being placed on the
  44 + // finalization queue
  45 + System.GC.SuppressFinalize(this);
  46 + }
  47 +
  48 + private void Cleanup()
  49 + {
  50 + DestroyOfflineStream(Handle);
  51 +
  52 + // Don't permit the handle to be used again.
  53 + _handle = new HandleRef(this, IntPtr.Zero);
  54 + }
  55 +
  56 + private HandleRef _handle;
  57 + public IntPtr Handle => _handle.Handle;
  58 +
  59 + [DllImport(Dll.Filename)]
  60 + private static extern void DestroyOfflineStream(IntPtr handle);
  61 +
  62 + [DllImport(Dll.Filename, EntryPoint = "AcceptWaveformOffline")]
  63 + private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);
  64 +
  65 + [DllImport(Dll.Filename, EntryPoint = "GetOfflineStreamResult")]
  66 + private static extern IntPtr GetResult(IntPtr handle);
  67 +
  68 + [DllImport(Dll.Filename, EntryPoint = "DestroyOfflineRecognizerResult")]
  69 + private static extern void DestroyResult(IntPtr handle);
  70 + }
  71 +
  72 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct OfflineTdnnModelConfig
  13 + {
  14 + public OfflineTdnnModelConfig()
  15 + {
  16 + Model = "";
  17 + }
  18 + [MarshalAs(UnmanagedType.LPStr)]
  19 + public string Model;
  20 + }
  21 +
  22 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct OfflineTransducerModelConfig
  13 + {
  14 + public OfflineTransducerModelConfig()
  15 + {
  16 + Encoder = "";
  17 + Decoder = "";
  18 + Joiner = "";
  19 + }
  20 + [MarshalAs(UnmanagedType.LPStr)]
  21 + public string Encoder;
  22 +
  23 + [MarshalAs(UnmanagedType.LPStr)]
  24 + public string Decoder;
  25 +
  26 + [MarshalAs(UnmanagedType.LPStr)]
  27 + public string Joiner;
  28 + }
  29 +
  30 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + // IntPtr is actuallly a `const float*` from C++
  12 + public delegate void OfflineTtsCallback(IntPtr samples, int n);
  13 +
  14 + public class OfflineTts : IDisposable
  15 + {
  16 + public OfflineTts(OfflineTtsConfig config)
  17 + {
  18 + IntPtr h = SherpaOnnxCreateOfflineTts(ref config);
  19 + _handle = new HandleRef(this, h);
  20 + }
  21 +
  22 + public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId)
  23 + {
  24 + IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, text, speakerId, speed);
  25 + return new OfflineTtsGeneratedAudio(p);
  26 + }
  27 +
  28 + public OfflineTtsGeneratedAudio GenerateWithCallback(String text, float speed, int speakerId, OfflineTtsCallback callback)
  29 + {
  30 + IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, text, speakerId, speed, callback);
  31 + return new OfflineTtsGeneratedAudio(p);
  32 + }
  33 +
  34 + public void Dispose()
  35 + {
  36 + Cleanup();
  37 + // Prevent the object from being placed on the
  38 + // finalization queue
  39 + System.GC.SuppressFinalize(this);
  40 + }
  41 +
  42 + ~OfflineTts()
  43 + {
  44 + Cleanup();
  45 + }
  46 +
  47 + private void Cleanup()
  48 + {
  49 + SherpaOnnxDestroyOfflineTts(_handle.Handle);
  50 +
  51 + // Don't permit the handle to be used again.
  52 + _handle = new HandleRef(this, IntPtr.Zero);
  53 + }
  54 +
  55 + private HandleRef _handle;
  56 +
  57 + public int SampleRate
  58 + {
  59 + get
  60 + {
  61 + return SherpaOnnxOfflineTtsSampleRate(_handle.Handle);
  62 + }
  63 + }
  64 +
  65 + public int NumSpeakers
  66 + {
  67 + get
  68 + {
  69 + return SherpaOnnxOfflineTtsNumSpeakers(_handle.Handle);
  70 + }
  71 + }
  72 +
  73 + [DllImport(Dll.Filename)]
  74 + private static extern IntPtr SherpaOnnxCreateOfflineTts(ref OfflineTtsConfig config);
  75 +
  76 + [DllImport(Dll.Filename)]
  77 + private static extern void SherpaOnnxDestroyOfflineTts(IntPtr handle);
  78 +
  79 + [DllImport(Dll.Filename)]
  80 + private static extern int SherpaOnnxOfflineTtsSampleRate(IntPtr handle);
  81 +
  82 + [DllImport(Dll.Filename)]
  83 + private static extern int SherpaOnnxOfflineTtsNumSpeakers(IntPtr handle);
  84 +
  85 + [DllImport(Dll.Filename)]
  86 + private static extern IntPtr SherpaOnnxOfflineTtsGenerate(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed);
  87 +
  88 + [DllImport(Dll.Filename, CallingConvention = CallingConvention.Cdecl)]
  89 + private static extern IntPtr SherpaOnnxOfflineTtsGenerateWithCallback(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed, OfflineTtsCallback callback);
  90 + }
  91 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct OfflineTtsConfig
  13 + {
  14 + public OfflineTtsConfig()
  15 + {
  16 + Model = new OfflineTtsModelConfig();
  17 + RuleFsts = "";
  18 + MaxNumSentences = 1;
  19 + RuleFars = "";
  20 + }
  21 + public OfflineTtsModelConfig Model;
  22 +
  23 + [MarshalAs(UnmanagedType.LPStr)]
  24 + public string RuleFsts;
  25 +
  26 + public int MaxNumSentences;
  27 +
  28 + [MarshalAs(UnmanagedType.LPStr)]
  29 + public string RuleFars;
  30 + }
  31 +
  32 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public class OfflineTtsGeneratedAudio
  12 + {
  13 + public OfflineTtsGeneratedAudio(IntPtr p)
  14 + {
  15 + _handle = new HandleRef(this, p);
  16 + }
  17 +
  18 + public bool SaveToWaveFile(String filename)
  19 + {
  20 + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
  21 + int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, filename);
  22 + return status == 1;
  23 + }
  24 +
  25 + ~OfflineTtsGeneratedAudio()
  26 + {
  27 + Cleanup();
  28 + }
  29 +
  30 + public void Dispose()
  31 + {
  32 + Cleanup();
  33 + // Prevent the object from being placed on the
  34 + // finalization queue
  35 + System.GC.SuppressFinalize(this);
  36 + }
  37 +
  38 + private void Cleanup()
  39 + {
  40 + SherpaOnnxDestroyOfflineTtsGeneratedAudio(Handle);
  41 +
  42 + // Don't permit the handle to be used again.
  43 + _handle = new HandleRef(this, IntPtr.Zero);
  44 + }
  45 +
  46 + [StructLayout(LayoutKind.Sequential)]
  47 + struct Impl
  48 + {
  49 + public IntPtr Samples;
  50 + public int NumSamples;
  51 + public int SampleRate;
  52 + }
  53 +
  54 + private HandleRef _handle;
  55 + public IntPtr Handle => _handle.Handle;
  56 +
  57 + public int NumSamples
  58 + {
  59 + get
  60 + {
  61 + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
  62 + return impl.NumSamples;
  63 + }
  64 + }
  65 +
  66 + public int SampleRate
  67 + {
  68 + get
  69 + {
  70 + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
  71 + return impl.SampleRate;
  72 + }
  73 + }
  74 +
  75 + public float[] Samples
  76 + {
  77 + get
  78 + {
  79 + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
  80 +
  81 + float[] samples = new float[impl.NumSamples];
  82 + Marshal.Copy(impl.Samples, samples, 0, impl.NumSamples);
  83 + return samples;
  84 + }
  85 + }
  86 +
  87 + [DllImport(Dll.Filename)]
  88 + private static extern void SherpaOnnxDestroyOfflineTtsGeneratedAudio(IntPtr handle);
  89 +
  90 + [DllImport(Dll.Filename)]
  91 + private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPStr)] string filename);
  92 + }
  93 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 +
  12 + [StructLayout(LayoutKind.Sequential)]
  13 + public struct OfflineTtsModelConfig
  14 + {
  15 + public OfflineTtsModelConfig()
  16 + {
  17 + Vits = new OfflineTtsVitsModelConfig();
  18 + NumThreads = 1;
  19 + Debug = 0;
  20 + Provider = "cpu";
  21 + }
  22 +
  23 + public OfflineTtsVitsModelConfig Vits;
  24 + public int NumThreads;
  25 + public int Debug;
  26 + [MarshalAs(UnmanagedType.LPStr)]
  27 + public string Provider;
  28 + }
  29 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct OfflineTtsVitsModelConfig
  13 + {
  14 + public OfflineTtsVitsModelConfig()
  15 + {
  16 + Model = "";
  17 + Lexicon = "";
  18 + Tokens = "";
  19 + DataDir = "";
  20 +
  21 + NoiseScale = 0.667F;
  22 + NoiseScaleW = 0.8F;
  23 + LengthScale = 1.0F;
  24 +
  25 + DictDir = "";
  26 + }
  27 + [MarshalAs(UnmanagedType.LPStr)]
  28 + public string Model;
  29 +
  30 + [MarshalAs(UnmanagedType.LPStr)]
  31 + public string Lexicon;
  32 +
  33 + [MarshalAs(UnmanagedType.LPStr)]
  34 + public string Tokens;
  35 +
  36 + [MarshalAs(UnmanagedType.LPStr)]
  37 + public string DataDir;
  38 +
  39 + public float NoiseScale;
  40 + public float NoiseScaleW;
  41 + public float LengthScale;
  42 +
  43 + [MarshalAs(UnmanagedType.LPStr)]
  44 + public string DictDir;
  45 + }
  46 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct OfflineWhisperModelConfig
  13 + {
  14 + public OfflineWhisperModelConfig()
  15 + {
  16 + Encoder = "";
  17 + Decoder = "";
  18 + Language = "";
  19 + Task = "transcribe";
  20 + TailPaddings = -1;
  21 + }
  22 + [MarshalAs(UnmanagedType.LPStr)]
  23 + public string Encoder;
  24 +
  25 + [MarshalAs(UnmanagedType.LPStr)]
  26 + public string Decoder;
  27 +
  28 + [MarshalAs(UnmanagedType.LPStr)]
  29 + public string Language;
  30 +
  31 + [MarshalAs(UnmanagedType.LPStr)]
  32 + public string Task;
  33 +
  34 + public int TailPaddings;
  35 + }
  36 +
  37 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 + [StructLayout(LayoutKind.Sequential)]
  14 + public struct OnlineCtcFstDecoderConfig
  15 + {
  16 + public OnlineCtcFstDecoderConfig()
  17 + {
  18 + Graph = "";
  19 + MaxActive = 3000;
  20 + }
  21 +
  22 + [MarshalAs(UnmanagedType.LPStr)]
  23 + public string Graph;
  24 +
  25 + public int MaxActive;
  26 + }
  27 +
  28 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 +
  14 + [StructLayout(LayoutKind.Sequential)]
  15 + public struct OnlineModelConfig
  16 + {
  17 + public OnlineModelConfig()
  18 + {
  19 + Transducer = new OnlineTransducerModelConfig();
  20 + Paraformer = new OnlineParaformerModelConfig();
  21 + Zipformer2Ctc = new OnlineZipformer2CtcModelConfig();
  22 + Tokens = "";
  23 + NumThreads = 1;
  24 + Provider = "cpu";
  25 + Debug = 0;
  26 + ModelType = "";
  27 + }
  28 +
  29 + public OnlineTransducerModelConfig Transducer;
  30 + public OnlineParaformerModelConfig Paraformer;
  31 + public OnlineZipformer2CtcModelConfig Zipformer2Ctc;
  32 +
  33 + [MarshalAs(UnmanagedType.LPStr)]
  34 + public string Tokens;
  35 +
  36 + /// Number of threads used to run the neural network model
  37 + public int NumThreads;
  38 +
  39 + [MarshalAs(UnmanagedType.LPStr)]
  40 + public string Provider;
  41 +
  42 + /// true to print debug information of the model
  43 + public int Debug;
  44 +
  45 + [MarshalAs(UnmanagedType.LPStr)]
  46 + public string ModelType;
  47 + }
  48 +
  49 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 +
  14 + [StructLayout(LayoutKind.Sequential)]
  15 + public struct OnlineParaformerModelConfig
  16 + {
  17 + public OnlineParaformerModelConfig()
  18 + {
  19 + Encoder = "";
  20 + Decoder = "";
  21 + }
  22 +
  23 + [MarshalAs(UnmanagedType.LPStr)]
  24 + public string Encoder;
  25 +
  26 + [MarshalAs(UnmanagedType.LPStr)]
  27 + public string Decoder;
  28 + }
  29 +
  30 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 + // please see
  14 + // https://www.mono-project.com/docs/advanced/pinvoke/#gc-safe-pinvoke-code
  15 + // https://www.mono-project.com/docs/advanced/pinvoke/#properly-disposing-of-resources
  16 + public class OnlineRecognizer : IDisposable
  17 + {
  18 + public OnlineRecognizer(OnlineRecognizerConfig config)
  19 + {
  20 + IntPtr h = CreateOnlineRecognizer(ref config);
  21 + _handle = new HandleRef(this, h);
  22 + }
  23 +
  24 + public OnlineStream CreateStream()
  25 + {
  26 + IntPtr p = CreateOnlineStream(_handle.Handle);
  27 + return new OnlineStream(p);
  28 + }
  29 +
  30 + /// Return true if the passed stream is ready for decoding.
  31 + public bool IsReady(OnlineStream stream)
  32 + {
  33 + return IsReady(_handle.Handle, stream.Handle) != 0;
  34 + }
  35 +
  36 + /// Return true if an endpoint is detected for this stream.
  37 + /// You probably need to invoke Reset(stream) when this method returns
  38 + /// true.
  39 + public bool IsEndpoint(OnlineStream stream)
  40 + {
  41 + return IsEndpoint(_handle.Handle, stream.Handle) != 0;
  42 + }
  43 +
  44 + /// You have to ensure that IsReady(stream) returns true before
  45 + /// you call this method
  46 + public void Decode(OnlineStream stream)
  47 + {
  48 + Decode(_handle.Handle, stream.Handle);
  49 + }
  50 +
  51 + // The caller should ensure all passed streams are ready for decoding.
  52 + public void Decode(IEnumerable<OnlineStream> streams)
  53 + {
  54 + IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();
  55 + Decode(_handle.Handle, ptrs, ptrs.Length);
  56 + }
  57 +
  58 + public OnlineRecognizerResult GetResult(OnlineStream stream)
  59 + {
  60 + IntPtr h = GetResult(_handle.Handle, stream.Handle);
  61 + OnlineRecognizerResult result = new OnlineRecognizerResult(h);
  62 + DestroyResult(h);
  63 + return result;
  64 + }
  65 +
  66 + /// When this method returns, IsEndpoint(stream) will return false.
  67 + public void Reset(OnlineStream stream)
  68 + {
  69 + Reset(_handle.Handle, stream.Handle);
  70 + }
  71 +
  72 + public void Dispose()
  73 + {
  74 + Cleanup();
  75 + // Prevent the object from being placed on the
  76 + // finalization queue
  77 + System.GC.SuppressFinalize(this);
  78 + }
  79 +
  80 + ~OnlineRecognizer()
  81 + {
  82 + Cleanup();
  83 + }
  84 +
  85 + private void Cleanup()
  86 + {
  87 + DestroyOnlineRecognizer(_handle.Handle);
  88 +
  89 + // Don't permit the handle to be used again.
  90 + _handle = new HandleRef(this, IntPtr.Zero);
  91 + }
  92 +
  93 + private HandleRef _handle;
  94 +
  95 + [DllImport(Dll.Filename)]
  96 + private static extern IntPtr CreateOnlineRecognizer(ref OnlineRecognizerConfig config);
  97 +
  98 + [DllImport(Dll.Filename)]
  99 + private static extern void DestroyOnlineRecognizer(IntPtr handle);
  100 +
  101 + [DllImport(Dll.Filename)]
  102 + private static extern IntPtr CreateOnlineStream(IntPtr handle);
  103 +
  104 + [DllImport(Dll.Filename, EntryPoint = "IsOnlineStreamReady")]
  105 + private static extern int IsReady(IntPtr handle, IntPtr stream);
  106 +
  107 + [DllImport(Dll.Filename, EntryPoint = "DecodeOnlineStream")]
  108 + private static extern void Decode(IntPtr handle, IntPtr stream);
  109 +
  110 + [DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOnlineStreams")]
  111 + private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
  112 +
  113 + [DllImport(Dll.Filename, EntryPoint = "GetOnlineStreamResult")]
  114 + private static extern IntPtr GetResult(IntPtr handle, IntPtr stream);
  115 +
  116 + [DllImport(Dll.Filename, EntryPoint = "DestroyOnlineRecognizerResult")]
  117 + private static extern void DestroyResult(IntPtr result);
  118 +
  119 + [DllImport(Dll.Filename)]
  120 + private static extern void Reset(IntPtr handle, IntPtr stream);
  121 +
  122 + [DllImport(Dll.Filename)]
  123 + private static extern int IsEndpoint(IntPtr handle, IntPtr stream);
  124 + }
  125 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 +
  14 + [StructLayout(LayoutKind.Sequential)]
  15 + public struct OnlineRecognizerConfig
  16 + {
  17 + public OnlineRecognizerConfig()
  18 + {
  19 + FeatConfig = new FeatureConfig();
  20 + ModelConfig = new OnlineModelConfig();
  21 + DecodingMethod = "greedy_search";
  22 + MaxActivePaths = 4;
  23 + EnableEndpoint = 0;
  24 + Rule1MinTrailingSilence = 1.2F;
  25 + Rule2MinTrailingSilence = 2.4F;
  26 + Rule3MinUtteranceLength = 20.0F;
  27 + HotwordsFile = "";
  28 + HotwordsScore = 1.5F;
  29 + CtcFstDecoderConfig = new OnlineCtcFstDecoderConfig();
  30 + }
  31 + public FeatureConfig FeatConfig;
  32 + public OnlineModelConfig ModelConfig;
  33 +
  34 + [MarshalAs(UnmanagedType.LPStr)]
  35 + public string DecodingMethod;
  36 +
  37 + /// Used only when decoding_method is modified_beam_search
  38 + /// Example value: 4
  39 + public int MaxActivePaths;
  40 +
  41 + /// 0 to disable endpoint detection.
  42 + /// A non-zero value to enable endpoint detection.
  43 + public int EnableEndpoint;
  44 +
  45 + /// An endpoint is detected if trailing silence in seconds is larger than
  46 + /// this value even if nothing has been decoded.
  47 + /// Used only when enable_endpoint is not 0.
  48 + public float Rule1MinTrailingSilence;
  49 +
  50 + /// An endpoint is detected if trailing silence in seconds is larger than
  51 + /// this value after something that is not blank has been decoded.
  52 + /// Used only when enable_endpoint is not 0.
  53 + public float Rule2MinTrailingSilence;
  54 +
  55 + /// An endpoint is detected if the utterance in seconds is larger than
  56 + /// this value.
  57 + /// Used only when enable_endpoint is not 0.
  58 + public float Rule3MinUtteranceLength;
  59 +
  60 + /// Path to the hotwords.
  61 + [MarshalAs(UnmanagedType.LPStr)]
  62 + public string HotwordsFile;
  63 +
  64 + /// Bonus score for each token in hotwords.
  65 + public float HotwordsScore;
  66 +
  67 + public OnlineCtcFstDecoderConfig CtcFstDecoderConfig;
  68 + }
  69 +
  70 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 +
  14 + public class OnlineRecognizerResult
  15 + {
  16 + public OnlineRecognizerResult(IntPtr handle)
  17 + {
  18 + Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
  19 + // PtrToStringUTF8() requires .net standard 2.1
  20 + // _text = Marshal.PtrToStringUTF8(impl.Text);
  21 +
  22 + int length = 0;
  23 +
  24 + unsafe
  25 + {
  26 + byte* buffer = (byte*)impl.Text;
  27 + while (*buffer != 0)
  28 + {
  29 + ++buffer;
  30 + length += 1;
  31 + }
  32 + }
  33 +
  34 + byte[] stringBuffer = new byte[length];
  35 + Marshal.Copy(impl.Text, stringBuffer, 0, length);
  36 + _text = Encoding.UTF8.GetString(stringBuffer);
  37 +
  38 + _tokens = new String[impl.Count];
  39 +
  40 + unsafe
  41 + {
  42 + byte* buf = (byte*)impl.Tokens;
  43 + for (int i = 0; i < impl.Count; i++)
  44 + {
  45 + length = 0;
  46 + byte* start = buf;
  47 + while (*buf != 0)
  48 + {
  49 + ++buf;
  50 + length += 1;
  51 + }
  52 + ++buf;
  53 +
  54 + stringBuffer = new byte[length];
  55 + fixed (byte* pTarget = stringBuffer)
  56 + {
  57 + for (int k = 0; k < length; k++)
  58 + {
  59 + pTarget[k] = start[k];
  60 + }
  61 + }
  62 +
  63 + _tokens[i] = Encoding.UTF8.GetString(stringBuffer);
  64 + }
  65 + }
  66 +
  67 + unsafe
  68 + {
  69 + float* t = (float*)impl.Timestamps;
  70 + if (t != null)
  71 + {
  72 + _timestamps = new float[impl.Count];
  73 + fixed (float* pTarget = _timestamps)
  74 + {
  75 + for (int i = 0; i < impl.Count; i++)
  76 + {
  77 + pTarget[i] = t[i];
  78 + }
  79 + }
  80 + }
  81 + else
  82 + {
  83 + _timestamps = Array.Empty<float>();
  84 + }
  85 + }
  86 + }
  87 + [StructLayout(LayoutKind.Sequential)]
  88 + struct Impl
  89 + {
  90 + public IntPtr Text;
  91 + public IntPtr Tokens;
  92 + public IntPtr TokensArr;
  93 + public IntPtr Timestamps;
  94 + public int Count;
  95 + }
  96 +
  97 + private String _text;
  98 + public String Text => _text;
  99 +
  100 + private String[] _tokens;
  101 + public String[] Tokens => _tokens;
  102 +
  103 + private float[] _timestamps;
  104 + public float[] Timestamps => _timestamps;
  105 + }
  106 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 +
  14 + public class OnlineStream : IDisposable
  15 + {
  16 + public OnlineStream(IntPtr p)
  17 + {
  18 + _handle = new HandleRef(this, p);
  19 + }
  20 +
  21 + public void AcceptWaveform(int sampleRate, float[] samples)
  22 + {
  23 + AcceptWaveform(Handle, sampleRate, samples, samples.Length);
  24 + }
  25 +
  26 + public void InputFinished()
  27 + {
  28 + InputFinished(Handle);
  29 + }
  30 +
  31 + ~OnlineStream()
  32 + {
  33 + Cleanup();
  34 + }
  35 +
  36 + public void Dispose()
  37 + {
  38 + Cleanup();
  39 + // Prevent the object from being placed on the
  40 + // finalization queue
  41 + System.GC.SuppressFinalize(this);
  42 + }
  43 +
  44 + private void Cleanup()
  45 + {
  46 + DestroyOnlineStream(Handle);
  47 +
  48 + // Don't permit the handle to be used again.
  49 + _handle = new HandleRef(this, IntPtr.Zero);
  50 + }
  51 +
  52 + private HandleRef _handle;
  53 + public IntPtr Handle => _handle.Handle;
  54 +
  55 + [DllImport(Dll.Filename)]
  56 + private static extern void DestroyOnlineStream(IntPtr handle);
  57 +
  58 + [DllImport(Dll.Filename)]
  59 + private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);
  60 +
  61 + [DllImport(Dll.Filename)]
  62 + private static extern void InputFinished(IntPtr handle);
  63 + }
  64 +
  65 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 +
  14 + [StructLayout(LayoutKind.Sequential)]
  15 + public struct OnlineTransducerModelConfig
  16 + {
  17 + public OnlineTransducerModelConfig()
  18 + {
  19 + Encoder = "";
  20 + Decoder = "";
  21 + Joiner = "";
  22 + }
  23 +
  24 + [MarshalAs(UnmanagedType.LPStr)]
  25 + public string Encoder;
  26 +
  27 + [MarshalAs(UnmanagedType.LPStr)]
  28 + public string Decoder;
  29 +
  30 + [MarshalAs(UnmanagedType.LPStr)]
  31 + public string Joiner;
  32 + }
  33 +
  34 +}
  1 +/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2 +/// Copyright (c) 2023 by manyeyes
  3 +/// Copyright (c) 2024.5 by 东风破
  4 +
  5 +using System.Collections.Generic;
  6 +using System.Linq;
  7 +using System.Runtime.InteropServices;
  8 +using System.Text;
  9 +using System;
  10 +
  11 +namespace SherpaOnnx
  12 +{
  13 + [StructLayout(LayoutKind.Sequential)]
  14 + public struct OnlineZipformer2CtcModelConfig
  15 + {
  16 + public OnlineZipformer2CtcModelConfig()
  17 + {
  18 + Model = "";
  19 + }
  20 +
  21 + [MarshalAs(UnmanagedType.LPStr)]
  22 + public string Model;
  23 + }
  24 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public class SpeakerEmbeddingExtractor : IDisposable
  12 + {
  13 + public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config)
  14 + {
  15 + IntPtr h = SherpaOnnxCreateSpeakerEmbeddingExtractor(ref config);
  16 + _handle = new HandleRef(this, h);
  17 + }
  18 +
  19 + public OnlineStream CreateStream()
  20 + {
  21 + IntPtr p = SherpaOnnxSpeakerEmbeddingExtractorCreateStream(_handle.Handle);
  22 + return new OnlineStream(p);
  23 + }
  24 +
  25 + public bool IsReady(OnlineStream stream)
  26 + {
  27 + return SherpaOnnxSpeakerEmbeddingExtractorIsReady(_handle.Handle, stream.Handle) != 0;
  28 + }
  29 +
  30 + public float[] Compute(OnlineStream stream)
  31 + {
  32 + IntPtr p = SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(_handle.Handle, stream.Handle);
  33 +
  34 + int dim = Dim;
  35 + float[] ans = new float[dim];
  36 + Marshal.Copy(p, ans, 0, dim);
  37 +
  38 + SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding(p);
  39 +
  40 + return ans;
  41 + }
  42 +
  43 + public int Dim
  44 + {
  45 + get
  46 + {
  47 + return SherpaOnnxSpeakerEmbeddingExtractorDim(_handle.Handle);
  48 + }
  49 + }
  50 +
  51 + public void Dispose()
  52 + {
  53 + Cleanup();
  54 + // Prevent the object from being placed on the
  55 + // finalization queue
  56 + System.GC.SuppressFinalize(this);
  57 + }
  58 +
  59 + ~SpeakerEmbeddingExtractor()
  60 + {
  61 + Cleanup();
  62 + }
  63 +
  64 + private void Cleanup()
  65 + {
  66 + SherpaOnnxDestroySpeakerEmbeddingExtractor(_handle.Handle);
  67 +
  68 + // Don't permit the handle to be used again.
  69 + _handle = new HandleRef(this, IntPtr.Zero);
  70 + }
  71 +
  72 + private HandleRef _handle;
  73 +
  74 + [DllImport(Dll.Filename)]
  75 + private static extern IntPtr SherpaOnnxCreateSpeakerEmbeddingExtractor(ref SpeakerEmbeddingExtractorConfig config);
  76 +
  77 + [DllImport(Dll.Filename)]
  78 + private static extern void SherpaOnnxDestroySpeakerEmbeddingExtractor(IntPtr handle);
  79 +
  80 + [DllImport(Dll.Filename)]
  81 + private static extern int SherpaOnnxSpeakerEmbeddingExtractorDim(IntPtr handle);
  82 +
  83 + [DllImport(Dll.Filename)]
  84 + private static extern IntPtr SherpaOnnxSpeakerEmbeddingExtractorCreateStream(IntPtr handle);
  85 +
  86 + [DllImport(Dll.Filename)]
  87 + private static extern int SherpaOnnxSpeakerEmbeddingExtractorIsReady(IntPtr handle, IntPtr stream);
  88 +
  89 + [DllImport(Dll.Filename)]
  90 + private static extern IntPtr SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(IntPtr handle, IntPtr stream);
  91 +
  92 + [DllImport(Dll.Filename)]
  93 + private static extern void SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding(IntPtr p);
  94 + }
  95 +
  96 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct SpeakerEmbeddingExtractorConfig
  13 + {
  14 + public SpeakerEmbeddingExtractorConfig()
  15 + {
  16 + Model = "";
  17 + NumThreads = 1;
  18 + Debug = 0;
  19 + Provider = "cpu";
  20 + }
  21 +
  22 + [MarshalAs(UnmanagedType.LPStr)]
  23 + public string Model;
  24 +
  25 + public int NumThreads;
  26 + public int Debug;
  27 +
  28 + [MarshalAs(UnmanagedType.LPStr)]
  29 + public string Provider;
  30 + }
  31 +
  32 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public class SpeakerEmbeddingManager : IDisposable
  12 + {
  13 + public SpeakerEmbeddingManager(int dim)
  14 + {
  15 + IntPtr h = SherpaOnnxCreateSpeakerEmbeddingManager(dim);
  16 + _handle = new HandleRef(this, h);
  17 + this._dim = dim;
  18 + }
  19 +
  20 + public bool Add(string name, float[] v)
  21 + {
  22 + return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, name, v) == 1;
  23 + }
  24 +
  25 + public bool Add(string name, ICollection<float[]> v_list)
  26 + {
  27 + int n = v_list.Count;
  28 + float[] v = new float[n * _dim];
  29 + int i = 0;
  30 + foreach (var item in v_list)
  31 + {
  32 + item.CopyTo(v, i);
  33 + i += _dim;
  34 + }
  35 +
  36 + return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, name, v, n) == 1;
  37 + }
  38 +
  39 + public bool Remove(string name)
  40 + {
  41 + return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, name) == 1;
  42 + }
  43 +
  44 + public string Search(float[] v, float threshold)
  45 + {
  46 + IntPtr p = SherpaOnnxSpeakerEmbeddingManagerSearch(_handle.Handle, v, threshold);
  47 +
  48 + string s = "";
  49 + int length = 0;
  50 +
  51 + unsafe
  52 + {
  53 + byte* b = (byte*)p;
  54 + if (b != null)
  55 + {
  56 + while (*b != 0)
  57 + {
  58 + ++b;
  59 + length += 1;
  60 + }
  61 + }
  62 + }
  63 +
  64 + if (length > 0)
  65 + {
  66 + byte[] stringBuffer = new byte[length];
  67 + Marshal.Copy(p, stringBuffer, 0, length);
  68 + s = Encoding.UTF8.GetString(stringBuffer);
  69 + }
  70 +
  71 + SherpaOnnxSpeakerEmbeddingManagerFreeSearch(p);
  72 +
  73 + return s;
  74 + }
  75 +
  76 + public bool Verify(string name, float[] v, float threshold)
  77 + {
  78 + return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, name, v, threshold) == 1;
  79 + }
  80 +
  81 + public bool Contains(string name)
  82 + {
  83 + return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, name) == 1;
  84 + }
  85 +
  86 + public string[] GetAllSpeakers()
  87 + {
  88 + if (NumSpeakers == 0)
  89 + {
  90 + return new string[] { };
  91 + }
  92 +
  93 + IntPtr names = SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers(_handle.Handle);
  94 +
  95 + string[] ans = new string[NumSpeakers];
  96 +
  97 + unsafe
  98 + {
  99 + byte** p = (byte**)names;
  100 + for (int i = 0; i != NumSpeakers; i++)
  101 + {
  102 + int length = 0;
  103 + byte* s = p[i];
  104 + while (*s != 0)
  105 + {
  106 + ++s;
  107 + length += 1;
  108 + }
  109 + byte[] stringBuffer = new byte[length];
  110 + Marshal.Copy((IntPtr)p[i], stringBuffer, 0, length);
  111 + ans[i] = Encoding.UTF8.GetString(stringBuffer);
  112 + }
  113 + }
  114 +
  115 + SherpaOnnxSpeakerEmbeddingManagerFreeAllSpeakers(names);
  116 +
  117 + return ans;
  118 + }
  119 +
  120 + public void Dispose()
  121 + {
  122 + Cleanup();
  123 + // Prevent the object from being placed on the
  124 + // finalization queue
  125 + System.GC.SuppressFinalize(this);
  126 + }
  127 +
  128 + ~SpeakerEmbeddingManager()
  129 + {
  130 + Cleanup();
  131 + }
  132 +
  133 + private void Cleanup()
  134 + {
  135 + SherpaOnnxDestroySpeakerEmbeddingManager(_handle.Handle);
  136 +
  137 + // Don't permit the handle to be used again.
  138 + _handle = new HandleRef(this, IntPtr.Zero);
  139 + }
  140 +
  141 + public int NumSpeakers
  142 + {
  143 + get
  144 + {
  145 + return SherpaOnnxSpeakerEmbeddingManagerNumSpeakers(_handle.Handle);
  146 + }
  147 + }
  148 +
  149 + private HandleRef _handle;
  150 + private int _dim;
  151 +
  152 +
  153 + [DllImport(Dll.Filename)]
  154 + private static extern IntPtr SherpaOnnxCreateSpeakerEmbeddingManager(int dim);
  155 +
  156 + [DllImport(Dll.Filename)]
  157 + private static extern void SherpaOnnxDestroySpeakerEmbeddingManager(IntPtr handle);
  158 +
  159 + [DllImport(Dll.Filename)]
  160 + private static extern int SherpaOnnxSpeakerEmbeddingManagerAdd(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v);
  161 +
  162 + [DllImport(Dll.Filename)]
  163 + private static extern int SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v, int n);
  164 +
  165 + [DllImport(Dll.Filename)]
  166 + private static extern int SherpaOnnxSpeakerEmbeddingManagerRemove(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name);
  167 +
  168 + [DllImport(Dll.Filename)]
  169 + private static extern IntPtr SherpaOnnxSpeakerEmbeddingManagerSearch(IntPtr handle, float[] v, float threshold);
  170 +
  171 + [DllImport(Dll.Filename)]
  172 + private static extern void SherpaOnnxSpeakerEmbeddingManagerFreeSearch(IntPtr p);
  173 +
  174 + [DllImport(Dll.Filename)]
  175 + private static extern int SherpaOnnxSpeakerEmbeddingManagerVerify(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v, float threshold);
  176 +
  177 + [DllImport(Dll.Filename)]
  178 + private static extern int SherpaOnnxSpeakerEmbeddingManagerContains(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name);
  179 +
  180 + [DllImport(Dll.Filename)]
  181 + private static extern int SherpaOnnxSpeakerEmbeddingManagerNumSpeakers(IntPtr handle);
  182 +
  183 + [DllImport(Dll.Filename)]
  184 + private static extern IntPtr SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers(IntPtr handle);
  185 +
  186 + [DllImport(Dll.Filename)]
  187 + private static extern void SherpaOnnxSpeakerEmbeddingManagerFreeAllSpeakers(IntPtr names);
  188 + }
  189 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public class SpokenLanguageIdentification : IDisposable
  12 +{
  13 + public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config)
  14 + {
  15 + IntPtr h = SherpaOnnxCreateSpokenLanguageIdentification(ref config);
  16 + _handle = new HandleRef(this, h);
  17 + }
  18 +
  19 + public OfflineStream CreateStream()
  20 + {
  21 + IntPtr p = SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(_handle.Handle);
  22 + return new OfflineStream(p);
  23 + }
  24 +
  25 + public SpokenLanguageIdentificationResult Compute(OfflineStream stream)
  26 + {
  27 + IntPtr h = SherpaOnnxSpokenLanguageIdentificationCompute(_handle.Handle, stream.Handle);
  28 + SpokenLanguageIdentificationResult result = new SpokenLanguageIdentificationResult(h);
  29 + SherpaOnnxDestroySpokenLanguageIdentificationResult(h);
  30 + return result;
  31 + }
  32 +
  33 + public void Dispose()
  34 + {
  35 + Cleanup();
  36 + // Prevent the object from being placed on the
  37 + // finalization queue
  38 + System.GC.SuppressFinalize(this);
  39 + }
  40 +
  41 + ~SpokenLanguageIdentification()
  42 + {
  43 + Cleanup();
  44 + }
  45 +
  46 + private void Cleanup()
  47 + {
  48 + SherpaOnnxDestroySpokenLanguageIdentification(_handle.Handle);
  49 +
  50 + // Don't permit the handle to be used again.
  51 + _handle = new HandleRef(this, IntPtr.Zero);
  52 + }
  53 +
  54 + private HandleRef _handle;
  55 +
  56 + [DllImport(Dll.Filename)]
  57 + private static extern IntPtr SherpaOnnxCreateSpokenLanguageIdentification(ref SpokenLanguageIdentificationConfig config);
  58 +
  59 + [DllImport(Dll.Filename)]
  60 + private static extern void SherpaOnnxDestroySpokenLanguageIdentification(IntPtr handle);
  61 +
  62 + [DllImport(Dll.Filename)]
  63 + private static extern IntPtr SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(IntPtr handle);
  64 +
  65 + [DllImport(Dll.Filename)]
  66 + private static extern IntPtr SherpaOnnxSpokenLanguageIdentificationCompute(IntPtr handle, IntPtr stream);
  67 +
  68 + [DllImport(Dll.Filename)]
  69 + private static extern void SherpaOnnxDestroySpokenLanguageIdentificationResult(IntPtr handle);
  70 +}
  71 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public struct SpokenLanguageIdentificationConfig
  12 + {
  13 + public SpokenLanguageIdentificationConfig()
  14 + {
  15 + Whisper = new SpokenLanguageIdentificationWhisperConfig();
  16 + NumThreads = 1;
  17 + Debug = 0;
  18 + Provider = "cpu";
  19 + }
  20 + public SpokenLanguageIdentificationWhisperConfig Whisper;
  21 +
  22 + public int NumThreads;
  23 + public int Debug;
  24 +
  25 + [MarshalAs(UnmanagedType.LPStr)]
  26 + public string Provider;
  27 + }
  28 +
  29 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + public class SpokenLanguageIdentificationResult
  12 + {
  13 + public SpokenLanguageIdentificationResult(IntPtr handle)
  14 + {
  15 + Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
  16 +
  17 + // PtrToStringUTF8() requires .net standard 2.1
  18 + // _text = Marshal.PtrToStringUTF8(impl.Text);
  19 +
  20 + int length = 0;
  21 +
  22 + unsafe
  23 + {
  24 + byte* buffer = (byte*)impl.Lang;
  25 + while (*buffer != 0)
  26 + {
  27 + ++buffer;
  28 + length += 1;
  29 + }
  30 + }
  31 +
  32 + byte[] stringBuffer = new byte[length];
  33 + Marshal.Copy(impl.Lang, stringBuffer, 0, length);
  34 + _lang = Encoding.UTF8.GetString(stringBuffer);
  35 + }
  36 +
  37 + [StructLayout(LayoutKind.Sequential)]
  38 + struct Impl
  39 + {
  40 + public IntPtr Lang;
  41 + }
  42 +
  43 + private String _lang;
  44 + public String Lang => _lang;
  45 + }
  46 +}
  1 +/// Copyright (c) 2024.5 by 东风破
  2 +
  3 +using System.Linq;
  4 +using System.Collections.Generic;
  5 +using System.Runtime.InteropServices;
  6 +using System.Text;
  7 +using System;
  8 +
  9 +namespace SherpaOnnx
  10 +{
  11 + [StructLayout(LayoutKind.Sequential)]
  12 + public struct SpokenLanguageIdentificationWhisperConfig
  13 + {
  14 + public SpokenLanguageIdentificationWhisperConfig()
  15 + {
  16 + Encoder = "";
  17 + Decoder = "";
  18 + TailPaddings = -1;
  19 + }
  20 +
  21 + [MarshalAs(UnmanagedType.LPStr)]
  22 + public string Encoder;
  23 +
  24 + [MarshalAs(UnmanagedType.LPStr)]
  25 + public string Decoder;
  26 +
  27 + public int TailPaddings;
  28 + }
  29 +
  30 +}
1 -/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)  
2 -/// Copyright (c) 2023 by manyeyes  
3 -  
4 -using System.Linq;  
5 -using System.Collections.Generic;  
6 -using System.Runtime.InteropServices;  
7 -using System.Text;  
8 -using System;  
9 -  
10 -namespace SherpaOnnx  
11 -{  
12 -  
13 - [StructLayout(LayoutKind.Sequential)]  
14 - public struct OfflineTtsVitsModelConfig  
15 - {  
16 - public OfflineTtsVitsModelConfig()  
17 - {  
18 - Model = "";  
19 - Lexicon = "";  
20 - Tokens = "";  
21 - DataDir = "";  
22 -  
23 - NoiseScale = 0.667F;  
24 - NoiseScaleW = 0.8F;  
25 - LengthScale = 1.0F;  
26 -  
27 - DictDir = "";  
28 - }  
29 - [MarshalAs(UnmanagedType.LPStr)]  
30 - public string Model;  
31 -  
32 - [MarshalAs(UnmanagedType.LPStr)]  
33 - public string Lexicon;  
34 -  
35 - [MarshalAs(UnmanagedType.LPStr)]  
36 - public string Tokens;  
37 -  
38 - [MarshalAs(UnmanagedType.LPStr)]  
39 - public string DataDir;  
40 -  
41 - public float NoiseScale;  
42 - public float NoiseScaleW;  
43 - public float LengthScale;  
44 -  
45 - [MarshalAs(UnmanagedType.LPStr)]  
46 - public string DictDir;  
47 - }  
48 -  
49 - [StructLayout(LayoutKind.Sequential)]  
50 - public struct OfflineTtsModelConfig  
51 - {  
52 - public OfflineTtsModelConfig()  
53 - {  
54 - Vits = new OfflineTtsVitsModelConfig();  
55 - NumThreads = 1;  
56 - Debug = 0;  
57 - Provider = "cpu";  
58 - }  
59 -  
60 - public OfflineTtsVitsModelConfig Vits;  
61 - public int NumThreads;  
62 - public int Debug;  
63 - [MarshalAs(UnmanagedType.LPStr)]  
64 - public string Provider;  
65 - }  
66 -  
67 - [StructLayout(LayoutKind.Sequential)]  
68 - public struct OfflineTtsConfig  
69 - {  
70 - public OfflineTtsConfig()  
71 - {  
72 - Model = new OfflineTtsModelConfig();  
73 - RuleFsts = "";  
74 - MaxNumSentences = 1;  
75 - RuleFars = "";  
76 - }  
77 - public OfflineTtsModelConfig Model;  
78 -  
79 - [MarshalAs(UnmanagedType.LPStr)]  
80 - public string RuleFsts;  
81 -  
82 - public int MaxNumSentences;  
83 -  
84 - [MarshalAs(UnmanagedType.LPStr)]  
85 - public string RuleFars;  
86 - }  
87 -  
88 - public class OfflineTtsGeneratedAudio  
89 - {  
90 - public OfflineTtsGeneratedAudio(IntPtr p)  
91 - {  
92 - _handle = new HandleRef(this, p);  
93 - }  
94 -  
95 - public bool SaveToWaveFile(String filename)  
96 - {  
97 - Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));  
98 - int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, filename);  
99 - return status == 1;  
100 - }  
101 -  
102 - ~OfflineTtsGeneratedAudio()  
103 - {  
104 - Cleanup();  
105 - }  
106 -  
107 - public void Dispose()  
108 - {  
109 - Cleanup();  
110 - // Prevent the object from being placed on the  
111 - // finalization queue  
112 - System.GC.SuppressFinalize(this);  
113 - }  
114 -  
115 - private void Cleanup()  
116 - {  
117 - SherpaOnnxDestroyOfflineTtsGeneratedAudio(Handle);  
118 -  
119 - // Don't permit the handle to be used again.  
120 - _handle = new HandleRef(this, IntPtr.Zero);  
121 - }  
122 -  
123 - [StructLayout(LayoutKind.Sequential)]  
124 - struct Impl  
125 - {  
126 - public IntPtr Samples;  
127 - public int NumSamples;  
128 - public int SampleRate;  
129 - }  
130 -  
131 - private HandleRef _handle;  
132 - public IntPtr Handle => _handle.Handle;  
133 -  
134 - public int NumSamples  
135 - {  
136 - get  
137 - {  
138 - Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));  
139 - return impl.NumSamples;  
140 - }  
141 - }  
142 -  
143 - public int SampleRate  
144 - {  
145 - get  
146 - {  
147 - Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));  
148 - return impl.SampleRate;  
149 - }  
150 - }  
151 -  
152 - public float[] Samples  
153 - {  
154 - get  
155 - {  
156 - Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));  
157 -  
158 - float[] samples = new float[impl.NumSamples];  
159 - Marshal.Copy(impl.Samples, samples, 0, impl.NumSamples);  
160 - return samples;  
161 - }  
162 - }  
163 -  
164 - [DllImport(Dll.Filename)]  
165 - private static extern void SherpaOnnxDestroyOfflineTtsGeneratedAudio(IntPtr handle);  
166 -  
167 - [DllImport(Dll.Filename)]  
168 - private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPStr)] string filename);  
169 - }  
170 -  
171 - // IntPtr is actuallly a `const float*` from C++  
172 - public delegate void OfflineTtsCallback(IntPtr samples, int n);  
173 -  
174 - public class OfflineTts : IDisposable  
175 - {  
176 - public OfflineTts(OfflineTtsConfig config)  
177 - {  
178 - IntPtr h = SherpaOnnxCreateOfflineTts(ref config);  
179 - _handle = new HandleRef(this, h);  
180 - }  
181 -  
182 - public OfflineTtsGeneratedAudio Generate(String text, float speed, int speakerId)  
183 - {  
184 - IntPtr p = SherpaOnnxOfflineTtsGenerate(_handle.Handle, text, speakerId, speed);  
185 - return new OfflineTtsGeneratedAudio(p);  
186 - }  
187 -  
188 - public OfflineTtsGeneratedAudio GenerateWithCallback(String text, float speed, int speakerId, OfflineTtsCallback callback)  
189 - {  
190 - IntPtr p = SherpaOnnxOfflineTtsGenerateWithCallback(_handle.Handle, text, speakerId, speed, callback);  
191 - return new OfflineTtsGeneratedAudio(p);  
192 - }  
193 -  
194 - public void Dispose()  
195 - {  
196 - Cleanup();  
197 - // Prevent the object from being placed on the  
198 - // finalization queue  
199 - System.GC.SuppressFinalize(this);  
200 - }  
201 -  
202 - ~OfflineTts()  
203 - {  
204 - Cleanup();  
205 - }  
206 -  
207 - private void Cleanup()  
208 - {  
209 - SherpaOnnxDestroyOfflineTts(_handle.Handle);  
210 -  
211 - // Don't permit the handle to be used again.  
212 - _handle = new HandleRef(this, IntPtr.Zero);  
213 - }  
214 -  
215 - private HandleRef _handle;  
216 -  
217 - public int SampleRate  
218 - {  
219 - get  
220 - {  
221 - return SherpaOnnxOfflineTtsSampleRate(_handle.Handle);  
222 - }  
223 - }  
224 -  
225 - public int NumSpeakers  
226 - {  
227 - get  
228 - {  
229 - return SherpaOnnxOfflineTtsNumSpeakers(_handle.Handle);  
230 - }  
231 - }  
232 -  
233 - [DllImport(Dll.Filename)]  
234 - private static extern IntPtr SherpaOnnxCreateOfflineTts(ref OfflineTtsConfig config);  
235 -  
236 - [DllImport(Dll.Filename)]  
237 - private static extern void SherpaOnnxDestroyOfflineTts(IntPtr handle);  
238 -  
239 - [DllImport(Dll.Filename)]  
240 - private static extern int SherpaOnnxOfflineTtsSampleRate(IntPtr handle);  
241 -  
242 - [DllImport(Dll.Filename)]  
243 - private static extern int SherpaOnnxOfflineTtsNumSpeakers(IntPtr handle);  
244 -  
245 - [DllImport(Dll.Filename)]  
246 - private static extern IntPtr SherpaOnnxOfflineTtsGenerate(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed);  
247 -  
248 - [DllImport(Dll.Filename, CallingConvention = CallingConvention.Cdecl)]  
249 - private static extern IntPtr SherpaOnnxOfflineTtsGenerateWithCallback(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text, int sid, float speed, OfflineTtsCallback callback);  
250 - }  
251 -  
252 -  
253 -  
254 - [StructLayout(LayoutKind.Sequential)]  
255 - public struct OfflineTransducerModelConfig  
256 - {  
257 - public OfflineTransducerModelConfig()  
258 - {  
259 - Encoder = "";  
260 - Decoder = "";  
261 - Joiner = "";  
262 - }  
263 - [MarshalAs(UnmanagedType.LPStr)]  
264 - public string Encoder;  
265 -  
266 - [MarshalAs(UnmanagedType.LPStr)]  
267 - public string Decoder;  
268 -  
269 - [MarshalAs(UnmanagedType.LPStr)]  
270 - public string Joiner;  
271 - }  
272 -  
273 - [StructLayout(LayoutKind.Sequential)]  
274 - public struct OfflineParaformerModelConfig  
275 - {  
276 - public OfflineParaformerModelConfig()  
277 - {  
278 - Model = "";  
279 - }  
280 - [MarshalAs(UnmanagedType.LPStr)]  
281 - public string Model;  
282 - }  
283 -  
284 - [StructLayout(LayoutKind.Sequential)]  
285 - public struct OfflineNemoEncDecCtcModelConfig  
286 - {  
287 - public OfflineNemoEncDecCtcModelConfig()  
288 - {  
289 - Model = "";  
290 - }  
291 - [MarshalAs(UnmanagedType.LPStr)]  
292 - public string Model;  
293 - }  
294 -  
295 - [StructLayout(LayoutKind.Sequential)]  
296 - public struct OfflineWhisperModelConfig  
297 - {  
298 - public OfflineWhisperModelConfig()  
299 - {  
300 - Encoder = "";  
301 - Decoder = "";  
302 - Language = "";  
303 - Task = "transcribe";  
304 - TailPaddings = -1;  
305 - }  
306 - [MarshalAs(UnmanagedType.LPStr)]  
307 - public string Encoder;  
308 -  
309 - [MarshalAs(UnmanagedType.LPStr)]  
310 - public string Decoder;  
311 -  
312 - [MarshalAs(UnmanagedType.LPStr)]  
313 - public string Language;  
314 -  
315 - [MarshalAs(UnmanagedType.LPStr)]  
316 - public string Task;  
317 -  
318 - public int TailPaddings;  
319 - }  
320 -  
321 - [StructLayout(LayoutKind.Sequential)]  
322 - public struct OfflineTdnnModelConfig  
323 - {  
324 - public OfflineTdnnModelConfig()  
325 - {  
326 - Model = "";  
327 - }  
328 - [MarshalAs(UnmanagedType.LPStr)]  
329 - public string Model;  
330 - }  
331 -  
332 - [StructLayout(LayoutKind.Sequential)]  
333 - public struct OfflineLMConfig  
334 - {  
335 - public OfflineLMConfig()  
336 - {  
337 - Model = "";  
338 - Scale = 0.5F;  
339 - }  
340 - [MarshalAs(UnmanagedType.LPStr)]  
341 - public string Model;  
342 -  
343 - public float Scale;  
344 - }  
345 -  
346 - [StructLayout(LayoutKind.Sequential)]  
347 - public struct OfflineModelConfig  
348 - {  
349 - public OfflineModelConfig()  
350 - {  
351 - Transducer = new OfflineTransducerModelConfig();  
352 - Paraformer = new OfflineParaformerModelConfig();  
353 - NeMoCtc = new OfflineNemoEncDecCtcModelConfig();  
354 - Whisper = new OfflineWhisperModelConfig();  
355 - Tdnn = new OfflineTdnnModelConfig();  
356 - Tokens = "";  
357 - NumThreads = 1;  
358 - Debug = 0;  
359 - Provider = "cpu";  
360 - ModelType = "";  
361 - }  
362 - public OfflineTransducerModelConfig Transducer;  
363 - public OfflineParaformerModelConfig Paraformer;  
364 - public OfflineNemoEncDecCtcModelConfig NeMoCtc;  
365 - public OfflineWhisperModelConfig Whisper;  
366 - public OfflineTdnnModelConfig Tdnn;  
367 -  
368 - [MarshalAs(UnmanagedType.LPStr)]  
369 - public string Tokens;  
370 -  
371 - public int NumThreads;  
372 -  
373 - public int Debug;  
374 -  
375 - [MarshalAs(UnmanagedType.LPStr)]  
376 - public string Provider;  
377 -  
378 - [MarshalAs(UnmanagedType.LPStr)]  
379 - public string ModelType;  
380 - }  
381 -  
382 - [StructLayout(LayoutKind.Sequential)]  
383 - public struct OfflineRecognizerConfig  
384 - {  
385 - public OfflineRecognizerConfig()  
386 - {  
387 - FeatConfig = new FeatureConfig();  
388 - ModelConfig = new OfflineModelConfig();  
389 - LmConfig = new OfflineLMConfig();  
390 -  
391 - DecodingMethod = "greedy_search";  
392 - MaxActivePaths = 4;  
393 - HotwordsFile = "";  
394 - HotwordsScore = 1.5F;  
395 -  
396 - }  
397 - public FeatureConfig FeatConfig;  
398 - public OfflineModelConfig ModelConfig;  
399 - public OfflineLMConfig LmConfig;  
400 -  
401 - [MarshalAs(UnmanagedType.LPStr)]  
402 - public string DecodingMethod;  
403 -  
404 - public int MaxActivePaths;  
405 -  
406 - [MarshalAs(UnmanagedType.LPStr)]  
407 - public string HotwordsFile;  
408 -  
409 - public float HotwordsScore;  
410 - }  
411 -  
412 - public class OfflineRecognizerResult  
413 - {  
414 - public OfflineRecognizerResult(IntPtr handle)  
415 - {  
416 - Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));  
417 -  
418 - // PtrToStringUTF8() requires .net standard 2.1  
419 - // _text = Marshal.PtrToStringUTF8(impl.Text);  
420 -  
421 - int length = 0;  
422 -  
423 - unsafe  
424 - {  
425 - byte* buffer = (byte*)impl.Text;  
426 - while (*buffer != 0)  
427 - {  
428 - ++buffer;  
429 - length += 1;  
430 - }  
431 - }  
432 -  
433 - byte[] stringBuffer = new byte[length];  
434 - Marshal.Copy(impl.Text, stringBuffer, 0, length);  
435 - _text = Encoding.UTF8.GetString(stringBuffer);  
436 - }  
437 -  
438 - [StructLayout(LayoutKind.Sequential)]  
439 - struct Impl  
440 - {  
441 - public IntPtr Text;  
442 - }  
443 -  
444 - private String _text;  
445 - public String Text => _text;  
446 - }  
447 -  
448 - public class OfflineStream : IDisposable  
449 - {  
450 - public OfflineStream(IntPtr p)  
451 - {  
452 - _handle = new HandleRef(this, p);  
453 - }  
454 -  
455 - public void AcceptWaveform(int sampleRate, float[] samples)  
456 - {  
457 - AcceptWaveform(Handle, sampleRate, samples, samples.Length);  
458 - }  
459 -  
460 - public OfflineRecognizerResult Result  
461 - {  
462 - get  
463 - {  
464 - IntPtr h = GetResult(_handle.Handle);  
465 - OfflineRecognizerResult result = new OfflineRecognizerResult(h);  
466 - DestroyResult(h);  
467 - return result;  
468 - }  
469 - }  
470 -  
471 - ~OfflineStream()  
472 - {  
473 - Cleanup();  
474 - }  
475 -  
476 - public void Dispose()  
477 - {  
478 - Cleanup();  
479 - // Prevent the object from being placed on the  
480 - // finalization queue  
481 - System.GC.SuppressFinalize(this);  
482 - }  
483 -  
484 - private void Cleanup()  
485 - {  
486 - DestroyOfflineStream(Handle);  
487 -  
488 - // Don't permit the handle to be used again.  
489 - _handle = new HandleRef(this, IntPtr.Zero);  
490 - }  
491 -  
492 - private HandleRef _handle;  
493 - public IntPtr Handle => _handle.Handle;  
494 -  
495 - [DllImport(Dll.Filename)]  
496 - private static extern void DestroyOfflineStream(IntPtr handle);  
497 -  
498 - [DllImport(Dll.Filename, EntryPoint = "AcceptWaveformOffline")]  
499 - private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);  
500 -  
501 - [DllImport(Dll.Filename, EntryPoint = "GetOfflineStreamResult")]  
502 - private static extern IntPtr GetResult(IntPtr handle);  
503 -  
504 - [DllImport(Dll.Filename, EntryPoint = "DestroyOfflineRecognizerResult")]  
505 - private static extern void DestroyResult(IntPtr handle);  
506 - }  
507 -  
508 - public class OfflineRecognizer : IDisposable  
509 - {  
510 - public OfflineRecognizer(OfflineRecognizerConfig config)  
511 - {  
512 - IntPtr h = CreateOfflineRecognizer(ref config);  
513 - _handle = new HandleRef(this, h);  
514 - }  
515 -  
516 - public OfflineStream CreateStream()  
517 - {  
518 - IntPtr p = CreateOfflineStream(_handle.Handle);  
519 - return new OfflineStream(p);  
520 - }  
521 -  
522 - public void Decode(OfflineStream stream)  
523 - {  
524 - Decode(_handle.Handle, stream.Handle);  
525 - }  
526 -  
527 - // The caller should ensure all passed streams are ready for decoding.  
528 - public void Decode(IEnumerable<OfflineStream> streams)  
529 - {  
530 - IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();  
531 - Decode(_handle.Handle, ptrs, ptrs.Length);  
532 - }  
533 -  
534 - public void Dispose()  
535 - {  
536 - Cleanup();  
537 - // Prevent the object from being placed on the  
538 - // finalization queue  
539 - System.GC.SuppressFinalize(this);  
540 - }  
541 -  
542 - ~OfflineRecognizer()  
543 - {  
544 - Cleanup();  
545 - }  
546 -  
547 - private void Cleanup()  
548 - {  
549 - DestroyOfflineRecognizer(_handle.Handle);  
550 -  
551 - // Don't permit the handle to be used again.  
552 - _handle = new HandleRef(this, IntPtr.Zero);  
553 - }  
554 -  
555 - private HandleRef _handle;  
556 -  
557 - [DllImport(Dll.Filename)]  
558 - private static extern IntPtr CreateOfflineRecognizer(ref OfflineRecognizerConfig config);  
559 -  
560 - [DllImport(Dll.Filename)]  
561 - private static extern void DestroyOfflineRecognizer(IntPtr handle);  
562 -  
563 - [DllImport(Dll.Filename)]  
564 - private static extern IntPtr CreateOfflineStream(IntPtr handle);  
565 -  
566 - [DllImport(Dll.Filename, EntryPoint = "DecodeOfflineStream")]  
567 - private static extern void Decode(IntPtr handle, IntPtr stream);  
568 -  
569 - [DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOfflineStreams")]  
570 - private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);  
571 - }  
572 -  
573 - [StructLayout(LayoutKind.Sequential)]  
574 - public struct SpeakerEmbeddingExtractorConfig  
575 - {  
576 - public SpeakerEmbeddingExtractorConfig()  
577 - {  
578 - Model = "";  
579 - NumThreads = 1;  
580 - Debug = 0;  
581 - Provider = "cpu";  
582 - }  
583 -  
584 - [MarshalAs(UnmanagedType.LPStr)]  
585 - public string Model;  
586 -  
587 - public int NumThreads;  
588 - public int Debug;  
589 -  
590 - [MarshalAs(UnmanagedType.LPStr)]  
591 - public string Provider;  
592 - }  
593 -  
594 - public class SpeakerEmbeddingExtractor : IDisposable  
595 - {  
596 - public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config)  
597 - {  
598 - IntPtr h = SherpaOnnxCreateSpeakerEmbeddingExtractor(ref config);  
599 - _handle = new HandleRef(this, h);  
600 - }  
601 -  
602 - public OnlineStream CreateStream()  
603 - {  
604 - IntPtr p = SherpaOnnxSpeakerEmbeddingExtractorCreateStream(_handle.Handle);  
605 - return new OnlineStream(p);  
606 - }  
607 -  
608 - public bool IsReady(OnlineStream stream)  
609 - {  
610 - return SherpaOnnxSpeakerEmbeddingExtractorIsReady(_handle.Handle, stream.Handle) != 0;  
611 - }  
612 -  
613 - public float[] Compute(OnlineStream stream)  
614 - {  
615 - IntPtr p = SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(_handle.Handle, stream.Handle);  
616 -  
617 - int dim = Dim;  
618 - float[] ans = new float[dim];  
619 - Marshal.Copy(p, ans, 0, dim);  
620 -  
621 - SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding(p);  
622 -  
623 - return ans;  
624 - }  
625 -  
626 - public int Dim  
627 - {  
628 - get  
629 - {  
630 - return SherpaOnnxSpeakerEmbeddingExtractorDim(_handle.Handle);  
631 - }  
632 - }  
633 -  
634 - public void Dispose()  
635 - {  
636 - Cleanup();  
637 - // Prevent the object from being placed on the  
638 - // finalization queue  
639 - System.GC.SuppressFinalize(this);  
640 - }  
641 -  
642 - ~SpeakerEmbeddingExtractor()  
643 - {  
644 - Cleanup();  
645 - }  
646 -  
647 - private void Cleanup()  
648 - {  
649 - SherpaOnnxDestroySpeakerEmbeddingExtractor(_handle.Handle);  
650 -  
651 - // Don't permit the handle to be used again.  
652 - _handle = new HandleRef(this, IntPtr.Zero);  
653 - }  
654 -  
655 - private HandleRef _handle;  
656 -  
657 - [DllImport(Dll.Filename)]  
658 - private static extern IntPtr SherpaOnnxCreateSpeakerEmbeddingExtractor(ref SpeakerEmbeddingExtractorConfig config);  
659 -  
660 - [DllImport(Dll.Filename)]  
661 - private static extern void SherpaOnnxDestroySpeakerEmbeddingExtractor(IntPtr handle);  
662 -  
663 - [DllImport(Dll.Filename)]  
664 - private static extern int SherpaOnnxSpeakerEmbeddingExtractorDim(IntPtr handle);  
665 -  
666 - [DllImport(Dll.Filename)]  
667 - private static extern IntPtr SherpaOnnxSpeakerEmbeddingExtractorCreateStream(IntPtr handle);  
668 -  
669 - [DllImport(Dll.Filename)]  
670 - private static extern int SherpaOnnxSpeakerEmbeddingExtractorIsReady(IntPtr handle, IntPtr stream);  
671 -  
672 - [DllImport(Dll.Filename)]  
673 - private static extern IntPtr SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(IntPtr handle, IntPtr stream);  
674 -  
675 - [DllImport(Dll.Filename)]  
676 - private static extern void SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding(IntPtr p);  
677 - }  
678 -  
679 - [StructLayout(LayoutKind.Sequential)]  
680 - public struct SpokenLanguageIdentificationWhisperConfig  
681 - {  
682 - public SpokenLanguageIdentificationWhisperConfig()  
683 - {  
684 - Encoder = "";  
685 - Decoder = "";  
686 - TailPaddings = -1;  
687 - }  
688 -  
689 - [MarshalAs(UnmanagedType.LPStr)]  
690 - public string Encoder;  
691 -  
692 - [MarshalAs(UnmanagedType.LPStr)]  
693 - public string Decoder;  
694 -  
695 - public int TailPaddings;  
696 - }  
697 -  
698 - public struct SpokenLanguageIdentificationConfig  
699 - {  
700 - public SpokenLanguageIdentificationConfig()  
701 - {  
702 - Whisper = new SpokenLanguageIdentificationWhisperConfig();  
703 - NumThreads = 1;  
704 - Debug = 0;  
705 - Provider = "cpu";  
706 - }  
707 - public SpokenLanguageIdentificationWhisperConfig Whisper;  
708 -  
709 - public int NumThreads;  
710 - public int Debug;  
711 -  
712 - [MarshalAs(UnmanagedType.LPStr)]  
713 - public string Provider;  
714 - }  
715 -  
716 - public class SpeakerEmbeddingManager : IDisposable  
717 - {  
718 - public SpeakerEmbeddingManager(int dim)  
719 - {  
720 - IntPtr h = SherpaOnnxCreateSpeakerEmbeddingManager(dim);  
721 - _handle = new HandleRef(this, h);  
722 - this._dim = dim;  
723 - }  
724 -  
725 - public bool Add(string name, float[] v)  
726 - {  
727 - return SherpaOnnxSpeakerEmbeddingManagerAdd(_handle.Handle, name, v) == 1;  
728 - }  
729 -  
730 - public bool Add(string name, ICollection<float[]> v_list)  
731 - {  
732 - int n = v_list.Count;  
733 - float[] v = new float[n * _dim];  
734 - int i = 0;  
735 - foreach (var item in v_list)  
736 - {  
737 - item.CopyTo(v, i);  
738 - i += _dim;  
739 - }  
740 -  
741 - return SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(_handle.Handle, name, v, n) == 1;  
742 - }  
743 -  
744 - public bool Remove(string name)  
745 - {  
746 - return SherpaOnnxSpeakerEmbeddingManagerRemove(_handle.Handle, name) == 1;  
747 - }  
748 -  
749 - public string Search(float[] v, float threshold)  
750 - {  
751 - IntPtr p = SherpaOnnxSpeakerEmbeddingManagerSearch(_handle.Handle, v, threshold);  
752 -  
753 - string s = "";  
754 - int length = 0;  
755 -  
756 - unsafe  
757 - {  
758 - byte* b = (byte*)p;  
759 - if (b != null)  
760 - {  
761 - while (*b != 0)  
762 - {  
763 - ++b;  
764 - length += 1;  
765 - }  
766 - }  
767 - }  
768 -  
769 - if (length > 0)  
770 - {  
771 - byte[] stringBuffer = new byte[length];  
772 - Marshal.Copy(p, stringBuffer, 0, length);  
773 - s = Encoding.UTF8.GetString(stringBuffer);  
774 - }  
775 -  
776 - SherpaOnnxSpeakerEmbeddingManagerFreeSearch(p);  
777 -  
778 - return s;  
779 - }  
780 -  
781 - public bool Verify(string name, float[] v, float threshold)  
782 - {  
783 - return SherpaOnnxSpeakerEmbeddingManagerVerify(_handle.Handle, name, v, threshold) == 1;  
784 - }  
785 -  
786 - public bool Contains(string name)  
787 - {  
788 - return SherpaOnnxSpeakerEmbeddingManagerContains(_handle.Handle, name) == 1;  
789 - }  
790 -  
791 - public string[] GetAllSpeakers()  
792 - {  
793 - if (NumSpeakers == 0)  
794 - {  
795 - return new string[] { };  
796 - }  
797 -  
798 - IntPtr names = SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers(_handle.Handle);  
799 -  
800 - string[] ans = new string[NumSpeakers];  
801 -  
802 - unsafe  
803 - {  
804 - byte** p = (byte**)names;  
805 - for (int i = 0; i != NumSpeakers; i++)  
806 - {  
807 - int length = 0;  
808 - byte* s = p[i];  
809 - while (*s != 0)  
810 - {  
811 - ++s;  
812 - length += 1;  
813 - }  
814 - byte[] stringBuffer = new byte[length];  
815 - Marshal.Copy((IntPtr)p[i], stringBuffer, 0, length);  
816 - ans[i] = Encoding.UTF8.GetString(stringBuffer);  
817 - }  
818 - }  
819 -  
820 - SherpaOnnxSpeakerEmbeddingManagerFreeAllSpeakers(names);  
821 -  
822 - return ans;  
823 - }  
824 -  
825 - public void Dispose()  
826 - {  
827 - Cleanup();  
828 - // Prevent the object from being placed on the  
829 - // finalization queue  
830 - System.GC.SuppressFinalize(this);  
831 - }  
832 -  
833 - ~SpeakerEmbeddingManager()  
834 - {  
835 - Cleanup();  
836 - }  
837 -  
838 - private void Cleanup()  
839 - {  
840 - SherpaOnnxDestroySpeakerEmbeddingManager(_handle.Handle);  
841 -  
842 - // Don't permit the handle to be used again.  
843 - _handle = new HandleRef(this, IntPtr.Zero);  
844 - }  
845 -  
846 - public int NumSpeakers  
847 - {  
848 - get  
849 - {  
850 - return SherpaOnnxSpeakerEmbeddingManagerNumSpeakers(_handle.Handle);  
851 - }  
852 - }  
853 -  
854 - private HandleRef _handle;  
855 - private int _dim;  
856 -  
857 -  
858 - [DllImport(Dll.Filename)]  
859 - private static extern IntPtr SherpaOnnxCreateSpeakerEmbeddingManager(int dim);  
860 -  
861 - [DllImport(Dll.Filename)]  
862 - private static extern void SherpaOnnxDestroySpeakerEmbeddingManager(IntPtr handle);  
863 -  
864 - [DllImport(Dll.Filename)]  
865 - private static extern int SherpaOnnxSpeakerEmbeddingManagerAdd(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v);  
866 -  
867 - [DllImport(Dll.Filename)]  
868 - private static extern int SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v, int n);  
869 -  
870 - [DllImport(Dll.Filename)]  
871 - private static extern int SherpaOnnxSpeakerEmbeddingManagerRemove(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name);  
872 -  
873 - [DllImport(Dll.Filename)]  
874 - private static extern IntPtr SherpaOnnxSpeakerEmbeddingManagerSearch(IntPtr handle, float[] v, float threshold);  
875 -  
876 - [DllImport(Dll.Filename)]  
877 - private static extern void SherpaOnnxSpeakerEmbeddingManagerFreeSearch(IntPtr p);  
878 -  
879 - [DllImport(Dll.Filename)]  
880 - private static extern int SherpaOnnxSpeakerEmbeddingManagerVerify(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name, float[] v, float threshold);  
881 -  
882 - [DllImport(Dll.Filename)]  
883 - private static extern int SherpaOnnxSpeakerEmbeddingManagerContains(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name);  
884 -  
885 - [DllImport(Dll.Filename)]  
886 - private static extern int SherpaOnnxSpeakerEmbeddingManagerNumSpeakers(IntPtr handle);  
887 -  
888 - [DllImport(Dll.Filename)]  
889 - private static extern IntPtr SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers(IntPtr handle);  
890 -  
891 - [DllImport(Dll.Filename)]  
892 - private static extern void SherpaOnnxSpeakerEmbeddingManagerFreeAllSpeakers(IntPtr names);  
893 - }  
894 -  
895 - public class SpokenLanguageIdentificationResult  
896 - {  
897 - public SpokenLanguageIdentificationResult(IntPtr handle)  
898 - {  
899 - Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));  
900 -  
901 - // PtrToStringUTF8() requires .net standard 2.1  
902 - // _text = Marshal.PtrToStringUTF8(impl.Text);  
903 -  
904 - int length = 0;  
905 -  
906 - unsafe  
907 - {  
908 - byte* buffer = (byte*)impl.Lang;  
909 - while (*buffer != 0)  
910 - {  
911 - ++buffer;  
912 - length += 1;  
913 - }  
914 - }  
915 -  
916 - byte[] stringBuffer = new byte[length];  
917 - Marshal.Copy(impl.Lang, stringBuffer, 0, length);  
918 - _lang = Encoding.UTF8.GetString(stringBuffer);  
919 - }  
920 -  
921 - [StructLayout(LayoutKind.Sequential)]  
922 - struct Impl  
923 - {  
924 - public IntPtr Lang;  
925 - }  
926 -  
927 - private String _lang;  
928 - public String Lang => _lang;  
929 - }  
930 -  
931 - public class SpokenLanguageIdentification : IDisposable  
932 - {  
933 - public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config)  
934 - {  
935 - IntPtr h = SherpaOnnxCreateSpokenLanguageIdentification(ref config);  
936 - _handle = new HandleRef(this, h);  
937 - }  
938 -  
939 - public OfflineStream CreateStream()  
940 - {  
941 - IntPtr p = SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(_handle.Handle);  
942 - return new OfflineStream(p);  
943 - }  
944 -  
945 - public SpokenLanguageIdentificationResult Compute(OfflineStream stream)  
946 - {  
947 - IntPtr h = SherpaOnnxSpokenLanguageIdentificationCompute(_handle.Handle, stream.Handle);  
948 - SpokenLanguageIdentificationResult result = new SpokenLanguageIdentificationResult(h);  
949 - SherpaOnnxDestroySpokenLanguageIdentificationResult(h);  
950 - return result;  
951 - }  
952 -  
953 - public void Dispose()  
954 - {  
955 - Cleanup();  
956 - // Prevent the object from being placed on the  
957 - // finalization queue  
958 - System.GC.SuppressFinalize(this);  
959 - }  
960 -  
961 - ~SpokenLanguageIdentification()  
962 - {  
963 - Cleanup();  
964 - }  
965 -  
966 - private void Cleanup()  
967 - {  
968 - SherpaOnnxDestroySpokenLanguageIdentification(_handle.Handle);  
969 -  
970 - // Don't permit the handle to be used again.  
971 - _handle = new HandleRef(this, IntPtr.Zero);  
972 - }  
973 -  
974 - private HandleRef _handle;  
975 -  
976 - [DllImport(Dll.Filename)]  
977 - private static extern IntPtr SherpaOnnxCreateSpokenLanguageIdentification(ref SpokenLanguageIdentificationConfig config);  
978 -  
979 - [DllImport(Dll.Filename)]  
980 - private static extern void SherpaOnnxDestroySpokenLanguageIdentification(IntPtr handle);  
981 -  
982 - [DllImport(Dll.Filename)]  
983 - private static extern IntPtr SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(IntPtr handle);  
984 -  
985 - [DllImport(Dll.Filename)]  
986 - private static extern IntPtr SherpaOnnxSpokenLanguageIdentificationCompute(IntPtr handle, IntPtr stream);  
987 -  
988 - [DllImport(Dll.Filename)]  
989 - private static extern void SherpaOnnxDestroySpokenLanguageIdentificationResult(IntPtr handle);  
990 - }  
991 -}  
1 -/// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)  
2 -/// Copyright (c) 2023 by manyeyes  
3 -  
4 -using System.Collections.Generic;  
5 -using System.Linq;  
6 -using System.Runtime.InteropServices;  
7 -using System.Text;  
8 -using System;  
9 -  
10 -namespace SherpaOnnx  
11 -{  
12 - internal static class Dll  
13 - {  
14 - public const string Filename = "sherpa-onnx-c-api";  
15 - }  
16 -  
17 - [StructLayout(LayoutKind.Sequential)]  
18 - public struct OnlineTransducerModelConfig  
19 - {  
20 - public OnlineTransducerModelConfig()  
21 - {  
22 - Encoder = "";  
23 - Decoder = "";  
24 - Joiner = "";  
25 - }  
26 -  
27 - [MarshalAs(UnmanagedType.LPStr)]  
28 - public string Encoder;  
29 -  
30 - [MarshalAs(UnmanagedType.LPStr)]  
31 - public string Decoder;  
32 -  
33 - [MarshalAs(UnmanagedType.LPStr)]  
34 - public string Joiner;  
35 - }  
36 -  
37 - [StructLayout(LayoutKind.Sequential)]  
38 - public struct OnlineParaformerModelConfig  
39 - {  
40 - public OnlineParaformerModelConfig()  
41 - {  
42 - Encoder = "";  
43 - Decoder = "";  
44 - }  
45 -  
46 - [MarshalAs(UnmanagedType.LPStr)]  
47 - public string Encoder;  
48 -  
49 - [MarshalAs(UnmanagedType.LPStr)]  
50 - public string Decoder;  
51 - }  
52 -  
53 - [StructLayout(LayoutKind.Sequential)]  
54 - public struct OnlineZipformer2CtcModelConfig  
55 - {  
56 - public OnlineZipformer2CtcModelConfig()  
57 - {  
58 - Model = "";  
59 - }  
60 -  
61 - [MarshalAs(UnmanagedType.LPStr)]  
62 - public string Model;  
63 - }  
64 -  
65 - [StructLayout(LayoutKind.Sequential)]  
66 - public struct OnlineModelConfig  
67 - {  
68 - public OnlineModelConfig()  
69 - {  
70 - Transducer = new OnlineTransducerModelConfig();  
71 - Paraformer = new OnlineParaformerModelConfig();  
72 - Zipformer2Ctc = new OnlineZipformer2CtcModelConfig();  
73 - Tokens = "";  
74 - NumThreads = 1;  
75 - Provider = "cpu";  
76 - Debug = 0;  
77 - ModelType = "";  
78 - }  
79 -  
80 - public OnlineTransducerModelConfig Transducer;  
81 - public OnlineParaformerModelConfig Paraformer;  
82 - public OnlineZipformer2CtcModelConfig Zipformer2Ctc;  
83 -  
84 - [MarshalAs(UnmanagedType.LPStr)]  
85 - public string Tokens;  
86 -  
87 - /// Number of threads used to run the neural network model  
88 - public int NumThreads;  
89 -  
90 - [MarshalAs(UnmanagedType.LPStr)]  
91 - public string Provider;  
92 -  
93 - /// true to print debug information of the model  
94 - public int Debug;  
95 -  
96 - [MarshalAs(UnmanagedType.LPStr)]  
97 - public string ModelType;  
98 - }  
99 -  
100 - /// It expects 16 kHz 16-bit single channel wave format.  
101 - [StructLayout(LayoutKind.Sequential)]  
102 - public struct FeatureConfig  
103 - {  
104 - public FeatureConfig()  
105 - {  
106 - SampleRate = 16000;  
107 - FeatureDim = 80;  
108 - }  
109 - /// Sample rate of the input data. MUST match the one expected  
110 - /// by the model. For instance, it should be 16000 for models provided  
111 - /// by us.  
112 - public int SampleRate;  
113 -  
114 - /// Feature dimension of the model.  
115 - /// For instance, it should be 80 for models provided by us.  
116 - public int FeatureDim;  
117 - }  
118 -  
119 - [StructLayout(LayoutKind.Sequential)]  
120 - public struct OnlineCtcFstDecoderConfig  
121 - {  
122 - public OnlineCtcFstDecoderConfig()  
123 - {  
124 - Graph = "";  
125 - MaxActive = 3000;  
126 - }  
127 -  
128 - [MarshalAs(UnmanagedType.LPStr)]  
129 - public string Graph;  
130 -  
131 - public int MaxActive;  
132 - }  
133 -  
134 - [StructLayout(LayoutKind.Sequential)]  
135 - public struct OnlineRecognizerConfig  
136 - {  
137 - public OnlineRecognizerConfig()  
138 - {  
139 - FeatConfig = new FeatureConfig();  
140 - ModelConfig = new OnlineModelConfig();  
141 - DecodingMethod = "greedy_search";  
142 - MaxActivePaths = 4;  
143 - EnableEndpoint = 0;  
144 - Rule1MinTrailingSilence = 1.2F;  
145 - Rule2MinTrailingSilence = 2.4F;  
146 - Rule3MinUtteranceLength = 20.0F;  
147 - HotwordsFile = "";  
148 - HotwordsScore = 1.5F;  
149 - CtcFstDecoderConfig = new OnlineCtcFstDecoderConfig();  
150 - }  
151 - public FeatureConfig FeatConfig;  
152 - public OnlineModelConfig ModelConfig;  
153 -  
154 - [MarshalAs(UnmanagedType.LPStr)]  
155 - public string DecodingMethod;  
156 -  
157 - /// Used only when decoding_method is modified_beam_search  
158 - /// Example value: 4  
159 - public int MaxActivePaths;  
160 -  
161 - /// 0 to disable endpoint detection.  
162 - /// A non-zero value to enable endpoint detection.  
163 - public int EnableEndpoint;  
164 -  
165 - /// An endpoint is detected if trailing silence in seconds is larger than  
166 - /// this value even if nothing has been decoded.  
167 - /// Used only when enable_endpoint is not 0.  
168 - public float Rule1MinTrailingSilence;  
169 -  
170 - /// An endpoint is detected if trailing silence in seconds is larger than  
171 - /// this value after something that is not blank has been decoded.  
172 - /// Used only when enable_endpoint is not 0.  
173 - public float Rule2MinTrailingSilence;  
174 -  
175 - /// An endpoint is detected if the utterance in seconds is larger than  
176 - /// this value.  
177 - /// Used only when enable_endpoint is not 0.  
178 - public float Rule3MinUtteranceLength;  
179 -  
180 - /// Path to the hotwords.  
181 - [MarshalAs(UnmanagedType.LPStr)]  
182 - public string HotwordsFile;  
183 -  
184 - /// Bonus score for each token in hotwords.  
185 - public float HotwordsScore;  
186 -  
187 - public OnlineCtcFstDecoderConfig CtcFstDecoderConfig;  
188 - }  
189 -  
190 - public class OnlineRecognizerResult  
191 - {  
192 - public OnlineRecognizerResult(IntPtr handle)  
193 - {  
194 - Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));  
195 - // PtrToStringUTF8() requires .net standard 2.1  
196 - // _text = Marshal.PtrToStringUTF8(impl.Text);  
197 -  
198 - int length = 0;  
199 -  
200 - unsafe  
201 - {  
202 - byte* buffer = (byte*)impl.Text;  
203 - while (*buffer != 0)  
204 - {  
205 - ++buffer;  
206 - length += 1;  
207 - }  
208 - }  
209 -  
210 - byte[] stringBuffer = new byte[length];  
211 - Marshal.Copy(impl.Text, stringBuffer, 0, length);  
212 - _text = Encoding.UTF8.GetString(stringBuffer);  
213 -  
214 - _tokens = new String[impl.Count];  
215 -  
216 - unsafe  
217 - {  
218 - byte* buf = (byte*)impl.Tokens;  
219 - for (int i = 0; i < impl.Count; i++)  
220 - {  
221 - length = 0;  
222 - byte* start = buf;  
223 - while (*buf != 0)  
224 - {  
225 - ++buf;  
226 - length += 1;  
227 - }  
228 - ++buf;  
229 -  
230 - stringBuffer = new byte[length];  
231 - fixed (byte* pTarget = stringBuffer)  
232 - {  
233 - for (int k = 0; k < length; k++)  
234 - {  
235 - pTarget[k] = start[k];  
236 - }  
237 - }  
238 -  
239 - _tokens[i] = Encoding.UTF8.GetString(stringBuffer);  
240 - }  
241 - }  
242 -  
243 - unsafe  
244 - {  
245 - float* t = (float*)impl.Timestamps;  
246 - if (t != null)  
247 - {  
248 - _timestamps = new float[impl.Count];  
249 - fixed (float* pTarget = _timestamps)  
250 - {  
251 - for (int i = 0; i < impl.Count; i++)  
252 - {  
253 - pTarget[i] = t[i];  
254 - }  
255 - }  
256 - }  
257 - else  
258 - {  
259 - _timestamps = Array.Empty<float>();  
260 - }  
261 - }  
262 - }  
263 -  
264 - [StructLayout(LayoutKind.Sequential)]  
265 - struct Impl  
266 - {  
267 - public IntPtr Text;  
268 - public IntPtr Tokens;  
269 - public IntPtr TokensArr;  
270 - public IntPtr Timestamps;  
271 - public int Count;  
272 - }  
273 -  
274 - private String _text;  
275 - public String Text => _text;  
276 -  
277 - private String[] _tokens;  
278 - public String[] Tokens => _tokens;  
279 -  
280 - private float[] _timestamps;  
281 - public float[] Timestamps => _timestamps;  
282 - }  
283 -  
284 - public class OnlineStream : IDisposable  
285 - {  
286 - public OnlineStream(IntPtr p)  
287 - {  
288 - _handle = new HandleRef(this, p);  
289 - }  
290 -  
291 - public void AcceptWaveform(int sampleRate, float[] samples)  
292 - {  
293 - AcceptWaveform(Handle, sampleRate, samples, samples.Length);  
294 - }  
295 -  
296 - public void InputFinished()  
297 - {  
298 - InputFinished(Handle);  
299 - }  
300 -  
301 - ~OnlineStream()  
302 - {  
303 - Cleanup();  
304 - }  
305 -  
306 - public void Dispose()  
307 - {  
308 - Cleanup();  
309 - // Prevent the object from being placed on the  
310 - // finalization queue  
311 - System.GC.SuppressFinalize(this);  
312 - }  
313 -  
314 - private void Cleanup()  
315 - {  
316 - DestroyOnlineStream(Handle);  
317 -  
318 - // Don't permit the handle to be used again.  
319 - _handle = new HandleRef(this, IntPtr.Zero);  
320 - }  
321 -  
322 - private HandleRef _handle;  
323 - public IntPtr Handle => _handle.Handle;  
324 -  
325 - [DllImport(Dll.Filename)]  
326 - private static extern void DestroyOnlineStream(IntPtr handle);  
327 -  
328 - [DllImport(Dll.Filename)]  
329 - private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);  
330 -  
331 - [DllImport(Dll.Filename)]  
332 - private static extern void InputFinished(IntPtr handle);  
333 - }  
334 -  
335 - // please see  
336 - // https://www.mono-project.com/docs/advanced/pinvoke/#gc-safe-pinvoke-code  
337 - // https://www.mono-project.com/docs/advanced/pinvoke/#properly-disposing-of-resources  
338 - public class OnlineRecognizer : IDisposable  
339 - {  
340 - public OnlineRecognizer(OnlineRecognizerConfig config)  
341 - {  
342 - IntPtr h = CreateOnlineRecognizer(ref config);  
343 - _handle = new HandleRef(this, h);  
344 - }  
345 -  
346 - public OnlineStream CreateStream()  
347 - {  
348 - IntPtr p = CreateOnlineStream(_handle.Handle);  
349 - return new OnlineStream(p);  
350 - }  
351 -  
352 - /// Return true if the passed stream is ready for decoding.  
353 - public bool IsReady(OnlineStream stream)  
354 - {  
355 - return IsReady(_handle.Handle, stream.Handle) != 0;  
356 - }  
357 -  
358 - /// Return true if an endpoint is detected for this stream.  
359 - /// You probably need to invoke Reset(stream) when this method returns  
360 - /// true.  
361 - public bool IsEndpoint(OnlineStream stream)  
362 - {  
363 - return IsEndpoint(_handle.Handle, stream.Handle) != 0;  
364 - }  
365 -  
366 - /// You have to ensure that IsReady(stream) returns true before  
367 - /// you call this method  
368 - public void Decode(OnlineStream stream)  
369 - {  
370 - Decode(_handle.Handle, stream.Handle);  
371 - }  
372 -  
373 - // The caller should ensure all passed streams are ready for decoding.  
374 - public void Decode(IEnumerable<OnlineStream> streams)  
375 - {  
376 - IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();  
377 - Decode(_handle.Handle, ptrs, ptrs.Length);  
378 - }  
379 -  
380 - public OnlineRecognizerResult GetResult(OnlineStream stream)  
381 - {  
382 - IntPtr h = GetResult(_handle.Handle, stream.Handle);  
383 - OnlineRecognizerResult result = new OnlineRecognizerResult(h);  
384 - DestroyResult(h);  
385 - return result;  
386 - }  
387 -  
388 - /// When this method returns, IsEndpoint(stream) will return false.  
389 - public void Reset(OnlineStream stream)  
390 - {  
391 - Reset(_handle.Handle, stream.Handle);  
392 - }  
393 -  
394 - public void Dispose()  
395 - {  
396 - Cleanup();  
397 - // Prevent the object from being placed on the  
398 - // finalization queue  
399 - System.GC.SuppressFinalize(this);  
400 - }  
401 -  
402 - ~OnlineRecognizer()  
403 - {  
404 - Cleanup();  
405 - }  
406 -  
407 - private void Cleanup()  
408 - {  
409 - DestroyOnlineRecognizer(_handle.Handle);  
410 -  
411 - // Don't permit the handle to be used again.  
412 - _handle = new HandleRef(this, IntPtr.Zero);  
413 - }  
414 -  
415 - private HandleRef _handle;  
416 -  
417 - [DllImport(Dll.Filename)]  
418 - private static extern IntPtr CreateOnlineRecognizer(ref OnlineRecognizerConfig config);  
419 -  
420 - [DllImport(Dll.Filename)]  
421 - private static extern void DestroyOnlineRecognizer(IntPtr handle);  
422 -  
423 - [DllImport(Dll.Filename)]  
424 - private static extern IntPtr CreateOnlineStream(IntPtr handle);  
425 -  
426 - [DllImport(Dll.Filename, EntryPoint = "IsOnlineStreamReady")]  
427 - private static extern int IsReady(IntPtr handle, IntPtr stream);  
428 -  
429 - [DllImport(Dll.Filename, EntryPoint = "DecodeOnlineStream")]  
430 - private static extern void Decode(IntPtr handle, IntPtr stream);  
431 -  
432 - [DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOnlineStreams")]  
433 - private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);  
434 -  
435 - [DllImport(Dll.Filename, EntryPoint = "GetOnlineStreamResult")]  
436 - private static extern IntPtr GetResult(IntPtr handle, IntPtr stream);  
437 -  
438 - [DllImport(Dll.Filename, EntryPoint = "DestroyOnlineRecognizerResult")]  
439 - private static extern void DestroyResult(IntPtr result);  
440 -  
441 - [DllImport(Dll.Filename)]  
442 - private static extern void Reset(IntPtr handle, IntPtr stream);  
443 -  
444 - [DllImport(Dll.Filename)]  
445 - private static extern int IsEndpoint(IntPtr handle, IntPtr stream);  
446 - }  
447 -}  
@@ -128,8 +128,7 @@ popd @@ -128,8 +128,7 @@ popd
128 128
129 mkdir -p macos linux windows-x64 windows-x86 all 129 mkdir -p macos linux windows-x64 windows-x86 all
130 130
131 -cp ./online.cs all  
132 -cp ./offline.cs all 131 +cp ./*.cs all
133 132
134 ./generate.py 133 ./generate.py
135 134