Audio Output
Node.js Quick Start
Platforms
- Linux (x86_64)
- macOS (x86_64, arm64)
- Windows (x86_64)
- Raspberry Pi (3, 4, 5)
Requirements
- Node.js 16+
- npm
Quick Start
Setup
Install Node.js.
Install the pvspeaker-node npm package:
Usage
Initialize and start PvSpeaker
:
Write PCM data to the speaker:
Note: the write()
method only writes as much PCM data as the internal circular buffer can currently fit, and returns the number of samples that were successfully written.
When all frames have been written, run flush()
to wait for all buffered PCM data (i.e. previously buffered via write()
) to be played:
Note: calling flush()
with PCM data as an argument will both write that PCM data and wait for all buffered PCM data to finish.
To stop the audio output device, run stop()
:
Once you are done (i.e. no longer need PvSpeaker to write and/or play PCM), free the resources acquired by PvSpeaker by calling release()
. You do not have to call stop()
before release()
:
Selecting an Audio Device
To get a list of available audio devices:
The index of the device in the returned list can be used in the constructor()
to select that device for audio output:
Demo
For the PvSpeaker Node.js SDK, we offer a demo application that demonstrates how use PvSpeaker
to play audio from an audio file (.wav
).
Setup
Install the PvSpeaker demo package:
This package installs command-line utilities for the PvSpeaker Node.js demo.
Usage
Use the --help
flag to see the usage options for the demo:
Run the following to see what devices are available for audio playback:
Run the demo with the desired audio device (or -1 for the default one) and an input file path:
For more information about our PvSpeaker demo for Node.js, head over to our GitHub repository.