Skip to content

Commit

Permalink
Support dual_mono
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
mnogu committed May 5, 2021
1 parent ac5cdba commit f19f841
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 38 deletions.
58 changes: 37 additions & 21 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
from aqt.qt import QDialog
from aqt.qt import QDialogButtonBox
from aqt.qt import QGridLayout
from aqt.qt import QGroupBox
from aqt.qt import QHBoxLayout
from aqt.qt import QLabel
from aqt.qt import QMessageBox
from aqt.qt import QSizePolicy
from aqt.qt import QSlider
from aqt.qt import QSpinBox
from aqt.qt import QVBoxLayout
Expand Down Expand Up @@ -66,7 +69,10 @@ def did_begin_playing(player: Any, _: AVTag) -> None:
# How can we retrieve the current value of the af property?
# "player.get_property('af')" always returns "[]"
if volume_config.loudnorm.enabled:
loudnorm_value = 'loudnorm=I=' + str(volume_config.loudnorm.i)
loudnorm_value = 'loudnorm=I={}:dual_mono={}'.format(
volume_config.loudnorm.i,
# True => true, False => false
str(volume_config.loudnorm.dual_mono).lower())
else:
loudnorm_value = ''
player.set_property('af', loudnorm_value)
Expand All @@ -76,6 +82,7 @@ def _create_config_widgets(text: str, min_max: Tuple[int, int]) \
-> Tuple[QLabel, QSlider, QSpinBox]:
label = QLabel()
label.setText(text)
label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)

slider = QSlider()
slider.setOrientation(Qt.Horizontal)
Expand Down Expand Up @@ -106,31 +113,41 @@ def __init__(self, parent: QWidget) -> None:
volume_label, self.volume_slider, self.volume_spin_box = _create_config_widgets(
'Volume', (0, 100))

self.loudnorm_check_box = QCheckBox(
'Enable loudness normalization (mpv only)')
volume_layout = QHBoxLayout()
volume_layout.addWidget(volume_label)
volume_layout.addWidget(self.volume_slider)
volume_layout.addWidget(self.volume_spin_box)

volume_group_box = QGroupBox()
volume_group_box.setLayout(volume_layout)
volume_group_box.setTitle('General')

i_label, self.i_slider, self.i_spin_box = _create_config_widgets(
'Integrated loudness', (-70, -5))
for widget in [i_label, self.i_slider, self.i_spin_box]:
self.loudnorm_check_box.toggled.connect(widget.setEnabled)
self.loudnorm_check_box.toggled.connect(self._show_warning_on_non_mpv)

grid_layout = QGridLayout()
grid_layout.addWidget(volume_label, 0, 0)
grid_layout.addWidget(self.volume_slider, 0, 1)
grid_layout.addWidget(self.volume_spin_box, 0, 2)
grid_layout.addWidget(self.loudnorm_check_box, 1, 0, 1, 3)
grid_layout.addWidget(i_label, 2, 0)
grid_layout.addWidget(self.i_slider, 2, 1)
grid_layout.addWidget(self.i_spin_box, 2, 2)
self.dual_mono_check_box = QCheckBox(
'Treat mono input as dual-mono')

loudnorm_layout = QGridLayout()
loudnorm_layout.addWidget(i_label, 0, 0)
loudnorm_layout.addWidget(self.i_slider, 0, 1)
loudnorm_layout.addWidget(self.i_spin_box, 0, 2)
loudnorm_layout.addWidget(self.dual_mono_check_box, 1, 0, 1, 3)

self.loudnorm_group_box = QGroupBox()
self.loudnorm_group_box.setLayout(loudnorm_layout)
self.loudnorm_group_box.setCheckable(True)
self.loudnorm_group_box.setTitle(
'Loudness Normalization (mpv only)')
self.loudnorm_group_box.toggled.connect(self._show_warning_on_non_mpv)

button_box = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)

layout = QVBoxLayout()
layout.addLayout(grid_layout)
layout.addWidget(volume_group_box)
layout.addWidget(self.loudnorm_group_box)
layout.addStretch()
layout.addWidget(button_box)

Expand Down Expand Up @@ -158,21 +175,20 @@ def show(self) -> None:

loudnorm = volume_config.loudnorm

enabled = loudnorm.enabled
self.loudnorm_check_box.setChecked(enabled)
for widget in [self.i_slider, self.i_spin_box]:
widget.setEnabled(enabled)
self.loudnorm_group_box.setChecked(loudnorm.enabled)

_set_value(loudnorm.i, self.i_slider, self.i_spin_box)
self.dual_mono_check_box.setChecked(loudnorm.dual_mono)

super().show()

def accept(self) -> None:
"""Save the sound volume and hide the dialog window."""
volume_config = config.VolumeConfig()
volume_config.volume = self.volume_slider.value()
volume_config.loudnorm.enabled = self.loudnorm_check_box.isChecked()
volume_config.loudnorm.enabled = self.loudnorm_group_box.isChecked()
volume_config.loudnorm.i = self.i_slider.value()
volume_config.loudnorm.dual_mono = self.dual_mono_check_box.isChecked()

save_config(volume_config)
super().accept()
Expand Down
23 changes: 15 additions & 8 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class LoudnormConfig:
"""The loudnorm filter configuration"""
enabled: bool = False
i: int = -24
dual_mono: bool = False


@dataclass
Expand Down Expand Up @@ -61,15 +62,21 @@ def load_config() -> VolumeConfig:
if value is not None:
volume_config.volume = value

if 'loudnorm' in config:
loudnorm_config = config['loudnorm']
if 'loudnorm' not in config:
return volume_config

loudnorm_config = config['loudnorm']

value = _load_value(loudnorm_config, 'enabled', bool)
if value is not None:
volume_config.loudnorm.enabled = value

value = _load_value(loudnorm_config, 'enabled', bool)
if value is not None:
volume_config.loudnorm.enabled = value
value = _load_value(loudnorm_config, 'i', int)
if value is not None:
volume_config.loudnorm.i = value

value = _load_value(loudnorm_config, 'i', int)
if value is not None:
volume_config.loudnorm.i = value
value = _load_value(loudnorm_config, 'dual_mono', bool)
if value is not None:
volume_config.loudnorm.dual_mono = value

return volume_config
65 changes: 56 additions & 9 deletions test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def test_default(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=False,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -61,7 +62,8 @@ def test_none(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=False,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -73,7 +75,8 @@ def test_empty(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=False,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -87,7 +90,8 @@ def test_valid_volume(self) -> None:
volume=70,
loudnorm=LoudnormConfig(
enabled=False,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -101,7 +105,8 @@ def test_invalid_volume(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=False,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -118,7 +123,8 @@ def test_valid_loudnorm(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=True,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -135,7 +141,8 @@ def test_invalid_loudnorm(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=False,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -153,7 +160,8 @@ def test_valid_loudnorm_i(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=True,
i=-12
i=-12,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand All @@ -171,7 +179,46 @@ def test_invalid_loudnorm_i(self) -> None:
volume=100,
loudnorm=LoudnormConfig(
enabled=True,
i=-24
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)

def test_valid_loudnorm_dual_mono(self) -> None:
"""Test with a valid dual-mono value."""
actual = self._get_config({
'volume': 100,
'loudnorm': {
'enabled': True,
'dual_mono': True
}
})
expected = VolumeConfig(
volume=100,
loudnorm=LoudnormConfig(
enabled=True,
i=-24,
dual_mono=True
)
)
self.assertEqual(actual, expected)

def test_invalid_loudnorm_dual_mono(self) -> None:
"""Test with an invalid dual-mono value."""
actual = self._get_config({
'volume': 100,
'loudnorm': {
'enabled': True,
'dual_mono': 7
}
})
expected = VolumeConfig(
volume=100,
loudnorm=LoudnormConfig(
enabled=True,
i=-24,
dual_mono=False
)
)
self.assertEqual(actual, expected)
Expand Down

0 comments on commit f19f841

Please sign in to comment.