From the deadwax
How We Built a FLAC Codec in Pure Swift
Private Press ships a native Swift FLAC artwork and metadata editor that preserves encoded audio frames. It uses metadata parsing and writing utilities from our FLACCodec module. That same module also contains a full audio encoder and decoder. The frame-processing work is described below. Those audio-conversion APIs are implemented and tested, but are not invoked by the shipped app’s pressing workflow.
Building a FLAC stream
A FLAC encoder turns sample blocks into frames. It estimates each block with predictor strategies, encodes the residuals, and writes frame headers and checksums. A stream adds metadata and a final integrity value. The decoder reads those structures in reverse and reconstructs the samples.
The codec's bit reader and writer handle fields that do not align to byte boundaries. CRC-8 and CRC-16 checks protect frame headers and frame contents. Rice coding represents prediction residuals compactly, with partition selection based on the residual data.
Prediction and residuals
The encoder evaluates fixed predictors of orders 0 through 4. For material where a linear predictor can do better, it computes autocorrelation with Accelerate's vDSP_dotprD, solves the coefficients with Levinson-Durbin recursion, and quantizes them into the integer representation written to the FLAC frame. The predicted samples are subtracted from the source block; the residuals are then Rice-coded.
This is a useful combination of numerical work and exact integer rules. The coefficients can be estimated as floating-point values, but the encoded subframe has integer coefficients, a shift field, warm-up samples, and residuals that must satisfy FLAC's range rules.
Stereo and stream integrity
Stereo encoding can evaluate independent channels, left-side, right-side, and mid-side decorrelation. The side channel can require one more bit than the original sample depth, so the implementation preserves that width through prediction, residual coding, and reconstruction.
The streaming encoder writes metadata, frames, a seek table, and the stream's MD5 value without keeping all encoded frames in memory. The decoder validates frame sequence, checksums, metadata, sample ranges, and the final sample digest as it reads.
Testing against real streams
Round-trip tests encode PCM, decode the resulting stream, and compare every sample. The test suite also carries positive and negative FLAC fixtures produced by libFLAC and FFmpeg. Positive fixtures exercise interoperability with streams written by those tools; negative fixtures check that malformed headers, checksums, partitions, and sample data are rejected.
These tests cover more than whether one player can start playback. They exercise frame boundaries, predictor forms, residual partitions, channel assignments, metadata, and the end-to-end sample result.
Why this work matters
Together, this work covers both sides of the format: production metadata editing that keeps the existing audio intact, and a native encoder/decoder that implements prediction, bit packing, checksums, and stream integrity in Swift. The app’s current pressing workflow uses the metadata layer; full audio conversion remains an experimental product capability.
FLAC artwork editing ships today; full audio conversion is an experimental capability.
Read the current format support notesFrom the deadwax
Subscribe to Deadwax
New posts on music collections, artwork quality, and the tech behind Private Press.
We respect your privacy. Unsubscribe anytime.