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.7 KiB68 linesraw
1using System.Runtime.InteropServices;
2
3/// <summary>Utility class to setup console coloring with ANSI codes.</summary>
4/// <remarks>The feature requires Windows 10 or newer</remarks>
5static class AnsiCodes
6{
7	const string dll = "kernel32.dll";
8
9	[DllImport( dll, SetLastError = true )]
10	static extern IntPtr GetStdHandle( int nStdHandle );
11
12	const int STD_OUTPUT_HANDLE = -11;
13
14	[Flags]
15	enum ConsoleModes: uint
16	{
17		// Input flags
18		ENABLE_PROCESSED_INPUT = 0x0001,
19		ENABLE_LINE_INPUT = 0x0002,
20		ENABLE_ECHO_INPUT = 0x0004,
21		ENABLE_WINDOW_INPUT = 0x0008,
22		ENABLE_MOUSE_INPUT = 0x0010,
23		ENABLE_INSERT_MODE = 0x0020,
24		ENABLE_QUICK_EDIT_MODE = 0x0040,
25		ENABLE_EXTENDED_FLAGS = 0x0080,
26		ENABLE_AUTO_POSITION = 0x0100,
27		ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200,
28
29		// Output flags
30		ENABLE_PROCESSED_OUTPUT = 0x0001,
31		ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002,
32		ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004,
33		DISABLE_NEWLINE_AUTO_RETURN = 0x0008,
34		ENABLE_LVB_GRID_WORLDWIDE = 0x0010
35	}
36
37	[DllImport( dll, SetLastError = true )]
38	static extern bool GetConsoleMode( IntPtr hConsoleHandle, out ConsoleModes mode );
39
40	[DllImport( dll, SetLastError = true )]
41	static extern bool SetConsoleMode( IntPtr hConsoleHandle, ConsoleModes mode );
42
43	static AnsiCodes()
44	{
45		IntPtr h = GetStdHandle( STD_OUTPUT_HANDLE );
46		IntPtr INVALID_HANDLE_VALUE = (IntPtr)( -1 );
47		if( h == INVALID_HANDLE_VALUE )
48			return;
49
50		if( !GetConsoleMode( h, out ConsoleModes mode ) )
51			return;
52
53		if( mode.HasFlag( ConsoleModes.ENABLE_VIRTUAL_TERMINAL_PROCESSING ) )
54		{
55			enabled = true;
56			return;
57		}
58
59		mode |= ConsoleModes.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
60		if( SetConsoleMode( h, mode ) )
61		{
62			enabled = true;
63			return;
64		}
65	}
66
67	public static readonly bool enabled = false;
68}