PvSpeaker
Node.js API
API Reference for the Node.js PvSpeaker SDK (npmjs).
PvSpeaker
Class for the PvSpeaker.
PvSpeaker can be initialized using the class constructor()
.
Resources should be cleaned when you are done using the release()
method.
PvSpeaker.constructor()
constructor
method for PvSpeaker
.
Parameters
sampleRate
number : The sample rate of the audio to be played.bitsPerSample
number : The number of bits per sample.options
: Optional configuration arguments.bufferSizeSecs
number : The size in seconds of the internal buffer used to buffer PCM data - i.e. internal circular buffer will be of sizesampleRate
*bufferSizeSecs
.deviceIndex
number : The audio device index to use to play audio. A value of (-1) will use the machine's default audio device.
Returns
PvSpeaker
: An instance of PvSpeaker.
Throws
Error
: If an error occurs while initializing PvSpeaker.
PvSpeaker.release()
Releases resources acquired by PvSpeaker
.
PvSpeaker.start()
Starts the audio output device.
Throws
Error
: If an error occurs while starting audio device.
PvSpeaker.stop()
Stops the audio output device.
Throws
Error
: If an error occurs while stopping audio device.
PvSpeaker.write()
Synchronous call to write PCM data to the internal circular buffer for audio playback. Only writes as much PCM data as the internal circular buffer can currently fit, and returns the number of samples that were successfully written.
Parameters
pcm
ArrayBuffer : PCM data to be played.
Returns
number
: Number of samples that were successfully written.
Throws
Error
: If an error occurs while writing PCM data.
PvSpeaker.flush()
Synchronous call to write PCM data to the internal circular buffer for audio playback. This call blocks the thread until all PCM data (including PCM data written in previous calls to write()
) has been successfully written and played.
To simply wait for PCM data written in previous calls to write()
to finish playing, call flush()
with no arguments.
Parameters
pcm
ArrayBuffer : Optional PCM data to be played.
Returns
number
: Number of samples that were successfully written.
Throws
Error
: If an error occurs while writing PCM data.
PvSpeaker.writeToFile()
Writes PCM data passed to PvSpeaker to a specified WAV file.
Parameters
outputPath
string : Path to the output WAV file where the PCM data will be written.
Throws
Error
: If an error occurs while writing PCM data to file.
PvSpeaker.getSelectedDevice()
Returns the name of the selected device used to play audio.
Returns
string
: The name of the selected audio device.
PvSpeaker.isStarted
Whether the speaker has started and is available to receive PCM frames or not.
PvSpeaker.sampleRate
The sample rate matching the value passed to the constructor.
PvSpeaker.bitsPerSample
The bits per sample matching the value passed to the constructor.
PvSpeaker.bufferSizeSecs
The buffer size in seconds matching the value passed to the constructor.
PvSpeaker.version
Version of the PvSpeaker
.
PvSpeaker.getAvailableDevices()
Gets the list of available audio devices.
Returns
string[]
: An array of the available device names.
Throws
Error
: If an error occurs while getting available audio devices.