Audio Output
Python Quick Start
Platforms
- Linux (x86_64)
- macOS (x86_64, arm64)
- Windows (x86_64)
- Raspberry Pi (3, 4, 5)
Requirements
- Python 3.8+
- PIP
Quick Start
Setup
Install Python 3.
Install the pvspeaker Python package using PIP:
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()
:
Note that in order to stop the audio before it finishes playing, stop()
must be run on a separate thread from flush()
.
Once you are done (i.e. no longer need PvSpeaker to write and/or play PCM), free the resources acquired by PvSpeaker by calling delete()
. Be sure to first call stop()
if the audio is still playing. Otherwise, if the audio has already finished playing, you do not have to call stop()
before delete()
:
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 __init__()
to select that device for audio output:
Demo
For the PvSpeaker Python SDK, we offer a demo application that demonstrates how to use PvSpeaker
to play audio from an audio file (.wav
).
Setup
Install the pvspeakerdemo Python package using PIP:
This package installs command-line utilities for the PvSpeaker Python 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 Python, head over to our GitHub repository.