Skip to content

Commit

Permalink
Nodejs sdk (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
albho authored Feb 5, 2024
1 parent 1cbca1e commit ff143be
Show file tree
Hide file tree
Showing 49 changed files with 6,048 additions and 3 deletions.
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
62 changes: 62 additions & 0 deletions .github/workflows/nodejs-demos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 }}

- 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, rpi5-64, jetson, beaglebone]

steps:
- uses: actions/checkout@v3

- name: Install dependencies
run: yarn install

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

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- 'binding/nodejs/perf.test.js'
- 'lib/node/**'
- '.github/workflows/nodejs-perf.yml'

pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- 'binding/nodejs/perf.test.js'
- '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
num_test_iterations: 50
proc_performance_threshold_sec: 0.005
- os: windows-latest
num_test_iterations: 50
proc_performance_threshold_sec: 0.005
- os: macos-latest
num_test_iterations: 50
proc_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=${{matrix.num_test_iterations}} --proc_performance_threshold_sec=${{matrix.proc_performance_threshold_sec}}

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

strategy:
fail-fast: false
matrix:
machine: [rpi2, rpi3-32, rpi3-64, rpi4-32, rpi4-64, rpi5-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: rpi5-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}} --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, rpi5-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}}
61 changes: 59 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,43 @@ 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");

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

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

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

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

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

```javascript
cobra.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

0 comments on commit ff143be

Please sign in to comment.