mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2024-12-18 20:27:53 +00:00
whisper.wasm : do not block page while processing (close #86)
This commit is contained in:
parent
0f619b52ce
commit
be16dfa038
@ -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());
|
||||
@ -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()) {
|
||||
@ -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()) {
|
||||
@ -80,10 +94,15 @@ EMSCRIPTEN_BINDINGS(whisper) {
|
||||
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 {
|
||||
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);
|
||||
|
||||
@ -142,6 +141,16 @@ private:
|
||||
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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user