1
+ //
2
+ // This is the Javascript API of whisper.cpp
3
+ //
4
+ // Very crude at the moment.
5
+ // Feel free to contribute and make this better!
6
+ //
7
+ // See the tests/test-whisper.js for sample usage
8
+ //
9
+
1
10
#include " whisper.h"
2
11
3
12
#include < emscripten.h>
4
13
#include < emscripten/bind.h>
5
14
6
- #include < vector>
7
15
#include < thread>
16
+ #include < vector>
8
17
9
- std::thread g_worker;
10
-
11
- std::vector<struct whisper_context *> g_contexts (4 , nullptr );
18
+ struct whisper_context * g_context;
12
19
13
20
EMSCRIPTEN_BINDINGS (whisper) {
14
21
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 ());
22
- if (g_contexts[i] != nullptr ) {
23
- return i + 1 ;
24
- } else {
25
- return (size_t ) 0 ;
26
- }
22
+ if (g_context == nullptr ) {
23
+ g_context = whisper_init (path_model.c_str ());
24
+ if (g_context != nullptr ) {
25
+ return true ;
26
+ } else {
27
+ return false ;
27
28
}
28
29
}
29
30
30
- return ( size_t ) 0 ;
31
+ return false ;
31
32
}));
32
33
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 ()) {
41
- whisper_free (g_contexts[index ]);
42
- g_contexts[index ] = nullptr ;
34
+ emscripten::function (" free" , emscripten::optional_override ([]() {
35
+ if (g_context) {
36
+ whisper_free (g_context);
37
+ g_context = nullptr ;
43
38
}
44
39
}));
45
40
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 ()) {
41
+ emscripten::function (" full_default" , emscripten::optional_override ([](const emscripten::val & audio, const std::string & lang, bool translate) {
42
+ if (g_context == nullptr ) {
54
43
return -1 ;
55
44
}
56
45
57
- if (g_contexts[index ] == nullptr ) {
58
- return -2 ;
59
- }
60
-
61
46
struct whisper_full_params params = whisper_full_default_params (whisper_sampling_strategy::WHISPER_SAMPLING_GREEDY);
62
47
63
48
params.print_realtime = true ;
64
49
params.print_progress = false ;
65
50
params.print_timestamps = true ;
66
51
params.print_special = false ;
67
52
params.translate = translate;
68
- params.language = whisper_is_multilingual (g_contexts[ index ] ) ? lang.c_str () : " en" ;
53
+ params.language = whisper_is_multilingual (g_context ) ? lang.c_str () : " en" ;
69
54
params.n_threads = std::min (8 , (int ) std::thread::hardware_concurrency ());
70
55
params.offset_ms = 0 ;
71
56
@@ -82,9 +67,11 @@ EMSCRIPTEN_BINDINGS(whisper) {
82
67
83
68
// print system information
84
69
{
70
+ printf (" \n " );
85
71
printf (" system_info: n_threads = %d / %d | %s\n " ,
86
72
params.n_threads , std::thread::hardware_concurrency (), whisper_print_system_info ());
87
73
74
+ printf (" \n " );
88
75
printf (" %s: processing %d samples, %.1f sec, %d threads, %d processors, lang = %s, task = %s ...\n " ,
89
76
__func__, int (pcmf32.size ()), float (pcmf32.size ())/WHISPER_SAMPLE_RATE,
90
77
params.n_threads , 1 ,
@@ -94,13 +81,11 @@ EMSCRIPTEN_BINDINGS(whisper) {
94
81
printf (" \n " );
95
82
}
96
83
97
- // run the worker
84
+ // run whisper
98
85
{
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
- });
86
+ whisper_reset_timings (g_context);
87
+ whisper_full (g_context, params, pcmf32.data (), pcmf32.size ());
88
+ whisper_print_timings (g_context);
104
89
}
105
90
106
91
return 0 ;
0 commit comments