Detecting Fake FLAC Files in the Browser: Raw STREAMINFO Parsing and 80-Slice 2048-Point FFT Forensics
If you inspect a FLAC file with ffprobe or a media player and see 16-bit / 44.1 kHz / 942 kbps, you still do not know whether the audio is lossless. Transcoding a 128 kbps MP3 into FLAC does not restore discarded spectra
If you inspect a FLAC file with ffprobe or a media player and see 16-bit / 44.1 kHz / 942 kbps, you still do not know whether the audio is lossless. Transcoding a 128 kbps MP3 into FLAC does not restore discarded spectral data; it simply wraps the decoded, quantized PCM stream inside a lossless entropy coder. The resulting file passes every structural integrity checkβvalid fLaC framing, clean CRC-16 frame checksums, and a matching PCM MD5βwhile carrying a high bitrate tag generated strictly from the encoded file size divided by duration.
Catching a lossy-to-lossless transcode requires two layers of inspection: raw binary header verification and statistical spectral forensics. Both run comfortably in the browser without uploading files or installing desktop spectrogram utilities like Spek.
Step 1: Parsing Raw STREAMINFO Before Decoding
Before invoking Web Audio's decodeAudioData, read the raw ArrayBuffer to verify the container's true PCM geometry. Relying solely on AudioBuffer.sampleRate can mislead you because browsers often resample decoded buffers to the host hardware clock (typically 48 kHz).
A valid FLAC stream begins with the 4-byte ASCII signature fLaC (0x664C6143), immediately followed by a 4-byte metadata block header and the mandatory 34-byte STREAMINFO block (type 0). The sample rate, channel count, bit depth, and total sample count are bit-packed across bytes 18 to 25 of the file:
JavaScript
function parseFlacStreamInfo(buffer) {
const view = new DataView(buffer);
if (view.getUint32(0) !== 0x664c6143) throw new Error("Not a FLAC stream");
const blockType = view.getUint8(4) & 0x7f;
if (blockType !== 0) throw new Error("First metadata block is not STREAMINFO");
// Bytes 18..25 hold: 20-bit sampleRate | 3-bit (ch-1) | 5-bit (bps-1) | 36-bit totalSamples
const hi = view.getUint32(18);
const lo = view.getUint32(22);
const sampleRate = hi >>> 12;
const channels = ((hi >>> 9) & 0x07) + 1;
const bitsPerSample = ((hi >>> 4) & 0x1f) + 1;
const totalSamples = ((hi & 0x0f) * 2 ** 32) + lo;
return { sampleRate, channels, bitsPerSample, totalSamples, nyquist: sampleRate / 2 };
}
This establishes the true Nyquist limit (fNβ=fsβ/2, or 22,050 Hz for Red Book audio) and prevents false alarms on legitimately 32 kHz source recordings.
Step 2: Brickwall Shelves vs. Natural Analog Roll-Off
Lossy psychoacoustic codecs (MP3, AAC, Ogg Vorbis) discard high-frequency MDCT bins to preserve bit budget for midband masking thresholds. In LAME MP3 encoders, scalefactor band 21 (sfb21, above 16 kHz) lacks its own scalefactor, making high-frequency coding disproportionately expensive. Consequently, encoders apply steep low-pass brickwall filters at predictable cutoffs:
| Codec Profile | Typical Brickwall Shelf | Forensic Signature |
|---|---|---|
| 128 kbps MP3 (CBR) | 15.8 β 16.2 kHz | Sudden 35β50 dB cliff; flat digital silence above 16.5 kHz |
| 192 kbps MP3 / AAC-LC | 17.8 β 18.5 kHz | Sharp shelf with sporadic energy islands from temporal noise shaping |
| 320 kbps MP3 (CBR) | 19.8 β 20.5 kHz | Steep drop near 20 kHz, often with visible sfb21 notch gaps |
| True Lossless (44.1 kHz) | No brickwall | Gradual 6β12 dB/octave roll-off extending to 21.5β22.05 kHz |
Authentic analog and lossless digital recordings rarely drop to ββ dBFS at a straight horizontal frequency line. Cymbal transients, room air, tape hiss, and microphone self-noise decay smoothly toward Nyquist.
Step 3: 80-Slice 2048-Point FFT and 90th-Percentile Band Math
Scanning an entire 5-minute track frame-by-frame is unnecessary. Instead, extract M=80 evenly spaced slices across the active 5%β95% duration of the mono-summed signal. Each slice takes N=2048 samples multiplied by a Hann window:
w[n]=0.5(1βcos(Nβ12Οnβ))
At fsβ=44,100 Hz, the frequency resolution is Ξf=204844100ββ21.533 Hz/bin. Compute each bin's power in dBFS:
Pmβ[k]=20log10β(N/2β£Xmβ[k]β£β+10β12)
Do not average Pmβ[k] across the 80 slices, and do not take the maximum. Arithmetic mean drags high-frequency energy down during quiet verses, while maximum gets fooled by a single transient click or dither spike. Take the 90th percentile (P90β[k]) across the 80 slices for every bin k. This isolates sustained energetic passages (choruses, hi-hats, vocal sibilance).
Next, evaluate median P90β levels across a reference band Brefβ=[10,14] kHz and three diagnostic shelf bands:
- B16β=[16.5,17.8] kHz
- B18β=[18.5,19.8] kHz
- B20β=[20.5,21.8] kHz
Compute the band drop Ξbβ=mediankβBbββ(P90β[k])βmediankβBrefββ(P90β[k]). When median(Brefβ) sits above β55 dBFS (confirming the track actually has bright program material) and B16β plunges below β85 dBFS with Ξ16ββ€β32 dB, you have a definitive 128 kbps transcode. Similar calibrated thresholds on B18β and B20β, combined with local spectral slope βfβP90ββ, distinguish 192 kbps and 320 kbps shelves from dark acoustic recordings.
Practical Browser-Local Workflow
Running this exact STREAMINFO parser and 80-slice FFT forensic pass requires zero server infrastructure. You can verify suspicious downloads directly in RAMβwith no file uploads and no Spek installationβusing AIAC's Fake FLAC Checker. Once you confirm a master is genuinely lossless and need compact delivery files where you control the exact encoding bitrate rather than inheriting unknown generational loss, AIAC's FLAC to MP3 Converter handles the transcoding 100% locally in the browser as well.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.