whisper.wasm : do not block page while processing (close #86)

pull/194/head
Georgi Gerganov 2 years ago
parent 0f619b52ce
commit be16dfa038
No known key found for this signature in database
GPG Key ID: 449E073F9DC10735

@ -6,10 +6,16 @@
#include <vector> #include <vector>
#include <thread> #include <thread>
std::thread g_worker;
std::vector<struct whisper_context *> g_contexts(4, nullptr); std::vector<struct whisper_context *> g_contexts(4, nullptr);
EMSCRIPTEN_BINDINGS(whisper) { EMSCRIPTEN_BINDINGS(whisper) {
emscripten::function("init", emscripten::optional_override([](const std::string & path_model) { 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) { for (size_t i = 0; i < g_contexts.size(); ++i) {
if (g_contexts[i] == nullptr) { if (g_contexts[i] == nullptr) {
g_contexts[i] = whisper_init(path_model.c_str()); g_contexts[i] = whisper_init(path_model.c_str());
@ -25,6 +31,10 @@ EMSCRIPTEN_BINDINGS(whisper) {
})); }));
emscripten::function("free", emscripten::optional_override([](size_t index) { emscripten::function("free", emscripten::optional_override([](size_t index) {
if (g_worker.joinable()) {
g_worker.join();
}
--index; --index;
if (index < g_contexts.size()) { if (index < g_contexts.size()) {
@ -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) { 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; --index;
if (index >= g_contexts.size()) { if (index >= g_contexts.size()) {
@ -80,10 +94,15 @@ EMSCRIPTEN_BINDINGS(whisper) {
printf("\n"); printf("\n");
} }
int ret = whisper_full(g_contexts[index], params, pcmf32.data(), pcmf32.size()); // 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]); whisper_print_timings(g_contexts[index]);
});
}
return ret; return 0;
})); }));
} }

File diff suppressed because one or more lines are too long

@ -109,9 +109,8 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
class audio_async { class audio_async {
public: public:
audio_async(int len_ms) { audio_async(int len_ms);
m_len_ms = len_ms; ~audio_async();
}
bool init(int capture_id, int sample_rate); bool init(int capture_id, int sample_rate);
@ -142,6 +141,16 @@ private:
size_t m_audio_len = 0; 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) { bool audio_async::init(int capture_id, int sample_rate) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

@ -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_print_timings(ctx);
whisper_free(ctx); whisper_free(ctx);

@ -526,7 +526,6 @@
if (instance) { if (instance) {
printTextarea(''); printTextarea('');
printTextarea('js: processing - this might take a while ...'); printTextarea('js: processing - this might take a while ...');
printTextarea('js: the page will be unresponsive until the processing is completed');
printTextarea(''); printTextarea('');
setTimeout(function() { setTimeout(function() {

@ -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); 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) { struct whisper_full_params whisper_full_default_params(enum whisper_sampling_strategy strategy) {

@ -167,6 +167,7 @@ extern "C" {
// Performance information // Performance information
WHISPER_API void whisper_print_timings(struct whisper_context * ctx); WHISPER_API void whisper_print_timings(struct whisper_context * ctx);
WHISPER_API void whisper_reset_timings(struct whisper_context * ctx);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save