Skip to content
QuaAudio
On this page

DSP & Cleanroom IP

Inside QuaAudio's reversible 3-shear IntMDCT, 6-stage blank elision, QAA-Spatial binaural VBR, 3-stream Lazy LZ77 + rANS, and Cleanroom provenance.

QuaAudio (@quajs/quaaudio / quaaudio) is an original cleanroom implementation derived from first-principles mathematics and classical public-domain literature. It contains zero third-party codec libraries, zero copied tables, and zero patent-encumbered mechanisms (see CLEANROOM.md).

1. Deterministic reversible IntMDCT (lossless)

Instead of running time-domain LPC alone, mode: 'lossless' defaults to an exact reversible Integer Lapped Cosine Transform (IntMDCT):

  • 3-shear Givens lifting: Each 2×22 \times 2 rotation R(θ)∈SO(2)R(\theta) \in SO(2) is quadrant-normalized into [−π/2,π/2][-\pi/2, \pi/2] (guaranteeing shear multipliers ∣s∣≤1|s| \le 1) and factored into three integer triangular shear steps, guaranteeing algebraic reversibility: IntMDCT−1(IntMDCT(x))≡x\text{IntMDCT}^{-1}(\text{IntMDCT}(x)) \equiv x.
  • Deterministic Q24 polynomial trigonometry: Twiddle rotations evaluate fixed-point Q24 integer polynomials on i64, eliminating platform libm divergence across x86_64, aarch64, and wasm32.

Multi-stage blank and redundancy elision

Before entropy coding, each lossless cluster passes through deterministic compaction stages evaluated by real compressed byte cost:

  1. Silence trimming & zero clusters: Leading/trailing digital silence collapses into u64 header counters; silent mid-stream clusters emit CLUSTER_FLAG_ALL_ZERO.
  2. Reversible Mid/Side lifting: Computes S=L−RS = L - R, M=R+⌊S/2⌋M = R + \lfloor S/2 \rfloor and elides zero Side channels (common in centered dialogue recordings) in 1 bit.
  3. LSB zero bitplane stripping & 32-band mask: Strips unused zero LSB planes and omits completely empty spectral bands.
  4. Per-channel mode competition: Chooses the smallest compressed representation across Direct IntMDCT (CH_MODE_SPECTRAL), 2-tap Pre-Emphasized IntMDCT (CH_MODE_PREEMPH_SPECTRAL), and Levinson-Durbin Order-8 Integer LPC (CH_MODE_WAVE_DELTA).
  5. Spectral prediction & micro-tuplet packing: Applies integer backward/forward AR(2) sinusoidal predictors and pitch-warped Lag-2, elides all-zero residual bands, and packs low-magnitude residuals as Base-3 quintets / Base-5 triplets.

2. Full-bandwidth psychoacoustics & QAA-Spatial (VBR)

With mode: 'lossy' and a configurable targetRatio:

  • Adaptive Princen-Bradley TDAC switching: Combines 2048-sample Long windows (FRAME_SAMPLES = 1024) and 8 × 256 Short transient windows (SHORT_SAMPLES = 128) connected via aliasing-free bridge windows (sin⁡2θ+cos⁡2θ=1\sin^2\theta + \cos^2\theta = 1).
  • Full Nyquist bandwidth: Analytical Bark/Terhardt simultaneous masking and < 10 kHz temporal post-masking leave 10–24 kHz air bands unmasked, paired with energy-conserving coherent HF fine-structure restoration to eliminate metallic "birdies".
  • Binaural Spatial Stereo (QAA-Spatial):
    • Locks exact Mid/Side phase below 2.6 kHz to preserve Interaural Time Difference (ITD);
    • Delta-codes Gram-Schmidt orthogonalized panning (alpha) and orthogonal diffuse Side energy (ortho_rms) above 2.6 kHz to preserve both Interaural Level Difference (ILD) and Interaural Coherence (IC) without collapsing ambient width to mono.
  • Global two-pass Lagrangian bisection: Solves q_db to match the user-requested compression ratio closely.

3. 3-stream demux, Lazy LZ77 + rANS & .qaab solid banks

Both standalone .qaa clusters and cross-track .qaab solid chunks (512 KiB default structured budget, 1 MiB LZ77 window) split structured syntax from raw thermal remainder bits:

  • s0 (meta / ctrl): Headers, predictor choices, band activity masks, and scale parameters;
  • s1 (quot): Golomb-Rice quotients;
  • s2 (micro): Base-3 quintet and Base-5 triplet streams;
  • rem: Uniform Golomb-Rice remainder bits (stored unpolluted outside LZ77/rANS).

Each structured stream is compressed using 1-step Lazy LZ77 with a 3-slot repeat-offset stride LRU (rep0, rep1, rep2) followed by multi-context 32-bit rANS (including 8-bucket logarithmic mag_ctx8). Across tracks in .qaab, QuaAudio extracts 32-band Bark spectral fingerprints, orders tracks via Greedy Nearest-Neighbor TSP within (mode, channels, sample_rate) groups, and optionally trains a shared 256-entry LBG spectral envelope codebook so voices and instruments with matching timbre sit adjacent inside the 1 MiB dictionary.