Skip to content

Normalize Audio to -14 LUFS in Python ​

To bring a file to -14 LUFS from a script, load it with libsonare.Audio.from_file and call mastering(target_lufs=-14.0, ceiling_dbtp=-1.0). The call measures the integrated loudness, applies the gain that lands it on the target, and limits any true peak that would cross -1 dBTP. The CLI and Node do the same job with one call each; the tabs below show all three, and the Python script also writes the result as a 16-bit WAV.

bash
sonare mastering-normalize-loudness mix.wav --target-lufs -14 --ceiling-dbtp -1 -o mix-14lufs.wav --json
typescript
import { writeFileSync } from 'node:fs';
import { Audio } from '@libraz/libsonare-native';

const audio = Audio.fromFile('mix.wav');
const result = audio.masteringNormalizeLoudness({ targetLufs: -14, ceilingDbtp: -1 });
console.log(`${result.inputLufs.toFixed(1)} LUFS in, ${result.outputLufs.toFixed(1)} LUFS out`);

const n = result.samples.length;
const wav = Buffer.alloc(44 + n * 2);
wav.write('RIFF', 0);
wav.writeUInt32LE(36 + n * 2, 4);
wav.write('WAVEfmt ', 8);
wav.writeUInt32LE(16, 16);
wav.writeUInt16LE(1, 20);
wav.writeUInt16LE(1, 22);
wav.writeUInt32LE(result.sampleRate, 24);
wav.writeUInt32LE(result.sampleRate * 2, 28);
wav.writeUInt16LE(2, 32);
wav.writeUInt16LE(16, 34);
wav.write('data', 36);
wav.writeUInt32LE(n * 2, 40);
result.samples.forEach((v, i) => wav.writeInt16LE(Math.round(Math.max(-1, Math.min(1, v)) * 32767), 44 + i * 2));
writeFileSync('mix-14lufs.wav', wav);
python
import wave

import numpy as np

import libsonare

audio = libsonare.Audio.from_file("mix.wav")
result = audio.mastering(target_lufs=-14.0, ceiling_dbtp=-1.0)
print(f"{result.input_lufs:.1f} LUFS in, {result.output_lufs:.1f} LUFS out")

pcm = np.clip(result.samples, -1.0, 1.0)
with wave.open("mix-14lufs.wav", "wb") as out:
    out.setnchannels(1)
    out.setsampwidth(2)
    out.setframerate(result.sample_rate)
    out.writeframes((pcm * 32767).round().astype("<i2").tobytes())

In the browser the same call is masteringNormalizeLoudness({ samples, sampleRate, targetLufs: -14, ceilingDbtp: -1 }), which returns the processed samples without touching a file.

The CLI keeps the channel layout of the input. In Node and Python, Audio holds one channel, so a stereo file is downmixed to mono on load and the output is mono. To keep a stereo pair there, load each channel with Audio.from_file(path, channel_index=0) and channel_index=1 and run them through master_audio_stereo, which applies one gain to both; see Presets and full chains.

What you need

  • Install: pip install libsonare (Python 3.11 or later; NumPy comes with it). The sonare command ships with it; Node uses @libraz/libsonare-native.
  • An audio file: WAV or MP3. Other formats need a build with FFmpeg.
  • To reproduce this page: the mix below is synthetic, so the result can be checked.

Check it on a quiet mix ​

The script writes mix.wav: 20 s of an A minor chord with a kick on every half second, 44.1 kHz mono, far quieter than -14 LUFS. Run it, then run one of the tabs above in the same folder.

A script that writes the test mix
python
# Writes mix.wav: 20 s of quiet chords and a kick, 44.1 kHz mono, well under -14 LUFS.
import wave

import numpy as np

rate = 44100
t = np.arange(20 * rate) / rate
mix = np.zeros_like(t)
for midi in (57, 60, 64, 45):
    f = 440 * 2 ** ((midi - 69) / 12)
    mix += 0.04 * (np.sin(2 * np.pi * f * t) + 0.4 * np.sin(4 * np.pi * f * t))
beat = (t % 0.5) < 0.12
local = t % 0.5
mix += np.where(beat, 0.3 * np.sin(2 * np.pi * 60 * local) * np.exp(-20 * local), 0.0)

with wave.open("mix.wav", "wb") as out:
    out.setnchannels(1)
    out.setsampwidth(2)
    out.setframerate(rate)
    out.writeframes((np.clip(mix, -1, 1) * 32767).round().astype("<i2").tobytes())

mix-14lufs.wav measures -14 LUFS integrated, within 0.5 LU (-14.2 in this run), with its true peak no more than 0.1 dB over the -1 dBTP ceiling. Check any output yourself with libsonare.Audio.from_file("mix-14lufs.wav").lufs().integrated_lufs and .true_peak_dbtp().

The demo meters a clip as it plays: integrated LUFS, true peak and loudness range are the numbers this page targets.

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

Choosing the numbers ​

  • Why -14 LUFS. It is a balanced streaming-style target for many music releases; -16 suits speech, podcasts, or more headroom. Streaming platforms normalize playback loudness, so aiming higher mostly makes the limiter work harder. Delivery Targets compares the targets.
  • Why -1 dBTP. A platform's lossy encoder can push peaks back up after the limiter, so the ceiling leaves headroom below full scale. True-Peak Ceiling covers when to go lower or higher.
  • When the target cannot be reached. A dense, already-loud mix may hit the ceiling before it reaches -14. The limiter then holds the peaks and the output reads under the target; result.loudness_target_limited is True in that case, and result.output_lufs reports what was reached.

The limiter's job is easy to hear. The demo drives a clip into a -1 dBTP ceiling: the input climbs past full scale while the output stays at the ceiling and the gain reduction takes up the difference.

MAXIMIZER · TRUE PEAK LIMITERIDLE
True Peak limiter — drive in, ceiling out

The band clip driven into maximizer.truePeakLimiter with a −1 dBTP ceiling. Raise the drive: the input (rose) climbs past 0 dBFS, the output (teal) stays at −1.0 dBTP at every setting, and the gain reduction below deepens to take up the difference. The loudness bought shrinks as you push: 18 dB of drive raises the output RMS by only about 9 dB, and the rest is peaks pressed flat. Press play to hear the limited output.

Drive (input gain)
9 dB