Add C# API for speech enhancement GTCRN models (#1990)

This commit is contained in:
Fangjun Kuang
2025-03-11 18:58:17 +08:00
committed by GitHub
parent c12d1d88c0
commit d3e27d5e21
10 changed files with 301 additions and 1 deletions

View File

@@ -0,0 +1,94 @@
/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang)
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SherpaOnnx
{
public class DenoisedAudio
{
public DenoisedAudio(IntPtr p)
{
_handle = new HandleRef(this, p);
}
public bool SaveToWaveFile(String filename)
{
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
byte[] utf8Filename = Encoding.UTF8.GetBytes(filename);
byte[] utf8FilenameWithNull = new byte[utf8Filename.Length + 1]; // +1 for null terminator
Array.Copy(utf8Filename, utf8FilenameWithNull, utf8Filename.Length);
utf8FilenameWithNull[utf8Filename.Length] = 0; // Null terminator
int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8FilenameWithNull);
return status == 1;
}
~DenoisedAudio()
{
Cleanup();
}
public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
private void Cleanup()
{
SherpaOnnxDestroyDenoisedAudio(Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
[StructLayout(LayoutKind.Sequential)]
struct Impl
{
public IntPtr Samples;
public int NumSamples;
public int SampleRate;
}
private HandleRef _handle;
public IntPtr Handle => _handle.Handle;
public int NumSamples
{
get
{
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
return impl.NumSamples;
}
}
public int SampleRate
{
get
{
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
return impl.SampleRate;
}
}
public float[] Samples
{
get
{
Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl));
float[] samples = new float[impl.NumSamples];
Marshal.Copy(impl.Samples, samples, 0, impl.NumSamples);
return samples;
}
}
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyDenoisedAudio(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Filename);
}
}

View File

@@ -0,0 +1,64 @@
/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
public class OfflineSpeechDenoiser: IDisposable
{
public OfflineSpeechDenoiser(OfflineSpeechDenoiserConfig config)
{
IntPtr h = SherpaOnnxCreateOfflineSpeechDenoiser(ref config);
_handle = new HandleRef(this, h);
}
public DenoisedAudio Run(float[] samples, int sampleRate)
{
IntPtr p = SherpaOnnxOfflineSpeechDenoiserRun(_handle.Handle, samples, samples.Length, sampleRate);
return new DenoisedAudio(p);
}
public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
~OfflineSpeechDenoiser()
{
Cleanup();
}
private void Cleanup()
{
SherpaOnnxDestroyOfflineSpeechDenoiser(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
private HandleRef _handle;
public int SampleRate
{
get
{
return SherpaOnnxOfflineSpeechDenoiserGetSampleRate(_handle.Handle);
}
}
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCreateOfflineSpeechDenoiser(ref OfflineSpeechDenoiserConfig config);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyOfflineSpeechDenoiser(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern int SherpaOnnxOfflineSpeechDenoiserGetSampleRate(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxOfflineSpeechDenoiserRun(IntPtr handle, float[] samples, int n, int sampleRate);
}
}

View File

@@ -0,0 +1,16 @@
/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflineSpeechDenoiserConfig
{
public OfflineSpeechDenoiserConfig()
{
Model = new OfflineSpeechDenoiserModelConfig();
}
public OfflineSpeechDenoiserModelConfig Model;
}
}

View File

@@ -0,0 +1,17 @@
/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflineSpeechDenoiserGtcrnModelConfig
{
public OfflineSpeechDenoiserGtcrnModelConfig()
{
Model = "";
}
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
}
}

View File

@@ -0,0 +1,27 @@
/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang)
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflineSpeechDenoiserModelConfig
{
public OfflineSpeechDenoiserModelConfig()
{
Gtcrn = new OfflineSpeechDenoiserGtcrnModelConfig();
NumThreads = 1;
Debug = 0;
Provider = "cpu";
}
public OfflineSpeechDenoiserGtcrnModelConfig Gtcrn;
public int NumThreads;
public int Debug;
[MarshalAs(UnmanagedType.LPStr)]
public string Provider;
}
}