Wrap VAD APIs to C# (#946)

This commit is contained in:
Fangjun Kuang
2024-05-30 18:29:38 +08:00
committed by GitHub
parent a99c7cb35c
commit 0196f1d546
17 changed files with 474 additions and 10 deletions

View File

@@ -0,0 +1,112 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;
namespace SherpaOnnx
{
public class CircularBuffer : IDisposable
{
public CircularBuffer(int capacity)
{
IntPtr h = SherpaOnnxCreateCircularBuffer(capacity);
_handle = new HandleRef(this, h);
}
public void Push(float[] data)
{
SherpaOnnxCircularBufferPush(_handle.Handle, data, data.Length);
}
public float[] Get(int startIndex, int n)
{
IntPtr p = SherpaOnnxCircularBufferGet(_handle.Handle, startIndex, n);
float[] ans = new float[n];
Marshal.Copy(p, ans, 0, n);
SherpaOnnxCircularBufferFree(p);
return ans;
}
public void Pop(int n)
{
SherpaOnnxCircularBufferPop(_handle.Handle, n);
}
public int Size
{
get
{
return SherpaOnnxCircularBufferSize(_handle.Handle);
}
}
public int Head
{
get
{
return SherpaOnnxCircularBufferHead(_handle.Handle);
}
}
public void Reset()
{
SherpaOnnxCircularBufferReset(_handle.Handle);
}
public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
~CircularBuffer()
{
Cleanup();
}
private void Cleanup()
{
SherpaOnnxDestroyCircularBuffer(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
private HandleRef _handle;
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCreateCircularBuffer(int capacity);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyCircularBuffer(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxCircularBufferPush(IntPtr handle, float[] p, int n);
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCircularBufferGet(IntPtr handle, int startIndex, int n);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxCircularBufferFree(IntPtr p);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxCircularBufferPop(IntPtr handle, int n);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxCircularBufferSize(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxCircularBufferHead(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxCircularBufferReset(IntPtr handle);
}
}

View File

@@ -0,0 +1,34 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct SileroVadModelConfig
{
public SileroVadModelConfig()
{
Model = "";
Threshold = 0.5F;
MinSilenceDuration = 0.5F;
MinSpeechDuration = 0.25F;
WindowSize = 512;
}
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
public float Threshold;
public float MinSilenceDuration;
public float MinSpeechDuration;
public int WindowSize;
}
}

View File

@@ -0,0 +1,47 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;
namespace SherpaOnnx
{
public class SpeechSegment
{
public SpeechSegment(IntPtr handle)
{
Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
_start = impl.Start;
unsafe
{
float* t = (float*)impl.Samples;
_samples = new float[impl.Count];
fixed (float* pTarget = _samples)
{
for (int i = 0; i < impl.Count; i++)
{
pTarget[i] = t[i];
}
}
}
}
public int _start;
public int Start => _start;
private float[] _samples;
public float[] Samples => _samples;
[StructLayout(LayoutKind.Sequential)]
struct Impl
{
public int Start;
public IntPtr Samples;
public int Count;
}
}
}

View File

@@ -0,0 +1,35 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct VadModelConfig
{
public VadModelConfig()
{
SileroVad = new SileroVadModelConfig();
SampleRate = 16000;
NumThreads = 1;
Provider = "cpu";
Debug = 0;
}
public SileroVadModelConfig SileroVad;
public int SampleRate;
public int NumThreads;
[MarshalAs(UnmanagedType.LPStr)]
public string Provider;
public int Debug;
}
}

View File

@@ -0,0 +1,115 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;
namespace SherpaOnnx
{
public class VoiceActivityDetector : IDisposable
{
public VoiceActivityDetector(VadModelConfig config, float bufferSizeInSeconds)
{
IntPtr h = SherpaOnnxCreateVoiceActivityDetector(ref config, bufferSizeInSeconds);
_handle = new HandleRef(this, h);
}
public void AcceptWaveform(float[] samples)
{
SherpaOnnxVoiceActivityDetectorAcceptWaveform(_handle.Handle, samples, samples.Length);
}
public bool IsEmpty()
{
return SherpaOnnxVoiceActivityDetectorEmpty(_handle.Handle) == 1;
}
public bool IsSpeechDetected()
{
return SherpaOnnxVoiceActivityDetectorDetected(_handle.Handle) == 1;
}
public void Pop()
{
SherpaOnnxVoiceActivityDetectorPop(_handle.Handle);
}
public SpeechSegment Front()
{
IntPtr p = SherpaOnnxVoiceActivityDetectorFront(_handle.Handle);
SpeechSegment segment = new SpeechSegment(p);
SherpaOnnxDestroySpeechSegment(p);
return segment;
}
public void Clear()
{
SherpaOnnxVoiceActivityDetectorClear(_handle.Handle);
}
public void Reset()
{
SherpaOnnxVoiceActivityDetectorReset(_handle.Handle);
}
public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
~VoiceActivityDetector()
{
Cleanup();
}
private void Cleanup()
{
SherpaOnnxDestroyVoiceActivityDetector(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
private HandleRef _handle;
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCreateVoiceActivityDetector(ref VadModelConfig config, float bufferSizeInSeconds);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyVoiceActivityDetector(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxVoiceActivityDetectorAcceptWaveform(IntPtr handle, float[] samples, int n);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxVoiceActivityDetectorEmpty(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxVoiceActivityDetectorDetected(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxVoiceActivityDetectorPop(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxVoiceActivityDetectorClear(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxVoiceActivityDetectorFront(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroySpeechSegment(IntPtr segment);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxVoiceActivityDetectorReset(IntPtr handle);
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>vad_non_streaming_asr_paraformer</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<RestoreSources>/tmp/packages;$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
</ItemGroup>
</Project>

View File

@@ -4,7 +4,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<OutputType>Library</OutputType>
<LangVersion>10.0</LangVersion>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<RuntimeIdentifiers>linux-x64;osx-x64;win-x64</RuntimeIdentifiers>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>sherpa-onnx</AssemblyName>

View File

@@ -3,7 +3,7 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<RuntimeIdentifier>{{ dotnet_rid }}</RuntimeIdentifier>
<AssemblyName>sherpa-onnx</AssemblyName>
<Version>{{ version }}</Version>