Skip to main content

Introduction

Documentation Status Discord Build Status Code Style: Ruff

Play FLAC files on CircuitPython. FLACDecoder decodes FLAC (lossless) audio to 16-bit PCM, and FLACPlayer streams it to an I2SOut, PWMAudioOut or AudioOut while your code keeps running. On an RP2350 it plays 16-bit/44.1 kHz and 24-bit/96 kHz files from a microSD card at the stock 150 MHz.

The decoder is written from RFC 9639 and decodes bit-exact. Its hot loops run in a turbo (@micropython.viper) kernel, prebuilt for each chip architecture.

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:

flac_kernel file

Boards

armv6m

RP2040

armv7emsp

SAMD51 (M4), nRF52840, RP2350, STM32F4

xtensawin

ESP32-S2, ESP32-S3

rv32imc

ESP32-C3, C5, C6

Without a kernel the decoder falls back to plain Python, which is correct but far slower than real time for music. Only the RP2350 has been tested for playback.

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_flac

Or the following command to update an existing version:

circup update

Usage Example

import adafruit_flac
from adafruit_fruitjam.peripherals import Peripherals

decoder = adafruit_flac.FLACDecoder("song.flac")
fruit_jam = Peripherals(audio_output="headphone", sample_rate=decoder.sample_rate)

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

Speed and memory

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

Format

150 MHz

300 MHz

16-bit, 44.1 kHz stereo

30%

15%

24-bit, 96 kHz stereo

89%

46%

  • Keep the decoder and the audio buffer in internal SRAM. On the RP2350, decoding into PSRAM nearly doubled the load at 96 kHz. Create them first in code.py, before other large allocations, or pass a preallocated array("h") as FLACPlayer(audio, buffer=...).

  • FLACPlayer 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.

  • At 24/96 the decoder uses about 83 KB and a 64 ms double buffer 48 KB (plus the output’s own DMA buffers).

  • examples/flac_fruitjam_sd_hires.py plays 24/96 from the Fruit Jam’s microSD card over 4-bit SDIO.

Building the kernel

src/flac_turbo.py is the kernel source, turbo (@micropython.viper) functions. tools/gen_turbo_lpc.py writes the unrolled predictors into it. src/Makefile compiles it with mpy-cross -march for each architecture into adafruit_flac/flac_kernel.<arch>.mpy:

cd src
make MPY_CROSS=path/to/circuitpython/mpy-cross/build/mpy-cross

Use an mpy-cross from the CircuitPython version the files will run on.

Testing

tools/make_test_media.py builds test clips (it downloads a CC0 recording and needs flac and ffmpeg). tests/test_decoder.py checks the plain Python decoder against libFLAC under CPython, tests/test_kernel_model.py runs the same with a CPython model of the kernel that fails on any 32-bit overflow, and tools/turbo_check.py compares the kernel with the plain Python path on a board.

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-flac 1.0.3

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-flac 1.0.3
File Size Uploaded Provenance Actions
adafruit_circuitpython_flac-1.0.3.tar.gz 74.0 kB Without provenance Details for adafruit_circuitpython_flac-1.0.3.tar.gz

Built distribution (wheel)

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

Total release size: 117.5 kB

Release files / adafruit_circuitpython_flac-1.0.3.tar.gz

Download URL adafruit_circuitpython_flac-1.0.3.tar.gz
Size 74.0 kB
Tags Source
SHA-256 checksum
How to use checksums
58e0ebf795686bba77d4319054fd496212c8162e7bdec1f082297dbe890c752b
BLAKE2b-256 checksum
How to use checksums
1fac0d607b0f7274d2a97cb59e7a5a568184a1352488f05ac919d9214cfe3c83
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_flac-1.0.3-py3-none-any.whl

Download URL adafruit_circuitpython_flac-1.0.3-py3-none-any.whl
Size 43.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59def60dfaec6a255bb12eea7c82ef266102ff520340e46e34cd3d7944fc5a26
BLAKE2b-256 checksum
How to use checksums
7a339af280d69db00ce422530ca42ff4204ff63f978ee55fdd59fe32c59bf73e
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-flac 1.0.3

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.3 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