Skip to main content

Speaker library for Picovoice.

Project description

PvSpeaker Binding for Python

PvSpeaker

PvSpeaker is an easy-to-use, cross-platform audio player designed for real-time speech audio processing. It allows developers to send raw PCM frames to an audio device's output stream.

Compatibility

  • Python 3.8+
  • Runs on Linux (x86_64), macOS (x86_64 and arm64), Windows (x86_64), and Raspberry Pi (3, 4, 5).

Installation

pip3 install pvspeaker

Usage

Initialize and start PvSpeaker:

from pvspeaker import PvSpeaker

speaker = PvSpeaker(
    sample_rate=22050,
    bits_per_sample=16,
    buffer_size_secs=20,
    device_index=0)

speaker.start()

(or)

Use get_available_devices() to get a list of available devices and then initialize the instance based on the index of a device:

from pvspeaker import PvSpeaker

devices = PvSpeaker.get_available_devices()

speaker = PvSpeaker(
    sample_rate=22050,
    bits_per_sample=16,
    buffer_size_secs=20,
    device_index=0)

speaker.start()

Write PCM data to the speaker:

def get_next_audio_frame():
    pass

speaker.write(get_next_audio_frame())

Note: the write() method only writes as much PCM data as the internal circular buffer can currently fit, and returns the length of the PCM data that was 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:

speaker.flush()

Note: calling flush() with PCM data as an argument will both write that PCM data and wait for all buffered PCM data to finish.

def get_remaining_audio_frames():
    pass

speaker.flush(get_remaining_audio_frames())

To stop the audio output device, run stop():

speaker.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:

speaker.delete()

Demos

pvspeakerdemo provides command-line utilities for playing audio from a file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pvspeaker-1.0.3.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

pvspeaker-1.0.3-py3-none-any.whl (3.5 MB view details)

Uploaded Python 3

File details

Details for the file pvspeaker-1.0.3.tar.gz.

File metadata

  • Download URL: pvspeaker-1.0.3.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for pvspeaker-1.0.3.tar.gz
Algorithm Hash digest
SHA256 f8c8b79b8ab942fb08ea0c8bf020897411b035752db585030acc7943b146e211
MD5 21cb7e514d9d02ef3870eccf7aa260ff
BLAKE2b-256 17bfd7a8b51a6642e8a6cf69506af3584c96d2b2bb5cf4f0763b63c8923e4d4f

See more details on using hashes here.

File details

Details for the file pvspeaker-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pvspeaker-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for pvspeaker-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0fd5c0e6cb86a1b670fae219517dbf59ff6d76da9c1aeda872aa6fede08dda50
MD5 3c27914f4cbc7cfc4b88d760def2d994
BLAKE2b-256 e6a28ac17750fa8ecbe3d215093efdf92697d98fed5bc7517f17bf641dd7b770

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page