v0.52.0: volume normalization — every song plays at one loudness

Measured once per file with ffmpeg's ebur128 (cached on this machine),
applied as a gain on the output volume; the next songs are measured
ahead and an unmeasured one glides to its level. Toggle in Controls and
Preferences → General.
This commit is contained in:
2026-10-13 13:29:37 -07:00
parent 78338d84a7
commit 6178f5d0f7
8 changed files with 571 additions and 7 deletions
+26 -1
View File
@@ -10,7 +10,7 @@ from PyQt6.QtWidgets import (
from PyQt6.QtCore import Qt, QEvent, QObject, QTimer, pyqtSignal
from PyQt6.QtGui import QAction, QKeySequence
from lintunes import bpm_detect, device_sync, music_folder, theme, url_import
from lintunes import bpm_detect, device_sync, loudness, music_folder, theme, url_import
from lintunes.andtunes import layout as andtunes_layout
from lintunes.andtunes import install as andtunes_install
from lintunes.andtunes import sync as andtunes_sync
@@ -114,6 +114,9 @@ class MainWindow(QMainWindow):
self._lastfm = lastfm
self._prefs_dialog = None
self.player = Player(manager, self)
# Volume normalization measures each song with ffmpeg as it comes up;
# nothing is measured while it's off (the default, and in tests).
self.player.loudness = loudness.LoudnessAnalyzer(parent=self)
# Owns the cast session, if any. Built before the transport bar, which
# takes it to drive the little cast indicator under the volume slider.
self._cast = CastController(self.player, self)
@@ -367,7 +370,21 @@ class MainWindow(QMainWindow):
# ---- preferences / theming ----
def _apply_normalize_pref(self):
on = bool(self._prefs.get("normalize_volume", False))
if on and not loudness.ffmpeg_available():
on = False
self.statusBar().showMessage(
"Volume normalization needs ffmpeg, which isn't installed.",
8000)
self.player.set_normalize(on)
if self._normalize_action.isChecked() != on:
self._normalize_action.blockSignals(True)
self._normalize_action.setChecked(on)
self._normalize_action.blockSignals(False)
def _on_prefs_changed(self):
self._apply_normalize_pref()
theme.apply_theme(QApplication.instance(), self._prefs)
self.apply_ui_metrics()
self._transport.refresh_theme()
@@ -441,6 +458,13 @@ class MainWindow(QMainWindow):
self._add_action(controls, "Play/Pause", "", self.play_pause)
self._add_action(controls, "Next", "", self.player.next)
self._add_action(controls, "Previous", "", self.player.previous)
controls.addSeparator()
self._normalize_action = QAction("Normalize Volume", self)
self._normalize_action.setCheckable(True)
self._normalize_action.toggled.connect(
lambda on: self._prefs.set("normalize_volume", on))
controls.addAction(self._normalize_action)
self._apply_normalize_pref()
view_menu = bar.addMenu("&View")
self._add_action(view_menu, "Toggle Column Browser", "Ctrl+B",
@@ -2361,5 +2385,6 @@ class MainWindow(QMainWindow):
self._cassette.shutdown()
self._cast.shutdown()
self.player.shutdown()
self.player.loudness.save()
self._manager.flush()
super().closeEvent(event)