From 8c4603c73675958efc960fbd4bb599a2909d106a Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 16 Jan 2023 14:52:43 +0100 Subject: Source codes --- Examples/MicrophoneCS/CaptureThread.cs | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Examples/MicrophoneCS/CaptureThread.cs (limited to 'Examples/MicrophoneCS/CaptureThread.cs') diff --git a/Examples/MicrophoneCS/CaptureThread.cs b/Examples/MicrophoneCS/CaptureThread.cs new file mode 100644 index 0000000..b76a929 --- /dev/null +++ b/Examples/MicrophoneCS/CaptureThread.cs @@ -0,0 +1,61 @@ +using System.Runtime.ExceptionServices; +using Whisper; + +namespace MicrophoneCS +{ + sealed class CaptureThread: CaptureCallbacks + { + public CaptureThread( CommandLineArgs args, Context context, iAudioCapture source ) + { + callbacks = new TranscribeCallbacks( args ); + this.context = context; + this.source = source; + + thread = new Thread( threadMain ) { Name = "Capture Thread" }; + Console.WriteLine( "Press any key to quit" ); + thread.Start(); + } + + static void readKeyCallback( object? state ) + { + CaptureThread ct = ( state as CaptureThread ) ?? throw new ApplicationException(); + Console.ReadKey(); + ct.shouldQuit = true; + } + + public void join() + { + ThreadPool.QueueUserWorkItem( readKeyCallback, this ); + thread.Join(); + edi?.Throw(); + } + + volatile bool shouldQuit = false; + + protected override bool shouldCancel( Context sender ) => + shouldQuit; + + protected override void captureStatusChanged( Context sender, eCaptureStatus status ) + { + Console.WriteLine( $"CaptureStatusChanged: {status}" ); + } + + readonly TranscribeCallbacks callbacks; + readonly Thread thread; + readonly Context context; + readonly iAudioCapture source; + ExceptionDispatchInfo? edi = null; + + void threadMain() + { + try + { + context.runCapture( source, callbacks, this ); + } + catch( Exception ex ) + { + edi = ExceptionDispatchInfo.Capture( ex ); + } + } + } +} \ No newline at end of file -- cgit v1.2.3