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.
sonare mastering-normalize-loudness mix.wav --target-lufs -14 --ceiling-dbtp -1 -o mix-14lufs.wav --jsonimport { 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);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). Thesonarecommand 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
# 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.
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_limitedisTruein that case, andresult.output_lufsreports 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.
Related
- Audio Mastering in the Browser: the full mastering chain, with EQ, dynamics and reference matching, in a page you can try.
- Python Mastering and Mixing API:
mastering(), the preset chains and the streaming chain. - LUFS: what integrated loudness measures.