Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm detach fix #118

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binding/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Octopus library for web browsers (via WebAssembly)",
"author": "Picovoice Inc",
"license": "Apache-2.0",
"version": "1.2.14",
"version": "1.2.15",
"keywords": [
"octopus",
"web",
Expand Down
29 changes: 16 additions & 13 deletions binding/web/src/octopus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export class Octopus {

private _wasmMemory?: WebAssembly.Memory;
private _pvFree: pv_free_type;
private readonly _memoryBufferView: DataView;
private readonly _processMutex: Mutex;

private readonly _objectAddress: number;
Expand Down Expand Up @@ -110,7 +109,6 @@ export class Octopus {
this._octopusMatchAddressAddress = handleWasm.octopusMatchAddressAddress;
this._octopusMatchLengthAddress = handleWasm.octopusMatchLengthAddress;

this._memoryBufferView = new DataView(handleWasm.memory.buffer);
this._processMutex = new Mutex();

this._pvError = handleWasm.pvError;
Expand Down Expand Up @@ -246,13 +244,15 @@ export class Octopus {
`${msg}\nDetails: ${this._pvError.getErrorString()}`
);
}

const memoryBufferView = new DataView(this._wasmMemory.buffer);

const metadataAddress = this._memoryBufferView.getInt32(
const metadataAddress = memoryBufferView.getInt32(
this._metadataAddressAddress,
true
);

const metadataLength = this._memoryBufferView.getInt32(
const metadataLength = memoryBufferView.getInt32(
this._metadataLengthAddress,
true
);
Expand Down Expand Up @@ -303,15 +303,15 @@ export class Octopus {
throw new Error('malloc failed: Cannot allocate memory');
}

const memoryBufferUint8 = new Uint8Array(this._wasmMemory.buffer);
memoryBufferUint8.set(encoded, phraseAddress);
memoryBufferUint8[phraseAddress + encoded.length] = 0;

const metadataAddress = await this._alignedAlloc(
Uint8Array.BYTES_PER_ELEMENT,
octopusMetadata.buffer.length * Uint8Array.BYTES_PER_ELEMENT
);

let memoryBufferUint8 = new Uint8Array(this._wasmMemory.buffer);
memoryBufferUint8.set(encoded, phraseAddress);
memoryBufferUint8[phraseAddress + encoded.length] = 0;

memoryBufferUint8.set(octopusMetadata.buffer, metadataAddress);

const status = await this._pvOctopusSearch(
Expand All @@ -324,6 +324,7 @@ export class Octopus {
);
await this._pvFree(phraseAddress);
if (status !== PV_STATUS_SUCCESS) {
memoryBufferUint8 = new Uint8Array(this._wasmMemory.buffer);
const msg = `process failed with status ${arrayBufferToStringAtIndex(
memoryBufferUint8,
await this._pvStatusToString(status),
Expand All @@ -334,16 +335,18 @@ export class Octopus {
);
}

const memoryBufferView = new DataView(this._wasmMemory.buffer);

const matches: OctopusMatch[] = [];
const octopusMatchAddress = this._memoryBufferView.getInt32(this._octopusMatchAddressAddress, true);
const octopusMatchLength = this._memoryBufferView.getInt32(this._octopusMatchLengthAddress, true);
const octopusMatchAddress = memoryBufferView.getInt32(this._octopusMatchAddressAddress, true);
const octopusMatchLength = memoryBufferView.getInt32(this._octopusMatchLengthAddress, true);

for (let i = 0; i < octopusMatchLength; i++) {
const octopusMatch = octopusMatchAddress + i * (3 * Number(Float32Array.BYTES_PER_ELEMENT));

const startSec = this._memoryBufferView.getFloat32(octopusMatch, true);
const endSec = this._memoryBufferView.getFloat32(octopusMatch + Number(Float32Array.BYTES_PER_ELEMENT), true);
const probability = this._memoryBufferView.getFloat32(octopusMatch + 2 * Number(Float32Array.BYTES_PER_ELEMENT), true);
const startSec = memoryBufferView.getFloat32(octopusMatch, true);
const endSec = memoryBufferView.getFloat32(octopusMatch + Number(Float32Array.BYTES_PER_ELEMENT), true);
const probability = memoryBufferView.getFloat32(octopusMatch + 2 * Number(Float32Array.BYTES_PER_ELEMENT), true);

matches.push({
startSec,
Expand Down
2 changes: 1 addition & 1 deletion demo/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"author": "Picovoice Inc",
"license": "Apache-2.0",
"dependencies": {
"@picovoice/octopus-web": "~1.2.14"
"@picovoice/octopus-web": "~1.2.15"
},
"devDependencies": {
"http-server": "^14.0.0"
Expand Down
8 changes: 4 additions & 4 deletions demo/web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@picovoice/octopus-web@~1.2.14":
version "1.2.14"
resolved "https://registry.yarnpkg.com/@picovoice/octopus-web/-/octopus-web-1.2.14.tgz#fc7609bbe20fb0aee30038767fc0c93d278158d7"
integrity sha512-xT3hiNm//vIDmREqwaYZ42lPvOkBpGXXfScy3MxShA13TFXrmw5flJgHckf/6X06Xe71RTcS+GMK/y0U1uFTsg==
"@picovoice/octopus-web@~1.2.15":
version "1.2.15"
resolved "https://registry.yarnpkg.com/@picovoice/octopus-web/-/octopus-web-1.2.15.tgz#072e6bf6b860eb4fc16e619d1f6713ab1af4e4cc"
integrity sha512-MJ/Dxmu9kRBYM1loYO1TWKgoBzHqKdrH9AijMoa1RvKmWFKSrY2IkL2LEjjbJdPry67ViNWaTtHDyH7Dr1xkdg==
dependencies:
"@picovoice/web-utils" "=1.3.1"

Expand Down