Skip to main content

Introduction

Documentation Status Discord Build Status Code Style: Ruff

Play Ogg Vorbis files on CircuitPython. OggDecoder decodes Ogg Vorbis audio to 16-bit PCM, and OggPlayer streams it to an I2SOut, PWMAudioOut or AudioOut while your code keeps running. On an RP2350 it plays 44.1 kHz stereo files at the stock 150 MHz with about a quarter of the CPU.

The decoder is written from the Xiph.Org Vorbis I specification. No third-party decoder code is included. The audio decode runs in a prebuilt kernel for each chip architecture, in fixed point, and stays within a few LSB of ffmpeg’s floating point decode.

Dependencies

This library depends on:

  • Adafruit CircuitPython 11 or later, on a board that can load native .mpy files (microcontroller.cpu.architecture is not None)

The decoding loops are a precompiled kernel, one file per architecture. Arm and Xtensa (ESP32) chips use a C build of the kernel, ogg_native; RISC-V chips use a turbo (@micropython.viper) build, ogg_kernel. Both have the same functions and give the same output to within an LSB.

Architecture

Kernel

Boards

armv6m

ogg_native (C)

RP2040

armv7emsp

ogg_native (C)

SAMD51 (M4), nRF52840, RP2350, STM32F4

xtensawin

ogg_native (C)

ESP32, ESP32-S2, ESP32-S3

rv32imc

ogg_kernel (turbo)

ESP32-C3, C5, C6

ogg_kernel is also built for the Arm and Xtensa architectures, as a fallback. Without a kernel the decoder falls back to plain Python, which is correct but far slower than real time for music. Playback has been tested on the RP2350 and the ESP32-S3.

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.

Installing to a Connected CircuitPython Device with Circup

Make sure that you have circup installed in your Python environment. Install it with the following command if necessary:

pip3 install circup

With circup installed and your CircuitPython device connected use the following command to install:

circup install adafruit_ogg

Or the following command to update an existing version:

circup update

Usage Example

import adafruit_ogg
from adafruit_fruitjam.peripherals import Peripherals

decoder = adafruit_ogg.OggDecoder("song.ogg")
fruit_jam = Peripherals(audio_output="headphone", sample_rate=decoder.sample_rate)

player = adafruit_ogg.OggPlayer(fruit_jam.audio)
player.play(decoder)
while player.playing:
    player.update()  # decode the next piece; call this often while playing

decoder.comments holds the file’s tags (TITLE, ARTIST and so on), and decoder.open(path) switches to another file, reusing the buffers.

Speed and memory

Measured on an Adafruit Fruit Jam (RP2350B) at 150 MHz. Load is decode time divided by audio time; under 100% keeps up.

Format

Load

Opening a file

44.1 kHz stereo

24%

0.5 s

22.05 kHz mono

8%

0.5 s

11.025 kHz mono

5%

0.3 s

  • The decoder needs about 100 KB (11 kHz mono) to 175 KB (48 kHz stereo), most of it codebook tables, which depend on the file.

  • Keep the decoder in internal SRAM. On the RP2350, with its buffers in PSRAM it decodes about four times slower (93% load at 44.1 kHz stereo). Create it first in code.py, before other large allocations such as Peripherals, and reuse it with decoder.open().

  • OggPlayer decodes straight into the half of its buffer that the audio DMA has already copied, so it does not work with audiomixer, which reads the buffer in place.

  • Opening a file parses its codebooks, which takes a fraction of a second. Let the player finish or stop it before opening the next file.

Building the kernels

src/ogg_turbo.py is the turbo kernel (@micropython.viper functions) and src/native/ogg_native.c the same functions in C. src/Makefile compiles the turbo kernel with mpy-cross -march for each architecture into adafruit_ogg/ogg_kernel.<arch>.mpy; src/native/Makefile builds the C kernel with the CircuitPython native module tools (py/dynruntime.mk, needs arm-none-eabi-gcc, xtensa-esp32-elf-gcc from the ESP-IDF tools, and pyelftools) into adafruit_ogg/ogg_native.<arch>.mpy:

make -C src MPY_CROSS=path/to/circuitpython/mpy-cross/build/mpy-cross
make -C src/native MPY_DIR=path/to/circuitpython all-arches

Use the CircuitPython version the files will run on.

Testing

tools/make_test_media.py downloads public domain and CC0 recordings from Wikimedia Commons and makes smaller versions (it needs ffmpeg and the soundfile package). tests/test_decoder.py decodes them with the kernel, run under CPython by a model that fails on any 32-bit overflow, and with the plain Python decoder, and compares both with ffmpeg. tests/test_codebook.py and tests/test_ogg_reader.py need no media. tools/kernel_check.py compares the C and turbo kernels on a board and times them.

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Metadata

Release files for adafruit-circuitpython-ogg 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for adafruit-circuitpython-ogg 1.0.1
File Size Uploaded Provenance Actions
adafruit_circuitpython_ogg-1.0.1.tar.gz 106.9 kB Without provenance Details for adafruit_circuitpython_ogg-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for adafruit-circuitpython-ogg 1.0.1
File Interpreter ABI Platform Actions
adafruit_circuitpython_ogg-1.0.1-py3-none-any.whl Python 3 none any Details for adafruit_circuitpython_ogg-1.0.1-py3-none-any.whl

Total release size: 178.1 kB

Release files / adafruit_circuitpython_ogg-1.0.1.tar.gz

Download URL adafruit_circuitpython_ogg-1.0.1.tar.gz
Size 106.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b777ee57d0b1e62e1daf33a73300bbadcdec83a6a0a0df155814e35657ffe9c2
BLAKE2b-256 checksum
How to use checksums
d9518afa752b92728597c608e76c27c6f8a4720b64cefc9c54214bf1c1750284
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Release files / adafruit_circuitpython_ogg-1.0.1-py3-none-any.whl

Download URL adafruit_circuitpython_ogg-1.0.1-py3-none-any.whl
Size 71.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa34c21bb66b45241172d4323443626af19632408093a65d82ef694b4284e512
BLAKE2b-256 checksum
How to use checksums
4b7c4ecb1be3f62536dacc7478805635f4871b6cb0b98d494766c8dd37086248
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Security for adafruit-circuitpython-ogg 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page