1
0
mirror of https://github.com/Picovoice/porcupine.git synced 2022-01-28 03:27:53 +03:00

use web-core package in web (+ ui frameworks) bindings (#624)

This commit is contained in:
Kwangsoo Yeo
2022-01-14 10:45:00 -08:00
committed by GitHub
parent 437707759b
commit 81a472b780
23 changed files with 2955 additions and 524 deletions

View File

@@ -19,6 +19,7 @@
"@angular/platform-browser": "~11.0.5",
"@angular/platform-browser-dynamic": "~11.0.5",
"@angular/router": "~11.0.5",
"@picovoice/porcupine-web-core": "^2.0.1",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"

View File

@@ -3,5 +3,6 @@
"dest": "../../dist/porcupine-web-angular",
"lib": {
"entryFile": "src/public-api.ts"
}
},
"allowedNonPeerDependencies": ["@picovoice/porcupine-web-core"]
}

View File

@@ -21,6 +21,7 @@
"author": "Picovoice Inc",
"license": "Apache-2.0",
"dependencies": {
"@picovoice/porcupine-web-core": "^2.0.1",
"tslib": "^2.0.0"
}
}

View File

@@ -1,12 +1,32 @@
/*
Copyright 2022 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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
import { Injectable, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { WebVoiceProcessor } from '@picovoice/web-voice-processor';
import type {
import {
PorcupineKeyword,
PorcupineWorkerFactory,
PorcupineServiceArgs,
PorcupineWorker,
} from './porcupine_types';
} from '@picovoice/porcupine-web-core';
export type PorcupineServiceArgs = {
/** Immediately start the microphone upon initialization? (defaults to true) */
start?: boolean;
/** AccessKey obtained from Picovoice Console (https://picovoice.ai/console/) */
accessKey: string;
/** Arguments forwarded to PorcupineWorkerFactory */
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string;
};
@Injectable({
providedIn: 'root',
@@ -69,7 +89,7 @@ export class PorcupineService implements OnDestroy {
);
} catch (error) {
this.isInit = false;
this.error$.next(error);
this.error$.next(error as Error);
this.isError$.next(true);
throw error;
}
@@ -84,7 +104,7 @@ export class PorcupineService implements OnDestroy {
this.porcupineWorker.postMessage({ command: 'release' });
this.porcupineWorker = null;
this.isInit = false;
this.error$.next(error);
this.error$.next(error as Error);
this.isError$.next(true);
throw error;
}
@@ -100,7 +120,7 @@ export class PorcupineService implements OnDestroy {
this.keyword$.next(label);
};
private errorCallback = (error: string): void => {
private errorCallback = (error: string | Error): void => {
this.error$.next(error);
this.isError$.next(true);
};

View File

@@ -1,105 +0,0 @@
export type PorcupineWorkerResponseReady = {
command: 'ppn-ready';
};
export type PorcupineWorkerResponseFailed = {
command: 'ppn-failed';
message: string;
};
export type PorcupineWorkerResponseError = {
command: 'ppn-error';
message: string;
};
export type PorcupineWorkerResponseFileOperation = {
command: 'file-save' | 'file-load' | 'file-exists' | 'file-delete';
path: string;
content?: string;
};
export type PorcupineWorkerResponseKeyword = {
command: 'ppn-keyword';
keywordLabel: string;
};
export type PorcupineWorkerResponse =
| PorcupineWorkerResponseReady
| PorcupineWorkerResponseFailed
| PorcupineWorkerResponseKeyword
| PorcupineWorkerResponseError
| PorcupineWorkerResponseFileOperation;
export interface PorcupineWorkerFactory {
create(
accessKey: string,
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string,
keywordDetectionCallback?: CallableFunction,
processErrorCallback?: CallableFunction,
start?: boolean
): Promise<PorcupineWorker>;
}
export type PorcupineKeywordCustom = {
base64: string;
custom: string;
sensitivity?: number;
};
export type PorcupineKeywordBuiltin = {
builtin: string;
sensitivity?: number;
};
export type PorcupineKeyword = PorcupineKeywordCustom | PorcupineKeywordBuiltin;
// Worker
export type PorcupineWorkerRequestProcess = {
command: 'process';
/** A frame of 16-bit 16kHz PCM audio */
inputFrame: Int16Array;
};
export type PorcupineWorkerRequestVoid = {
command: 'reset' | 'pause' | 'resume' | 'release';
};
export type PorcupineWorkerRequestInit = {
command: 'init';
accessKey: string;
keywords: Array<PorcupineKeyword | string>;
};
export type PorcupineWorkerRequestFileOperation = {
command:
| 'file-save-succeeded'
| 'file-save-failed'
| 'file-load-succeeded'
| 'file-load-failed'
| 'file-exists-succeeded'
| 'file-exists-failed'
| 'file-delete-succeeded'
| 'file-delete-failed';
message: string;
content?: string;
};
export type PorcupineWorkerRequest =
| PorcupineWorkerRequestInit
| PorcupineWorkerRequestProcess
| PorcupineWorkerRequestVoid
| PorcupineWorkerRequestFileOperation;
export interface PorcupineWorker extends Omit<Worker, 'postMessage'> {
postMessage(command: PorcupineWorkerRequest): void;
}
export type PorcupineServiceArgs = {
/** Immediately start the microphone upon initialization? (defaults to true) */
start?: boolean;
/** AccessKey obtained from Picovoice Console (https://picovoice.ai/console/) */
accessKey: string;
/** Arguments forwarded to PorcupineWorkerFactory */
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string;
};

View File

@@ -3,6 +3,6 @@
tslib@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"
integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==
version "2.3.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

View File

@@ -1223,6 +1223,11 @@
mkdirp "^1.0.4"
rimraf "^3.0.2"
"@picovoice/porcupine-web-core@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@picovoice/porcupine-web-core/-/porcupine-web-core-2.0.1.tgz#f4346c408a241c582000955fff081ac679f88696"
integrity sha512-ouj9MJHwqz/PNdACPwUxzkmExY/5Kep3a/H6zq5C17FVlWF+D2akevleFChoVd9wCLcKLtWRkF98PWq4HljPjw==
"@picovoice/web-voice-processor@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@picovoice/web-voice-processor/-/web-voice-processor-2.1.2.tgz#cac4456b3ef06dee5664e27be6db348ae4e2d46a"

View File

@@ -1,6 +1,6 @@
{
"name": "@picovoice/porcupine-web-react",
"version": "2.0.1",
"version": "2.0.2",
"description": "React component for Porcupine Web SDK",
"entry": "src/index.ts",
"module": "dist/esm/index.js",
@@ -33,6 +33,9 @@
"watch": "rollup --config --watch",
"format": "prettier --write \"**/*.{js,ts,json}\""
},
"dependencies": {
"@picovoice/porcupine-web-core": "^2.0.1"
},
"devDependencies": {
"@babel/core": "^7.12.13",
"@babel/plugin-transform-runtime": "^7.12.15",

View File

@@ -1,18 +1,6 @@
import { usePorcupine } from './usePorcupine';
import {
PorcupineHookArgs,
PorcupineKeyword,
PorcupineKeywordBuiltin,
PorcupineKeywordCustom,
PorcupineWorkerFactory,
} from './porcupine_types';
import { usePorcupine, PorcupineHookArgs } from './use_porcupine';
export {
usePorcupine,
PorcupineHookArgs,
PorcupineKeyword,
PorcupineKeywordBuiltin,
PorcupineKeywordCustom,
PorcupineWorkerFactory,
PorcupineHookArgs
};

View File

@@ -1,138 +0,0 @@
/*
Copyright 2021 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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
export type PorcupineKeywordCustom = {
/** Base64 representation of a trained Porcupine keyword (`.ppn` file) */
base64: string;
/** An arbitrary label that you want Picovoice to report when the detection occurs */
custom: string;
/** Value in range [0,1] that trades off miss rate for false alarm */
sensitivity?: number;
};
export type PorcupineKeywordBuiltin = {
/** Name of a builtin keyword for the specific language (e.g. "Grasshopper" for English, or "Ananas" for German) */
builtin: string;
/** Value in range [0,1] that trades off miss rate for false alarm */
sensitivity?: number;
};
export type PorcupineKeyword = PorcupineKeywordCustom | PorcupineKeywordBuiltin;
export interface PorcupineEngine {
/** Release all resources acquired by Porcupine */
release(): Promise<void>;
/** Process a single frame of 16-bit 16kHz PCM audio */
process(frame: Int16Array): Promise<number>;
/** The version of the Porcupine engine */
readonly version: string;
/** The sampling rate of audio expected by the Porcupine engine */
readonly sampleRate: number;
/** The frame length of audio expected by the Porcupine engine */
readonly frameLength: number;
/** Maps the keyword detection index (e.g. 0, 1) returned by Porcupine to the label (e.g. "Hey Pico", "Grasshopper") */
readonly keywordLabels: Map<number, string>;
}
// Worker
export type PorcupineWorkerRequestProcess = {
command: 'process';
/** A frame of 16-bit 16kHz PCM audio */
inputFrame: Int16Array;
};
export type PorcupineWorkerRequestVoid = {
command: 'reset' | 'pause' | 'resume' | 'release';
};
export type PorcupineWorkerRequestInit = {
command: 'init';
accessKey: string;
keywords: Array<PorcupineKeyword | string>;
};
export type PorcupineWorkerRequestFileOperation = {
command:
| 'file-save-succeeded'
| 'file-save-failed'
| 'file-load-succeeded'
| 'file-load-failed'
| 'file-exists-succeeded'
| 'file-exists-failed'
| 'file-delete-succeeded'
| 'file-delete-failed';
message: string;
content?: string;
};
export type PorcupineWorkerRequest =
| PorcupineWorkerRequestInit
| PorcupineWorkerRequestProcess
| PorcupineWorkerRequestVoid
| PorcupineWorkerRequestFileOperation;
export type PorcupineWorkerResponseReady = {
command: 'ppn-ready';
};
export type PorcupineWorkerResponseFailed = {
command: 'ppn-failed';
message: string;
};
export type PorcupineWorkerResponseError = {
command: 'ppn-error';
message: string;
};
export type PorcupineWorkerResponseFileOperation = {
command: 'file-save' | 'file-load' | 'file-exists' | 'file-delete';
path: string;
content?: string;
};
export type PorcupineWorkerResponseKeyword = {
command: 'ppn-keyword';
keywordLabel: string;
};
export type PorcupineWorkerResponse =
| PorcupineWorkerResponseReady
| PorcupineWorkerResponseFailed
| PorcupineWorkerResponseKeyword
| PorcupineWorkerResponseError
| PorcupineWorkerResponseFileOperation;
export interface PorcupineWorker extends Omit<Worker, 'postMessage'> {
postMessage(command: PorcupineWorkerRequest): void;
}
export interface PorcupineWorkerFactory {
create: (
accessKey: string,
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string,
keywordDetectionCallback?: CallableFunction,
processErrorCallback?: CallableFunction,
start?: boolean
) => Promise<PorcupineWorker>;
}
// React
export type PorcupineHookArgs = {
/** Immediately start the microphone upon initialization? */
start: boolean;
/** AccessKey obtained from Picovoice Console (https://picovoice.ai/console/) */
accessKey: string;
/** Keywords to listen for */
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string;
};

View File

@@ -1,12 +1,12 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2022 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.
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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
import { useState, useEffect } from 'react';
@@ -14,10 +14,19 @@ import { useState, useEffect } from 'react';
import { WebVoiceProcessor } from '@picovoice/web-voice-processor';
import {
PorcupineHookArgs,
PorcupineKeyword,
PorcupineWorker,
PorcupineWorkerFactory,
} from './porcupine_types';
} from '@picovoice/porcupine-web-core';
export type PorcupineHookArgs = {
/** Immediately start the microphone upon initialization? */
start: boolean;
/** AccessKey obtained from Picovoice Console (https://picovoice.ai/console/) */
accessKey: string;
/** Keywords to listen for */
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string;
};
export function usePorcupine(
/** The language-specific worker factory, imported as { PorcupineWorkerFactory }
@@ -77,7 +86,7 @@ export function usePorcupine(
return false;
};
const processErrorCallback = (error: string): void => {
const processErrorCallback = (error: string | Error): void => {
setIsError(true);
setErrorMessage(error.toString());
};

View File

@@ -886,6 +886,11 @@
"@nodelib/fs.scandir" "2.1.4"
fastq "^1.6.0"
"@picovoice/porcupine-web-core@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@picovoice/porcupine-web-core/-/porcupine-web-core-2.0.1.tgz#f4346c408a241c582000955fff081ac679f88696"
integrity sha512-ouj9MJHwqz/PNdACPwUxzkmExY/5Kep3a/H6zq5C17FVlWF+D2akevleFChoVd9wCLcKLtWRkF98PWq4HljPjw==
"@picovoice/web-voice-processor@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@picovoice/web-voice-processor/-/web-voice-processor-2.1.2.tgz#cac4456b3ef06dee5664e27be6db348ae4e2d46a"

View File

@@ -1,6 +1,6 @@
{
"name": "@picovoice/porcupine-web-vue",
"version": "2.0.1",
"version": "2.0.2",
"description": "Vue mixin for Porcupine Web SDK",
"author": "Picovoice Inc",
"entry": "src/index.ts",
@@ -17,6 +17,9 @@
"start": "cross-env TARGET='debug' rollup --config --watch",
"watch": "rollup --config --watch"
},
"dependencies": {
"@picovoice/porcupine-web-core": "^2.0.1"
},
"devDependencies": {
"@babel/core": "^7.12.13",
"@babel/plugin-transform-runtime": "^7.12.15",

View File

@@ -1,13 +1,4 @@
import porcupineMixin from './porcupine';
import {
PorcupineKeyword,
PorcupineKeywordCustom,
PorcupineKeywordBuiltin,
PorcupineWorkerFactoryArgs,
PorcupineWorkerFactory,
PorcupineVue
} from './porcupine_types';
import porcupineMixin, { PorcupineVue, PorcupineWorkerFactoryArgs } from './porcupine';
// Create module definition for Vue.use()
const plugin = {
@@ -34,11 +25,4 @@ if (GlobalVue) {
export default porcupineMixin;
// export types
export {
PorcupineKeyword,
PorcupineKeywordCustom,
PorcupineKeywordBuiltin,
PorcupineWorkerFactoryArgs,
PorcupineWorkerFactory,
PorcupineVue
};
export { PorcupineVue, PorcupineWorkerFactoryArgs };

View File

@@ -1,5 +1,48 @@
/*
Copyright 2022 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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
import { WebVoiceProcessor } from '@picovoice/web-voice-processor';
import { PorcupineVue } from './porcupine_types';
import {
PorcupineKeyword,
PorcupineWorker,
PorcupineWorkerFactory
} from "@picovoice/porcupine-web-core";
/**
* Type alias for PorcupineWorkerFactory arguments.
*/
export type PorcupineWorkerFactoryArgs = {
accessKey: string;
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string;
start?: boolean;
};
/**
* Type alias for Porcupine Vue Mixin.
* Use with `Vue as VueConstructor extends {$porcupine: PorcupineVue}` to get types in typescript.
*/
export interface PorcupineVue {
$_ppnWorker_: Worker | null;
$_webVp_: WebVoiceProcessor | null;
init: (
porcupineFactoryArgs: PorcupineWorkerFactoryArgs,
porcupineFactory: PorcupineWorkerFactory,
keywordCallback: (label: string) => void,
readyCallback: () => void,
errorCallback: (error: string | Error) => void) => void;
start: () => boolean;
pause: () => boolean;
delete: () => void;
}
export default {
computed: {
@@ -8,7 +51,7 @@ export default {
*/
$porcupine(): PorcupineVue {
return {
$_ppnWorker_: null as Worker | null,
$_ppnWorker_: null as PorcupineWorker | null,
$_webVp_: null as WebVoiceProcessor | null,
/**
* Init function for Porcupine.
@@ -24,7 +67,7 @@ export default {
porcupineFactory,
keywordCallback = (_: string) => {},
readyCallback = () => {},
errorCallback = (error: Error) => {console.error(error)}
errorCallback = (error: string | Error) => {console.error(error)}
) {
try {
const { accessKey, keywords } = porcupineFactoryArgs;

View File

@@ -1,64 +0,0 @@
import { WebVoiceProcessor } from '@picovoice/web-voice-processor';
/**
* Type alias for the Porcupine keywords.
*/
export type PorcupineKeyword = PorcupineKeywordCustom | PorcupineKeywordBuiltin;
/**
* Type alias for builtin keywords.
*/
export type PorcupineKeywordBuiltin = {
builtin: string;
sensitivity?: number;
};
/**
* Type alias for custom keywords.
*/
export type PorcupineKeywordCustom = {
base64: string;
custom: string;
sensitivity?: number;
};
/**
* Type alias for PorcupineWorkerFactory arguments.
*/
export type PorcupineWorkerFactoryArgs = {
accessKey: string;
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string;
start?: boolean;
};
/**
* The language-specific worker factory, imported as { PorcupineWorkerFactory } from the
* @picovoice/porcupine-web-xx-worker series of packages, where xx is the two-letter language code.
*/
export interface PorcupineWorkerFactory extends Object {
create: (
accessKey: string,
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string,
keywordDetectionCallback?: CallableFunction,
processErrorCallback?: CallableFunction,
start?: boolean) => Promise<Worker>,
};
/**
* Type alias for Porcupine Vue Mixin.
* Use with `Vue as VueConstructor extends {$porcupine: PorcupineVue}` to get types in typescript.
*/
export interface PorcupineVue {
$_ppnWorker_: Worker | null;
$_webVp_: WebVoiceProcessor | null;
init: (
porcupineFactoryArgs: PorcupineWorkerFactoryArgs,
porcupineFactory: PorcupineWorkerFactory,
keywordCallback: (label: string) => void,
readyCallback: () => void,
errorCallback: (error: Error) => void) => void;
start: () => boolean;
pause: () => boolean;
delete: () => void;
}

View File

@@ -924,6 +924,11 @@
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==
"@picovoice/porcupine-web-core@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@picovoice/porcupine-web-core/-/porcupine-web-core-2.0.1.tgz#f4346c408a241c582000955fff081ac679f88696"
integrity sha512-ouj9MJHwqz/PNdACPwUxzkmExY/5Kep3a/H6zq5C17FVlWF+D2akevleFChoVd9wCLcKLtWRkF98PWq4HljPjw==
"@picovoice/web-voice-processor@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@picovoice/web-voice-processor/-/web-voice-processor-2.1.2.tgz#cac4456b3ef06dee5664e27be6db348ae4e2d46a"

View File

@@ -1,6 +1,6 @@
{
"name": "$",
"version": "2.0.3",
"version": "2.0.4",
"description": "Porcupine library for web browsers (via WebAssembly)",
"author": "Picovoice Inc",
"license": "Apache-2.0",
@@ -34,6 +34,9 @@
"format": "prettier --write \"**/*.{js,ts,json}\"",
"test": "jest"
},
"dependencies": {
"@picovoice/porcupine-web-core": "^2.0.1"
},
"devDependencies": {
"@babel/core": "^7.12.13",
"@babel/plugin-transform-runtime": "^7.12.15",

View File

@@ -1,12 +1,12 @@
/*
Copyright 2018-2021 Picovoice Inc.
Copyright 2018-2022 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.
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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
/* eslint camelcase: 0 */
@@ -15,7 +15,7 @@
import * as Asyncify from 'asyncify-wasm';
import { Mutex } from 'async-mutex';
import { PorcupineKeyword, PorcupineEngine } from './porcupine_types';
import { PorcupineKeyword, PorcupineEngine } from '@picovoice/porcupine-web-core';
// @ts-ignore
import { PORCUPINE_WASM_BASE64 } from './lang/porcupine_b64';
@@ -39,6 +39,26 @@ import {
const DEFAULT_SENSITIVITY = 0.5;
const PV_STATUS_SUCCESS = 10000;
/**
* WebAssembly function types
*/
type aligned_alloc_type = (alignment: number, size: number) => Promise<number>;
type pv_porcupine_init_type = (
accessKey: number,
numKeywords: number,
keywordModelSizes: number,
keywordModels: number,
sensitivies: number,
object: number
) => Promise<number>;
type pv_porcupine_process_type = (object: number, buffer: number, keywordIndex: number) => Promise<number>;
type pv_porcupine_delete_type = (object: number) => Promise<void>;
type pv_status_to_string_type = (status: number) => Promise<number>
type pv_sample_rate_type = () => Promise<number>;
type pv_porcupine_frame_length_type = () => Promise<number>;
type pv_porcupine_version_type = () => Promise<number>;
/**
* JavaScript/WebAssembly Binding for the Picovoice Porcupine wake word engine.
*
@@ -54,18 +74,18 @@ type PorcupineWasmOutput = {
inputBufferAddress: number;
memory: WebAssembly.Memory;
objectAddress: number;
pvPorcupineDelete: CallableFunction;
pvPorcupineProcess: CallableFunction;
pvStatusToString: CallableFunction;
pvPorcupineDelete: pv_porcupine_delete_type;
pvPorcupineProcess: pv_porcupine_process_type;
pvStatusToString: pv_status_to_string_type;
sampleRate: number;
version: string;
keywordIndexAddress: number;
};
export class Porcupine implements PorcupineEngine {
private _pvPorcupineDelete: CallableFunction;
private _pvPorcupineProcess: CallableFunction;
private _pvStatusToString: CallableFunction;
private _pvPorcupineDelete: pv_porcupine_delete_type;
private _pvPorcupineProcess: pv_porcupine_process_type;
private _pvStatusToString: pv_status_to_string_type;
private _wasmMemory: WebAssembly.Memory;
private _memoryBuffer: Int16Array;
@@ -604,19 +624,19 @@ export class Porcupine implements PorcupineEngine {
importObject
);
const aligned_alloc = instance.exports.aligned_alloc as CallableFunction;
const aligned_alloc = instance.exports.aligned_alloc as aligned_alloc_type;
const pv_porcupine_version = instance.exports
.pv_porcupine_version as CallableFunction;
.pv_porcupine_version as pv_porcupine_version_type;
const pv_porcupine_frame_length = instance.exports
.pv_porcupine_frame_length as CallableFunction;
.pv_porcupine_frame_length as pv_porcupine_frame_length_type;
const pv_porcupine_process = instance.exports
.pv_porcupine_process as CallableFunction;
.pv_porcupine_process as pv_porcupine_process_type;
const pv_porcupine_delete = instance.exports
.pv_porcupine_delete as CallableFunction;
const pv_porcupine_init = instance.exports.pv_porcupine_init as CallableFunction;
.pv_porcupine_delete as pv_porcupine_delete_type;
const pv_porcupine_init = instance.exports.pv_porcupine_init as pv_porcupine_init_type;
const pv_status_to_string = instance.exports
.pv_status_to_string as CallableFunction;
const pv_sample_rate = instance.exports.pv_sample_rate as CallableFunction;
.pv_status_to_string as pv_status_to_string_type;
const pv_sample_rate = instance.exports.pv_sample_rate as pv_sample_rate_type;
const keywordIndexAddress = await aligned_alloc(
Int32Array.BYTES_PER_ELEMENT,
Int32Array.BYTES_PER_ELEMENT

View File

@@ -1,89 +0,0 @@
/*
Copyright 2018-2021 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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
export type PorcupineKeywordCustom = {
/** Base64 representation of a trained Porcupine keyword (`.ppn` file) */
base64: string;
/** An arbitrary label that you want Picovoice to report when the detection occurs */
custom: string;
/** Value in range [0,1] that trades off miss rate for false alarm */
sensitivity?: number;
};
export type PorcupineKeywordBuiltin = {
/** Name of a builtin keyword for the specific language (e.g. "Grasshopper" for English, or "Ananas" for German) */
builtin: string;
/** Value in range [0,1] that trades off miss rate for false alarm */
sensitivity?: number;
};
export type PorcupineKeyword = PorcupineKeywordCustom | PorcupineKeywordBuiltin;
export type PorcupineWorkerRequestProcess = {
command: 'process';
inputFrame: Int16Array;
};
export type PorcupineWorkerRequestVoid = {
command: 'reset' | 'pause' | 'resume' | 'release';
};
export type PorcupineWorkerRequestInit = {
command: 'init';
accessKey: string;
keywords: Array<PorcupineKeyword | string>;
start?: boolean;
};
export type PorcupineWorkerRequest =
| PorcupineWorkerRequestInit
| PorcupineWorkerRequestProcess
| PorcupineWorkerRequestVoid;
export type PorcupineWorkerResponseReady = {
command: 'ppn-ready';
};
export type PorcupineWorkerResponseFailed = {
command: 'ppn-failed';
message: string;
};
export type PorcupineWorkerResponseKeyword = {
command: 'ppn-keyword';
keywordLabel: string;
};
export type PorcupineWorkerResponseError = {
command: 'ppn-error';
message: string;
};
export type PorcupineWorkerResponse =
| PorcupineWorkerResponseReady
| PorcupineWorkerResponseFailed
| PorcupineWorkerResponseKeyword
| PorcupineWorkerResponseError;
export interface PorcupineEngine {
/** Release all resources acquired by Porcupine */
release(): Promise<void>;
/** Process a single frame of 16-bit 16kHz PCM audio */
process(frame: Int16Array): Promise<number>;
/** The version of the Porcupine engine */
readonly version: string;
/** The sampling rate of audio expected by the Porcupine engine */
readonly sampleRate: number;
/** The frame length of audio expected by the Porcupine engine */
readonly frameLength: number;
/** Maps the keyword detection index (e.g. 0, 1) returned by Porcupine to the label (e.g. "Hey Pico", "Grasshopper") */
readonly keywordLabels: Map<number, string>;
}

View File

@@ -1,12 +1,12 @@
/*
Copyright 2018-2021 Picovoice Inc.
Copyright 2018-2022 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.
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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
import {
@@ -17,7 +17,7 @@ import {
PorcupineWorkerResponseKeyword,
PorcupineWorkerResponseError,
PorcupineWorkerRequest,
} from './porcupine_types';
} from '@picovoice/porcupine-web-core';
// @ts-ignore
import Porcupine from './porcupine';

View File

@@ -1,12 +1,12 @@
/*
Copyright 2018-2021 Picovoice Inc.
Copyright 2018-2022 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.
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.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
import PorcupineWorker from 'web-worker:./porcupine_worker.ts';
@@ -14,7 +14,7 @@ import {
PorcupineKeyword,
PorcupineWorkerRequestInit,
PorcupineWorkerResponse,
} from './porcupine_types';
} from '@picovoice/porcupine-web-core';
export default class PorcupineWorkerFactory {
private constructor() { }
@@ -33,8 +33,8 @@ export default class PorcupineWorkerFactory {
public static async create(
accessKey: string,
keywords: Array<PorcupineKeyword | string> | PorcupineKeyword | string,
keywordDetectionCallback?: CallableFunction,
processErrorCallback?: CallableFunction,
keywordDetectionCallback?: (label: string) => Promise<void>,
processErrorCallback?: (error: string) => Promise<void>,
start?: boolean
): Promise<Worker> {
// n.b. The *worker* creation is itself synchronous. But, inside the worker is an async

File diff suppressed because it is too large Load Diff