yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
1.6 KiB64 linesraw
1using System.Runtime.InteropServices;
2
3namespace Whisper.Internal
4{
5	/// <summary>Function pointer to report model loading progress</summary>
6	[UnmanagedFunctionPointer( CallingConvention.StdCall )]
7	delegate int pfnLoadProgress( double progress, IntPtr pv );
8
9	/// <summary>Function pointer to implement cooperative cancellation</summary>
10	[UnmanagedFunctionPointer( CallingConvention.StdCall )]
11	delegate int pfnCancel( IntPtr pv );
12
13	/// <summary>Callback functions for loading models</summary>
14	public struct sLoadModelCallbacks
15	{
16		/// <summary>Function pointer to report model loading progress</summary>
17		[MarshalAs( UnmanagedType.FunctionPtr )]
18		pfnLoadProgress? progress;
19
20		/// <summary>Function pointer to implement cooperative cancellation</summary>
21		[MarshalAs( UnmanagedType.FunctionPtr )]
22		pfnCancel? cancel;
23
24		// Not needed in C#, delegates can capture things
25		IntPtr pv;
26
27		/// <summary>Wrap idiomatic C# things into these low-level C callbacks</summary>
28		internal sLoadModelCallbacks( CancellationToken cancelToken, Action<double>? pfnProgress )
29		{
30			if( cancelToken != CancellationToken.None )
31			{
32				cancel = delegate ( IntPtr pv )
33				{
34					if( cancelToken.IsCancellationRequested )
35						return 1;   // S_FALSE
36					return 0;   // S_OK
37				};
38			}
39			else
40				cancel = null;
41
42			if( null != pfnProgress )
43			{
44				progress = delegate ( double val, IntPtr pv )
45				{
46					try
47					{
48						pfnProgress( val );
49						return 0;   // S_OK
50					}
51					catch( Exception ex )
52					{
53						NativeLogger.captureException( ex );
54						return ex.HResult;
55					}
56				};
57			}
58			else
59				progress = null;
60
61			pv = IntPtr.Zero;
62		}
63	}
64}