summaryrefslogtreecommitdiffstats
path: root/Examples/MicrophoneCS/MicrophoneCS.cs
blob: c095ee182c11ee959b485a666f779d29b4556e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Whisper;

namespace MicrophoneCS
{
	static class Program
	{
		static int Main( string[] args )
		{
			try
			{
				CommandLineArgs cla;
				try
				{
					cla = new CommandLineArgs( args );
				}
				catch( OperationCanceledException )
				{
					return 1;
				}
				const eLoggerFlags loggerFlags = eLoggerFlags.UseStandardError | eLoggerFlags.SkipFormatMessage;
				Library.setLogSink( eLogLevel.Debug, loggerFlags );

				using iMediaFoundation mf = Library.initMediaFoundation();
				CaptureDeviceId[] devices = mf.listCaptureDevices() ??
					throw new ApplicationException( "This computer has no audio capture devices" );

				if( cla.listDevices )
				{
					for( int i = 0; i < devices.Length; i++ )
						Console.WriteLine( "#{0}: {1}", i, devices[ i ].displayName );
					return 0;
				}
				if( cla.captureDeviceIndex < 0 || cla.captureDeviceIndex >= devices.Length )
					throw new ApplicationException( $"Capture device index is out of range; the valid range is [ 0 .. {devices.Length - 1} ]" );

				using iAudioCapture captureDev = mf.openCaptureDevice( devices[ cla.captureDeviceIndex ] );

				using iModel model = Library.loadModel( cla.model );
				using Context context = model.createContext();
				cla.apply( ref context.parameters );

				CaptureThread thread = new CaptureThread( cla, context, captureDev );
				thread.join();

				context.timingsPrint();
				return 0;
			}
			catch( Exception ex )
			{
				// Console.WriteLine( ex.Message );
				Console.WriteLine( ex.ToString() );
				return ex.HResult;
			}
		}
	}
}