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

Nodejs sdk #206

Merged
merged 31 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 2 additions & 7 deletions .github/workflows/nodejs-perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ on:
branches: [ main ]
paths:
- 'binding/nodejs/perf.test.js'
- 'lib/common/**'
- 'lib/node/**'
- '.github/workflows/nodejs-perf.yml'

pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- 'binding/nodejs/perf.test.js'
- 'lib/common/**'
- 'lib/node/**'
- '.github/workflows/nodejs-perf.yml'

Expand All @@ -33,15 +31,12 @@ jobs:
include:
- os: ubuntu-latest
num_test_iterations: 50
init_performance_threshold_sec: 0.001
proc_performance_threshold_sec: 0.005
- os: windows-latest
num_test_iterations: 50
init_performance_threshold_sec: 0.5
proc_performance_threshold_sec: 0.005
- os: macos-latest
num_test_iterations: 50
init_performance_threshold_sec: 0.005
proc_performance_threshold_sec: 0.005

steps:
Expand All @@ -56,7 +51,7 @@ jobs:
run: yarn install

- name: Test
run: yarn test perf.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}} --num_test_iterations=${{matrix.num_test_iterations}} --init_performance_threshold_sec=${{matrix.init_performance_threshold_sec}} --proc_performance_threshold_sec=${{matrix.proc_performance_threshold_sec}}
run: yarn test perf.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}} --num_test_iterations=${{matrix.num_test_iterations}} --proc_performance_threshold_sec=${{matrix.proc_performance_threshold_sec}}

perf-self-hosted:
runs-on: ${{ matrix.machine }}
Expand Down Expand Up @@ -97,7 +92,7 @@ jobs:
run: bash machine-state.sh

- name: Test
run: yarn test perf.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}} --num_test_iterations=${{matrix.num_test_iterations}} --init_performance_threshold_sec=0.01 --proc_performance_threshold_sec=0.05
run: yarn test perf.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}} --num_test_iterations=${{matrix.num_test_iterations}} --proc_performance_threshold_sec=0.05

- name: Machine state after
working-directory: res/scripts
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ Create instances of the Cobra class:
const { Cobra } = require("@picovoice/cobra-node");

const accessKey = "${ACCESS_KEY}"; // Obtained from the Picovoice Console (https://console.picovoice.ai/)
const handle = new Cobra(accessKey);
const cobra = new Cobra(accessKey);
```

When instantiated, `handle` can process audio via its `.process` method.
When instantiated, `cobra` can process audio via its `.process` method.

```javascript
function getNextAudioFrame() {
Expand All @@ -360,15 +360,15 @@ function getNextAudioFrame() {

while (true) {
const audioFrame = getNextAudioFrame();
const voiceProbability = handle.process(audioFrame);
const voiceProbability = cobra.process(audioFrame);
console.log(voiceProbability);
}
```

When done be sure to release resources using `release()`:

```javascript
handle.release();
cobra.release();
```

### Rust
Expand Down
20 changes: 11 additions & 9 deletions binding/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,36 @@ Signup or Login to [Picovoice Console](https://console.picovoice.ai/) to get you

Create an instance of the engine and detect voice activity in the audio:

Replace `${AccessKey}` with your AccessKey obtained from [Picovoice Console](https://console.picovoice.ai/). `handle` is
Replace `${AccessKey}` with your AccessKey obtained from [Picovoice Console](https://console.picovoice.ai/). `cobra` is
an instance of Cobra.

When initialized, the valid sample rate is given by `handle.sampleRate`. Expected frame length (number of audio samples
in an input array) is `handle.frameLength`. The engine accepts 16-bit linearly-encoded PCM and operates on
single-channel audio.

```javascript
const { Cobra } = require("@picovoice/cobra-node");

const accessKey = "${ACCESS_KEY}"; // Obtained from the Picovoice Console (https://console.picovoice.ai/)
const handle = new Cobra(accessKey);
const cobra = new Cobra(accessKey);
```

Once initialized, the valid sample rate is given by `cobra.sampleRate`. The expected frame length (number of audio samples
in an input array) is `cobra.frameLength`. The engine accepts 16-bit linearly-encoded PCM and operates on
single-channel audio.

```javascript
function getNextAudioFrame() {
// ...
return audioFrame;
}

while (true) {
const audioFrame = getNextAudioFrame();
const voiceProbability = handle.process(audioFrame);
const voiceProbability = cobra.process(audioFrame);
console.log(voiceProbability);
}
```

Finally, when done be sure to explicitly release the resources using
`handle.release()`.
`cobra.release()`.

## Demos

[Cobra Node.js demo package](../../demo/nodejs) provides command-line utilities for processing audio using cobra.
The [Cobra Node.js demo package](../../demo/nodejs) provides command-line utilities for processing audio using Cobra.
26 changes: 0 additions & 26 deletions binding/nodejs/copy.js

This file was deleted.

1 change: 0 additions & 1 deletion binding/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"build:all": "tsc",
"build:types": "tsc --declaration --declarationMap --emitDeclarationOnly --outDir ./dist/types",
"prepack": "npm run build",
"prepare": "node copy.js",
"test": "jest --no-cache",
"lint": "eslint . --ext .js,.ts"
},
Expand Down
10 changes: 5 additions & 5 deletions binding/nodejs/src/cobra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class Cobra {
*
* @param {Int16Array} pcm Audio data. The audio needs to have a sample rate equal to `Cobra.sampleRate` and be 16-bit linearly-encoded.
* The specific array length can be attained by calling `Cobra.frameLength`. This function operates on single-channel audio.
* @returns {number} Voice probability result.
* @returns {number} Probability of voice activity. It is a floating-point number within [0, 1].
*/
process(pcm: Int16Array): number {
assert(pcm instanceof Int16Array);
Expand All @@ -142,19 +142,19 @@ export default class Cobra {
);
}

let VoiceProbabilityAndStatus: VoiceProbabilityAndStatus | null = null;
let voiceProbabilityAndStatus: VoiceProbabilityAndStatus | null = null;
try {
VoiceProbabilityAndStatus = this._pvCobra.process(this._handle, pcm);
voiceProbabilityAndStatus = this._pvCobra.process(this._handle, pcm);
} catch (err: any) {
pvStatusToException(PvStatus[err.code as keyof typeof PvStatus], err);
}

const status = VoiceProbabilityAndStatus!.status;
const status = voiceProbabilityAndStatus!.status;
if (status !== PvStatus.SUCCESS) {
this.handlePvStatus(status, 'Cobra failed to process the audio frame');
}

return VoiceProbabilityAndStatus!.is_voiced;
return voiceProbabilityAndStatus!.is_voiced;
}

/**
Expand Down
26 changes: 0 additions & 26 deletions binding/nodejs/test/perf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,13 @@ const NUM_TEST_ITERATIONS = Number(
.filter(x => x.startsWith('--num_test_iterations='))[0]
?.split('--num_test_iterations=')[1] ?? 0
);
const INIT_PERFORMANCE_THRESHOLD_SEC = Number(
process.argv
.filter(x => x.startsWith('--init_performance_threshold_sec='))[0]
?.split('--init_performance_threshold_sec=')[1] ?? 0
);
const PROC_PERFORMANCE_THRESHOLD_SEC = Number(
process.argv
.filter(x => x.startsWith('--proc_performance_threshold_sec='))[0]
?.split('--proc_performance_threshold_sec=')[1] ?? 0
);

describe('Performance', () => {
test('init performance', () => {
let perfResults = [];
for (let i = 0; i < NUM_TEST_ITERATIONS; i++) {
const before = performance.now();
let cobraEngine = new Cobra(ACCESS_KEY);
let initTime = performance.now() - before;

cobraEngine.release();

if (i > 0) {
perfResults.push(initTime);
}
}
let avgPerfMs =
perfResults.reduce((acc, a) => acc + a, 0) / NUM_TEST_ITERATIONS;
let avgPerfSec = Number((avgPerfMs / 1000).toFixed(3));
// eslint-disable-next-line no-console
console.log('Average init performance: ' + avgPerfSec);
expect(avgPerfSec).toBeLessThanOrEqual(INIT_PERFORMANCE_THRESHOLD_SEC);
});

test('proc performance', () => {
let cobraEngine = new Cobra(ACCESS_KEY);

Expand Down
8 changes: 8 additions & 0 deletions demo/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ cobra-file-demo --access_key ${ACCESS_KEY} --input_audio_file_path ${AUDIO_PATH}
Replace `${ACCESS_KEY}` with yours obtained from Picovoice Console and `${AUDIO_PATH}` with a path to an audio file you
wish to use for voice activity detection.

The threshold of the engine can be tuned using the `threshold` input argument:

```console
cobra-file-demo --access_key ${ACCESS_KEY} --input_audio_file_path ${AUDIO_PATH} --threshold ${DETECTION_THRESHOLD}
```

Threshold is a floating point number within `[0, 1]`. A higher threshold reduces the miss rate at the cost of increased false alarm rate.

### Microphone Demo

You need a working microphone connected to your machine for this demo. Run the following in the terminal:
Expand Down
22 changes: 17 additions & 5 deletions demo/nodejs/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ program
.option(
"-l, --library_file_path <string>",
"absolute path to cobra dynamic library"
)
.option(
"-t --threshold <string>",
"Threshold for the probability of voice activity"
);

if (process.argv.length < 2) {
Expand All @@ -42,6 +46,7 @@ function fileDemo() {
let audioPath = program["input_audio_file_path"];
let accessKey = program["access_key"];
let libraryFilePath = program["library_file_path"];
let threshold = program["threshold"] ?? 0.8;

let engineInstance = new Cobra(accessKey, {
libraryPath: libraryFilePath,
Expand Down Expand Up @@ -70,13 +75,20 @@ function fileDemo() {

let frames = getInt16Frames(inputWaveFile, engineInstance.frameLength);

let voiceProbabilities = [];
for (let frame of frames) {
const voiceProbability = engineInstance.process(frame);
voiceProbabilities.push(voiceProbability);
const printedNums = new Set();
for (let i = 0; i < frames.length; i++) {
const result = engineInstance.process(frames[i]);
const timestamp = (
(i * engineInstance.frameLength) /
engineInstance.sampleRate
).toFixed(1);

if (result >= threshold && !printedNums.has(timestamp)) {
console.log(`Detected voice activity at ${timestamp} sec`);
printedNums.add(timestamp);
}
}

console.log(voiceProbabilities);
engineInstance.release();
}

Expand Down
12 changes: 11 additions & 1 deletion demo/nodejs/mic.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ async function micDemo() {
const pcm = await recorder.read();
try {
const voiceProbability = engineInstance.process(pcm);
console.log(voiceProbability);
const percentage = voiceProbability * 100;
albho marked this conversation as resolved.
Show resolved Hide resolved
const barLength = Math.floor((percentage / 10) * 3);
const emptyLength = 30 - barLength;
const spacer = ` `.repeat(3 - percentage.toFixed(0).length);

process.stdout.write(
`\r[${spacer}${percentage.toFixed(0)}]|${"█".repeat(
barLength
)}${" ".repeat(emptyLength)}|`
albho marked this conversation as resolved.
Show resolved Hide resolved
);
} catch (err) {
if (err instanceof CobraActivationLimitReachedError) {
console.error(
Expand All @@ -106,6 +115,7 @@ async function micDemo() {
recorder.stop();
recorder.release();
engineInstance.release();
process.stdout.write("\n");
process.exit();
}

Expand Down
56 changes: 0 additions & 56 deletions demo/nodejs/wave_util.js

This file was deleted.

Loading