Measure File Loudness (LUFS) in the Browser
To measure the loudness of a file a visitor picks, decode it with AudioContext.decodeAudioData, then pass every channel to lufsMultichannel() for the integrated loudness in LUFS and each channel to meteringTruePeakDbtp() for the true peak in dBTP. The whole measurement runs in the page through WebAssembly; the file is never uploaded. The block creates its own file input and result line, so it runs as it stands.
js
import { init, lufsMultichannel, meteringTruePeakDbtp } from '@libraz/libsonare';
await init();
const input = document.createElement('input');
input.type = 'file';
input.accept = 'audio/*';
input.id = 'file';
const result = document.createElement('p');
result.id = 'loudness';
document.body.append(input, result);
input.addEventListener('change', async () => {
const context = new AudioContext();
const decoded = await context.decodeAudioData(await input.files[0].arrayBuffer());
await context.close();
const channels = Array.from({ length: decoded.numberOfChannels }, (_, i) => decoded.getChannelData(i));
const { integratedLufs } = lufsMultichannel({ channels, sampleRate: decoded.sampleRate });
const truePeak = Math.max(
...channels.map((samples) => meteringTruePeakDbtp({ samples, sampleRate: decoded.sampleRate })),
);
const loudness = Number.isFinite(integratedLufs)
? `${integratedLufs.toFixed(1)} LUFS integrated`
: 'LUFS was not measured';
const peak = Number.isFinite(truePeak)
? `${truePeak.toFixed(1)} dBTP true peak`
: 'True peak was not measured';
result.textContent = `${loudness}, ${peak}`;
});What you need
- Install:
npm install @libraz/libsonare. - An audio file: anything the browser decodes, such as WAV, MP3, AAC, OGG or FLAC.
Reading the result
integratedLufsis the programme loudness of the whole file in LUFS (loudness units relative to full scale), measured as ITU-R BS.1770 specifies with gating, so silence and very quiet passages do not drag it down.lufsMultichannel()also returnsloudnessRangeLuin LU and the momentary and short-term values; LUFS explains each.- True peak is in dBTP, the highest level the reconstructed waveform reaches between samples, and it is not part of the loudness result, which is why it is a second call. It can read above the highest sample value; True Peak shows why.
- Pass every channel.
lufsMultichannel()sums the channels the way the standard does, so a stereo file reads its loudness as a pair. Passing onlygetChannelData(0)would measure the left channel alone. - The sample rate is the decoded one.
decodeAudioDataresamples to the context's rate (often 44.1 or 48 kHz), which is the rate the measurement must be given, so the block readsdecoded.sampleRaterather than the file's own. - Long files. The calls run synchronously on the thread that makes them. For whole albums, make them inside a Web Worker; Web Worker Usage has the setup.
The demo meters a clip as it plays: integrated LUFS, true peak and loudness range side by side.
Related
- LUFS: what integrated loudness measures and how gating works.
- True Peak: why the true peak can exceed the sample peak, and what ceiling to use.
- Normalize Audio to -14 LUFS in Python: once you have the number, bring a file to a target loudness.
- Check Loudness and True Peak in CI: the same numbers as a pass/fail gate.