Skip to content

Commit

Permalink
whisper.wasm : do not block page while processing (close ggerganov#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Nov 25, 2022
1 parent 0f619b5 commit be16dfa
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
27 changes: 23 additions & 4 deletions bindings/javascript/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
#include <vector>
#include <thread>

std::thread g_worker;

std::vector<struct whisper_context *> g_contexts(4, nullptr);

EMSCRIPTEN_BINDINGS(whisper) {
emscripten::function("init", emscripten::optional_override([](const std::string & path_model) {
if (g_worker.joinable()) {
g_worker.join();
}

for (size_t i = 0; i < g_contexts.size(); ++i) {
if (g_contexts[i] == nullptr) {
g_contexts[i] = whisper_init(path_model.c_str());
Expand All @@ -25,6 +31,10 @@ EMSCRIPTEN_BINDINGS(whisper) {
}));

emscripten::function("free", emscripten::optional_override([](size_t index) {
if (g_worker.joinable()) {
g_worker.join();
}

--index;

if (index < g_contexts.size()) {
Expand All @@ -34,6 +44,10 @@ EMSCRIPTEN_BINDINGS(whisper) {
}));

emscripten::function("full_default", emscripten::optional_override([](size_t index, const emscripten::val & audio, const std::string & lang, bool translate) {
if (g_worker.joinable()) {
g_worker.join();
}

--index;

if (index >= g_contexts.size()) {
Expand Down Expand Up @@ -80,10 +94,15 @@ EMSCRIPTEN_BINDINGS(whisper) {
printf("\n");
}

int ret = whisper_full(g_contexts[index], params, pcmf32.data(), pcmf32.size());

whisper_print_timings(g_contexts[index]);
// run the worker
{
g_worker = std::thread([index, params, pcmf32 = std::move(pcmf32)]() {
whisper_reset_timings(g_contexts[index]);
whisper_full(g_contexts[index], params, pcmf32.data(), pcmf32.size());
whisper_print_timings(g_contexts[index]);
});
}

return ret;
return 0;
}));
}
2 changes: 1 addition & 1 deletion bindings/javascript/whisper.js

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions examples/command/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)

class audio_async {
public:
audio_async(int len_ms) {
m_len_ms = len_ms;
}
audio_async(int len_ms);
~audio_async();

bool init(int capture_id, int sample_rate);

Expand Down Expand Up @@ -142,6 +141,16 @@ class audio_async {
size_t m_audio_len = 0;
};

audio_async::audio_async(int len_ms) {
m_len_ms = len_ms;
}

audio_async::~audio_async() {
if (m_dev_id_in) {
SDL_CloseAudioDevice(m_dev_id_in);
}
}

bool audio_async::init(int capture_id, int sample_rate) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

Expand Down
4 changes: 4 additions & 0 deletions examples/stream/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ int main(int argc, char ** argv) {
}
}

if (g_dev_id_in >= 0) {
SDL_CloseAudioDevice(g_dev_id_in);
}

whisper_print_timings(ctx);
whisper_free(ctx);

Expand Down
1 change: 0 additions & 1 deletion examples/whisper.wasm/index-tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@
if (instance) {
printTextarea('');
printTextarea('js: processing - this might take a while ...');
printTextarea('js: the page will be unresponsive until the processing is completed');
printTextarea('');

setTimeout(function() {
Expand Down
6 changes: 6 additions & 0 deletions whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,12 @@ void whisper_print_timings(struct whisper_context * ctx) {
fprintf(stderr, "%s: total time = %8.2f ms\n", __func__, (t_end_us - ctx->t_start_us)/1000.0f);
}

void whisper_reset_timings(struct whisper_context * ctx) {
ctx->t_sample_us = 0;
ctx->t_encode_us = 0;
ctx->t_decode_us = 0;
}

////////////////////////////////////////////////////////////////////////////

struct whisper_full_params whisper_full_default_params(enum whisper_sampling_strategy strategy) {
Expand Down
1 change: 1 addition & 0 deletions whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ extern "C" {

// Performance information
WHISPER_API void whisper_print_timings(struct whisper_context * ctx);
WHISPER_API void whisper_reset_timings(struct whisper_context * ctx);

////////////////////////////////////////////////////////////////////////////

Expand Down

0 comments on commit be16dfa

Please sign in to comment.