Use C++ iostream in untar, enforce naming convention, remodel threading to use join, reduce lambda usage

This commit is contained in:
msqr1
2024-08-20 22:59:13 -07:00
parent 2122902190
commit 53262fe7f3
12 changed files with 125 additions and 102 deletions

View File

@@ -1,8 +1,10 @@
#include "CommonModel.h"
CommonModel::CommonModel(int index, bool normalMdl, std::string storepath, std::string id, int tarStart, int tarSize) : normalMdl{normalMdl}, index{index}, storepath{std::move(storepath)}, id{std::move(id)} {
t = std::thread{extractAndLoad, reinterpret_cast<unsigned char*>(tarStart), tarSize};
}
CommonModel::CommonModel(int index, bool normalMdl, std::string storepath, std::string id, int tarStart, int tarSize) :
normalMdl{normalMdl}, index{index},
t{&CommonModel::extractAndLoad, this, reinterpret_cast<unsigned char*>(tarStart), tarSize},
storepath{std::move(storepath)},
id{std::move(id)} {}
void CommonModel::extractAndLoad(unsigned char* tar, int tarSize) {
int res{untar(tar, tarSize, storepath)};
free(tar);
@@ -28,6 +30,10 @@ void CommonModel::extractAndLoad(unsigned char* tar, int tarSize) {
if(normalMdl ? std::get<VoskModel*>(mdl) != nullptr : std::get<VoskSpkModel*>(mdl) != nullptr) fireEv(index, "0");
else fireEv(index, "Unable to load model for recognition");
fs::remove_all(storepath);
// Wait for potential recognizer usage
thrdUsed.wait(false, std::memory_order_relaxed);
if(recognizerMain) recognizerMain();
}
int CommonModel::findWord(std::string word) {
return vosk_model_find_word(std::get<VoskModel*>(mdl), word.c_str());
@@ -35,5 +41,9 @@ int CommonModel::findWord(std::string word) {
CommonModel::~CommonModel() {
if(normalMdl) vosk_model_free(std::get<VoskModel*>(mdl));
else vosk_spk_model_free(std::get<VoskSpkModel*>(mdl));
if(t.joinable()) t.join();
if(t.joinable()) {
thrdUsed.store(true, std::memory_order_relaxed);
thrdUsed.notify_one();
t.join();
}
}