Spaces:
Running
Running
whisper.wasm : do not block page while processing (close #86)
Browse files- bindings/javascript/emscripten.cpp +23 -4
- bindings/javascript/whisper.js +0 -0
- examples/command/command.cpp +12 -3
- examples/stream/stream.cpp +4 -0
- examples/whisper.wasm/index-tmpl.html +0 -1
- whisper.cpp +6 -0
- whisper.h +1 -0
bindings/javascript/emscripten.cpp
CHANGED
|
@@ -6,10 +6,16 @@
|
|
| 6 |
#include <vector>
|
| 7 |
#include <thread>
|
| 8 |
|
|
|
|
|
|
|
| 9 |
std::vector<struct whisper_context *> g_contexts(4, nullptr);
|
| 10 |
|
| 11 |
EMSCRIPTEN_BINDINGS(whisper) {
|
| 12 |
emscripten::function("init", emscripten::optional_override([](const std::string & path_model) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
for (size_t i = 0; i < g_contexts.size(); ++i) {
|
| 14 |
if (g_contexts[i] == nullptr) {
|
| 15 |
g_contexts[i] = whisper_init(path_model.c_str());
|
|
@@ -25,6 +31,10 @@ EMSCRIPTEN_BINDINGS(whisper) {
|
|
| 25 |
}));
|
| 26 |
|
| 27 |
emscripten::function("free", emscripten::optional_override([](size_t index) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
--index;
|
| 29 |
|
| 30 |
if (index < g_contexts.size()) {
|
|
@@ -34,6 +44,10 @@ EMSCRIPTEN_BINDINGS(whisper) {
|
|
| 34 |
}));
|
| 35 |
|
| 36 |
emscripten::function("full_default", emscripten::optional_override([](size_t index, const emscripten::val & audio, const std::string & lang, bool translate) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
--index;
|
| 38 |
|
| 39 |
if (index >= g_contexts.size()) {
|
|
@@ -80,10 +94,15 @@ EMSCRIPTEN_BINDINGS(whisper) {
|
|
| 80 |
printf("\n");
|
| 81 |
}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
return
|
| 88 |
}));
|
| 89 |
}
|
|
|
|
| 6 |
#include <vector>
|
| 7 |
#include <thread>
|
| 8 |
|
| 9 |
+
std::thread g_worker;
|
| 10 |
+
|
| 11 |
std::vector<struct whisper_context *> g_contexts(4, nullptr);
|
| 12 |
|
| 13 |
EMSCRIPTEN_BINDINGS(whisper) {
|
| 14 |
emscripten::function("init", emscripten::optional_override([](const std::string & path_model) {
|
| 15 |
+
if (g_worker.joinable()) {
|
| 16 |
+
g_worker.join();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
for (size_t i = 0; i < g_contexts.size(); ++i) {
|
| 20 |
if (g_contexts[i] == nullptr) {
|
| 21 |
g_contexts[i] = whisper_init(path_model.c_str());
|
|
|
|
| 31 |
}));
|
| 32 |
|
| 33 |
emscripten::function("free", emscripten::optional_override([](size_t index) {
|
| 34 |
+
if (g_worker.joinable()) {
|
| 35 |
+
g_worker.join();
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
--index;
|
| 39 |
|
| 40 |
if (index < g_contexts.size()) {
|
|
|
|
| 44 |
}));
|
| 45 |
|
| 46 |
emscripten::function("full_default", emscripten::optional_override([](size_t index, const emscripten::val & audio, const std::string & lang, bool translate) {
|
| 47 |
+
if (g_worker.joinable()) {
|
| 48 |
+
g_worker.join();
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
--index;
|
| 52 |
|
| 53 |
if (index >= g_contexts.size()) {
|
|
|
|
| 94 |
printf("\n");
|
| 95 |
}
|
| 96 |
|
| 97 |
+
// run the worker
|
| 98 |
+
{
|
| 99 |
+
g_worker = std::thread([index, params, pcmf32 = std::move(pcmf32)]() {
|
| 100 |
+
whisper_reset_timings(g_contexts[index]);
|
| 101 |
+
whisper_full(g_contexts[index], params, pcmf32.data(), pcmf32.size());
|
| 102 |
+
whisper_print_timings(g_contexts[index]);
|
| 103 |
+
});
|
| 104 |
+
}
|
| 105 |
|
| 106 |
+
return 0;
|
| 107 |
}));
|
| 108 |
}
|
bindings/javascript/whisper.js
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
examples/command/command.cpp
CHANGED
|
@@ -109,9 +109,8 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
|
|
| 109 |
|
| 110 |
class audio_async {
|
| 111 |
public:
|
| 112 |
-
audio_async(int len_ms)
|
| 113 |
-
|
| 114 |
-
}
|
| 115 |
|
| 116 |
bool init(int capture_id, int sample_rate);
|
| 117 |
|
|
@@ -142,6 +141,16 @@ private:
|
|
| 142 |
size_t m_audio_len = 0;
|
| 143 |
};
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
bool audio_async::init(int capture_id, int sample_rate) {
|
| 146 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
| 147 |
|
|
|
|
| 109 |
|
| 110 |
class audio_async {
|
| 111 |
public:
|
| 112 |
+
audio_async(int len_ms);
|
| 113 |
+
~audio_async();
|
|
|
|
| 114 |
|
| 115 |
bool init(int capture_id, int sample_rate);
|
| 116 |
|
|
|
|
| 141 |
size_t m_audio_len = 0;
|
| 142 |
};
|
| 143 |
|
| 144 |
+
audio_async::audio_async(int len_ms) {
|
| 145 |
+
m_len_ms = len_ms;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
audio_async::~audio_async() {
|
| 149 |
+
if (m_dev_id_in) {
|
| 150 |
+
SDL_CloseAudioDevice(m_dev_id_in);
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
bool audio_async::init(int capture_id, int sample_rate) {
|
| 155 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
| 156 |
|
examples/stream/stream.cpp
CHANGED
|
@@ -384,6 +384,10 @@ int main(int argc, char ** argv) {
|
|
| 384 |
}
|
| 385 |
}
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
whisper_print_timings(ctx);
|
| 388 |
whisper_free(ctx);
|
| 389 |
|
|
|
|
| 384 |
}
|
| 385 |
}
|
| 386 |
|
| 387 |
+
if (g_dev_id_in >= 0) {
|
| 388 |
+
SDL_CloseAudioDevice(g_dev_id_in);
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
whisper_print_timings(ctx);
|
| 392 |
whisper_free(ctx);
|
| 393 |
|
examples/whisper.wasm/index-tmpl.html
CHANGED
|
@@ -526,7 +526,6 @@
|
|
| 526 |
if (instance) {
|
| 527 |
printTextarea('');
|
| 528 |
printTextarea('js: processing - this might take a while ...');
|
| 529 |
-
printTextarea('js: the page will be unresponsive until the processing is completed');
|
| 530 |
printTextarea('');
|
| 531 |
|
| 532 |
setTimeout(function() {
|
|
|
|
| 526 |
if (instance) {
|
| 527 |
printTextarea('');
|
| 528 |
printTextarea('js: processing - this might take a while ...');
|
|
|
|
| 529 |
printTextarea('');
|
| 530 |
|
| 531 |
setTimeout(function() {
|
whisper.cpp
CHANGED
|
@@ -2380,6 +2380,12 @@ void whisper_print_timings(struct whisper_context * ctx) {
|
|
| 2380 |
fprintf(stderr, "%s: total time = %8.2f ms\n", __func__, (t_end_us - ctx->t_start_us)/1000.0f);
|
| 2381 |
}
|
| 2382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2383 |
////////////////////////////////////////////////////////////////////////////
|
| 2384 |
|
| 2385 |
struct whisper_full_params whisper_full_default_params(enum whisper_sampling_strategy strategy) {
|
|
|
|
| 2380 |
fprintf(stderr, "%s: total time = %8.2f ms\n", __func__, (t_end_us - ctx->t_start_us)/1000.0f);
|
| 2381 |
}
|
| 2382 |
|
| 2383 |
+
void whisper_reset_timings(struct whisper_context * ctx) {
|
| 2384 |
+
ctx->t_sample_us = 0;
|
| 2385 |
+
ctx->t_encode_us = 0;
|
| 2386 |
+
ctx->t_decode_us = 0;
|
| 2387 |
+
}
|
| 2388 |
+
|
| 2389 |
////////////////////////////////////////////////////////////////////////////
|
| 2390 |
|
| 2391 |
struct whisper_full_params whisper_full_default_params(enum whisper_sampling_strategy strategy) {
|
whisper.h
CHANGED
|
@@ -167,6 +167,7 @@ extern "C" {
|
|
| 167 |
|
| 168 |
// Performance information
|
| 169 |
WHISPER_API void whisper_print_timings(struct whisper_context * ctx);
|
|
|
|
| 170 |
|
| 171 |
////////////////////////////////////////////////////////////////////////////
|
| 172 |
|
|
|
|
| 167 |
|
| 168 |
// Performance information
|
| 169 |
WHISPER_API void whisper_print_timings(struct whisper_context * ctx);
|
| 170 |
+
WHISPER_API void whisper_reset_timings(struct whisper_context * ctx);
|
| 171 |
|
| 172 |
////////////////////////////////////////////////////////////////////////////
|
| 173 |
|