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