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

KonstantinMinorc4cf795

master
4.7 KiB218 linesraw
1#include "stdafx.h"
2#include "AppState.h"
3#include "Utils/miscUtils.h"
4#include <commctrl.h>
5#pragma comment(lib, "Comctl32.lib")
6#include "CircleIndicator.h"
7
8namespace
9{
10	static const HKEY regKeyRoot = HKEY_CURRENT_USER;
11	const LPCTSTR regKey = LR"(SOFTWARE\const.me\WhisperDesktop)";
12	const LPCTSTR regValPath = L"modelPath";
13	const LPCTSTR regValImpl = L"modelImpl";
14	const LPCTSTR regValLang = L"language";
15	const LPCTSTR regValLastScreen = L"screen";
16	const LPCTSTR regValGpuFlags = L"gpuFlags";
17
18	static HRESULT readString( CRegKey& k, LPCTSTR name, CString& rdi )
19	{
20		ULONG nChars = 0;
21		LSTATUS lss = k.QueryStringValue( name, nullptr, &nChars );
22		if( lss != ERROR_SUCCESS )
23			return HRESULT_FROM_WIN32( lss );
24		if( nChars == 0 )
25		{
26			rdi = L"";
27			return S_FALSE;
28		}
29
30		lss = k.QueryStringValue( name, rdi.GetBufferSetLength( nChars ), &nChars );
31		rdi.ReleaseBuffer();
32		if( lss != ERROR_SUCCESS )
33			return HRESULT_FROM_WIN32( lss );
34
35		return S_OK;
36	}
37
38	using Whisper::eModelImplementation;
39}
40
41HRESULT AppState::startup()
42{
43	HRESULT hr = CoInitializeEx( nullptr, COINIT_MULTITHREADED );
44	if( FAILED( hr ) )
45	{
46		reportFatalError( "CoInitializeEx failed", hr );
47		return hr;
48	}
49	coInit = true;
50
51	LSTATUS lss = registryKey.Create( regKeyRoot, regKey );
52	if( lss != ERROR_SUCCESS )
53	{
54		hr = HRESULT_FROM_WIN32( lss );
55		reportFatalError( "Unable to open the registry key", hr );
56		return hr;
57	}
58
59	INITCOMMONCONTROLSEX init;
60	init.dwSize = sizeof( init );
61	init.dwICC = ICC_LINK_CLASS | ICC_PROGRESS_CLASS | ICC_STANDARD_CLASSES | ICC_TAB_CLASSES;
62	const BOOL icc = InitCommonControlsEx( &init );
63	if( !icc )
64	{
65		reportFatalError( "InitCommonControlsEx failed", HRESULT_FROM_WIN32( GetLastError() ) );
66		return E_FAIL;
67	}
68
69	hr = initMediaFoundation( &mediaFoundation );
70	if( FAILED( hr ) )
71	{
72		reportFatalError( "Unable to initialize Media Foundation runtime", hr );
73		return hr;
74	}
75
76	hr = console.initialize();
77	if( FAILED( hr ) )
78	{
79		reportFatalError( "Unable to initialize logging", hr );
80		return hr;
81	}
82
83	hr = CircleIndicator::registerClass();
84	if( FAILED( hr ) )
85	{
86		reportFatalError( "Unable to register custom controls", hr );
87		return hr;
88	}
89	appIcon.LoadIcon( IDI_WHISPERDESKTOP );
90	return S_OK;
91}
92
93AppState::~AppState()
94{
95	if( coInit )
96	{
97		CoUninitialize();
98		coInit = false;
99	}
100}
101
102HRESULT AppState::findModelSource()
103{
104	CHECK( readString( registryKey, regValPath, source.path ) );
105
106	{
107		CAtlFile file;
108		CHECK( file.Create( source.path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING ) );
109		ULONGLONG len;
110		CHECK( file.GetSize( len ) );
111		source.sizeInBytes = len;
112	}
113
114	CString impl;
115	CHECK( readString( registryKey, regValImpl, impl ) );
116	CHECK( implParse( impl, source.impl ) );
117	source.found = true;
118	return S_OK;
119}
120
121HRESULT AppState::saveModelSource()
122{
123	LSTATUS lss = registryKey.SetStringValue( regValPath, source.path );
124	if( lss != ERROR_SUCCESS )
125		return HRESULT_FROM_WIN32( lss );
126
127	LPCTSTR impl = implString( source.impl );
128	if( nullptr == impl )
129		return E_INVALIDARG;
130	lss = registryKey.SetStringValue( regValImpl, impl );
131	if( lss != ERROR_SUCCESS )
132		return HRESULT_FROM_WIN32( lss );
133
134	return S_OK;
135}
136
137uint32_t AppState::languageRead()
138{
139	DWORD dw;
140	LSTATUS lss = registryKey.QueryDWORDValue( regValLang, dw );
141	if( lss == ERROR_SUCCESS )
142		return dw;
143	return UINT_MAX;
144}
145
146void AppState::languageWrite( uint32_t key )
147{
148	registryKey.SetDWORDValue( regValLang, key );
149}
150
151CString AppState::stringLoad( LPCTSTR name )
152{
153	CString res;
154	readString( registryKey, name, res );
155	return res;
156}
157void AppState::stringStore( LPCTSTR name, LPCTSTR value )
158{
159	registryKey.SetStringValue( name, value );
160}
161uint32_t AppState::dwordLoad( LPCTSTR name, uint32_t fallback )
162{
163	DWORD dw;
164	LSTATUS lss = registryKey.QueryDWORDValue( name, dw );
165	if( lss == ERROR_SUCCESS )
166		return dw;
167	return fallback;
168}
169void AppState::dwordStore( LPCTSTR name, uint32_t value )
170{
171	registryKey.SetDWORDValue( name, value );
172}
173
174void AppState::lastScreenSave( HRESULT code )
175{
176	dwordStore( regValLastScreen, (uint32_t)code );
177}
178
179HRESULT AppState::lastScreenLoad()
180{
181	return (HRESULT)dwordLoad( regValLastScreen, SCREEN_TRANSCRIBE );
182}
183
184void AppState::setupIcon( CWindow* wnd )
185{
186	HICON ic = appIcon;
187	if( nullptr != ic )
188	{
189		wnd->SendMessage( WM_SETICON, ICON_SMALL, (LPARAM)ic );
190		wnd->SendMessage( WM_SETICON, ICON_BIG, (LPARAM)ic );
191	}
192}
193
194uint32_t AppState::gpuFlagsLoad()
195{
196	return dwordLoad( regValGpuFlags, 0 );
197}
198
199void AppState::gpuFlagsStore( uint32_t flags )
200{
201	if( 0 == flags )
202		registryKey.DeleteValue( regValGpuFlags );
203	else
204		dwordStore( regValGpuFlags, flags );
205}
206
207bool AppState::boolLoad( LPCTSTR name )
208{
209	return dwordLoad( name, 0 ) != 0;
210}
211
212void AppState::boolStore( LPCTSTR name, bool val )
213{
214	if( val )
215		dwordStore( name, 1 );
216	else
217		registryKey.DeleteValue( name );
218}