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,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();
};