C and JS interface, untested

This commit is contained in:
msqr1
2024-01-15 23:36:20 -08:00
parent db2acc30be
commit ab8d869dd9
22 changed files with 7862 additions and 1 deletions

56
src/addCOI.js Normal file
View File

@@ -0,0 +1,56 @@
// Add cross-origin isolation (COI) into the page when the user visits it for the first time via a service worker and refreshing to apply the headers.
// Taken, and modified from https://github.com/orgs/community/discussions/13309#discussioncomment-3844940
if(typeof window === 'undefined') {
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));
async function handleFetch(request) {
if(request.cache === "only-if-cached" && request.mode !== "same-origin") {
return;
}
if(request.mode === "no-cors") {
request = new Request(request.url, {
cache: request.cache,
credentials: "omit",
headers: request.headers,
integrity: request.integrity,
destination: request.destination,
keepalive: request.keepalive,
method: request.method,
mode: request.mode,
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
signal: request.signal,
});
}
let r = await fetch(request).catch(e => console.error(e));
if(r.status === 0) {
return r;
}
const headers = new Headers(r.headers);
headers.set("Cross-Origin-Embedder-Policy", "require-corp");
headers.set("Cross-Origin-Opener-Policy", "same-origin");
return new Response(r.body, { status: r.status, statusText: r.statusText, headers });
}
self.addEventListener("fetch", function(e) {
e.respondWith(handleFetch(e.request));
});
} else {
(async function() {
if(window.crossOriginIsolated !== false) return;
let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));
if(registration) {
registration.addEventListener("updatefound", () => {
window.location.reload();
});
if(registration.active && !navigator.serviceWorker.controller) {
window.location.reload();
}
}
})();
}

26
src/bindings.cc Normal file
View File

@@ -0,0 +1,26 @@
#include "spkModel.h"
#include "model.h"
#include "recognizer.h"
using namespace emscripten;
EMSCRIPTEN_BINDINGS(BrowserRecognizer) {
function("setLogLevel", &vosk_set_log_level, allow_raw_pointers());
class_<Model>("__Model__")
.constructor<std::string, std::string, std::string, int>(allow_raw_pointers());
class_<SpkModel>("__SpkModel__")
.constructor<std::string, std::string, std::string, const int>(allow_raw_pointers());
class_<Recognizer>("__Recognizer__")
.constructor<Model*, int, int>(allow_raw_pointers())
.function("start", &Recognizer::start, allow_raw_pointers())
.function("stop", &Recognizer::stop, allow_raw_pointers())
.function("deinit", &Recognizer::deinit, allow_raw_pointers())
.function("setWords", &Recognizer::setWords, allow_raw_pointers())
.function("setPartialWords", &Recognizer::setPartialWords, allow_raw_pointers())
.function("setGrm", &Recognizer::setGrm, allow_raw_pointers())
.function("setNLSML", &Recognizer::setNLSML, allow_raw_pointers())
.function("setSpkModel", &Recognizer::setSpkModel, allow_raw_pointers())
.function("setMaxAlternatives", &Recognizer::setMaxAlternatives, allow_raw_pointers());
};

69
src/genericModel.cc Normal file
View File

@@ -0,0 +1,69 @@
#include "genericModel.h"
bool GenericModel::first = true;
GenericModel::GenericModel(const std::string &url, const std::string& storepath, const std::string &id, int index) : url(url), id(id), storepath(("opfs/" + storepath)), GenericObj(index) {
if(first) {
vosk_set_log_level(-1);
int res{};
std::thread t{[&res](){
res = wasmfs_create_directory("opfs",0777,wasmfs_create_opfs_backend());
}};
t.join();
if(res == 1){
fireEv("error", "Unable to create OPFS directory");
return;
}
first = false;
}
}
bool GenericModel::checkId(const std::string& path, const std::string& id) {
std::ifstream file {(path + "/id"), std::ifstream::binary};
if(!file.is_open()) {
return false;
};
long long size {file.seekg(0, std::ios::end).tellg()};
std::string oldid(size, ' ');
file.seekg(0);
file.read(&oldid[0], size);
return id.compare(oldid) == 0 ? true : false;
}
bool GenericModel::loadModel() {
if(!checkModel(storepath) || !checkId(storepath, id)) {
if(emscripten_wget(url.c_str(), "opfs/model.tzst") == 1) {
fireEv("error", "Unable to fetch model");
return false;
}
if(!extractModel("opfs/model.tzst", storepath)) {
fireEv("error", "Unable to extract model");
}
fs::remove("opfs/model.tzst");
if(!checkModel(storepath)) {
fireEv("error", "Model URL contains invalid model files");
}
std::ofstream idFile((storepath + "/id"));
if(!idFile.is_open()) {
fireEv("error", "Unable to write new id");
fs::remove_all(storepath);
return false;
}
idFile << id;
}
return true;
}
bool GenericModel::extractModel(const char* target, const std::string& dest) {
std::string path{};
archive* src {archive_read_new()};
archive_entry* entry {};
archive_read_support_filter_all(src);
archive_read_support_format_all(src);
archive_read_open_filename(src, target,22480);
if(archive_errno(src) != 0) return false;
while (archive_read_next_header(src, &entry) == ARCHIVE_OK) {
path = archive_entry_pathname(entry);
archive_entry_set_pathname(entry, (dest + path.substr(path.find("/"))).c_str());
if(archive_errno(src) != 0) return false;
archive_read_extract(src, entry, ARCHIVE_EXTRACT_UNLINK);
}
archive_read_free(src);
return true;
}

28
src/genericModel.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include "genericObj.h"
#include <string>
#include <thread>
#include <filesystem>
#include <fstream>
#include <vosk_api.h>
#include <archive.h>
#include <archive_entry.h>
#include <emscripten/wasmfs.h>
#include <emscripten/bind.h>
namespace fs = std::filesystem;
class GenericModel : public GenericObj {
static bool first;
const std::string url{};
const std::string id{};
static bool extractModel(const char* target, const std::string& dest);
static bool checkId(const std::string& path, const std::string& id);
public:
const std::string storepath{};
virtual bool checkModel(const std::string& path) = 0;
bool loadModel();
GenericModel(const std::string& url, const std::string& storepath, const std::string& id, int index);
};

13
src/genericObj.cc Normal file
View File

@@ -0,0 +1,13 @@
#include "genericObj.h"
void GenericObj::fireEv(const char *type, const char *content) {
if(content == nullptr) {
MAIN_THREAD_EM_ASM({
__GenericObj__.objects[$0].dispatchEvent(new Event(UTF8ToString($1)));
},this->index, type);
return;
}
MAIN_THREAD_EM_ASM({
__GenericObj__.objects[$0].dispatchEvent(new CustomEvent(UTF8ToString($0), {"details" : UTF8ToString($1)}));
},this->index, type, content);
};

11
src/genericObj.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <emscripten.h>
class GenericObj {
const int index{};
public:
GenericObj(int index) : index(index) {};
void fireEv(const char *type, const char *content = nullptr);
};

1
src/genericObj.js Normal file
View File

@@ -0,0 +1 @@
class __GenericObj__ {static objects = []}

52
src/kaldi.patch Normal file
View File

@@ -0,0 +1,52 @@
diff --git a/src/matrix/Makefile b/src/matrix/Makefile
index 398179a35..c903fbfd4 100644
--- a/src/matrix/Makefile
+++ b/src/matrix/Makefile
@@ -10,7 +10,6 @@ include ../kaldi.mk
# you can uncomment matrix-lib-speed-test if you want to do the speed tests.
-TESTFILES = matrix-lib-test sparse-matrix-test numpy-array-test #matrix-lib-speed-test
OBJFILES = kaldi-matrix.o kaldi-vector.o packed-matrix.o sp-matrix.o tp-matrix.o \
matrix-functions.o qr.o srfft.o compressed-matrix.o \
diff --git a/src/util/kaldi-thread.cc b/src/util/kaldi-thread.cc
index 4573e24f1..4af4e73ea 100644
--- a/src/util/kaldi-thread.cc
+++ b/src/util/kaldi-thread.cc
@@ -22,7 +22,7 @@
#include "util/kaldi-thread.h"
namespace kaldi {
-int32 g_num_threads = 4; // Initialize this global variable.
+int32 g_num_threads = 1; // Initialize this global variable.
MultiThreadable::~MultiThreadable() {
// default implementation does nothing
diff --git a/src/base/kaldi-types.h b/src/base/kaldi-types.h
index 7ebf4f853..2f5979e42 100644
--- a/src/base/kaldi-types.h
+++ b/src/base/kaldi-types.h
@@ -20,6 +20,7 @@
#ifndef KALDI_BASE_KALDI_TYPES_H_
#define KALDI_BASE_KALDI_TYPES_H_ 1
+#define KALDI_DOUBLEPRECISION 1
namespace kaldi {
// TYPEDEFS ..................................................................
diff --git a/src/ivector/ivector-extractor.cc b/src/ivector/ivector-extractor.cc
index c3a122281..71d37256d 100644
--- a/src/ivector/ivector-extractor.cc
+++ b/src/ivector/ivector-extractor.cc
@@ -195,7 +195,7 @@ void IvectorExtractor::ComputeDerivedVars() {
// could because some tasks finish before others.
{
TaskSequencerConfig sequencer_opts;
- sequencer_opts.num_threads = g_num_threads;
+ sequencer_opts.num_threads = 0;
TaskSequencer<IvectorExtractorComputeDerivedVarsClass> sequencer(
sequencer_opts);
for (int32 i = 0; i < NumGauss(); i++)

27
src/model.cc Normal file
View File

@@ -0,0 +1,27 @@
#include "model.h"
Model::Model(const std::string &url, const std::string& storepath, const std::string& id, int index) : GenericModel(url, storepath, id, index) {
if(!loadModel()) return;
model = vosk_model_new(this->storepath.c_str());
if(model == nullptr) {
fireEv("error", "Unable to initialize model");
return;
}
fireEv("ready");
};
bool Model::checkModel(const std::string& path) {
return fs::exists(path + "/am/final.mdl") &&
fs::exists(path + "/conf/mfcc.conf") &&
fs::exists(path + "/conf/model.conf") &&
fs::exists(path + "/graph/phones/word_boundary.int") &&
fs::exists(path + "/graph/Gr.fst") &&
fs::exists(path + "/graph/HCLr.fst") &&
fs::exists(path + "/graph/disambig_tid.int") &&
fs::exists(path + "/ivector/final.dubm") &&
fs::exists(path + "/ivector/final.ie") &&
fs::exists(path + "/ivector/final.mat") &&
fs::exists(path + "/ivector/global_cmvn.stats") &&
fs::exists(path + "/ivector/online_cmvn.conf") &&
fs::exists(path + "/ivector/splice.conf");
}

12
src/model.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include "genericModel.h"
class Model : public GenericModel {
bool checkModel(const std::string& path);
public:
VoskModel* model{};
Model(const std::string &url, const std::string& storepath, const std::string& id, int index);
};

10
src/model.js Normal file
View File

@@ -0,0 +1,10 @@
class Model extends EventTarget{
constructor(url, storepath, id) {
super()
this.obj = new Module.__Model__(url, storepath, id, __GenericObj__.objects.length)
__GenericObj__.objects.push(this)
}
delete() {
this.obj.delete()
}
}

74
src/recognizer.cc Normal file
View File

@@ -0,0 +1,74 @@
#include "./recognizer.h"
void Recognizer::start() {
controller.test_and_set(std::memory_order_relaxed);
controller.notify_all();
}
void Recognizer::stop() {
controller.clear(std::memory_order_relaxed);
controller.notify_all();
}
void Recognizer::deinit() {
done.test_and_set(std::memory_order_relaxed);
done.notify_all();
stop();
}
Recognizer::Recognizer(Model* model, int sampleRate, int index) : GenericObj(index) {
mic = alcCaptureOpenDevice("Emscripten OpenAL capture",sampleRate, AL_FORMAT_MONO16, 22480);
if(alcGetError(mic) != 0) {
fireEv("error", "Unable to initialize microphone");
return;
}
std::thread t{[this](Model* model, int sampleRate) {
recognizer = vosk_recognizer_new(model->model,static_cast<float>(sampleRate));
if(recognizer == nullptr) {
fireEv("error", "Unable to construct recognizer");
return;
}
main();
}, model, sampleRate};
t.detach();
}
void Recognizer::main() {
char buffer[22480];
int sample{};
fireEv("ready");
while(!done.test()) {
controller.wait(done.test(std::memory_order_relaxed), std::memory_order_relaxed);
alcCaptureStart(mic);
while(controller.test()) {
alcGetIntegerv(mic, ALC_CAPTURE_SAMPLES, sizeof(int), &sample);
alcCaptureSamples(mic, buffer, sample);
switch(vosk_recognizer_accept_waveform(recognizer, buffer, 22480)) {
case 0:
fireEv("result", vosk_recognizer_result(recognizer));
break;
case 1:
fireEv("partialResult", vosk_recognizer_partial_result(recognizer));
break;
default:
fireEv("error", "Recognition result error");
}
}
alcCaptureStop(mic);
}
vosk_recognizer_free(recognizer);
alcCaptureCloseDevice(mic);
}
void Recognizer::setGrm(const std::string& grm) {
vosk_recognizer_set_grm(recognizer, grm.c_str());
}
void Recognizer::setSpkModel(SpkModel* model) {
vosk_recognizer_set_spk_model(recognizer,model->model);
}
void Recognizer::setWords(bool words) {
vosk_recognizer_set_words(recognizer,words);
}
void Recognizer::setPartialWords(bool partialWords) {
vosk_recognizer_set_partial_words(recognizer, partialWords);
}
void Recognizer::setNLSML(bool nlsml) {
vosk_recognizer_set_nlsml(recognizer, nlsml);
}
void Recognizer::setMaxAlternatives(int alts) {
vosk_recognizer_set_max_alternatives(recognizer, alts);
}

36
src/recognizer.h Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include "model.h"
#include "spkModel.h"
#include "genericObj.h"
#include <filesystem>
#include <atomic>
#include <thread>
#include <emscripten/bind.h>
#include <emscripten/wasmfs.h>
#include <emscripten/console.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <archive.h>
#include <archive_entry.h>
namespace fs = std::filesystem;
class Recognizer : public GenericObj {
VoskRecognizer* recognizer{};
ALCdevice* mic{};
std::atomic_flag done {false};
std::atomic_flag controller{false};
void main();
public:
Recognizer(Model* model, int sampleRate, int index);
void start();
void stop();
void deinit();
void setSpkModel(SpkModel* model);
void setGrm(const std::string& grm);
void setWords(bool words);
void setPartialWords(bool partialWords);
void setNLSML(bool nlsml);
void setMaxAlternatives(int alts);
};

36
src/recognizer.js Normal file
View File

@@ -0,0 +1,36 @@
class Recognizer extends EventTarget {
constructor(model) {
ctx = new (AudioContext || webkitAudioContext)()
this.obj = new Module.__Recognizer__(model.obj,ctx.sampleRate,__GenericObj__.objects.length)
__GenericObj__.objects.push(this)
ctx.close()
}
start() {
this.obj.start()
}
stop() {
this.obj.stop()
}
delete() {
this.obj.deinit()
this.obj.delete()
}
setWords(words) {
this.obj.setWords(words)
}
setPartialWords(partialWords) {
this.obj.setPartialWords(words)
}
setGrm(grm) {
this.obj.setGrm(grm)
}
setSpkModel(model) {
this.obj.setSpkModel(model.obj)
}
setNLSML(nlsml) {
this.obj.setNLSML(nlsml)
}
setMaxAlternatives(alts) {
this.obj.setMaxAlternatives(alts)
}
}

15
src/spkModel.cc Normal file
View File

@@ -0,0 +1,15 @@
#include "spkModel.h"
SpkModel::SpkModel(const std::string &url, const std::string& storepath, const std::string& id, int index) : GenericModel(url, storepath, id, index) {
if(!loadModel()) return;
model = vosk_spk_model_new(this->storepath.c_str());
if(model == nullptr) {
fireEv("error", "Unable to initialize speaker model");
}
fireEv("ready");
};
bool SpkModel::checkModel(const std::string& path) {
return fs::exists((path + "/mfcc.conf")) &&
fs::exists((path + "/final.ext.raw")) &&
fs::exists((path + "/mean.vec")) &&
fs::exists((path + "/transform.mat"));
}

12
src/spkModel.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include "genericModel.h"
class SpkModel : public GenericModel {
bool checkModel(const std::string& path);
public:
SpkModel(const std::string &url, const std::string& storepath, const std::string& id, const int index);
VoskSpkModel* model{};
};

10
src/spkModel.js Normal file
View File

@@ -0,0 +1,10 @@
class SpkModel extends EventTarget{
constructor(url, storepath, id) {
super()
this.obj = new Module.__SpkModel__(url, storepath, id, __GenericObj__.objects.length)
__GenericObj__.objects.push(this)
}
delete() {
this.obj.delete()
}
}