a promising run

This commit is contained in:
msqr1
2024-01-28 22:05:58 -08:00
parent 8796f35445
commit c34d3f8974
20 changed files with 6317 additions and 167 deletions

View File

@@ -6,7 +6,7 @@
| Function signature (global) | Description |
|---|---|
| ```Promise<Model> makeModel(path: string, url: string, id: string)```<br><br>```Promise<SpkModel> makeSpkModel(path: string, url: string, id: string)``` | Make a ```Model``` or ```SpkModel```<br>- If **path** contains valid model files and **id** is the same, there will not be a fetch from **url**.<br>- If **path** doesn't contain valid model files, or if it contains valid model files but **id** is different, there will be a fetch from **url**, and the model is stored with **id**. |
| ```Promise<Recognizer> makeRecognizer(model: Model, sampleRate: float)``` | Make a ```Recognizer```, it will use a separate thread for recognition
| ```Promise<Recognizer> makeRecognizer(model: Model, sampleRate: float)``` | Make a ```Recognizer```, it will use **model**'s thread if it's the first to use **model**, else it will use a new thread.
| ```setLogLevel(lvl: int)``` | Set Vosk's log level (default: -1) <br>- 2: Error<br>- 1: Warning<br>- 0: Info <br>- 1: Verbose<br>- 2: More verbose<br>- 3: Debug |
| ```deleteAll()``` | Call ```delete()``` on all objects, it is recommended to run this at the API usage end to automatically clean up everything. See [why](https://emscripten.org/docs/getting_started/FAQ.html#what-does-exiting-the-runtime-mean-why-don-t-atexit-s-run).|
@@ -39,7 +39,7 @@ cd Browser-recognizer &&
| Option | Description | Default value |
|---|---|---|
| MAX_MEMORY | Set max memory, valid suffixes: kb, mb, gb, tb or none (bytes) | ```300mb```, as [recommended](https://alphacephei.com/vosk/models) |
| MAX_THREADS | Set the max number of thread (2 min) | ```2``` (1 OPFS thread + 1 recognizer thread) |
| MAX_THREADS | Set the max number of thread (2 min) | ```2``` (1 OPFS thread + 1 model/recognizer thread) |
| COMPILE_JOBS | Set the number of jobs (threads) when compiling | ```$(nproc)``` |
| EMSDK | Set EMSDK's path (will install EMSDK in root folder if unset) | ```.``` |
## Response headers

View File

@@ -1,28 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<script src="BrowserRecognizer.js"></script>
</head>
<script>
const BrRec = await loadBR()
const model = await BrRec.makeModel("test/model.tzst")
const recognizer = await BrRec.makeRecognizer(model)
recognizer.addEventListener("result", e => {
console.log("Result: ",e.details)
})
recognizer.addEventListener("partialResult", e => {
console.log("Partial result: ",e.details)
})
let ctx = new AudioContext()
media = await navigator.mediaDevices.getUserMedia({
video: false,
audio: {
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
sampleRate: 16000
},
})
</script>
</html>

View File

@@ -6,10 +6,16 @@ using namespace emscripten;
EMSCRIPTEN_BINDINGS() {
function("setLogLevel", &vosk_set_log_level, allow_raw_pointers());
class_<model>("Model")
.constructor<std::string, std::string, std::string>(allow_raw_pointers());
.constructor<std::string, std::string>(allow_raw_pointers())
.function("checkModelFiles", &model::checkModelFiles, allow_raw_pointers())
.function("checkModelId", &model::checkModelId, allow_raw_pointers())
.function("afterFetch", &model::afterFetch, allow_raw_pointers());
class_<spkModel>("SpkModel")
.constructor<std::string, std::string, std::string>(allow_raw_pointers());
.constructor<std::string, std::string>(allow_raw_pointers())
.function("checkModelFiles", &spkModel::checkModelFiles, allow_raw_pointers())
.function("checkModelId", &spkModel::checkModelId, allow_raw_pointers())
.function("afterFetch", &spkModel::afterFetch, allow_raw_pointers());
class_<recognizer>("recognizer")
.constructor<model*, float, int>(allow_raw_pointers())

View File

@@ -1,11 +1,12 @@
#include "genericModel.h"
fetchData::fetchData(const std::string& storepath, bool* successful, std::atomic_flag* blocker, genericModel* self) : storepath(storepath), successful(successful), blocker(blocker), self(self) {};
genericModel::genericModel(const std::string &url, const std::string& storepath, const std::string &id) : url(url), id(id) {
genericModel::genericModel(const std::string& storepath, const std::string &id) : storepath(storepath), id(id) {
fs::current_path("/opfs");
fs::create_directories(storepath);
fs::current_path(storepath);
}
bool genericModel::checkId(const std::string& id) {
bool genericModel::checkModelId() {
static std::error_code c{};
if(!fs::exists("id", c)) return false;
std::ifstream file {"id", std::ifstream::binary};
if(!file.is_open()) return false;
long long size {file.seekg(0, std::ios::end).tellg()};
@@ -14,53 +15,32 @@ bool genericModel::checkId(const std::string& id) {
file.read(&oldid[0], size);
return id.compare(oldid) == 0 ? true : false;
}
bool genericModel::loadModel(const std::string& storepath) {
if(checkModel() && checkId(id)) return true;
std::atomic_flag blocker{};
bool successful{};
fetchData data{storepath, &successful, &blocker, this};
emscripten_async_wget2(url.c_str(), "A_fIlEnAmE_tHaT_dOeS_nOt_CoNfLiCt.tzst", "GET", nullptr, (void*)&data, [](unsigned handle, void* arg, const char* fname){
fetchData* data = (fetchData*)arg;
if(!extractModel()) {
throwJS("Unable to extract model");
return;
}
fs::remove(fname);
if(!data->self->checkModel()) {
throwJS("Model URL contains invalid model files");
fs::current_path("/opfs");
fs::remove_all(data->storepath);
return;
}
std::ofstream idFile("id");
if(!idFile.is_open()) {
throwJS("Unable to write new id");
fs::current_path("/opfs");
fs::remove_all(data->storepath);
return;
}
idFile << data->self->id;
*data->successful = true;
data->blocker->notify_one();
}, [](unsigned handle, void* arg, int status) {
throwJS("Unable to fetch model");
((fetchData*)arg)->blocker->notify_one();
}, nullptr);
blocker.wait(false, std::memory_order_relaxed);
return successful;
bool genericModel::afterFetch(int memAddr, size_t size) {
if(!extractModel(reinterpret_cast<char*>(memAddr), size)) {
return false;
}
std::ofstream idFile("id");
if(!idFile.is_open()) {
fs::current_path("/opfs");
fs::remove_all(storepath);
return false;
}
idFile << id;
return true;
}
bool genericModel::extractModel() {
bool genericModel::extractModel(const char* fileBuf, size_t size) {
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, "A_fIlEnAmE_tHaT_dOeS_nOt_CoNfLiCt.tzst",10240);
archive_read_open_memory(src, fileBuf, size);
if(archive_errno(src) != 0) return false;
while (archive_read_next_header(src, &entry) == ARCHIVE_OK) {
path = archive_entry_pathname(entry);
// Strip first component
archive_entry_set_pathname(entry, path.substr(path.find("/")).c_str());
// Strip first component, keep relative path
path = "." + path.substr(path.find("/"));
archive_entry_set_pathname(entry, path.c_str());
if(archive_errno(src) != 0) return false;
archive_read_extract(src, entry, ARCHIVE_EXTRACT_UNLINK);
}

View File

@@ -9,22 +9,15 @@
#include <vosk_api.h>
#include <archive.h>
#include <archive_entry.h>
extern void throwJS(const char* msg, bool err);
namespace fs = std::filesystem;
struct genericModel {
const std::string url{};
const std::string storepath{};
const std::string id{};
static bool extractModel();
static bool checkId(const std::string& id);
virtual bool checkModel() = 0;
bool loadModel(const std::string& storepath);
genericModel(const std::string &url, const std::string &storepath, const std::string &id);
};
struct fetchData {
const std::string storepath{};
std::atomic_flag* blocker{};
bool* successful{};
genericModel* self{};
fetchData(const std::string& storepath, bool* successful, std::atomic_flag* blocker, genericModel* self);
static bool extractModel(const char* fileBuf, size_t size);
virtual bool checkModelFiles() = 0;
bool checkModelId();
bool afterFetch(int memAddr, size_t size);
genericModel(const std::string &storepath, const std::string &id);
};

View File

@@ -1,12 +1,26 @@
#include "global.h"
// Throw error for user, or just throw the message for internal communication
void throwJS(const char* msg, bool err) {
EM_ASM({
if($1) {
throw Error(UTF8ToString($0));
return;
}
throw UTF8ToString($0);
},msg, err);
static pthread_t targetThrd{pthread_self()};
static ProxyingQueue pq{};
if(pthread_self() == targetThrd) {
EM_ASM({
if($1) {
throw Error(UTF8ToString($0));
return;
}
throw UTF8ToString($0);
},msg, err);
}
pq.proxyAsync(targetThrd, [&](){
EM_ASM({
if($1) {
throw Error(UTF8ToString($0));
return;
}
throw UTF8ToString($0);
},msg, err);
});
}
int main() {
//vosk_set_log_level(-1);
@@ -14,4 +28,5 @@ int main() {
wasmfs_create_directory("/opfs",0777,wasmfs_create_opfs_backend());
}};
t.detach();
emscripten_exit_with_live_runtime();
}

View File

@@ -2,8 +2,11 @@
#include <thread>
#include <atomic>
#include <emscripten.h>
#include <emscripten/wasmfs.h>
#include <emscripten/console.h>
#include <emscripten/em_asm.h>
#include <emscripten/proxying.h>
using namespace emscripten;
void throwJS(const char* msg, bool err = false);
int main();

View File

@@ -1,27 +1,28 @@
#include "model.h"
model::model(const std::string &url, const std::string& storepath, const std::string& id) : genericModel(url, storepath, id) {
if(!loadModel(storepath)) return;
mdl = vosk_model_new(".");
if(mdl == nullptr) {
throwJS("Unable to initialize model");
}
};
model::model(const std::string& storepath, const std::string& id) : genericModel(storepath, id) {}
model::~model() {
vosk_model_free(mdl);
}
bool model::checkModel() {
return fs::exists("am/final.mdl") &&
fs::exists("conf/mfcc.conf") &&
fs::exists("conf/model.conf") &&
fs::exists("graph/phones/word_boundary.int") &&
fs::exists("graph/Gr.fst") &&
fs::exists("graph/HCLr.fst") &&
fs::exists("graph/disambig_tid.int") &&
fs::exists("ivector/final.dubm") &&
fs::exists("ivector/final.ie") &&
fs::exists("ivector/final.mat") &&
fs::exists("ivector/global_cmvn.stats") &&
fs::exists("ivector/online_cmvn.conf") &&
fs::exists("ivector/splice.conf");
bool model::checkModelId() {
return genericModel::checkModelId();
}
bool model::afterFetch(int addr, size_t size) {
return genericModel::afterFetch(addr,size);
}
bool model::checkModelFiles() {
static std::error_code c{};
return fs::exists("am/final.mdl", c) &&
fs::exists("conf/mfcc.conf", c) &&
fs::exists("conf/model.conf", c) &&
fs::exists("graph/phones/word_boundary.int", c) &&
fs::exists("graph/Gr.fst", c) &&
fs::exists("graph/HCLr.fst", c) &&
fs::exists("graph/disambig_tid.int", c) &&
fs::exists("ivector/final.dubm", c) &&
fs::exists("ivector/final.ie", c) &&
fs::exists("ivector/final.mat", c) &&
fs::exists("ivector/global_cmvn.stats", c) &&
fs::exists("ivector/online_cmvn.conf", c) &&
fs::exists("ivector/splice.conf", c);
}

View File

@@ -2,9 +2,11 @@
#include "genericModel.h"
struct model : genericModel {
bool checkModel();
bool checkModelFiles();
VoskModel* mdl{};
model(const std::string &url, const std::string& storepath, const std::string& id);
model(const std::string& storepath, const std::string& id);
bool checkModelId();
bool afterFetch(int addr, size_t size);
~model();
};

View File

@@ -1,4 +1,3 @@
// @externs
let objs = []
class Recognizer extends EventTarget {
constructor(rec) {
@@ -6,7 +5,6 @@ class Recognizer extends EventTarget {
this.obj = rec
objs.push(this)
this.ptr = Module._malloc(512)
this.arr = Module.HEAPF32.subarray(this.ptr, this.ptr+512)
}
async getNode(ctx, channelIndex = 0) {
if(typeof this.node === "undefined") {
@@ -17,11 +15,11 @@ class Recognizer extends EventTarget {
msgChannel.port1.onmessage = (ev) => {
this.obj.acceptWaveForm()
}
return this.node
}
return this.node
}
recognize(buf, channelIndex = 0) {
buf.copyFromChannel(this.arr, channelIndex)
Module.HEAPF32.set(buf.getChannelData(channelIndex).subarray(0, 512), this.ptr);
this.obj.acceptWaveForm()
}
delete() {
@@ -52,26 +50,54 @@ class Recognizer extends EventTarget {
Module.deleteAll = () => {
objs.forEach(obj => obj.delete())
}
Module.makeModel = async (url, path, id) => {
let mdl
Module.makeModel = async (url, storepath, id) => {
let mdl = new Module.Model(storepath, id)
let mdlMem;
if(mdl.checkModelFiles() && mdl.checkModelId()) {
objs.push(mdl)
return mdl
}
try {
mdl = new Module.Model(url, path, id)
let res = await fetch(url)
if(!res.ok) throw res.statusText
let arr = await res.arrayBuffer()
mdlMem = Module._malloc(arr.byteLength)
Module.HEAP8.set(new Int8Array(arr), mdlMem)
if(!mdl.afterFetch(mdlMem, arr.byteLength)) throw "Unable to extract model and write ID"
if(!mdl.checkModelFiles()) throw "Model contains invalid model files"
}
catch(e) {
mdl.delete()
return Promise.reject(e)
return Promise.reject(e.message || e)
}
finally {
Module._free(mdlMem)
}
objs.push(mdl)
return mdl
}
Module.makeSpkModel = async (url, path, id) => {
let mdl
Module.makeSpkModel = async (url, storepath, id) => {
let mdl = new Module.SpkModel(storepath, id)
let mdlMem;
if(mdl.checkModelFiles() && mdl.checkModelId()) {
objs.push(mdl)
return mdl
}
try {
mdl = new Module.SpkModel(url, path, id)
let res = await fetch(url)
if(!res.ok) throw res.statusText
let arr = await res.arrayBuffer()
mdlMem = Module._malloc(arr.byteLength)
Module.HEAP8.set(new Int8Array(arr), mdlMem)
if(!mdl.afterFetch(mdlMem, arr.byteLength)) throw "Unable to extract model and write ID"
if(!mdl.checkModelFiles()) throw "Model contains invalid model files"
}
catch(e) {
mdl.delete()
return Promise.reject(e)
return Promise.reject(e.message || e)
}
finally {
Module._free(mdlMem)
}
objs.push(mdl)
return mdl

View File

@@ -9,6 +9,7 @@ if(typeof BRProcessor === "undefined") {
case "init":
this.recognizerPort = ev.ports[0]
this.wasmMem = new Float32Array(WebAssembly.Memory.buffer).subarray(ev.ptr, ev.ptr+512)
this.channel = ev.channel;
break
case "deinit":
this.done = true
@@ -18,7 +19,7 @@ if(typeof BRProcessor === "undefined") {
}
process(inputs, outputs, params) {
if(this.done) return false;
inputs[0].copyFromChannel(this.wasmMem, this.channel)
this.wasmMem.set(inputs[0].getChannelData(this.channel));
this.recognizerPort.postMessage(".") // Basically an empty message
outputs = inputs
return true

View File

@@ -1,44 +1,57 @@
#include "recognizer.h"
recognizer::recognizer(model* mdl, float sampleRate, int index) : index(index) {
rec = vosk_recognizer_new(mdl->mdl,sampleRate);
if(rec == nullptr) {
throwJS("Unable to initialize recognizer");
return;
}
controller.lock();
std::thread t{[this](const pthread_t& caller){
fs::current_path("/opfs");
fs::current_path(mdl->storepath);
std::thread t{[this](VoskModel* mdl, VoskRecognizer* rec, float sampleRate){
if(mdl == nullptr) {
mdl = vosk_model_new(".");
if(mdl == nullptr) {
throwJS("Unable to load model");
return;
}
}
rec = vosk_recognizer_new(mdl,sampleRate);
if(rec == nullptr) {
throwJS("Unable to initialize recognizer");
return;
}
while(!done.test()) {
controller.lock();
emscripten_console_log("In loop");
controller.wait(false, std::memory_order_relaxed);
if(!done.test()) {
switch(vosk_recognizer_accept_waveform_f(rec, dataPtr, 512)) {
case 0:
fireEv("result", vosk_recognizer_result(rec), caller);
fireEv("result", vosk_recognizer_result(rec));
break;
case 1:
fireEv("partialResult", vosk_recognizer_partial_result(rec), caller);
fireEv("partialResult", vosk_recognizer_partial_result(rec));
}
}
}
},pthread_self()};
},mdl->mdl, rec, sampleRate};
t.detach();
}
recognizer::~recognizer() {
done.test_and_set(std::memory_order_relaxed);
controller.unlock();
controller.notify_one();
vosk_recognizer_free(rec);
free(dataPtr);
}
void recognizer::fireEv(const char *type, const char *content, const pthread_t& caller) {
void recognizer::fireEv(const char *type, const char *content) {
static pthread_t targetThrd{pthread_self()};
static ProxyingQueue pq{};
pq.proxyAsync(caller, [&](){
pq.proxyAsync(targetThrd, [&](){
EM_ASM({
objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"details" : UTF8ToString($2)}));
let ev = new CustomEvent(UTF8ToString($1), {"details" : UTF8ToString($2)});
objs[$0].dispatchEvent(ev);
console.log(objs[$0], ev)
},index, type, content);
});
}
void recognizer::acceptWaveForm() {
controller.unlock();
controller.lock();
controller.notify_one();
emscripten_console_log("Unblocked");
fireEv("result", "Test event");
}
void recognizer::setGrm(const std::string& grm) {
vosk_recognizer_set_grm(rec, grm.c_str());

View File

@@ -9,19 +9,18 @@
#include <AL/alc.h>
#include <archive.h>
#include <archive_entry.h>
#include <emscripten/proxying.h>
namespace fs = std::filesystem;
using namespace emscripten;
struct recognizer {
std::atomic_flag done{};
std::mutex controller{};
std::atomic_flag controller{};
float* dataPtr{};
int index{};
VoskRecognizer* rec{};
recognizer(model* model, float sampleRate, int index);
~recognizer();
void acceptWaveForm();
void fireEv(const char* type, const char* content, const pthread_t& caller);
void fireEv(const char* type, const char* content);
void setSpkModel(spkModel* model);
void setGrm(const std::string& grm);
void setWords(bool words);

View File

@@ -1,15 +1,20 @@
#include "spkModel.h"
spkModel::spkModel(const std::string &url, const std::string& storepath, const std::string& id) : genericModel(url, storepath, id) {
if(!loadModel(storepath)) return;
spkModel::spkModel(const std::string& storepath, const std::string& id) : genericModel(storepath, id) {
mdl = vosk_spk_model_new(".");
if(mdl == nullptr) {
throwJS("Unable to initialize speaker model");
}
};
}
spkModel::~spkModel() {
vosk_spk_model_free(mdl);
}
bool spkModel::checkModel() {
bool spkModel::checkModelId() {
return genericModel::checkModelId();
}
bool spkModel::afterFetch(int addr, size_t size) {
return genericModel::afterFetch(addr,size);
}
bool spkModel::checkModelFiles() {
return fs::exists("mfcc.conf") &&
fs::exists("final.ext.raw") &&
fs::exists("mean.vec") &&

View File

@@ -2,9 +2,11 @@
#include "genericModel.h"
struct spkModel : genericModel {
bool checkModel();
bool checkModelFiles();
VoskSpkModel* mdl{};
spkModel(const std::string &url, const std::string& storepath, const std::string& id);
spkModel(const std::string& storepath, const std::string& id);
bool checkModelId();
bool afterFetch(int addr, size_t size);
~spkModel();
};

File diff suppressed because one or more lines are too long

View File

@@ -1 +1,161 @@
"use strict";var Module={};var initializedJS=false;function threadPrintErr(){var text=Array.prototype.slice.call(arguments).join(" ");console.error(text)}function threadAlert(){var text=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:text,threadId:Module["_pthread_self"]()})}var err=threadPrintErr;self.alert=threadAlert;Module["instantiateWasm"]=(info,receiveInstance)=>{var module=Module["wasmModule"];Module["wasmModule"]=null;var instance=new WebAssembly.Instance(module,info);return receiveInstance(instance)};self.onunhandledrejection=e=>{throw e.reason||e};function handleMessage(e){try{if(e.data.cmd==="load"){let messageQueue=[];self.onmessage=e=>messageQueue.push(e);self.startWorker=instance=>{Module=instance;postMessage({"cmd":"loaded"});for(let msg of messageQueue){handleMessage(msg)}self.onmessage=handleMessage};Module["wasmModule"]=e.data.wasmModule;for(const handler of e.data.handlers){Module[handler]=(...args)=>{postMessage({cmd:"callHandler",handler:handler,args:args})}}Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob=="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}loadBR(Module)}else if(e.data.cmd==="run"){Module["__emscripten_thread_init"](e.data.pthread_ptr,0,0,1);Module["__emscripten_thread_mailbox_await"](e.data.pthread_ptr);Module["establishStackSpace"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInitTLS();if(!initializedJS){Module["__embind_initialize_bindings"]();initializedJS=true}try{Module["invokeEntryPoint"](e.data.start_routine,e.data.arg)}catch(ex){if(ex!="unwind"){throw ex}}}else if(e.data.cmd==="cancel"){if(Module["_pthread_self"]()){Module["__emscripten_thread_exit"](-1)}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="checkMailbox"){if(initializedJS){Module["checkMailbox"]()}}else if(e.data.cmd){err(`worker.js received unknown command ${e.data.cmd}`);err(e.data)}}catch(ex){Module["__emscripten_thread_crashed"]?.();throw ex}}self.onmessage=handleMessage;
/**
* @license
* Copyright 2015 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// Pthread Web Worker startup routine:
// This is the entry point file that is loaded first by each Web Worker
// that executes pthreads on the Emscripten application.
'use strict';
var Module = {};
// Thread-local guard variable for one-time init of the JS state
var initializedJS = false;
function assert(condition, text) {
if (!condition) abort('Assertion failed: ' + text);
}
function threadPrintErr() {
var text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
}
function threadAlert() {
var text = Array.prototype.slice.call(arguments).join(' ');
postMessage({cmd: 'alert', text, threadId: Module['_pthread_self']()});
}
// We don't need out() for now, but may need to add it if we want to use it
// here. Or, if this code all moves into the main JS, that problem will go
// away. (For now, adding it here increases code size for no benefit.)
var out = () => { throw 'out() is not defined in worker.js.'; }
var err = threadPrintErr;
self.alert = threadAlert;
var dbg = threadPrintErr;
Module['instantiateWasm'] = (info, receiveInstance) => {
// Instantiate from the module posted from the main thread.
// We can just use sync instantiation in the worker.
var module = Module['wasmModule'];
// We don't need the module anymore; new threads will be spawned from the main thread.
Module['wasmModule'] = null;
var instance = new WebAssembly.Instance(module, info);
// TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193,
// the above line no longer optimizes out down to the following line.
// When the regression is fixed, we can remove this if/else.
return receiveInstance(instance);
}
// Turn unhandled rejected promises into errors so that the main thread will be
// notified about them.
self.onunhandledrejection = (e) => {
throw e.reason || e;
};
function handleMessage(e) {
try {
if (e.data.cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
// Until we initialize the runtime, queue up any further incoming messages.
let messageQueue = [];
self.onmessage = (e) => messageQueue.push(e);
// And add a callback for when the runtime is initialized.
self.startWorker = (instance) => {
Module = instance;
// Notify the main thread that this thread has loaded.
postMessage({ 'cmd': 'loaded' });
// Process any messages that were queued before the thread was ready.
for (let msg of messageQueue) {
handleMessage(msg);
}
// Restore the real message handler.
self.onmessage = handleMessage;
};
// Module and memory were sent from main thread
Module['wasmModule'] = e.data.wasmModule;
// Use `const` here to ensure that the variable is scoped only to
// that iteration, allowing safe reference from a closure.
for (const handler of e.data.handlers) {
Module[handler] = (...args) => {
postMessage({ cmd: 'callHandler', handler, args: args });
}
}
Module['wasmMemory'] = e.data.wasmMemory;
Module['buffer'] = Module['wasmMemory'].buffer;
Module['workerID'] = e.data.workerID;
Module['ENVIRONMENT_IS_PTHREAD'] = true;
if (typeof e.data.urlOrBlob == 'string') {
importScripts(e.data.urlOrBlob);
} else {
var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
importScripts(objectUrl);
URL.revokeObjectURL(objectUrl);
}
loadBR(Module);
} else if (e.data.cmd === 'run') {
// Pass the thread address to wasm to store it for fast access.
Module['__emscripten_thread_init'](e.data.pthread_ptr, /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1);
// Await mailbox notifications with `Atomics.waitAsync` so we can start
// using the fast `Atomics.notify` notification path.
Module['__emscripten_thread_mailbox_await'](e.data.pthread_ptr);
assert(e.data.pthread_ptr);
// Also call inside JS module to set up the stack frame for this pthread in JS module scope
Module['establishStackSpace']();
Module['PThread'].receiveObjectTransfer(e.data);
Module['PThread'].threadInitTLS();
if (!initializedJS) {
// Embind must initialize itself on all threads, as it generates support JS.
// We only do this once per worker since they get reused
Module['__embind_initialize_bindings']();
initializedJS = true;
}
try {
Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
} catch(ex) {
if (ex != 'unwind') {
// The pthread "crashed". Do not call `_emscripten_thread_exit` (which
// would make this thread joinable). Instead, re-throw the exception
// and let the top level handler propagate it back to the main thread.
throw ex;
}
}
} else if (e.data.cmd === 'cancel') { // Main thread is asking for a pthread_cancel() on this thread.
if (Module['_pthread_self']()) {
Module['__emscripten_thread_exit'](-1);
}
} else if (e.data.target === 'setimmediate') {
// no-op
} else if (e.data.cmd === 'checkMailbox') {
if (initializedJS) {
Module['checkMailbox']();
}
} else if (e.data.cmd) {
// The received message looks like something that should be handled by this message
// handler, (since there is a e.data.cmd field present), but is not one of the
// recognized commands:
err(`worker.js received unknown command ${e.data.cmd}`);
err(e.data);
}
} catch(ex) {
err(`worker.js onmessage() captured an uncaught exception: ${ex}`);
if (ex?.stack) err(ex.stack);
Module['__emscripten_thread_crashed']?.();
throw ex;
}
};
self.onmessage = handleMessage;

View File

@@ -1,12 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<script src="BrowserRecognizer.js" async defer>
<script src="BrowserRecognizer.js"></script>
<script>
/*(async () => {
const BrRec = await loadBR()
const model = await BrRec.makeModel("en-model.tzst","model","en0.0.1")
let ctx = new AudioContext()
const recognizer = await BrRec.makeRecognizer(model, ctx.sampleRate)
recognizer.addEventListener("result", e => {
console.log("Result: ",e.details)
})
recognizer.addEventListener("partialResult", e => {
console.log("Partial result: ",e.details)
})
let micNode = ctx.createMediaStreamSource(await navigator.mediaDevices.getUserMedia({
video: false,
audio: {
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
sampleRate: 16000
},
}));
let recNode = recognizer.getNode(ctx)
})()*/
</script>
<!--
<script src="src/genericObj.js"></script>
<script src="src/model.js"></script>
<script src="src/spkModel.js"></script>
<script src="src/recognizer.js"></script>-->
</head>
</html>

View File

@@ -35,4 +35,4 @@ fi
export PATH=:$PATH:$EMSDK/upstream/bin &&
cd $SRC &&
em++ -O3 global.cc genericModel.cc model.cc spkModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sEMBIND_STD_STRING_IS_UTF8 -sSUPPORT_LONGJMP=0 -sMODULARIZE -sEXPORT_NAME=loadBR -sENVIRONMENT=web,worker -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPTHREAD_POOL_SIZE_STRICT -sPTHREAD_POOL_DELAY_LOAD -sALLOW_BLOCKING_ON_MAIN_THREAD=1 -sPOLYFILL=0 --pre-js pre.js -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$ZSTD/lib -lzstd -L$KALDI/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L$OPENFST/lib -l:libfst.a -l:libfstngram.a -L$CLAPACK_WASM -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L$VOSK/src -l:vosk.a -lopfs.js -lembind -pthread -o ../test/BrowserRecognizer.js
em++ -O0 global.cc genericModel.cc model.cc spkModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sEMBIND_STD_STRING_IS_UTF8 -sASSERTIONS=2 -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sEXPORTED_FUNCTIONS=_malloc,_free,_main -sSUPPORT_LONGJMP=0 -sMODULARIZE -sEXPORT_NAME=loadBR -sENVIRONMENT=web,worker -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPTHREAD_POOL_SIZE_STRICT -sALLOW_BLOCKING_ON_MAIN_THREAD=1 -sPOLYFILL=0 --pre-js pre.js -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$ZSTD/lib -lzstd -L$KALDI/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L$OPENFST/lib -l:libfst.a -l:libfstngram.a -L$CLAPACK_WASM -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L$VOSK/src -l:vosk.a -lopfs.js -lembind -pthread -flto -o ../test/BrowserRecognizer.js

BIN
test/test.wav Normal file

Binary file not shown.