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

Konstantin“Text with timestamps” output format option509c0ca

master
4.1 KiB134 linesraw
1#pragma once
2#include "AppState.h"
3#include "Utils/WTL/atlddx.h"
4#include "Utils/WTL/atlcrack.h"
5#include "Utils/miscUtils.h"
6#include "Utils/LanguageDropdown.h"
7#include "Utils/TranslateCheckbox.h"
8#include "Utils/PendingState.h"
9
10class TranscribeDlg :
11	public CDialogImpl<TranscribeDlg>,
12	public CWinDataExchange<TranscribeDlg>,
13	public iThreadPoolCallback
14{
15	AppState& appState;
16
17public:
18	static constexpr UINT IDD = IDD_TRANSCRIBE_DIALOG;
19	static constexpr UINT WM_CALLBACK_STATUS = WM_APP + 1;
20
21	TranscribeDlg( AppState& app ) : appState( app ) { }
22
23	// Show this dialog modally, without parent.
24	HRESULT show();
25
26	BEGIN_MSG_MAP( LoadModelDlg )
27		MESSAGE_HANDLER( WM_INITDIALOG, OnInitDialog )
28		ON_BUTTON_CLICK( IDC_CONSOLE, cbConsole.click )
29		ON_BUTTON_CLICK( IDCANCEL, onClose )
30		ON_BUTTON_CLICK( IDC_BACK, onBack )
31		ON_BUTTON_CLICK( IDC_USE_INPUT_FOLDER, onInputFolderCheck )
32		ON_BUTTON_CLICK( IDC_BROWSE_MEDIA, onBrowseMedia )
33		ON_BUTTON_CLICK( IDC_BROWSE_RESULT, onBrowseOutput )
34		ON_BUTTON_CLICK( IDC_TRANSCRIBE, onTranscribe )
35		ON_BUTTON_CLICK( IDC_CAPTURE, onCapture )
36		COMMAND_HANDLER( IDC_OUTPUT_FORMAT, CBN_SELCHANGE, onOutFormatChange )
37		COMMAND_HANDLER( IDC_PATH_MEDIA, EN_CHANGE, onInputChange )
38		MESSAGE_HANDLER( WM_CALLBACK_STATUS, onCallbackStatus )
39		MSG_WM_CLOSE( onWmClose )
40	END_MSG_MAP()
41
42	BEGIN_DDX_MAP( LoadModelDlg )
43		DDX_CONTROL_HANDLE( IDC_MODEL_DESC, modelDesc )
44		DDX_CONTROL_HANDLE( IDC_PATH_MEDIA, sourceMediaPath )
45		DDX_CONTROL_HANDLE( IDC_OUTPUT_FORMAT, transcribeOutFormat )
46		DDX_CONTROL_HANDLE( IDC_USE_INPUT_FOLDER, useInputFolder )
47		DDX_CONTROL_HANDLE( IDC_PATH_RESULT, transcribeOutputPath )
48		DDX_CONTROL_HANDLE( IDC_BROWSE_RESULT, transcribeOutputBrowse );
49		DDX_CONTROL_HANDLE( IDC_TRANSCRIBE_PROGRESS, progressBar );
50	END_DDX_MAP()
51
52private:
53	PendingState pendingState;
54	void setPending( bool nowPending );
55	void transcribeError( LPCTSTR text, HRESULT hr = S_FALSE );
56
57	LRESULT OnInitDialog( UINT nMessage, WPARAM wParam, LPARAM lParam, BOOL& bHandled );
58
59	void onClose()
60	{
61		ATLVERIFY( EndDialog( IDCANCEL ) );
62	}
63	void onBack()
64	{
65		ATLVERIFY( EndDialog( IDC_BACK ) );
66	}
67
68	void printModelDescription();
69	CStatic modelDesc;
70	ConsoleCheckbox cbConsole;
71
72	LanguageDropdown languageSelector;
73	TranslateCheckbox cbTranslate;
74
75	CEdit sourceMediaPath;
76	CButton useInputFolder;
77	CEdit transcribeOutputPath;
78	CButton transcribeOutputBrowse;
79	CComboBox transcribeOutFormat;
80	CProgressBarCtrl progressBar;
81	void populateOutputFormats();
82
83	LRESULT onOutFormatChange( UINT, INT, HWND, BOOL& bHandled );
84	LRESULT onInputChange( UINT, INT, HWND, BOOL& );
85	void onInputFolderCheck();
86	void onBrowseMedia();
87	void onBrowseOutput();
88	void onTranscribe();
89	void onCapture()
90	{
91		EndDialog( IDC_CAPTURE );
92	}
93
94	ThreadPoolWork work;
95
96	enum struct eOutputFormat : uint8_t;
97
98	struct TranscribeArgs
99	{
100		CString pathMedia;
101		CString pathOutput;
102		uint32_t language;
103		bool translate;
104		eOutputFormat format;
105		Whisper::eResultFlags resultFlags;
106		uint64_t startTime;
107		int64_t mediaDuration;
108		CString errorMessage;
109	};
110	TranscribeArgs transcribeArgs;
111
112	void __stdcall poolCallback() noexcept override final;
113
114	LRESULT onCallbackStatus( UINT, WPARAM wParam, LPARAM, BOOL& bHandled );
115
116	HRESULT transcribe();
117	void getThreadError();
118	
119	static HRESULT writeTextFile( const Whisper::sSegment* const segments, const size_t length, CAtlFile& file, bool timestamps );
120	static HRESULT writeSubRip( const Whisper::sSegment* const segments, const size_t length, CAtlFile& file );
121	static HRESULT writeWebVTT( const Whisper::sSegment* const segments, const size_t length, CAtlFile& file );
122
123	static HRESULT __cdecl newSegmentCallbackStatic( Whisper::iContext* ctx, uint32_t n_new, void* user_data ) noexcept;
124	HRESULT newSegmentCallback( Whisper::iContext* ctx, uint32_t n_new );
125
126	static HRESULT __cdecl progressCallbackStatic( double p, Whisper::iContext* ctx, void* pv ) noexcept;
127	HRESULT progressCallback( double p ) noexcept;
128
129	void onWmClose();
130	// Populate output path based on the provided input media path
131	void setOutputPath( const CString& input );
132	// Populate output path based on the input media path in the edit box
133	void setOutputPath();
134};