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.4 KiB61 linesraw
1using System.Runtime.ExceptionServices;
2using Whisper;
3
4namespace MicrophoneCS
5{
6	sealed class CaptureThread: CaptureCallbacks
7	{
8		public CaptureThread( CommandLineArgs args, Context context, iAudioCapture source )
9		{
10			callbacks = new TranscribeCallbacks( args );
11			this.context = context;
12			this.source = source;
13
14			thread = new Thread( threadMain ) { Name = "Capture Thread" };
15			Console.WriteLine( "Press any key to quit" );
16			thread.Start();
17		}
18
19		static void readKeyCallback( object? state )
20		{
21			CaptureThread ct = ( state as CaptureThread ) ?? throw new ApplicationException();
22			Console.ReadKey();
23			ct.shouldQuit = true;
24		}
25
26		public void join()
27		{
28			ThreadPool.QueueUserWorkItem( readKeyCallback, this );
29			thread.Join();
30			edi?.Throw();
31		}
32
33		volatile bool shouldQuit = false;
34
35		protected override bool shouldCancel( Context sender ) =>
36			shouldQuit;
37
38		protected override void captureStatusChanged( Context sender, eCaptureStatus status )
39		{
40			Console.WriteLine( $"CaptureStatusChanged: {status}" );
41		}
42
43		readonly TranscribeCallbacks callbacks;
44		readonly Thread thread;
45		readonly Context context;
46		readonly iAudioCapture source;
47		ExceptionDispatchInfo? edi = null;
48
49		void threadMain()
50		{
51			try
52			{
53				context.runCapture( source, callbacks, this );
54			}
55			catch( Exception ex )
56			{
57				edi = ExceptionDispatchInfo.Capture( ex );
58			}
59		}
60	}
61}