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
d55bca0
master
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> 9public static class ExtensionMethods 10{ 11/// <summary>Create a context to transcribe audio with this model</summary> 12public static Context createContext ( this iModel model ) 13{ 14iContext ctx = model . createContextInternal (); 15return new Context ( ctx ); 16} 17 18/// <summary>Convert language into a short ID string, like <c>"en"</c></summary> 19[ SkipLocalsInit ] 20public static string getCode ( this eLanguage lang ) 21{ 22 unsafe 23{ 24sbyte * ptr = stackalloc sbyte [ 5 ]; 25* ( uint * ) ptr = ( uint ) lang ; 26 ptr[ 4 ] = 0 ; 27return 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> 33public static string ? stringFromToken ( this iModel model , int idToken ) => 34Marshal . PtrToStringUTF8 ( model . stringFromTokenInternal ( idToken ) ); 35 36/// <summary>List capture devices</summary> 37public static CaptureDeviceId [] ? listCaptureDevices ( this iMediaFoundation mf ) 38{ 39CaptureDeviceId [] ? result = null ; 40 41pfnFoundCaptureDevices pfn = delegate ( int len , sCaptureDevice [] ? arr , IntPtr pv ) 42{ 43try 44{ 45if ( len == 0 || arr == null ) 46return 1 ; 47Debug . Assert ( len == arr . Length ); 48 49result = new CaptureDeviceId [ len ]; 50for ( int i = 0 ; i < len ; i ++ ) 51result [ i ] = new CaptureDeviceId ( arr [ i ] ); 52return 0 ; 53} 54catch ( Exception ex ) 55{ 56NativeLogger . captureException ( ex ); 57return ex . HResult ; 58} 59}; 60 61mf . listCaptureDevices ( pfn , IntPtr . Zero ); 62 63return result ; 64} 65 66/// <summary>Open audio capture device</summary> 67public static iAudioCapture openCaptureDevice ( this iMediaFoundation mf , in CaptureDeviceId id , in sCaptureParams ? cp = null ) 68{ 69sCaptureParams captureParams = cp ?? new sCaptureParams (); 70return mf . openCaptureDevice ( id . endpoint , ref captureParams ); 71} 72} 73}