|
| 1 | +/// Copyright (c) 2025 Xiaomi Corporation (authors: Fangjun Kuang) |
| 2 | +using System; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace SherpaOnnx |
| 7 | +{ |
| 8 | + public class DenoisedAudio |
| 9 | + { |
| 10 | + public DenoisedAudio(IntPtr p) |
| 11 | + { |
| 12 | + _handle = new HandleRef(this, p); |
| 13 | + } |
| 14 | + |
| 15 | + public bool SaveToWaveFile(String filename) |
| 16 | + { |
| 17 | + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); |
| 18 | + byte[] utf8Filename = Encoding.UTF8.GetBytes(filename); |
| 19 | + byte[] utf8FilenameWithNull = new byte[utf8Filename.Length + 1]; // +1 for null terminator |
| 20 | + Array.Copy(utf8Filename, utf8FilenameWithNull, utf8Filename.Length); |
| 21 | + utf8FilenameWithNull[utf8Filename.Length] = 0; // Null terminator |
| 22 | + int status = SherpaOnnxWriteWave(impl.Samples, impl.NumSamples, impl.SampleRate, utf8FilenameWithNull); |
| 23 | + return status == 1; |
| 24 | + } |
| 25 | + |
| 26 | + ~DenoisedAudio() |
| 27 | + { |
| 28 | + Cleanup(); |
| 29 | + } |
| 30 | + |
| 31 | + public void Dispose() |
| 32 | + { |
| 33 | + Cleanup(); |
| 34 | + // Prevent the object from being placed on the |
| 35 | + // finalization queue |
| 36 | + System.GC.SuppressFinalize(this); |
| 37 | + } |
| 38 | + |
| 39 | + private void Cleanup() |
| 40 | + { |
| 41 | + SherpaOnnxDestroyDenoisedAudio(Handle); |
| 42 | + |
| 43 | + // Don't permit the handle to be used again. |
| 44 | + _handle = new HandleRef(this, IntPtr.Zero); |
| 45 | + } |
| 46 | + |
| 47 | + [StructLayout(LayoutKind.Sequential)] |
| 48 | + struct Impl |
| 49 | + { |
| 50 | + public IntPtr Samples; |
| 51 | + public int NumSamples; |
| 52 | + public int SampleRate; |
| 53 | + } |
| 54 | + |
| 55 | + private HandleRef _handle; |
| 56 | + public IntPtr Handle => _handle.Handle; |
| 57 | + |
| 58 | + public int NumSamples |
| 59 | + { |
| 60 | + get |
| 61 | + { |
| 62 | + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); |
| 63 | + return impl.NumSamples; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public int SampleRate |
| 68 | + { |
| 69 | + get |
| 70 | + { |
| 71 | + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); |
| 72 | + return impl.SampleRate; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public float[] Samples |
| 77 | + { |
| 78 | + get |
| 79 | + { |
| 80 | + Impl impl = (Impl)Marshal.PtrToStructure(Handle, typeof(Impl)); |
| 81 | + |
| 82 | + float[] samples = new float[impl.NumSamples]; |
| 83 | + Marshal.Copy(impl.Samples, samples, 0, impl.NumSamples); |
| 84 | + return samples; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [DllImport(Dll.Filename)] |
| 89 | + private static extern void SherpaOnnxDestroyDenoisedAudio(IntPtr handle); |
| 90 | + |
| 91 | + [DllImport(Dll.Filename)] |
| 92 | + private static extern int SherpaOnnxWriteWave(IntPtr samples, int n, int sample_rate, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Filename); |
| 93 | + } |
| 94 | +} |
0 commit comments