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

v3.0 web #680

Merged
merged 18 commits into from
Oct 23, 2023
8 changes: 8 additions & 0 deletions .github/workflows/web-codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ jobs:
with:
node-version: lts/*

- name: Local Porcupine dependency
run: yarn && yarn copywasm && yarn copyppn && yarn build
working-directory: resources/porcupine/binding/web

- name: Local Rhino dependency
run: yarn && yarn copywasm && yarn build
working-directory: resources/rhino/binding/web

- name: Pre-build dependencies
run: npm install yarn

Expand Down
28 changes: 27 additions & 1 deletion .github/workflows/web-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand All @@ -36,6 +36,18 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Local Porcupine dependency
run: yarn && yarn copywasm && yarn copyppn && yarn build
working-directory: resources/porcupine/binding/web

- name: Local Rhino dependency
run: yarn && yarn copywasm && yarn build
working-directory: resources/rhino/binding/web

- name: Local dependency
run: yarn && yarn build
working-directory: sdk/web

- name: Pre-build dependencies
run: npm install yarn

Expand All @@ -50,12 +62,26 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Set up Node.js lts/*
uses: actions/setup-node@v3
with:
node-version: lts/*

- name: Local Porcupine dependency
run: yarn && yarn copywasm && yarn copyppn && yarn build
working-directory: resources/porcupine/binding/web

- name: Local Rhino dependency
run: yarn && yarn copywasm && yarn build
working-directory: resources/rhino/binding/web

- name: Local dependency
run: yarn && yarn build
working-directory: sdk/nodejs

- name: Pre-build dependencies
run: npm install yarn

Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand All @@ -45,6 +45,14 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Local Porcupine dependency
run: yarn && yarn copywasm && yarn copyppn && yarn build
working-directory: resources/porcupine/binding/web

- name: Local Rhino dependency
run: yarn && yarn copywasm && yarn build
working-directory: resources/rhino/binding/web

- name: Pre-build dependencies
run: npm install yarn

Expand Down
4 changes: 2 additions & 2 deletions demo/web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "picovoice-web-demo",
"private": true,
"version": "2.2.0",
"version": "3.0.0",
"description": "A basic demo to show how to use Picovoice for web browsers, using the IIFE version of the library",
"main": "index.js",
"scripts": {
Expand All @@ -26,7 +26,7 @@
"author": "Picovoice Inc",
"license": "Apache-2.0",
"dependencies": {
"@picovoice/picovoice-web": "~2.2.1",
"@picovoice/picovoice-web": "file:../../sdk/web",
"@picovoice/web-voice-processor": "~4.0.8"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/web/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ module.exports = {
// disallow shadowing of names such as arguments
'no-shadow-restricted-names': 2,
// disallow declaration of variables already declared in the outer scope
'no-shadow': 2,
'no-shadow': 0,
// disallow use of undefined when initializing variables
'no-undef-init': 0,
// disallow use of undeclared variables unless mentioned in a /*global */ block
Expand Down
8 changes: 4 additions & 4 deletions sdk/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Picovoice SDK for web browsers (via WebAssembly)",
"author": "Picovoice Inc",
"license": "Apache-2.0",
"version": "2.2.1",
"version": "3.0.0",
"keywords": [
"porcupine",
"rhino",
Expand Down Expand Up @@ -44,8 +44,8 @@
"test": "cypress run --spec test/picovoice.test.ts"
},
"dependencies": {
"@picovoice/porcupine-web": "=2.2.1",
"@picovoice/rhino-web": "=2.2.1",
"@picovoice/porcupine-web": "file:../../resources/porcupine/binding/web",
"@picovoice/rhino-web": "file:../../resources/rhino/binding/web",
"@picovoice/web-utils": "=1.3.1"
},
"devDependencies": {
Expand Down Expand Up @@ -74,6 +74,6 @@
"typescript": "^4.9.5"
},
"engines": {
"node": ">=14"
"node": ">=16"
}
}
3 changes: 3 additions & 0 deletions sdk/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
PicovoiceWorkerResponse,
} from './types';

import * as PicovoiceErrors from './picovoice_errors';

export {
BuiltInKeyword,
DetectionCallback,
Expand All @@ -53,6 +55,7 @@ export {
PicovoiceWorkerReleaseResponse,
PicovoiceWorkerRequest,
PicovoiceWorkerResponse,
PicovoiceErrors,
PorcupineDetection,
PorcupineKeyword,
PorcupineModel,
Expand Down
79 changes: 51 additions & 28 deletions sdk/web/src/picovoice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.

You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
file accompanying this source.
Expand All @@ -17,6 +17,7 @@ import {
PorcupineDetection,
PorcupineKeyword,
PorcupineModel,
PorcupineErrors,
} from '@picovoice/porcupine-web';

import {
Expand All @@ -25,16 +26,20 @@ import {
RhinoContext,
RhinoInference,
RhinoModel,
RhinoErrors,
} from '@picovoice/rhino-web';

import { loadPicovoiceArgs } from './utils';

import * as PicovoiceErrors from './picovoice_errors';
import { mapToPicovoiceError } from './picovoice_errors';

export class Picovoice {
private _porcupine: Porcupine | null = null;
private _rhino: Rhino | null = null;
private _isWakeWordDetected: boolean = false;

private readonly _version: string = '2.1.0';
private readonly _version: string = '3.0.0';

/**
* Get Picovoice SDK version.
Expand Down Expand Up @@ -148,30 +153,40 @@ export class Picovoice {
picovoice._isWakeWordDetected = true;
wakeWordCallback(detection);
};
picovoice._porcupine = await Porcupine._init(
accessKey,
[keywordPath],
[keywordLabel],
porcupineCallback,
new Float32Array([porcupineSensitivity]),
porcupineModelPath,
{ processErrorCallback }
);

const rhinoCallback = (inference: RhinoInference): void => {
if (inference.isFinalized) {
picovoice._isWakeWordDetected = false;
inferenceCallback(inference);
}
};
picovoice._rhino = await Rhino._init(
accessKey,
contextPath,
rhinoSensitivity,
rhinoCallback,
rhinoModelPath,
{ processErrorCallback, endpointDurationSec, requireEndpoint }
);

const errorCallback = (!processErrorCallback) ? undefined : (error: PorcupineErrors.PorcupineError | RhinoErrors.RhinoError): void => {
processErrorCallback(mapToPicovoiceError(error));
};

try {
picovoice._porcupine = await Porcupine._init(
accessKey,
[keywordPath],
[keywordLabel],
porcupineCallback,
new Float32Array([porcupineSensitivity]),
porcupineModelPath,
{ processErrorCallback: errorCallback }
);

picovoice._rhino = await Rhino._init(
accessKey,
contextPath,
rhinoSensitivity,
rhinoCallback,
rhinoModelPath,
{ processErrorCallback: errorCallback, endpointDurationSec, requireEndpoint }
);
} catch (error: any) {
throw mapToPicovoiceError(error);
}

return picovoice;
}
Expand All @@ -187,15 +202,19 @@ export class Picovoice {
*/
public async process(pcm: Int16Array): Promise<void> {
if (this._porcupine === null || this._rhino === null) {
throw Error(
throw new PicovoiceErrors.PicovoiceInvalidStateError(
'Picovoice has been released. You cannot call process after release.'
);
}

if (!this._isWakeWordDetected) {
await this._porcupine.process(pcm);
} else {
await this._rhino.process(pcm);
try {
if (!this._isWakeWordDetected) {
await this._porcupine.process(pcm);
} else {
await this._rhino.process(pcm);
}
} catch (error: any) {
mapToPicovoiceError(error);
}
}

Expand All @@ -204,14 +223,18 @@ export class Picovoice {
*/
public async reset(): Promise<void> {
if (this._porcupine === null || this._rhino === null) {
throw Error(
throw new PicovoiceErrors.PicovoiceInvalidStateError(
'Picovoice has been released. You cannot call reset after release.'
);
}

if (this._isWakeWordDetected) {
this._isWakeWordDetected = false;
await this._rhino.reset();
try {
if (this._isWakeWordDetected) {
this._isWakeWordDetected = false;
await this._rhino.reset();
}
} catch (error: any) {
mapToPicovoiceError(error);
}
}

Expand Down
Loading