Skip to content

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 ​

  • integratedLufs is 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 returns loudnessRangeLu in 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 only getChannelData(0) would measure the left channel alone.
  • The sample rate is the decoded one. decodeAudioData resamples to the context's rate (often 44.1 or 48 kHz), which is the rate the measurement must be given, so the block reads decoded.sampleRate rather 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.

METERS · LOUDNESSIDLE
Loudness metering — LUFS, true-peak, and range

The bar tracks momentary loudness as the clip plays; the panel is the loudness over time. Integrated LUFS is the single overall number, true-peak estimates peaks between samples, and LRA captures how much the loudness moves. Switch the window to compare the fast momentary meter with the smoother short-term one — each reports only once its window has filled, so the short-term contour starts three seconds into the clip.

Window