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

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.2
File Size Uploaded Provenance Actions
adafruit_circuitpython_flac-1.0.2.tar.gz 72.5 kB Without provenance Details for adafruit_circuitpython_flac-1.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 114.8 kB

Release files / adafruit_circuitpython_flac-1.0.2.tar.gz

Download URL adafruit_circuitpython_flac-1.0.2.tar.gz
Size 72.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d375d6e6ea0f08020c37777528124dbbdb8478b39e64c3af8998050ae1262cb7
BLAKE2b-256 checksum
How to use checksums
bd72b1614ab50aad6496f2e8cf9be3ee6e0b734ce01615ed7a1e38adb75d50a0
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.2-py3-none-any.whl

Download URL adafruit_circuitpython_flac-1.0.2-py3-none-any.whl
Size 42.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1ee08c97a95c801aad18f5f5958fe552ba69412275d54e9e9409e6261bd5f5a
BLAKE2b-256 checksum
How to use checksums
a50f2a0a5812b5feb65b894f71e9ea27ab997980c86c70fb3e38711476534fed
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.2

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