2023-02-15 17:28:10 +00:00
|
|
|
#include "napi.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include "whisper.h"
|
|
|
|
|
2023-02-04 07:10:25 +00:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
#include <cmath>
|
2023-02-15 17:28:10 +00:00
|
|
|
#include <cstdint>
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
struct whisper_params {
|
|
|
|
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
|
|
|
int32_t n_processors = 1;
|
|
|
|
int32_t offset_t_ms = 0;
|
|
|
|
int32_t offset_n = 0;
|
|
|
|
int32_t duration_ms = 0;
|
|
|
|
int32_t max_context = -1;
|
|
|
|
int32_t max_len = 0;
|
|
|
|
int32_t best_of = 5;
|
|
|
|
int32_t beam_size = -1;
|
2024-05-13 12:22:23 +00:00
|
|
|
int32_t audio_ctx = 0;
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
float word_thold = 0.01f;
|
|
|
|
float entropy_thold = 2.4f;
|
|
|
|
float logprob_thold = -1.0f;
|
|
|
|
|
|
|
|
bool translate = false;
|
|
|
|
bool diarize = false;
|
|
|
|
bool output_txt = false;
|
|
|
|
bool output_vtt = false;
|
|
|
|
bool output_srt = false;
|
|
|
|
bool output_wts = false;
|
|
|
|
bool output_csv = false;
|
|
|
|
bool print_special = false;
|
|
|
|
bool print_colors = false;
|
|
|
|
bool print_progress = false;
|
|
|
|
bool no_timestamps = false;
|
2024-05-13 12:15:43 +00:00
|
|
|
bool no_prints = false;
|
2023-11-06 09:04:24 +00:00
|
|
|
bool use_gpu = true;
|
2024-05-20 06:08:48 +00:00
|
|
|
bool flash_attn = false;
|
2024-05-13 12:15:43 +00:00
|
|
|
bool comma_in_time = true;
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
std::string language = "en";
|
|
|
|
std::string prompt;
|
|
|
|
std::string model = "../../ggml-large.bin";
|
|
|
|
|
|
|
|
std::vector<std::string> fname_inp = {};
|
2023-02-15 17:28:10 +00:00
|
|
|
std::vector<std::string> fname_out = {};
|
2024-05-13 12:22:23 +00:00
|
|
|
|
|
|
|
std::vector<float> pcmf32 = {}; // mono-channel F32 PCM
|
2023-02-04 07:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct whisper_print_user_data {
|
|
|
|
const whisper_params * params;
|
|
|
|
|
|
|
|
const std::vector<std::vector<float>> * pcmf32s;
|
|
|
|
};
|
|
|
|
|
2023-03-05 19:42:19 +00:00
|
|
|
void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
|
2023-02-04 07:10:25 +00:00
|
|
|
const auto & params = *((whisper_print_user_data *) user_data)->params;
|
|
|
|
const auto & pcmf32s = *((whisper_print_user_data *) user_data)->pcmf32s;
|
|
|
|
|
|
|
|
const int n_segments = whisper_full_n_segments(ctx);
|
|
|
|
|
|
|
|
std::string speaker = "";
|
|
|
|
|
|
|
|
int64_t t0;
|
|
|
|
int64_t t1;
|
|
|
|
|
|
|
|
// print the last n_new segments
|
|
|
|
const int s0 = n_segments - n_new;
|
|
|
|
|
|
|
|
if (s0 == 0) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = s0; i < n_segments; i++) {
|
|
|
|
if (!params.no_timestamps || params.diarize) {
|
|
|
|
t0 = whisper_full_get_segment_t0(ctx, i);
|
|
|
|
t1 = whisper_full_get_segment_t1(ctx, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.no_timestamps) {
|
|
|
|
printf("[%s --> %s] ", to_timestamp(t0).c_str(), to_timestamp(t1).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.diarize && pcmf32s.size() == 2) {
|
|
|
|
const int64_t n_samples = pcmf32s[0].size();
|
|
|
|
|
2024-02-19 08:50:15 +00:00
|
|
|
const int64_t is0 = timestamp_to_sample(t0, n_samples, WHISPER_SAMPLE_RATE);
|
|
|
|
const int64_t is1 = timestamp_to_sample(t1, n_samples, WHISPER_SAMPLE_RATE);
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
double energy0 = 0.0f;
|
|
|
|
double energy1 = 0.0f;
|
|
|
|
|
|
|
|
for (int64_t j = is0; j < is1; j++) {
|
|
|
|
energy0 += fabs(pcmf32s[0][j]);
|
|
|
|
energy1 += fabs(pcmf32s[1][j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (energy0 > 1.1*energy1) {
|
|
|
|
speaker = "(speaker 0)";
|
|
|
|
} else if (energy1 > 1.1*energy0) {
|
|
|
|
speaker = "(speaker 1)";
|
|
|
|
} else {
|
|
|
|
speaker = "(speaker ?)";
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf("is0 = %lld, is1 = %lld, energy0 = %f, energy1 = %f, %s\n", is0, is1, energy0, energy1, speaker.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// colorful print bug
|
|
|
|
//
|
|
|
|
const char * text = whisper_full_get_segment_text(ctx, i);
|
|
|
|
printf("%s%s", speaker.c_str(), text);
|
|
|
|
|
|
|
|
|
|
|
|
// with timestamps or speakers: each segment on new line
|
|
|
|
if (!params.no_timestamps || params.diarize) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:15:43 +00:00
|
|
|
void cb_log_disable(enum ggml_log_level, const char *, void *) {}
|
|
|
|
|
2023-02-04 07:10:25 +00:00
|
|
|
int run(whisper_params ¶ms, std::vector<std::vector<std::string>> &result) {
|
2024-05-13 12:15:43 +00:00
|
|
|
if (params.no_prints) {
|
|
|
|
whisper_log_set(cb_log_disable, NULL);
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:22:23 +00:00
|
|
|
if (params.fname_inp.empty() && params.pcmf32.empty()) {
|
|
|
|
fprintf(stderr, "error: no input files or audio buffer specified\n");
|
2023-02-04 07:10:25 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.language != "auto" && whisper_lang_id(params.language.c_str()) == -1) {
|
|
|
|
fprintf(stderr, "error: unknown language '%s'\n", params.language.c_str());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// whisper init
|
|
|
|
|
2024-02-11 14:39:12 +00:00
|
|
|
struct whisper_context_params cparams = whisper_context_default_params();
|
2023-11-06 09:04:24 +00:00
|
|
|
cparams.use_gpu = params.use_gpu;
|
2024-05-20 06:08:48 +00:00
|
|
|
cparams.flash_attn = params.flash_attn;
|
2023-11-06 09:04:24 +00:00
|
|
|
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
if (ctx == nullptr) {
|
|
|
|
fprintf(stderr, "error: failed to initialize whisper context\n");
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:22:23 +00:00
|
|
|
// if params.pcmf32 is provided, set params.fname_inp to "buffer"
|
|
|
|
// this is simpler than further modifications in the code
|
|
|
|
if (!params.pcmf32.empty()) {
|
|
|
|
fprintf(stderr, "info: using audio buffer as input\n");
|
|
|
|
params.fname_inp.clear();
|
|
|
|
params.fname_inp.emplace_back("buffer");
|
|
|
|
}
|
|
|
|
|
2023-02-04 07:10:25 +00:00
|
|
|
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {
|
|
|
|
const auto fname_inp = params.fname_inp[f];
|
2023-02-15 17:28:10 +00:00
|
|
|
const auto fname_out = f < (int)params.fname_out.size() && !params.fname_out[f].empty() ? params.fname_out[f] : params.fname_inp[f];
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
std::vector<float> pcmf32; // mono-channel F32 PCM
|
|
|
|
std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
|
|
|
|
|
2024-05-13 12:22:23 +00:00
|
|
|
// read the input audio file if params.pcmf32 is not provided
|
|
|
|
if (params.pcmf32.empty()) {
|
|
|
|
if (!::read_wav(fname_inp, pcmf32, pcmf32s, params.diarize)) {
|
|
|
|
fprintf(stderr, "error: failed to read WAV file '%s'\n", fname_inp.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pcmf32 = params.pcmf32;
|
2023-02-04 07:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// print system information
|
2024-05-13 12:15:43 +00:00
|
|
|
if (!params.no_prints) {
|
2023-02-04 07:10:25 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
|
|
|
|
params.n_threads*params.n_processors, std::thread::hardware_concurrency(), whisper_print_system_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
// print some info about the processing
|
2024-05-13 12:15:43 +00:00
|
|
|
if (!params.no_prints) {
|
2023-02-04 07:10:25 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
if (!whisper_is_multilingual(ctx)) {
|
|
|
|
if (params.language != "en" || params.translate) {
|
|
|
|
params.language = "en";
|
|
|
|
params.translate = false;
|
|
|
|
fprintf(stderr, "%s: WARNING: model is not multilingual, ignoring language and translation options\n", __func__);
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 12:22:23 +00:00
|
|
|
fprintf(stderr, "%s: processing '%s' (%d samples, %.1f sec), %d threads, %d processors, lang = %s, task = %s, timestamps = %d, audio_ctx = %d ...\n",
|
2023-02-04 07:10:25 +00:00
|
|
|
__func__, fname_inp.c_str(), int(pcmf32.size()), float(pcmf32.size())/WHISPER_SAMPLE_RATE,
|
|
|
|
params.n_threads, params.n_processors,
|
|
|
|
params.language.c_str(),
|
|
|
|
params.translate ? "translate" : "transcribe",
|
2024-05-13 12:22:23 +00:00
|
|
|
params.no_timestamps ? 0 : 1,
|
|
|
|
params.audio_ctx);
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// run the inference
|
|
|
|
{
|
|
|
|
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
|
|
|
|
|
|
|
wparams.strategy = params.beam_size > 1 ? WHISPER_SAMPLING_BEAM_SEARCH : WHISPER_SAMPLING_GREEDY;
|
|
|
|
|
|
|
|
wparams.print_realtime = false;
|
|
|
|
wparams.print_progress = params.print_progress;
|
|
|
|
wparams.print_timestamps = !params.no_timestamps;
|
|
|
|
wparams.print_special = params.print_special;
|
|
|
|
wparams.translate = params.translate;
|
|
|
|
wparams.language = params.language.c_str();
|
|
|
|
wparams.n_threads = params.n_threads;
|
|
|
|
wparams.n_max_text_ctx = params.max_context >= 0 ? params.max_context : wparams.n_max_text_ctx;
|
|
|
|
wparams.offset_ms = params.offset_t_ms;
|
|
|
|
wparams.duration_ms = params.duration_ms;
|
|
|
|
|
|
|
|
wparams.token_timestamps = params.output_wts || params.max_len > 0;
|
|
|
|
wparams.thold_pt = params.word_thold;
|
|
|
|
wparams.entropy_thold = params.entropy_thold;
|
|
|
|
wparams.logprob_thold = params.logprob_thold;
|
|
|
|
wparams.max_len = params.output_wts && params.max_len == 0 ? 60 : params.max_len;
|
2024-05-13 12:22:23 +00:00
|
|
|
wparams.audio_ctx = params.audio_ctx;
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
wparams.greedy.best_of = params.best_of;
|
|
|
|
wparams.beam_search.beam_size = params.beam_size;
|
|
|
|
|
2023-03-29 20:23:23 +00:00
|
|
|
wparams.initial_prompt = params.prompt.c_str();
|
2023-02-04 07:10:25 +00:00
|
|
|
|
2024-04-15 17:03:34 +00:00
|
|
|
wparams.no_timestamps = params.no_timestamps;
|
|
|
|
|
2023-02-04 07:10:25 +00:00
|
|
|
whisper_print_user_data user_data = { ¶ms, &pcmf32s };
|
|
|
|
|
|
|
|
// this callback is called on each new segment
|
|
|
|
if (!wparams.print_realtime) {
|
|
|
|
wparams.new_segment_callback = whisper_print_segment_callback;
|
|
|
|
wparams.new_segment_callback_user_data = &user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// example for abort mechanism
|
|
|
|
// in this example, we do not abort the processing, but we could if the flag is set to true
|
|
|
|
// the callback is called before every encoder run - if it returns false, the processing is aborted
|
|
|
|
{
|
|
|
|
static bool is_aborted = false; // NOTE: this should be atomic to avoid data race
|
|
|
|
|
2023-03-05 19:42:19 +00:00
|
|
|
wparams.encoder_begin_callback = [](struct whisper_context * /*ctx*/, struct whisper_state * /*state*/, void * user_data) {
|
2023-02-04 07:10:25 +00:00
|
|
|
bool is_aborted = *(bool*)user_data;
|
|
|
|
return !is_aborted;
|
|
|
|
};
|
|
|
|
wparams.encoder_begin_callback_user_data = &is_aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
|
|
|
|
fprintf(stderr, "failed to process audio\n");
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int n_segments = whisper_full_n_segments(ctx);
|
|
|
|
result.resize(n_segments);
|
|
|
|
for (int i = 0; i < n_segments; ++i) {
|
|
|
|
const char * text = whisper_full_get_segment_text(ctx, i);
|
|
|
|
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
|
|
|
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
|
|
|
|
2024-05-13 12:15:43 +00:00
|
|
|
result[i].emplace_back(to_timestamp(t0, params.comma_in_time));
|
|
|
|
result[i].emplace_back(to_timestamp(t1, params.comma_in_time));
|
2023-02-04 07:10:25 +00:00
|
|
|
result[i].emplace_back(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
whisper_print_timings(ctx);
|
|
|
|
whisper_free(ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:19:22 +00:00
|
|
|
class Worker : public Napi::AsyncWorker {
|
|
|
|
public:
|
|
|
|
Worker(Napi::Function& callback, whisper_params params)
|
|
|
|
: Napi::AsyncWorker(callback), params(params) {}
|
|
|
|
|
|
|
|
void Execute() override {
|
|
|
|
run(params, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnOK() override {
|
|
|
|
Napi::HandleScope scope(Env());
|
|
|
|
Napi::Object res = Napi::Array::New(Env(), result.size());
|
|
|
|
for (uint64_t i = 0; i < result.size(); ++i) {
|
|
|
|
Napi::Object tmp = Napi::Array::New(Env(), 3);
|
|
|
|
for (uint64_t j = 0; j < 3; ++j) {
|
|
|
|
tmp[j] = Napi::String::New(Env(), result[i][j]);
|
|
|
|
}
|
|
|
|
res[i] = tmp;
|
2023-02-04 07:10:25 +00:00
|
|
|
}
|
2023-03-22 20:19:22 +00:00
|
|
|
Callback().Call({Env().Null(), res});
|
|
|
|
}
|
2023-02-04 07:10:25 +00:00
|
|
|
|
2023-03-22 20:19:22 +00:00
|
|
|
private:
|
|
|
|
whisper_params params;
|
|
|
|
std::vector<std::vector<std::string>> result;
|
|
|
|
};
|
2023-02-04 07:10:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-22 20:19:22 +00:00
|
|
|
Napi::Value whisper(const Napi::CallbackInfo& info) {
|
|
|
|
Napi::Env env = info.Env();
|
|
|
|
if (info.Length() <= 0 || !info[0].IsObject()) {
|
|
|
|
Napi::TypeError::New(env, "object expected").ThrowAsJavaScriptException();
|
|
|
|
}
|
|
|
|
whisper_params params;
|
2023-02-04 07:10:25 +00:00
|
|
|
|
2023-03-22 20:19:22 +00:00
|
|
|
Napi::Object whisper_params = info[0].As<Napi::Object>();
|
|
|
|
std::string language = whisper_params.Get("language").As<Napi::String>();
|
|
|
|
std::string model = whisper_params.Get("model").As<Napi::String>();
|
|
|
|
std::string input = whisper_params.Get("fname_inp").As<Napi::String>();
|
2023-11-06 09:04:24 +00:00
|
|
|
bool use_gpu = whisper_params.Get("use_gpu").As<Napi::Boolean>();
|
2024-05-20 06:08:48 +00:00
|
|
|
bool flash_attn = whisper_params.Get("flash_attn").As<Napi::Boolean>();
|
2024-05-13 12:15:43 +00:00
|
|
|
bool no_prints = whisper_params.Get("no_prints").As<Napi::Boolean>();
|
2024-04-15 17:03:34 +00:00
|
|
|
bool no_timestamps = whisper_params.Get("no_timestamps").As<Napi::Boolean>();
|
2024-05-13 12:22:23 +00:00
|
|
|
int32_t audio_ctx = whisper_params.Get("audio_ctx").As<Napi::Number>();
|
2024-05-13 12:15:43 +00:00
|
|
|
bool comma_in_time = whisper_params.Get("comma_in_time").As<Napi::Boolean>();
|
2023-03-22 20:19:22 +00:00
|
|
|
|
2024-05-13 12:22:23 +00:00
|
|
|
Napi::Value pcmf32Value = whisper_params.Get("pcmf32");
|
|
|
|
std::vector<float> pcmf32_vec;
|
|
|
|
if (pcmf32Value.IsTypedArray()) {
|
|
|
|
Napi::Float32Array pcmf32 = pcmf32Value.As<Napi::Float32Array>();
|
|
|
|
size_t length = pcmf32.ElementLength();
|
|
|
|
pcmf32_vec.reserve(length);
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
pcmf32_vec.push_back(pcmf32[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:19:22 +00:00
|
|
|
params.language = language;
|
|
|
|
params.model = model;
|
|
|
|
params.fname_inp.emplace_back(input);
|
2023-11-06 09:04:24 +00:00
|
|
|
params.use_gpu = use_gpu;
|
2024-05-20 06:08:48 +00:00
|
|
|
params.flash_attn = flash_attn;
|
2024-05-13 12:15:43 +00:00
|
|
|
params.no_prints = no_prints;
|
2024-04-15 17:03:34 +00:00
|
|
|
params.no_timestamps = no_timestamps;
|
2024-05-13 12:22:23 +00:00
|
|
|
params.audio_ctx = audio_ctx;
|
|
|
|
params.pcmf32 = pcmf32_vec;
|
2024-05-13 12:15:43 +00:00
|
|
|
params.comma_in_time = comma_in_time;
|
2023-02-04 07:10:25 +00:00
|
|
|
|
2023-03-22 20:19:22 +00:00
|
|
|
Napi::Function callback = info[1].As<Napi::Function>();
|
|
|
|
Worker* worker = new Worker(callback, params);
|
|
|
|
worker->Queue();
|
|
|
|
return env.Undefined();
|
2023-02-04 07:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
2023-03-22 20:19:22 +00:00
|
|
|
exports.Set(
|
|
|
|
Napi::String::New(env, "whisper"),
|
|
|
|
Napi::Function::New(env, whisper)
|
|
|
|
);
|
|
|
|
return exports;
|
2023-02-04 07:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_API_MODULE(whisper, Init);
|