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 14 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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ body:
- Android
- C
- iOS
- Node.js
- Python
- Rust
- Web
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/nodejs-codestyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Node.js Codestyle

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- '**/nodejs/*.js'
- '**/nodejs/*.ts'
- '.github/workflows/nodejs-codestyle.yml'
pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- '**/nodejs/*.js'
- '**/nodejs/*.ts'
- '.github/workflows/nodejs-codestyle.yml'

jobs:
check-nodejs-codestyle:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

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

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

- name: Run Binding Linter
run: yarn && yarn lint
working-directory: binding/nodejs
74 changes: 74 additions & 0 deletions .github/workflows/nodejs-demos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Node.js Demos

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- 'demo/nodejs/**'
- '!demo/nodejs/README.md'
- 'lib/node/**'
- '.github/workflows/nodejs-demos.yml'

pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- 'demo/nodejs/**'
- '!demo/nodejs/README.md'
- 'lib/node/**'
- '.github/workflows/nodejs-demos.yml'

defaults:
run:
working-directory: demo/nodejs

jobs:
build-github-hosted:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

# ************** REMOVE AFTER RELEASE ********************
- name: Build Node.js SDK
run: yarn && yarn build
working-directory: binding/nodejs
# ********************************************************

- name: Install dependencies
run: yarn install

- name: Test
run: yarn file -a ${{secrets.PV_VALID_ACCESS_KEY}} -i ../../res/audio/sample.wav

build-self-hosted:
runs-on: ${{ matrix.machine }}

strategy:
matrix:
machine: [rpi2, rpi3-32, rpi3-64, rpi4-32, rpi4-64, jetson, beaglebone]

steps:
- uses: actions/checkout@v3

# ************** REMOVE AFTER RELEASE ********************
- name: Build Node.js SDK
run: yarn && yarn build
working-directory: binding/nodejs
# ********************************************************

- name: Install dependencies
run: yarn install

- name: Test
run: yarn file -a ${{secrets.PV_VALID_ACCESS_KEY}} -i ../../res/audio/sample.wav
96 changes: 96 additions & 0 deletions .github/workflows/nodejs-perf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Node.js Performance

on:
workflow_dispatch:
push:
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/**'
albho marked this conversation as resolved.
Show resolved Hide resolved
- 'lib/node/**'
- '.github/workflows/nodejs-perf.yml'

defaults:
run:
working-directory: binding/nodejs

jobs:
perf-github-hosted:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
init_performance_threshold_sec: 0.001
albho marked this conversation as resolved.
Show resolved Hide resolved
- os: windows-latest
init_performance_threshold_sec: 0.5
ksyeo1010 marked this conversation as resolved.
Show resolved Hide resolved
- os: macos-latest
init_performance_threshold_sec: 0.005

steps:
- uses: actions/checkout@v3

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

- name: Install dependencies
run: yarn install

- name: Test
run: yarn test perf.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}} --num_test_iterations=50 --init_performance_threshold_sec=${{matrix.init_performance_threshold_sec}} --proc_performance_threshold_sec=0.005
albho marked this conversation as resolved.
Show resolved Hide resolved

perf-self-hosted:
runs-on: ${{ matrix.machine }}

strategy:
fail-fast: false
matrix:
machine: [rpi2, rpi3-32, rpi3-64, rpi4-32, rpi4-64, jetson, beaglebone]
include:
- machine: rpi2
num_test_iterations: 20
- machine: rpi3-32
num_test_iterations: 50
- machine: rpi3-64
num_test_iterations: 50
- machine: rpi4-32
num_test_iterations: 50
- machine: rpi4-64
num_test_iterations: 50
- machine: jetson
num_test_iterations: 50
- machine: beaglebone
num_test_iterations: 10

steps:
- uses: actions/checkout@v3

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

- name: Install dependencies
run: yarn install

- name: Machine state before
working-directory: res/scripts
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.005 --proc_performance_threshold_sec=0.05

- name: Machine state after
working-directory: res/scripts
run: bash machine-state.sh
68 changes: 68 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Node.js

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- 'binding/nodejs/**'
- '!binding/nodejs/README.md'
- 'lib/node/**'
- '.github/workflows/nodejs.yml'

pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- 'binding/nodejs/**'
- '!binding/nodejs/README.md'
- 'lib/node/**'
- '.github/workflows/nodejs.yml'

defaults:
run:
working-directory: binding/nodejs

jobs:
build-github-hosted:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

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

- name: Install dependencies
run: yarn install

- name: Test
run: yarn test index.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}}

build-self-hosted:
runs-on: ${{ matrix.machine }}

strategy:
matrix:
machine: [rpi2, rpi3-32, rpi3-64, rpi4-32, rpi4-64, jetson, beaglebone]

steps:
- uses: actions/checkout@v3

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

- name: Install dependencies
run: yarn install

- name: Test
run: yarn test index.test.ts --access_key=${{secrets.PV_VALID_ACCESS_KEY}}
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ Cobra is a highly-accurate and lightweight voice activity detection (VAD) engine
- [Android](#android-demos)
- [iOS](#ios-demos)
- [Web](#web-demos)
- [NodeJS](#nodejs-demos)
- [Rust](#rust-demos)
- [SDKs](#sdks)
- [Python](#python)
- [C](#c)
- [Android](#android)
- [iOS](#ios)
- [Web](#web)
- [NodeJS](#nodejs)
- [Rust](#rust)
- [Releases](#releases)

Expand All @@ -46,14 +48,14 @@ Install the demo package:
sudo pip3 install pvcobrademo
```

With a working microphone connected to your device run the following in the terminal:
With a working microphone connected to your device, run the following in the terminal:

```console
cobra_demo_mic --access_key ${AccessKey}
```

Replace `${AccessKey}` with your AccessKey obtained from [Picovoice Console](https://console.picovoice.ai/). Cobra
starts processing the audio input from the microphone in realtime and outputs to the terminal when it detects any voice activities.
will start processing the audio input from the microphone in realtime and output to the terminal when it detects any voice activity.

For more information about the Python demos go to [demo/python](demo/python).

Expand Down Expand Up @@ -127,6 +129,24 @@ npm run start

Open `http://localhost:5000` in your browser to try the demo.

### NodeJS Demos

Install the demo package:

```console
yarn global add @picovoice/cobra-node-demo
```

With a working microphone connected to your device, run the following in the terminal:

```console
cobra-mic-demo --access_key ${ACCESS_KEY}
```

Cobra will start processing the audio input from the microphone in realtime and output to the terminal when it detects any voice activity.

For more information about NodeJS demos go to [demo/nodejs](demo/nodejs).

### Rust Demos

From [demo/rust/micdemo](demo/rust/micdemo) build and run the demo:
Expand Down Expand Up @@ -313,6 +333,45 @@ Replace `${ACCESS_KEY}` with yours obtained from [Picovoice Console](https://con

When done, release the resources allocated to Cobra using `cobra.release()`.

### NodeJS

Install NodeJS SDK:

```console
yarn add @picovoice/cobra-node
```

Create instances of the Cobra class:

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

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

const handle = new Cobra(accessKey);
```

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

```javascript
let getNextAudioFrame = function() {
...
};

while (true) {
let voiceProbability = handle.process(getNextAudioFrame());
if (voiceProbability !== -1) {
// detection event callback
}
albho marked this conversation as resolved.
Show resolved Hide resolved
}
```

When done be sure to release resources acquired by WebAssembly using `release()`:
albho marked this conversation as resolved.
Show resolved Hide resolved

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

### Rust

Expand Down
5 changes: 5 additions & 0 deletions binding/nodejs/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
lib
resources
.eslintrc.js
Loading
Loading