Repository navigation
Expand file tree
/
Copy pathwtalk.cpp
More file actions
307 lines (272 loc) · 10.3 KB
/
Copy pathwtalk.cpp
File metadata and controls
307 lines (272 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <mmsystem.h>
#include <vector>
#include <string>
#include <thread>
#include <cstdint>
#include <cstdio>
#include "whisper.h"
#pragma comment(lib, "winmm.lib")
#define WM_DONE (WM_USER + 1)
#define IDC_EDIT 1
#define IDC_BTN 2
#define IDC_CUT 3
#define HOTKEY_RAISE 1
static const wchar_t* MODEL_NAME = L"ggml-base.en.bin";
static const wchar_t* MODEL_FALLBACK = L"C:\\whisper.cpp\\ggml-base.en.bin";
static const int SAMPLE_RATE = 16000;
static const int NUM_BUFS = 8;
static const int BUF_SAMPLES = SAMPLE_RATE / 10; // 100 ms
static const int BUF_BYTES = BUF_SAMPLES * 2;
enum State { IDLE, RECORDING, STOPPING, TRANSCRIBING };
static HWND g_hMain;
static HWND g_hEdit;
static HWND g_hBtn;
static whisper_context* g_ctx;
static State g_state = IDLE;
static HWAVEIN g_hWaveIn;
static WAVEHDR g_headers[NUM_BUFS];
static int16_t g_bufs[NUM_BUFS][BUF_SAMPLES];
static std::vector<int16_t> g_captured;
static int g_outstanding = 0;
static std::wstring g_result;
static HWND g_hCut;
static HACCEL g_hAccel;
static HFONT g_hFont;
static void layout(HWND hwnd) {
RECT rc;
GetClientRect(hwnd, &rc);
int bh = 44;
int half = rc.right / 2;
SetWindowPos(g_hEdit, nullptr, 0, 0, rc.right, rc.bottom - bh, SWP_NOZORDER);
SetWindowPos(g_hBtn, nullptr, 0, rc.bottom - bh, half, bh, SWP_NOZORDER);
SetWindowPos(g_hCut, nullptr, half, rc.bottom - bh, rc.right - half, bh, SWP_NOZORDER);
}
static void requeue(WAVEHDR* hdr) {
hdr->dwFlags = 0;
waveInPrepareHeader(g_hWaveIn, hdr, sizeof(WAVEHDR));
waveInAddBuffer(g_hWaveIn, hdr, sizeof(WAVEHDR));
g_outstanding++;
}
static void exe_relative(const wchar_t* name, wchar_t* out) {
GetModuleFileNameW(nullptr, out, MAX_PATH);
wchar_t* slash = wcsrchr(out, L'\\');
if (slash) wcscpy(slash + 1, name);
else wcscpy(out, name);
}
// Prefer the model next to the executable, as shipped in the release zip;
// fall back to the whisper.cpp checkout used by the build instructions.
static std::string find_model() {
wchar_t path[MAX_PATH];
exe_relative(MODEL_NAME, path);
if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
wcscpy(path, MODEL_FALLBACK);
char narrow[MAX_PATH * 2];
int n = WideCharToMultiByte(CP_ACP, 0, path, -1, narrow, sizeof(narrow), nullptr, nullptr);
return n > 0 ? std::string(narrow) : std::string();
}
static void append_to_log(const std::string& text) {
wchar_t path[MAX_PATH];
exe_relative(L"wtalk.log", path);
FILE* f = _wfopen(path, L"ab");
if (!f) return;
SYSTEMTIME st;
GetLocalTime(&st);
char header[32];
int hlen = sprintf(header, "[%04d-%02d-%02d %02d:%02d:%02d] ",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond);
fwrite(header, 1, hlen, f);
fwrite(text.c_str(), 1, text.size(), f);
fwrite("\n", 1, 1, f);
fclose(f);
}
static void finish_recording() {
waveInClose(g_hWaveIn);
g_state = TRANSCRIBING;
std::thread([captured = std::move(g_captured)]() {
std::vector<float> samples(captured.size());
for (size_t i = 0; i < captured.size(); i++)
samples[i] = captured[i] / 32768.0f;
whisper_full_params p = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
p.print_progress = false;
p.print_realtime = false;
p.print_timestamps = false;
std::string text;
if (!samples.empty() &&
whisper_full(g_ctx, p, samples.data(), (int)samples.size()) == 0) {
int n = whisper_full_n_segments(g_ctx);
for (int i = 0; i < n; i++) {
const char* s = whisper_full_get_segment_text(g_ctx, i);
if (s) text += s;
}
if (!text.empty() && text[0] == ' ')
text.erase(0, 1);
}
if (!text.empty())
append_to_log(text);
int wlen = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, nullptr, 0);
std::wstring wtext(wlen > 1 ? wlen - 1 : 0, L'\0');
if (wlen > 1)
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, wtext.data(), wlen);
g_result = std::move(wtext);
PostMessageW(g_hMain, WM_DONE, 0, 0);
}).detach();
}
static void start_recording() {
WAVEFORMATEX wfx = {};
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 1;
wfx.nSamplesPerSec = SAMPLE_RATE;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = 2;
wfx.nAvgBytesPerSec = SAMPLE_RATE * 2;
if (waveInOpen(&g_hWaveIn, WAVE_MAPPER, &wfx,
(DWORD_PTR)g_hMain, 0, CALLBACK_WINDOW) != MMSYSERR_NOERROR) {
MessageBoxW(g_hMain, L"Failed to open microphone", L"wtalk", MB_ICONERROR);
return;
}
g_captured.clear();
g_outstanding = 0;
for (int i = 0; i < NUM_BUFS; i++) {
g_headers[i] = {};
g_headers[i].lpData = (LPSTR)g_bufs[i];
g_headers[i].dwBufferLength = BUF_BYTES;
waveInPrepareHeader(g_hWaveIn, &g_headers[i], sizeof(WAVEHDR));
waveInAddBuffer(g_hWaveIn, &g_headers[i], sizeof(WAVEHDR));
g_outstanding++;
}
waveInStart(g_hWaveIn);
g_state = RECORDING;
SetWindowTextW(g_hBtn, L"Stop (F10)");
}
static void stop_recording() {
g_state = STOPPING;
SetWindowTextW(g_hBtn, L"Transcribing...");
EnableWindow(g_hBtn, FALSE);
waveInReset(g_hWaveIn);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_CREATE: {
g_hEdit = CreateWindowExW(0, L"EDIT", nullptr,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN,
0, 0, 0, 0, hwnd, (HMENU)IDC_EDIT, nullptr, nullptr);
g_hBtn = CreateWindowW(L"BUTTON", L"Record (F10)",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0, hwnd, (HMENU)IDC_BTN, nullptr, nullptr);
g_hCut = CreateWindowW(L"BUTTON", L"Cut to Clipboard (F12)",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0, hwnd, (HMENU)IDC_CUT, nullptr, nullptr);
g_hFont = CreateFontW(-24, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Segoe UI");
SendMessageW(g_hEdit, WM_SETFONT, (WPARAM)g_hFont, FALSE);
SendMessageW(g_hBtn, WM_SETFONT, (WPARAM)g_hFont, FALSE);
SendMessageW(g_hCut, WM_SETFONT, (WPARAM)g_hFont, FALSE);
ACCEL accels[] = {
{ FVIRTKEY, VK_F10, IDC_BTN },
{ FVIRTKEY, VK_F12, IDC_CUT },
};
g_hAccel = CreateAcceleratorTableW(accels, 2);
g_ctx = whisper_init_from_file_with_params(find_model().c_str(),
whisper_context_default_params());
if (!g_ctx) {
MessageBoxW(hwnd,
L"Failed to load the whisper model.\n\n"
L"Put ggml-base.en.bin in the same folder as wtalk.exe. Download it from:\n"
L"https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin",
L"wtalk", MB_ICONERROR);
return -1;
}
RegisterHotKey(hwnd, HOTKEY_RAISE, 0, VK_ADD);
return 0;
}
case WM_SIZE:
layout(hwnd);
return 0;
case WM_COMMAND:
if (LOWORD(wp) == IDC_BTN) {
if (g_state == IDLE) start_recording();
else if (g_state == RECORDING) stop_recording();
} else if (LOWORD(wp) == IDC_CUT) {
SendMessageW(g_hEdit, EM_SETSEL, 0, -1);
SendMessageW(g_hEdit, WM_COPY, 0, 0);
SendMessageW(g_hEdit, WM_SETTEXT, 0, (LPARAM)L"");
ShowWindow(g_hMain, SW_MINIMIZE);
}
return 0;
case MM_WIM_DATA: {
WAVEHDR* hdr = (WAVEHDR*)lp;
if (g_state == RECORDING) {
int n = hdr->dwBytesRecorded / sizeof(int16_t);
auto* p = (int16_t*)hdr->lpData;
g_captured.insert(g_captured.end(), p, p + n);
waveInUnprepareHeader(g_hWaveIn, hdr, sizeof(WAVEHDR));
g_outstanding--;
requeue(hdr);
} else if (g_state == STOPPING) {
waveInUnprepareHeader(g_hWaveIn, hdr, sizeof(WAVEHDR));
if (--g_outstanding == 0)
finish_recording();
}
return 0;
}
case WM_DONE: {
if (!g_result.empty()) {
int len = GetWindowTextLengthW(g_hEdit);
SendMessageW(g_hEdit, EM_SETSEL, len, len);
if (len > 0)
SendMessageW(g_hEdit, EM_REPLACESEL, FALSE, (LPARAM)L"\r\n");
SendMessageW(g_hEdit, EM_REPLACESEL, FALSE, (LPARAM)g_result.c_str());
}
g_state = IDLE;
SetWindowTextW(g_hBtn, L"Record (F10)");
EnableWindow(g_hBtn, TRUE);
return 0;
}
case WM_HOTKEY:
if (wp == HOTKEY_RAISE) {
ShowWindow(g_hMain, SW_RESTORE);
SetForegroundWindow(g_hMain);
if (g_state == IDLE)
start_recording();
else if (g_state == RECORDING)
stop_recording();
}
return 0;
case WM_DESTROY:
UnregisterHotKey(g_hMain, HOTKEY_RAISE);
if (g_ctx) whisper_free(g_ctx);
if (g_hFont) DeleteObject(g_hFont);
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wp, lp);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow) {
WNDCLASSW wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = L"wtalk";
RegisterClassW(&wc);
g_hMain = CreateWindowW(L"wtalk", L"wtalk",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 900, 600,
nullptr, nullptr, hInst, nullptr);
ShowWindow(g_hMain, nCmdShow);
UpdateWindow(g_hMain);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0)) {
if (!TranslateAccelerator(g_hMain, g_hAccel, &msg)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return (int)msg.wParam;
}