Porcupine - NodeJS Quick Start
Requirements
- NodeJS 10+
- yarn (or npm)
Compatibility
- Linux (x86_64)
- macOS (x86_64)
- Raspberry Pi (2,3,4)
Web Browsers
Looking to run Picovoice in-browser? See the JavaScript WebAssembly demo instead.
Microphone Setup
macOS
See the documentation for node-record-lpm16 for instructions on installing SoX.
Linux & Raspberry Pi
Connect the microphone and get the list of available input audio devices:
arecord -L
The output will be similar to below
nullDiscard all samples (playback) or generate zero samples (capture)defaultmicsysdefault:CARD=DeviceUSB PnP Sound Device, USB AudioDefault Audio Devicehw:CARD=Device,DEV=0USB PnP Sound Device, USB AudioDirect hardware device without any conversionsplughw:CARD=Device,DEV=0USB PnP Sound Device, USB AudioHardware device with all software conversions
In this case, we pick plughw:CARD=Device,DEV=0
. Note that this device comes with software conversions which are handy
for resampling. In what follows we note this value as ${INPUT_AUDIO_DEVICE}
.
create ~/.asoundrc
pcm.!default {type asymcapture.pcm "mic"}pcm.mic {type plugslave {pcm ${INPUT_AUDIO_DEVICE}}}
If you have a speaker add a section for that to ~/.asoundrc
as well.
Check if the microphone works properly by recording audio into a file:
arecord --format=S16_LE --duration=5 --rate=16000 --file-type=wav ~/test.wav
If the command above executes without any errors, then the microphone is functioning as expected. We recommend inspecting the recorded file for recording side effects such as clipping.
Demo Applications
The Porcupine NodeJS demo package provides two demonstration command-line applications for Porcupine: a file-based demo, which scans a compatible WAV file for keywords, and a microphone demo, which listens for keywords and outputs detections live.
Install the demo using the global switch so that will be available on the command line:
yarn global add @picovoice/porcupine-node-demo
or
npm install -g @picovoice/porcupine-node-demo
Microphone Demo
The microphone demo allows you monitor microphone input for keywords using Porcupine. For microphone access, the node-record-lpm16 package is used. Please follow that documentation for troubleshooting. The node-record-lpm16 library spawns a different microphone recording process depending on the OS used. The microphone program (SoX or Arecord) must be setup manually and is not included with yarn/npm.
Use ppn-mic-demo
to run the mic demo. Specify either built-in keywords with --keywords
or paths to Porcupine .ppn
files with --keyword_file_paths
.
Here is an example which will listen for "grapefruit" keyword:
ppn-mic-demo --keywords grapefruit
Wake word detections will display in the console:
Platform: 'mac'; attempting to use 'sox' to access microphone ...Listening for wake word(s): grapefruitPress ctrl+c to exit.Detected 'grapefruit'
File Demo
The file-based demo allows you to scan a compatible WAV file for keywords using Porcupine. Run the file-based demo:
ppn-file-demo --input_audio_file_path ${PATH_TO_WAV_FILE} --keywords grasshopper,bumblebee
Replace ${PATH_TO_WAV_FILE}
with a path to a compatible WAV file. Detections of each keyword will be output in the
terminal, along with their time location in the file:
Detected keyword 'bumblebee' @ 16.096sDetected keyword 'grasshopper' @ 28.896s
Custom Wake Words
You can create custom Porcupine wake word models using Picovoice Console.