Skip to main content
Back to all articles
·6 min read·Engineering Note

Levyra Enhanced Audio: 2048-Point FFT Cutoff Detection and Band Replication

How Levyra detects brickwall codec cutoffs in lower-bitrate streams, restores missing high frequencies with an IIR Hilbert pair, and leaves true 320 kbps AAC and lossless files untouched.

KotlinMedia3Audio DSPFFT

Lossy encoders save bits at lower bitrates by applying a hard low-pass filter before quantization. A 128 kbps AAC fallback often drops everything above 16 or 17 kHz. When you switch between a full-band 320 kbps stream and a 128 kbps stream on decent headphones, that missing top octave is the first thing you notice.

Before writing any DSP code, I verified what streaming endpoints actually deliver. JioSaavn _320.mp4 streams return a genuine 320 kbps AAC-LC file inside an MP4/M4A container at 44.1 kHz stereo, with spectral energy extending up to roughly 20 kHz confirmed via ffprobe and FFT spectrograms. Because those streams are already full-band, Levyra Enhanced Audio bypasses them automatically and passes samples through bit-identically. The same bypass applies to local FLAC, ALAC, and PCM sources based on the decoder input format.

To avoid false positives on acoustic tracks or quiet intros, EnhancedAudioAnalyzer runs a 2048-point FFT on the mono mix about ten times per second and averages the spectrum over time. It scans between 11 kHz and 19.5 kHz and only flags a codec cutoff when three conditions hold at once: the level drops by at least 30 dB across the edge, the band above the edge sits at least 50 dB below the 1 to 6 kHz average, and real musical content is present just below the edge.

When a cutoff is confirmed, DspRestorationEngine rebuilds the missing upper band using spectral band replication. It band-passes the octave just below the cutoff per channel to preserve the stereo image, shifts it upward via single-sideband modulation using an IIR Hilbert transform pair, high-passes the result at the cutoff frequency, and mixes it in along the measured spectral slope with a 3 dB safety margin and a soft-knee limiter at -0.5 dBFS.

The engine allocates zero objects on the audio thread, adds no buffering latency, and takes 20 to 100 microseconds per 1024-frame block on the JVM. On 128 kbps AAC test masters, high-frequency log-spectral distance above 16 kHz drops from 24.90 dB to 13.22 dB on synthetic references and from 20.41 dB to 16.03 dB on real music masters, while source format labels in Technical Audio Info remain honest.

LUC4N3X
LUC4N3XIndependent software developer · Creator of Levyra