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.8 KiB87 linesraw
1#include "stdafx.h"
2#include "LanguageDropdown.h"
3#include "miscUtils.h"
4
5namespace
6{
7	inline wchar_t toUpper( wchar_t c )
8	{
9		size_t st = (uint16_t)c;
10		st = reinterpret_cast<size_t>( CharUpperW( reinterpret_cast<LPWSTR>( st ) ) );
11		return (wchar_t)(uint16_t)st;
12	}
13
14	void makeTitleCase( CString& s )
15	{
16		bool cap = true;
17		for( int i = 0; i < s.GetLength(); i++ )
18		{
19			wchar_t c = s[ i ];
20			if( cap )
21			{
22				c = toUpper( c );
23				s.SetAt( i, c );
24			}
25			cap = false;
26			if( c == ' ' )
27				cap = true;
28		}
29	}
30}
31
32int LanguageDropdown::getInitialSelection( AppState& state ) const
33{
34	constexpr uint32_t english = 0x6E65;
35
36	// Load preference from the registry
37	uint32_t id = state.languageRead();
38	if( id == UINT_MAX )
39		id = english;
40
41	auto it = std::find( keys.begin(), keys.end(), id );
42	if( it == keys.end() )
43	{
44		id = english;
45		it = std::find( keys.begin(), keys.end(), id );
46		assert( it != keys.end() );
47	}
48
49	ptrdiff_t idx = it - keys.begin();
50	return (int)idx;
51}
52
53void LanguageDropdown::initialize( HWND owner, int idc, AppState& state )
54{
55	m_hWnd = GetDlgItem( owner, idc );
56	assert( nullptr != m_hWnd );
57
58	Whisper::sLanguageList list;
59	Whisper::getSupportedLanguages( list );
60
61	const size_t length = list.length;
62	keys.resize( length );
63	CString utf16;
64	for( size_t i = 0; i < length; i++ )
65	{
66		keys[ i ] = list.pointer[ i ].key;
67		makeUtf16( utf16, list.pointer[ i ].name );
68		makeTitleCase( utf16 );
69		SendMessage( m_hWnd, CB_ADDSTRING, 0, (LPARAM)cstr( utf16 ) );
70	}
71
72	const int curSel = getInitialSelection( state );
73	SendMessage( m_hWnd, CB_SETCURSEL, curSel, 0 );
74}
75
76uint32_t LanguageDropdown::selectedLanguage()
77{
78	const int cs = (int)SendMessage( m_hWnd, CB_GETCURSEL, 0, 0 );
79	if( cs < 0 || cs >= keys.size() )
80		return UINT_MAX;
81	return keys[ cs ];
82}
83
84void LanguageDropdown::saveSelection( AppState& state )
85{
86	state.languageWrite( selectedLanguage() );
87}