Put id in url instead of response, add getModelCache

This commit is contained in:
msqr1
2024-10-07 14:37:57 -07:00
parent 182124e275
commit ed56baa1bb
5 changed files with 23 additions and 17 deletions

View File

@@ -12,7 +12,8 @@
| ```Promise<Recognizer> createRecognizer(model: Model, sampleRate: float)```<br><br>```Promise<Recognizer> createRecognizerWithSpkModel(model: Model, spkModel: spkModel, sampleRate: float)```<br><br>```Promise<Recognizer> createRecognizerWithGrm(model: Model, grammar: string, sampleRate: float)``` | Create a ```Recognizer``` |
| ```setLogLevel(lvl: int)``` | Set log level for Kaldi messages (default: ```0```: Info) <br>```-2```: Error<br>```-1```: Warning<br>```1```: Verbose<br>```2```: More verbose<br>```3```: Debug |
| ```Promise<AudioWorkletNode> createTransferer(ctx: AudioContext, bufferSize: int)``` | Create a node that transfer its inputs back to the main thread with custom buffer size (must be multiple of 128). Its port's ```onmessage``` handler can be set to get audio data. Has 1 input with 1 channel and no output. The the higher the size, the lesser the audio breaks up, but the higher the latency. Recomended value is around ```128 * 150```. |
| ```Promise<void> cleanUp()``` | A convenience function that call ```delete()``` on all objects and revoke all URLs. **Put this at the end of your code!** |
| ```Promise<void> cleanUp()``` | A convenience function that call ```delete()``` on all objects and revoke all URLs. **Run this when you're done!** |
| ```Promise<Cache> getModelCache()``` | Get ```Cache``` object that stores models. This allow for more granular read and writes to the model storage. |
| ```EpMode``` | Enum for endpointer modes | See Vosk's description |
## ```Model``` object

File diff suppressed because one or more lines are too long

View File

@@ -8,6 +8,7 @@ if(ENVIRONMENT_IS_WEB) {
// 'var' to expose this outside the if
var objs = [];
var events = ['status', 'partialResult', 'result'];
let _cache = caches.open('Vosklet');
let processorURL = URL.createObjectURL(new Blob(['(', (() => {
registerProcessor('VoskletTransferer', class extends AudioWorkletProcessor {
constructor(opts) {
@@ -50,16 +51,15 @@ class CommonModel extends EventTarget {
}, { once: true })
});
let cache = await caches.open('Vosklet');
let res = await cache.match(storepath);
let tar;
if(typeof res == 'undefined' || res.headers.get('id') != id) {
let req = (await cache.keys(storepath, { ignoreSearch: true }))[0]
let tar, res;
if (typeof req == 'undefined' || req.url.split('?')[1] != id) {
// Caching already handled explicitly
res = await fetch(url, { cache: 'no-store' });
if (!res.ok) throw 'Unable to fetch model, status: ' + res.status;
await cache.put(storepath, new Response(
res.clone().body, { headers: { 'id': id } }
));
await cache.put(storepath + '?' + id, res.clone());
}
else res = await cache.match(req)
tar = await new Response(res.body.pipeThrough(new DecompressionStream('gzip'))).arrayBuffer();
let tarStart = _malloc(tar.byteLength);
HEAPU8.set(new Uint8Array(tar), tarStart);
@@ -117,6 +117,8 @@ class Recognizer extends EventTarget {
}
}
Module = {
'getModelCache': () => _cache,
'cleanUp': async () => {
for(let obj of objs) await obj.delete();
URL.revokeObjectURL(processorURL);

File diff suppressed because one or more lines are too long

View File

@@ -8,6 +8,7 @@ if(ENVIRONMENT_IS_WEB) {
// 'var' to expose this outside the if
var objs = [];
var events = ['status', 'partialResult', 'result'];
let _cache = caches.open('Vosklet');
let processorURL = URL.createObjectURL(new Blob(['(', (() => {
registerProcessor('VoskletTransferer', class extends AudioWorkletProcessor {
constructor(opts) {
@@ -50,18 +51,18 @@ class CommonModel extends EventTarget {
}, { once: true })
});
let cache = await caches.open('Vosklet');
let res = await cache.match(storepath);
let tar;
if(typeof res == 'undefined' || res.headers.get('id') != id) {
let req = (await cache.keys(storepath, { ignoreSearch: true }))[0]
let tar, res;
if (typeof req == 'undefined' || req.url.split('?')[1] != id) {
// Caching already handled explicitly
res = await fetch(url, { cache: 'no-store' });
if (!res.ok) throw 'Unable to fetch model, status: ' + res.status;
await cache.put(storepath, new Response(
res.clone().body.pipeThrough(new CompressionStream('gzip')),
{ headers: { 'id': id } }
));
await cache.put(
storepath + '?' + id,
new Response(res.clone().body.pipeThrough(new CompressionStream('gzip')))
);
}
else res = await cache.match(req);
tar = await new Response(res.body.pipeThrough(new DecompressionStream('gzip'))).arrayBuffer();
let tarStart = _malloc(tar.byteLength);
HEAPU8.set(new Uint8Array(tar), tarStart);
@@ -119,6 +120,8 @@ class Recognizer extends EventTarget {
}
}
Module = {
'getModelCache': () => _cache,
'cleanUp': async () => {
for(let obj of objs) await obj.delete();
URL.revokeObjectURL(processorURL);