Skip to content

feat: even out the volume from song to song - #78

Open
madeofpendletonwool wants to merge 1 commit into
mainfrom
feat/volume-normalization
Open

madeofpendletonwool wants to merge 1 commit into
mainfrom
feat/volume-normalization

Conversation

@madeofpendletonwool

Copy link
Copy Markdown
Owner

Why

The volume varies a lot from song to song. Each track plays at the level it was mastered at, and in this library that ranges from about −21 to −7 LUFS.

What

Server: the analysis now measures each song's integrated loudness (server/internal/analysis/loudness.go) using ITU-R BS.1770 / EBU R128: K-weighting, 400 ms blocks, then the −70 LUFS absolute gate and the −10 LU relative gate. It runs on the mono 22.05 kHz signal the beat analysis already decodes, so it adds no extra decode. The result is exposed as BeatMap.lufs.

  • Calibrated against ffmpeg's ebur128 on 7 real songs, from a 60s single to modern masters: within ±0.7 LU. The mono mix reads about 0.8 LU under a stereo meter, and stereoLU adds that back.
  • analysis.Version goes to 2, so existing maps are recomputed when a song is next queued or played.

Web: web/src/lib/loudness.ts turns loud songs down to −16 LUFS (Apple Music's level), cutting at most 12 dB. The speaker applies this to each <audio> element when it loads or preloads a song. If the map arrives once the song is already playing, the change eases in over 2 s.

Choices and limits

  • Volume only goes down. <audio> volume tops out at 1, and Web Audio is avoided on purpose (mobile browsers suspend it in the background). Songs quieter than −16 stay as they are. In an 18-track sample spanning the Beatles to Daft Punk, about 90% were above −16, so most get evened out.
  • iOS ignores el.volume, so an iPhone acting as the speaker is unchanged. The box, desktops and Android work.
  • Remote playback (Spotify) isn't touched, since it plays on Spotify's own device.
  • No per-device off switch yet.

Testing

  • loudness_test.go: the BS.1770 sine reference, level tracking, gating of surrounding silence, bass weighting, and silence reading as no loudness.
  • loudness.test.ts: the gain maths.
  • Server analysis and httpapi tests pass, lint is clean, and all 329 web tests pass.
  • Not yet heard on a deployed speaker.

Songs play at whatever level they were mastered at, so the room's volume
jumps from track to track. The analysis now measures each song's
integrated loudness (ITU-R BS.1770: K-weighted, gated) alongside its beat
map, and the speaker turns loud songs down to -16 LUFS.

- Measured on the mono mix the analysis already decodes, calibrated
  against ffmpeg's ebur128 on real songs: within 0.7 LU.
- Beat map version 2, so songs are measured again when next played.
- The speaker sets each <audio> element's volume when it loads or
  preloads a song, easing it in if the map arrives once the song's
  under way. Volume can only go down, so songs under -16 LUFS stay as
  they are; at most 12 dB off.
- iOS ignores a page's volume, so iPhones as speakers are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant