summaryrefslogtreecommitdiffstats
path: root/WhisperNet/ExtensionMethods.cs
blob: 1bb594b1cef69db8d2f7021df44a172aadf7e21a (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Diagnostics;
using System.Runtime.InteropServices;
using Whisper.Internal;

namespace Whisper
{
	/// <summary>Extension methods of these COM interfaces</summary>
	public static class ExtensionMethods
	{
		/// <summary>Create a context to transcribe audio with this model</summary>
		public static Context createContext( this iModel model )
		{
			iContext ctx = model.createContextInternal();
			return new Context( ctx );
		}

		/// <summary>Convert language into a short ID string, like <c>"en"</c></summary>
		public static string getCode( this eLanguage lang )
		{
			unsafe
			{
				sbyte* ptr = stackalloc sbyte[ 5 ];
				*(uint*)ptr = (uint)lang;
				ptr[ 4 ] = 0;
				return new string( ptr );
			}
		}

		/// <summary>Resolve integer token ID into string.</summary>
		/// <remarks>If the token ID was not found in the model, the method returns null without raising exceptions.</remarks>
		public static string? stringFromToken( this iModel model, int idToken ) =>
			Marshal.PtrToStringUTF8( model.stringFromTokenInternal( idToken ) );

		/// <summary>List capture devices</summary>
		public static CaptureDeviceId[]? listCaptureDevices( this iMediaFoundation mf )
		{
			CaptureDeviceId[]? result = null;

			pfnFoundCaptureDevices pfn = delegate ( int len, sCaptureDevice[]? arr, IntPtr pv )
			{
				try
				{
					if( len == 0 || arr == null )
						return 1;
					Debug.Assert( len == arr.Length );

					result = new CaptureDeviceId[ len ];
					for( int i = 0; i < len; i++ )
						result[ i ] = new CaptureDeviceId( arr[ i ] );
					return 0;
				}
				catch( Exception ex )
				{
					NativeLogger.captureException( ex );
					return ex.HResult;
				}
			};

			mf.listCaptureDevices( pfn, IntPtr.Zero );

			return result;
		}

		/// <summary>Open audio capture device</summary>
		public static iAudioCapture openCaptureDevice( this iMediaFoundation mf, in CaptureDeviceId id, in sCaptureParams? cp = null )
		{
			sCaptureParams captureParams = cp ?? new sCaptureParams();
			return mf.openCaptureDevice( id.endpoint, ref captureParams );
		}
	}
}