Remove model id on c++ side constructor, use index as storepath

This commit is contained in:
msqr1
2024-10-03 19:24:57 -07:00
parent 30228eb0c1
commit 0d1acd0ebd
10 changed files with 18 additions and 21 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -14,13 +14,13 @@ EMSCRIPTEN_BINDINGS() {
.value("ANSWER_VERY_LONG", VOSK_EP_ANSWER_VERY_LONG); .value("ANSWER_VERY_LONG", VOSK_EP_ANSWER_VERY_LONG);
class_<CommonModel>("CommonModel") class_<CommonModel>("CommonModel")
.constructor<int, bool, std::string, std::string, int, int>(return_value_policy::take_ownership()) .constructor<int, bool, int, int>(return_value_policy::take_ownership())
.function("findWord", &CommonModel::findWord); .function("findWord", &CommonModel::findWord);
class_<Recognizer>("Recognizer") class_<Recognizer>("Recognizer")
.constructor<int, float, CommonModel*>(return_value_policy::take_ownership()) .constructor<int, float, CommonModel*>(return_value_policy::take_ownership())
.constructor<int, float, CommonModel*, CommonModel*>(return_value_policy::take_ownership()) .constructor<int, float, CommonModel*, CommonModel*>(return_value_policy::take_ownership())
.constructor<int, float, CommonModel*, std::string, int>(return_value_policy::take_ownership()) .constructor<int, float, CommonModel*, const std::string&, int>(return_value_policy::take_ownership())
.function("safeDelete", &Recognizer::safeDelete) .function("safeDelete", &Recognizer::safeDelete)
.function("acceptWaveform", &Recognizer::acceptWaveform) .function("acceptWaveform", &Recognizer::acceptWaveform)
.function("reset", &Recognizer::reset) .function("reset", &Recognizer::reset)

View File

@@ -1,16 +1,15 @@
#include "CommonModel.h" #include "CommonModel.h"
CommonModel::CommonModel(int index, bool normalMdl, std::string storepath, std::string id, int tarStart, int tarSize) : CommonModel::CommonModel(int index, bool normalMdl, int tarStart, int tarSize) :
normalMdl{normalMdl}, index{index}, normalMdl{normalMdl}, index{index}
storepath{std::move(storepath)},
id{std::move(id)}
{ {
globalPool.exec([this, tarStart, tarSize]{ globalPool.exec([this, tarStart, tarSize]{
extractAndLoad(reinterpret_cast<unsigned char*>(tarStart), tarSize); extractAndLoad(reinterpret_cast<unsigned char*>(tarStart), tarSize);
}); });
} }
void CommonModel::extractAndLoad(unsigned char* tar, int tarSize) { void CommonModel::extractAndLoad(unsigned char* tar, int tarSize) {
int res{untar(tar, tarSize, storepath)}; std::string storepath{'/' + std::to_string(index)};
int res{untar(tar, tarSize, storepath.c_str())};
free(tar); free(tar);
switch(res) { switch(res) {
case IncorrectFormat: case IncorrectFormat:

View File

@@ -6,12 +6,10 @@
struct CommonModel { struct CommonModel {
bool normalMdl; bool normalMdl;
int index; int index;
std::string storepath;
std::string id;
std::variant<VoskModel*, VoskSpkModel*> mdl; std::variant<VoskModel*, VoskSpkModel*> mdl;
void extractAndLoad(unsigned char* tarStart, int tarSize); void extractAndLoad(unsigned char* tarStart, int tarSize);
int findWord(std::string word); int findWord(std::string word);
CommonModel(int index, bool normalMdl, std::string storepath, std::string id, int tarStart, int tarSize); CommonModel(int index, bool normalMdl, int tarStart, int tarSize);
~CommonModel(); ~CommonModel();
}; };

View File

@@ -9,7 +9,7 @@ EM_JS(void, fireEv, (int idx, int typeIdx, const char* content), {
})); }));
else self.postMessage([idx, typeIdx, content]); else self.postMessage([idx, typeIdx, content]);
}); });
int untar(unsigned char* tar, int tarSize, const std::string& storepath) { int untar(unsigned char* tar, int tarSize, const char* storepath) {
if(std::memcmp(tar + 257, "ustar", 5)) return IncorrectFormat; if(std::memcmp(tar + 257, "ustar", 5)) return IncorrectFormat;
size_t size{}; size_t size{};
std::string path; std::string path;

View File

@@ -8,12 +8,6 @@
#include "emscripten/console.h" #include "emscripten/console.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
struct AudioData {
float* data;
int len;
AudioData(int start, int len) : data{reinterpret_cast<float*>(start)}, len{len} {}
};
enum Event { enum Event {
// Shared // Shared
status, status,
@@ -30,6 +24,12 @@ enum UntarStatus {
FailedWrite, FailedWrite,
FailedClose FailedClose
}; };
struct AudioData {
float* data;
int len;
AudioData(int start, int len) : data{reinterpret_cast<float*>(start)}, len{len} {}
};
#ifndef MAX_WORKERS #ifndef MAX_WORKERS
#define MAX_WORKERS 1 #define MAX_WORKERS 1
#endif #endif
@@ -43,6 +43,6 @@ struct WorkerPool {
}; };
extern "C" void fireEv(int idx, int typeIdx, const char* content = nullptr); extern "C" void fireEv(int idx, int typeIdx, const char* content = nullptr);
int untar(unsigned char* tar, int tarSize, const std::string& storepath); int untar(unsigned char* tar, int tarSize, const char* storepath);
extern WorkerPool globalPool; extern WorkerPool globalPool;

View File

@@ -92,7 +92,7 @@ class CommonModel extends EventTarget {
tar = tar.data; tar = tar.data;
let tarStart = _malloc(tar.byteLength); let tarStart = _malloc(tar.byteLength);
HEAPU8.set(new Uint8Array(tar), tarStart); HEAPU8.set(new Uint8Array(tar), tarStart);
mdl.obj = new Module['CommonModel'](objs.length - 1, normalMdl, '/' + storepath, id, tarStart, tar.byteLength); mdl.obj = new Module['CommonModel'](objs.length - 1, normalMdl, tarStart, tar.byteLength);
}, { once: true }); }, { once: true });
storageWorker.postMessage({ storageWorker.postMessage({
url: url, url: url,