Repository navigation
Expand file tree
/
Copy pathptt_whisper.cpp
More file actions
229 lines (196 loc) · 9.18 KB
/
Copy pathptt_whisper.cpp
File metadata and controls
229 lines (196 loc) · 9.18 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
// Absolute path to the core whisper header
#include "/home/sllu/whisper.cpp/include/whisper.h"
#include <SDL2/SDL.h>
#include <cstdio>
#include <string>
#include <vector>
#include <thread>
#include <chrono>
// sllu mod from Braille - starts here
#include <wchar.h> /* Required for wint_t and %lc */
#include <locale.h> /* Required to enable Unicode in terminal */
#include <liblouis/liblouis.h> // Braille library
#define MAX_BUFFER 2048
// sllu mod end
// Global audio structure matching SDL callbacks
struct AudioCaptureState {
std::vector<float> buffer;
bool is_recording = false;
};
// SDL Callback function to grab microphone chunks smoothly
void audio_callback(void* userdata, Uint8* stream, int len) {
AudioCaptureState* state = (AudioCaptureState*)userdata;
if (!state->is_recording) return;
int num_samples = len / sizeof(float);
float* incoming_samples = (float*)stream;
state->buffer.insert(state->buffer.end(), incoming_samples, incoming_samples + num_samples);
}
// Temporary internal struct to bridge your parameter variables without external common.h dependency
struct custom_whisper_params {
std::string model = "models/ggml-base.en.bin";
std::string language = "zh"; // Default to Chinese to test your fix, or "en"
bool translate = false;
bool no_timestamps = true;
bool no_context = true;
int n_threads = 4;
};
int main(int argc, char** argv) {
custom_whisper_params params;
// Direct manual parameter configuration check
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-m" && i + 1 < argc) params.model = argv[++i];
else if (arg == "-l" && i + 1 < argc) params.language = argv[++i];
else if (arg == "-t" && i + 1 < argc) params.n_threads = std::stoi(argv[++i]);
}
// 1. Initialize SDL2 Audio Subsystem
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "error: SDL could not initialize: %s\n", SDL_GetError());
return 1;
}
AudioCaptureState capture_state;
SDL_AudioSpec desired_spec, obtained_spec;
SDL_zero(desired_spec);
desired_spec.freq = 16000;
desired_spec.format = AUDIO_F32SYS;
desired_spec.channels = 1;
desired_spec.samples = 1024;
desired_spec.callback = audio_callback;
desired_spec.userdata = &capture_state;
SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(nullptr, 2, &desired_spec, &obtained_spec, 0);
if (audio_device == 0) {
fprintf(stderr, "error: Failed to open audio device: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_PauseAudioDevice(audio_device, 0);
// 2. Initialize Core Whisper Context Engine
struct whisper_context_params cparams = whisper_context_default_params();
cparams.use_gpu = true;
struct whisper_context* ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
if (ctx == nullptr) {
fprintf(stderr, "error: Failed to initialize whisper context\n");
SDL_CloseAudioDevice(audio_device);
SDL_Quit();
return 2;
}
std::vector<whisper_token> prompt_tokens;
// 3. Create Hardware Input Focus Window
SDL_Window* window = SDL_CreateWindow(
"Whisper Push-To-Talk",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
320, 100,
SDL_WINDOW_SHOWN | SDL_WINDOW_INPUT_FOCUS
);
printf("\n=======================================================\n");
printf(">>> CLICK THE 'Whisper Push-To-Talk' WINDOW TO FOCUS <<<\n");
printf(">>> HOLD [SPACEBAR] to talk. RELEASE to transcribe. <<<\n");
printf(">>> Press [Q] or close the window to exit safely. <<<\n");
printf("=======================================================\n\n");
fflush(stdout);
bool is_running = true;
while (is_running) {
SDL_Event event;
bool trigger_inference = false;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
is_running = false;
}
else if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
if (event.key.keysym.sym == SDLK_q) {
is_running = false;
}
else if (event.key.keysym.sym == SDLK_SPACE) {
SDL_LockAudioDevice(audio_device);
capture_state.buffer.clear();
capture_state.is_recording = true;
SDL_UnlockAudioDevice(audio_device);
printf("[Listening...] ");
fflush(stdout);
}
}
else if (event.type == SDL_KEYUP) {
if (event.key.keysym.sym == SDLK_SPACE && capture_state.is_recording) {
SDL_LockAudioDevice(audio_device);
capture_state.is_recording = false;
SDL_UnlockAudioDevice(audio_device);
printf("\n[Processing inference...]\n");
fflush(stdout);
trigger_inference = true;
}
}
}
if (!is_running) break;
// 4. Standalone Inference Pipeline execution
if (trigger_inference) {
SDL_LockAudioDevice(audio_device);
std::vector<float> pcmf32 = capture_state.buffer;
capture_state.buffer.clear();
SDL_UnlockAudioDevice(audio_device);
if (!pcmf32.empty()) {
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
wparams.print_progress = false;
wparams.print_special = false;
wparams.print_realtime = false;
wparams.print_timestamps = !params.no_timestamps;
wparams.translate = params.translate;
wparams.language = params.language.c_str();
wparams.n_threads = params.n_threads;
// =========================================================================
// SLLU ADDITION 1: CHINESE HALLUCINATION FIX
// =========================================================================
if (params.language == "zh") {
wparams.prompt_tokens = nullptr;
wparams.prompt_n_tokens = 0;
prompt_tokens.clear();
} else {
wparams.prompt_tokens = params.no_context ? nullptr : prompt_tokens.data();
wparams.prompt_n_tokens = params.no_context ? 0 : prompt_tokens.size();
}
// =========================================================================
if (whisper_full(ctx, wparams, pcmf32.data(), pcmf32.size()) != 0) {
fprintf(stderr, "error: Failed to process audio\n");
} else {
const int n_segments = whisper_full_n_segments(ctx);
for (int i = 0; i < n_segments; ++i) {
const char* text = whisper_full_get_segment_text(ctx, i);
// =========================================================================
// SLLU ADDITION 2: DUAL TEXT & BRAILLE OUTPUT PRINTING
// =========================================================================
if (params.no_timestamps) {
printf("%s", text);
fflush(stdout);
// sllu - print English braille grade 1 :: starts here
setlocale(LC_ALL, "");
const char *tableList = "en-ueb-g1.ctb,unicode.dis";
widechar inbuf[MAX_BUFFER];
widechar outbuf[MAX_BUFFER];
int inlen = (int)strlen(text);
int outlen = MAX_BUFFER;
for (int idx = 0; idx < inlen; idx++) inbuf[idx] = (widechar)text[idx];
if (lou_translateString(tableList, inbuf, &inlen, outbuf, &outlen, NULL, NULL, dotsIO|ucBrl)) {
for (int idx = 0; idx < outlen; idx++) printf("%lc", (wint_t)outbuf[idx]);
printf("\n");
}
else fprintf(stderr, "Translation failed. Check if 'unicode.dis' is installed.\n");
lou_free();
// sllu - print English braille grade 1 :: ends here
}
// =========================================================================
}
printf("\n>>> Ready (Hold Spacebar) <<<\n\n");
fflush(stdout);
}
} else {
printf("(No audio was captured)\n\n");
fflush(stdout);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(15));
}
SDL_DestroyWindow(window);
SDL_CloseAudioDevice(audio_device);
whisper_free(ctx);
SDL_Quit();
return 0;
}