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

KonstantinMinor, code generationd55bca0

master
2.1 KiB73 linesraw
1using System.Diagnostics;
2using System.Runtime.CompilerServices;
3using System.Runtime.InteropServices;
4using Whisper.Internal;
5
6namespace Whisper
7{
8	/// <summary>Extension methods of these COM interfaces</summary>
9	public static class ExtensionMethods
10	{
11		/// <summary>Create a context to transcribe audio with this model</summary>
12		public static Context createContext( this iModel model )
13		{
14			iContext ctx = model.createContextInternal();
15			return new Context( ctx );
16		}
17
18		/// <summary>Convert language into a short ID string, like <c>"en"</c></summary>
19		[SkipLocalsInit]
20		public static string getCode( this eLanguage lang )
21		{
22			unsafe
23			{
24				sbyte* ptr = stackalloc sbyte[ 5 ];
25				*(uint*)ptr = (uint)lang;
26				ptr[ 4 ] = 0;
27				return new string( ptr );
28			}
29		}
30
31		/// <summary>Resolve integer token ID into string.</summary>
32		/// <remarks>If the token ID was not found in the model, the method returns null without raising exceptions.</remarks>
33		public static string? stringFromToken( this iModel model, int idToken ) =>
34			Marshal.PtrToStringUTF8( model.stringFromTokenInternal( idToken ) );
35
36		/// <summary>List capture devices</summary>
37		public static CaptureDeviceId[]? listCaptureDevices( this iMediaFoundation mf )
38		{
39			CaptureDeviceId[]? result = null;
40
41			pfnFoundCaptureDevices pfn = delegate ( int len, sCaptureDevice[]? arr, IntPtr pv )
42			{
43				try
44				{
45					if( len == 0 || arr == null )
46						return 1;
47					Debug.Assert( len == arr.Length );
48
49					result = new CaptureDeviceId[ len ];
50					for( int i = 0; i < len; i++ )
51						result[ i ] = new CaptureDeviceId( arr[ i ] );
52					return 0;
53				}
54				catch( Exception ex )
55				{
56					NativeLogger.captureException( ex );
57					return ex.HResult;
58				}
59			};
60
61			mf.listCaptureDevices( pfn, IntPtr.Zero );
62
63			return result;
64		}
65
66		/// <summary>Open audio capture device</summary>
67		public static iAudioCapture openCaptureDevice( this iMediaFoundation mf, in CaptureDeviceId id, in sCaptureParams? cp = null )
68		{
69			sCaptureParams captureParams = cp ?? new sCaptureParams();
70			return mf.openCaptureDevice( id.endpoint, ref captureParams );
71		}
72	}
73}