Skip to content

Commit

Permalink
Attempt to fix MacOs #13
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr1 committed Sep 24, 2024
1 parent c35cd06 commit 14c913d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Examples/Vosklet.js

Large diffs are not rendered by default.

Binary file modified Examples/Vosklet.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion Examples/fromFile.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// All data is collected and transfered to the main thread so the AudioContext won't output anything. Set sinkId type to none to save power
let ctx = new AudioContext({sinkId: {type: "none"}})
let module = await loadVosklet()
let model = await module.createModel("https://ccoreilly.github.io/vosk-browser/models/vosk-model-small-en-us-0.15.tar.gz","English","vosk-model-small-en-us-0.15")
let model = await module.createModel("https://ccoreilly.github.io/vosk-browser/models/vosk-model-small-en-us-0.15.tar.gz", "English", "vosk-model-small-en-us-0.15")
let recognizer = await module.createRecognizer(model, ctx.sampleRate)

// Listen for result and partial result
Expand Down
2 changes: 1 addition & 1 deletion Examples/fromMic.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// Load Vosklet module, model and recognizer
let module = await loadVosklet()
let model = await module.createModel("https://ccoreilly.github.io/vosk-browser/models/vosk-model-small-en-us-0.15.tar.gz","English","vosk-model-small-en-us-0.15")
let model = await module.createModel("https://ccoreilly.github.io/vosk-browser/models/vosk-model-small-en-us-0.15.tar.gz", "English", "vosk-model-small-en-us-0.15")
let recognizer = await module.createRecognizer(model, ctx.sampleRate)

// Listen for result and partial result
Expand Down
2 changes: 1 addition & 1 deletion Vosklet.js

Large diffs are not rendered by default.

Binary file modified Vosklet.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions src/Clapack-wasm.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index bf7eda6..5fedaf6 100644
#-----------------------------------------------------------------------------

-CFLAGS = -DADD_ -O3
+CFLAGS = -DADD_ -O3 -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals
+CFLAGS = -DADD_ -O3 -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals

#-----------------------------------------------------------------------------
# Archive programs and flags
Expand All @@ -20,7 +20,7 @@ index 80037d0..a964b2d 100644
# if no wrapping of the blas library is needed, uncomment next line
CC = emcc # -DNO_BLAS_WRAP
-CFLAGS = -I$(TOPDIR)/INCLUDE -I$(TOPDIR)/../libf2c -O3
+CFLAGS = -I$(TOPDIR)/INCLUDE -I$(TOPDIR)/../libf2c -O3 -fno-rtti -fno-exceptions -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals
+CFLAGS = -I$(TOPDIR)/INCLUDE -I$(TOPDIR)/../libf2c -O3 -fno-rtti -fno-exceptions -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals
LOADER = $(CC)
LOADOPTS =
NOOPT = -O0 -I$(TOPDIR)/INCLUDE -I$(TOPDIR)/../libf2c
Expand All @@ -33,7 +33,7 @@ index e071614..4647c2b 100644
#
CC = emcc
-CFLAGS = -I../libf2c -O3
+CFLAGS = -I../libf2c -O3 -fno-rtti -fno-exceptions -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals
+CFLAGS = -I../libf2c -O3 -fno-rtti -fno-exceptions -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals
DRVOPTS = $(OPTS)
NOOPT =
LOADER = emcc
Expand All @@ -46,7 +46,7 @@ index 6221401..d93b87f 100644
CC = emcc
SHELL = /bin/sh
-CFLAGS = -DNON_UNIX_STDIO -O3
+CFLAGS = -DNON_UNIX_STDIO -O3 -fno-rtti -fno-exceptions -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals
+CFLAGS = -DNON_UNIX_STDIO -O3 -fno-rtti -fno-exceptions -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals

LD = wasm-ld
RANLIB = emranlib
Expand Down
229 changes: 120 additions & 109 deletions src/Wrapper.js
Original file line number Diff line number Diff line change
@@ -1,166 +1,177 @@
let objs = []
let events = ["status", "partialResult", "result"]
let objs = [];
let events = ["status", "partialResult", "result"];
let storageWorkerURL = URL.createObjectURL(new Blob(['(', (() => {
let txtDecoder = new TextDecoder();
let txtEncoder = new TextEncoder();
onmessage = async msg => {
msg = msg.data;
let components = msg.storepath.split("/");
let prevDir = await navigator.storage.getDirectory();
for(let component of components) prevDir = await prevDir.getDirectoryHandle(component, { create: true });
let idHandle = await prevDir.getFileHandle("id", { create: true });
let mdlHandle = await prevDir.getFileHandle("model.tgz", { create: true });
let idFile = await idHandle.createSyncAccessHandle();
let mdlFile = await mdlHandle.createSyncAccessHandle();
let oldIdBuf = new ArrayBuffer(idFile.getSize());
idFile.read(oldIdBuf);
let tar, tgz;
if(txtDecoder.decode(oldIdBuf) == msg.id) {
tgz = new ArrayBuffer(mdlFile.getSize());
mdlFile.read(tgz);
tar = await new Response(new Response(tgz).body.pipeThrough(new DecompressionStream("gzip"))).arrayBuffer();
}
else {
let res = await fetch(msg.url);
if(!res.ok) {
postMessage("Unable to download model");
return;
}
let teed = res.body.tee();
tgz = await new Response(teed[0].pipeThrough(new CompressionStream("gzip"))).arrayBuffer();
mdlFile.write(tgz, { at: 0 });
mdlFile.truncate(tgz.byteLength);
let newId = txtEncoder.encode(msg.id);
idFile.write(newId, { at: 0 });
idFile.truncate(newId.length);
tar = await new Response(teed[1]).arrayBuffer();
}
idFile.close();
mdlFile.close();
self.postMessage(tar, [tar]);
}
}).toString(), ')()'], { type: "text/javascript" }))
let storageWorker = new Worker(storageWorkerURL);
let processorURL = URL.createObjectURL(new Blob(['(', (() => {
registerProcessor("VoskletTransferer", class extends AudioWorkletProcessor {
constructor(opts) {
super()
this.count = 0
this.maxCount = opts.processorOptions.maxCount
this.buffer = new Float32Array(this.maxCount * 128)
super();
this.count = 0;
this.maxCount = opts.processorOptions.maxCount;
this.buffer = new Float32Array(this.maxCount * 128);
}
process(inputs) {
if(!inputs[0][0]) return true
this.buffer.set(inputs[0][0], this.count * 128)
this.count++
if(!inputs[0][0]) return true;
this.buffer.set(inputs[0][0], this.count++ * 128);
if(this.count >= this.maxCount) {
this.count = 0
this.port.postMessage(this.buffer, [this.buffer.buffer])
this.buffer = new Float32Array(this.maxCount * 128)
this.count = 0;
this.port.postMessage(this.buffer, [this.buffer.buffer]);
this.buffer = new Float32Array(this.maxCount * 128);
}
return true
return true;
}
})
}).toString(), ')()'], { type : "text/javascript" }))

}).toString(), ')()'], { type: "text/javascript" }));
Module.cleanUp = async () => {
for(let obj of objs) await obj.delete()
URL.revokeObjectURL(processorURL)
for(let obj of objs) await obj.delete();
URL.revokeObjectURL(processorURL);
URL.revokeObjectURL(storageWorkerURL);
storageWorker.terminate();
}

Module.createTransferer = async (ctx, bufferSize) => {
await ctx.audioWorklet.addModule(processorURL)
await ctx.audioWorklet.addModule(processorURL);
return new AudioWorkletNode(ctx, "VoskletTransferer", {
channelCountMode : "explicit",
numberOfInputs : 1,
numberOfOutputs : 0,
channelCount : 1,
processorOptions : { maxCount: bufferSize / 128 }
})
}

getFileHandle = async (path, create = false) => {
let components = path.split("/")
let prevDir = await navigator.storage.getDirectory()
for(let component of components.slice(0, -1)) prevDir = await prevDir.getDirectoryHandle(component, { create : create })
return prevDir.getFileHandle(components[components.length - 1], { create : create })
channelCountMode: "explicit",
numberOfInputs: 1,
numberOfOutputs: 0,
channelCount: 1,
processorOptions: { maxCount: bufferSize / 128 }
});
}

class CommonModel extends EventTarget {
constructor() {
super()
objs.push(this)
super();
objs.push(this);
}
delete() {
this.obj.delete()
this.obj.delete();
}
static async create(url, storepath, id, normalMdl) {
let mdl = new CommonModel()
static async mk(url, storepath, id, normalMdl) {
let mdl = new CommonModel();
let result = new Promise((resolve, reject) => {
mdl.addEventListener("status", ev => {
if(!ev.detail) {
if(normalMdl) mdl.findWord = (word) => mdl.obj.findWord(word)
if(normalMdl) mdl.findWord = word => mdl.obj.findWord(word)
resolve(mdl)
}
else reject(ev.detail)
}, { once : true })
})
let tar
try {
let dataFile = await (await getFileHandle(storepath + "/model.tgz")).getFile()
let idFile = await (await getFileHandle(storepath + "/id")).getFile()
if(await idFile.text() != id) throw ""
tar = await new Response(dataFile.stream().pipeThrough(new DecompressionStream("gzip"))).arrayBuffer()
}
catch {
try {
let res = await fetch(url)
if (!res.ok) throw "Unable to download model"
let teedBody = res.body.tee()
let newDataFile = await (await getFileHandle(storepath + "/model.tgz", true)).createWritable()
await newDataFile.write(await new Response(teedBody[0].pipeThrough(new CompressionStream("gzip"))).arrayBuffer())
await newDataFile.close()
let newIDFile = await (await getFileHandle(storepath + "/id", true)).createWritable()
await newIDFile.write(id)
await newIDFile.close()
tar = await new Response(teedBody[1]).arrayBuffer()
}
catch(e) {
mdl.delete()
throw e
}
}
let tarStart = _malloc(tar.byteLength)
HEAPU8.set(new Uint8Array(tar), tarStart)
mdl.obj = new Module.CommonModel(objs.length - 1, normalMdl, "/" + storepath, id, tarStart, tar.byteLength)
return result
});
storageWorker.addEventListener("message", tar => {
tar = tar.data;
let tarStart = _malloc(tar.byteLength);
HEAPU8.set(new Uint8Array(tar), tarStart);
mdl.obj = new Module.CommonModel(objs.length - 1, normalMdl, "/" + storepath, id, tarStart, tar.byteLength);
}, { once: true });
storageWorker.postMessage({
url: url,
storepath: storepath,
id: id
});
return result;
}
}

Module.createModel = async (url, storepath, id) => {
return CommonModel.create(url, storepath, id, true)
}
Module.createModel = async (url, storepath, id) =>
CommonModel.mk(url, storepath, id, true);

Module.createSpkModel = async (url, storepath, id) => {
return CommonModel.create(url, storepath, id, false)
}
Module.createSpkModel = async (url, storepath, id) =>
CommonModel.mk(url, storepath, id, false);

class Recognizer extends EventTarget {
constructor() {
super()
objs.push(this)
super();
objs.push(this);
return new Proxy(this, {
get(self, prop, _) {
if(self[prop] == undefined && self.obj[prop] == undefined) return undefined
let p = self[prop]
if(p) return p.bind ? p.bind(self) : p
p = self.obj[prop]
return p.bind ? p.bind(self.obj) : p
if(self[prop] == undefined && self.obj[prop] == undefined) return;
let p = self[prop];
if(p) return p.bind ? p.bind(self) : p;
p = self.obj[prop];
return p.bind ? p.bind(self.obj) : p;
}
})
}
acceptWaveform(audioData) {
let start = _malloc(audioData.length * 4)
HEAPF32.set(audioData, start / 4)
this.obj.acceptWaveform(start, audioData.length)
let start = _malloc(audioData.length * 4);
HEAPF32.set(audioData, start / 4);
this.obj.acceptWaveform(start, audioData.length);
}
async delete(processCurrent = false) {
let result = new Promise((resolve, _) => this.addEventListener("status", _ => {
this.obj.delete()
resolve()
}, { once : true }))
this.obj.safeDelete(processCurrent)
return result
this.obj.delete();
resolve();
}, { once: true }));
this.obj.safeDelete(processCurrent);
return result;
}
static async create(model, sampleRate, mode, grammar, spkModel) {
let rec = new Recognizer()
static async mk(model, sampleRate, mode, grammar, spkModel) {
let rec = new Recognizer();
let result = new Promise((resolve, reject) => {
rec.addEventListener("status", ev => {
if(!ev.detail) resolve(rec)
else reject(ev.detail)
}, { once : true })
if(!ev.detail) resolve(rec);
else reject(ev.detail);
}, { once: true });
})
switch(mode) {
case 1:
rec.obj = new Module.Recognizer(objs.length - 1, sampleRate, model)
break
rec.obj = new Module.Recognizer(objs.length - 1, sampleRate, model);
break;
case 2:
rec.obj = new Module.Recognizer(objs.length -1, sampleRate, model, spkModel)
break
rec.obj = new Module.Recognizer(objs.length -1, sampleRate, model, spkModel);
break;
default:
rec.obj = new Module.Recognizer(objs.length - 1, sampleRate, model, grammar, 0)
rec.obj = new Module.Recognizer(objs.length - 1, sampleRate, model, grammar, 0);
}
return result
return result;
}
}
Module.createRecognizer = (model, sampleRate) =>
Recognizer.mk(model.obj, sampleRate, 1);

Module.createRecognizer = (model, sampleRate) => {
return Recognizer.create(model.obj, sampleRate, 1)
}

Module.createRecognizerWithSpkModel = (model, sampleRate, spkModel) => {
return Recognizer.create(model.obj, sampleRate, 2, null, spkModel.obj)
}
Module.createRecognizerWithSpkModel = (model, sampleRate, spkModel) =>
Recognizer.mk(model.obj, sampleRate, 2, null, spkModel.obj);

Module.createRecognizerWithGrm = (model, sampleRate, grammar) => {
return Recognizer.create(model.obj, sampleRate, 3, grammar, null)
}
Module.createRecognizerWithGrm = (model, sampleRate, grammar) =>
Recognizer.mk(model.obj, sampleRate, 3, grammar, null);
10 changes: 5 additions & 5 deletions src/make
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if [ ! -d "$OPENFST" ]; then
git clone --depth=1 https://github.com/alphacep/openfst /tmp/openfst &&
cd /tmp/openfst &&
autoreconf -is &&
CXXFLAGS="-r -O3 -fno-rtti -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -flto" emconfigure ./configure --prefix="$OPENFST" --enable-static --disable-shared --enable-lookahead-fsts --enable-ngram-fsts --disable-bin &&
CXXFLAGS="-r -O3 -fno-rtti -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -flto" emconfigure ./configure --prefix="$OPENFST" --enable-static --disable-shared --enable-lookahead-fsts --enable-ngram-fsts --disable-bin &&
emmake make -j"$JOBS" install &&
echo "PACKAGE_VERSION = 1.8.0" >> "$OPENFST"/Makefile
fi
Expand All @@ -58,7 +58,7 @@ if [ ! -d "$KALDI" ]; then
git clone -b vosk --depth=1 https://github.com/alphacep/kaldi "$KALDI" &&
cd "$KALDI"/src &&
git apply "$SRC"/Kaldi.patch &&
CXXFLAGS="-O3 -UHAVE_EXECINFO_H -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -Wno-unused-variable -Wno-unused-but-set-variable -g0" LDFLAGS="-lembind" emconfigure ./configure --use-cuda=no --with-cudadecoder=no --static --static-math=yes --static-fst=yes --debug-level=0 --fst-root="$OPENFST" --clapack-root="$CLAPACK_WASM" --host=WASM &&
CXXFLAGS="-O3 -UHAVE_EXECINFO_H -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -Wno-unused-variable -Wno-unused-but-set-variable -g0" LDFLAGS="-lembind" emconfigure ./configure --use-cuda=no --with-cudadecoder=no --static --static-math=yes --static-fst=yes --debug-level=0 --fst-root="$OPENFST" --clapack-root="$CLAPACK_WASM" --host=WASM &&
emmake make -j"$JOBS" online2 rnnlm
fi

Expand All @@ -67,19 +67,19 @@ if [ ! -d "$VOSK" ]; then
cd "$VOSK"/src &&
git apply "$SRC"/Vosk.patch &&
VOSK_FILES="recognizer.cc language_model.cc model.cc spk_model.cc vosk_api.cc" &&
em++ -O3 -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -Wno-deprecated -I. -I"$KALDI"/src -I"$OPENFST"/include $VOSK_FILES -c &&
em++ -O3 -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -Wno-deprecated -I. -I"$KALDI"/src -I"$OPENFST"/include $VOSK_FILES -c &&
emar -rcs vosk.a ${VOSK_FILES//.cc/.o}
fi

cd "$SRC" &&
em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O3 -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -fno-rtti -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sEXPORTED_RUNTIME_METHODS=UTF8ToString -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -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 -lembind -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js -o ../Vosklet.js
em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O3 -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -fno-rtti -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sEXPORTED_RUNTIME_METHODS=UTF8ToString -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -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 -lembind -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js -o ../Vosklet.js
cd .. &&
rm -f Vosklet.worker.js

cp Vosklet.js Examples/Vosklet.js &&
cp Vosklet.wasm Examples/Vosklet.wasm &&

# Can't serve files from raw.githubusercontent with Content-Encoding: gzip header so the browser won't decompress automatically. Manually decompressing instead.
sed -i 's/res.body/new Response(res.body.pipeThrough(new DecompressionStream("gzip"))).body/' Examples/Vosklet.js &&
sed -i 's/.pipeThrough(new CompressionStream("gzip"))//;s/teed\[1\]/teed[1].pipeThrough(new DecompressionStream("gzip"))/' Examples/Vosklet.js &&

rm -rf /tmp/openfst
Loading

0 comments on commit 14c913d

Please sign in to comment.