| 1 |
from abc import ABC, abstractmethod |
| 2 |
from typing import Any, ClassVar |
| 3 |
|
| 4 |
import pytest |
| 5 |
from mediafile import MediaFile |
| 6 |
|
| 7 |
from beets.test.helper import ( |
| 8 |
AsIsImporterMixin, |
| 9 |
ImportHelper, |
| 10 |
PluginMixin, |
| 11 |
has_program, |
| 12 |
) |
| 13 |
from beetsplug.replaygain import ( |
| 14 |
FatalGstreamerPluginReplayGainError, |
| 15 |
GStreamerBackend, |
| 16 |
MetaflacBackend, |
| 17 |
) |
| 18 |
|
| 19 |
try: |
| 20 |
import gi |
| 21 |
|
| 22 |
gi.require_version("Gst", "1.0") |
| 23 |
GST_AVAILABLE = True |
| 24 |
except (ImportError, ValueError): |
| 25 |
GST_AVAILABLE = False |
| 26 |
|
| 27 |
|
| 28 |
GAIN_PROG = next( |
| 29 |
( |
| 30 |
cmd |
| 31 |
for cmd in ["mp3gain", "mp3rgain", "aacgain"] |
| 32 |
if has_program(cmd, ["-v"]) |
| 33 |
), |
| 34 |
None, |
| 35 |
) |
| 36 |
|
| 37 |
FFMPEG_AVAILABLE = has_program("ffmpeg", ["-version"]) |
| 38 |
|
| 39 |
METAFLAC_AVAILABLE = has_program("metaflac", ["--version"]) |
| 40 |
|
| 41 |
|
| 42 |
def reset_replaygain(item): |
| 43 |
item["rg_track_peak"] = None |
| 44 |
item["rg_track_gain"] = None |
| 45 |
item["rg_album_gain"] = None |
| 46 |
item["rg_album_gain"] = None |
| 47 |
item["r128_track_gain"] = None |
| 48 |
item["r128_album_gain"] = None |
| 49 |
item.write() |
| 50 |
item.store() |
| 51 |
|
| 52 |
|
| 53 |
class ReplayGainPluginHelper(PluginMixin, ImportHelper): |
| 54 |
db_on_disk = True |
| 55 |
plugin = "replaygain" |
| 56 |
preload_plugin = False |
| 57 |
|
| 58 |
plugin_config: ClassVar[dict[str, Any]] |
| 59 |
|
| 60 |
@property |
| 61 |
def backend(self): |
| 62 |
return self.plugin_config["backend"] |
| 63 |
|
| 64 |
def setup_beets(self): |
| 65 |
# Implemented by Mixins, see above. This may decide to skip the test. |
| 66 |
self.test_backend() |
| 67 |
|
| 68 |
super().setup_beets() |
| 69 |
self.config["replaygain"].set(self.plugin_config) |
| 70 |
|
| 71 |
self.load_plugins() |
| 72 |
|
| 73 |
|
| 74 |
class ThreadedImportMixin: |
| 75 |
def setup_beets(self): |
| 76 |
super().setup_beets() |
| 77 |
self.config["threaded"] = True |
| 78 |
|
| 79 |
|
| 80 |
class BackendMixin(ABC): |
| 81 |
plugin_config: ClassVar[dict[str, Any]] |
| 82 |
has_r128_support: bool |
| 83 |
|
| 84 |
@abstractmethod |
| 85 |
def test_backend(self): |
| 86 |
"""Check whether the backend actually has all required functionality.""" |
| 87 |
|
| 88 |
|
| 89 |
class GstBackendMixin(BackendMixin): |
| 90 |
plugin_config: ClassVar[dict[str, Any]] = {"backend": "gstreamer"} |
| 91 |
has_r128_support = True |
| 92 |
|
| 93 |
def test_backend(self): |
| 94 |
"""Check whether the backend actually has all required functionality.""" |
| 95 |
try: |
| 96 |
# Check if required plugins can be loaded by instantiating a |
| 97 |
# GStreamerBackend (via its .__init__). |
| 98 |
self.config["replaygain"]["targetlevel"] = 89 |
| 99 |
GStreamerBackend(self.config["replaygain"], None) |
| 100 |
except FatalGstreamerPluginReplayGainError as e: |
| 101 |
# Skip the test if plugins could not be loaded. |
| 102 |
pytest.skip(str(e)) |
| 103 |
|
| 104 |
|
| 105 |
class CmdBackendMixin(BackendMixin): |
| 106 |
plugin_config: ClassVar[dict[str, Any]] = { |
| 107 |
"backend": "command", |
| 108 |
"command": GAIN_PROG, |
| 109 |
} |
| 110 |
has_r128_support = False |
| 111 |
|
| 112 |
|
| 113 |
class FfmpegBackendMixin(BackendMixin): |
| 114 |
plugin_config: ClassVar[dict[str, Any]] = {"backend": "ffmpeg"} |
| 115 |
has_r128_support = True |
| 116 |
|
| 117 |
|
| 118 |
class MetaflacBackendMixin(BackendMixin): |
| 119 |
plugin_config: ClassVar[dict[str, Any]] = {"backend": "metaflac"} |
| 120 |
has_r128_support = False |
| 121 |
|
| 122 |
def test_backend(self): |
| 123 |
"""Skip the test when the metaflac tool is not installed.""" |
| 124 |
if not METAFLAC_AVAILABLE: |
| 125 |
pytest.skip("metaflac cannot be found") |
| 126 |
|
| 127 |
|
| 128 |
class ReplayGainCliTest: |
| 129 |
FNAME: str |
| 130 |
|
| 131 |
def _add_album(self, *args, **kwargs): |
| 132 |
# Use a file with non-zero volume (most test assets are total silence) |
| 133 |
album = self.add_album_fixture(*args, fname=self.FNAME, **kwargs) |
| 134 |
for item in album.items(): |
| 135 |
reset_replaygain(item) |
| 136 |
|
| 137 |
return album |
| 138 |
|
| 139 |
def test_cli_saves_track_gain(self): |
| 140 |
self._add_album(2) |
| 141 |
|
| 142 |
for item in self.lib.items(): |
| 143 |
assert item.rg_track_peak is None |
| 144 |
assert item.rg_track_gain is None |
| 145 |
mediafile = MediaFile(item.path) |
| 146 |
assert mediafile.rg_track_peak is None |
| 147 |
assert mediafile.rg_track_gain is None |
| 148 |
|
| 149 |
self.run_command("replaygain") |
| 150 |
|
| 151 |
# Skip the test if rg_track_peak and rg_track gain is None, assuming |
| 152 |
# that it could only happen if the decoder plugins are missing. |
| 153 |
if all( |
| 154 |
i.rg_track_peak is None and i.rg_track_gain is None |
| 155 |
for i in self.lib.items() |
| 156 |
): |
| 157 |
pytest.skip("decoder plugins could not be loaded.") |
| 158 |
|
| 159 |
for item in self.lib.items(): |
| 160 |
assert item.rg_track_peak is not None |
| 161 |
assert item.rg_track_gain is not None |
| 162 |
mediafile = MediaFile(item.path) |
| 163 |
assert mediafile.rg_track_peak == pytest.approx( |
| 164 |
item.rg_track_peak, abs=1e-6 |
| 165 |
) |
| 166 |
assert mediafile.rg_track_gain == pytest.approx( |
| 167 |
item.rg_track_gain, abs=1e-2 |
| 168 |
) |
| 169 |
|
| 170 |
def test_cli_skips_calculated_tracks(self): |
| 171 |
album_rg = self._add_album(1) |
| 172 |
item_rg = album_rg.items()[0] |
| 173 |
|
| 174 |
if self.has_r128_support: |
| 175 |
album_r128 = self._add_album(1, ext="opus") |
| 176 |
item_r128 = album_r128.items()[0] |
| 177 |
|
| 178 |
self.run_command("replaygain") |
| 179 |
|
| 180 |
item_rg.load() |
| 181 |
assert item_rg.rg_track_gain is not None |
| 182 |
assert item_rg.rg_track_peak is not None |
| 183 |
assert item_rg.r128_track_gain is None |
| 184 |
|
| 185 |
item_rg.rg_track_gain += 1.0 |
| 186 |
item_rg.rg_track_peak += 1.0 |
| 187 |
item_rg.store() |
| 188 |
rg_track_gain = item_rg.rg_track_gain |
| 189 |
rg_track_peak = item_rg.rg_track_peak |
| 190 |
|
| 191 |
if self.has_r128_support: |
| 192 |
item_r128.load() |
| 193 |
assert item_r128.r128_track_gain is not None |
| 194 |
assert item_r128.rg_track_gain is None |
| 195 |
assert item_r128.rg_track_peak is None |
| 196 |
|
| 197 |
item_r128.r128_track_gain += 1.0 |
| 198 |
item_r128.store() |
| 199 |
r128_track_gain = item_r128.r128_track_gain |
| 200 |
|
| 201 |
self.run_command("replaygain") |
| 202 |
|
| 203 |
item_rg.load() |
| 204 |
assert item_rg.rg_track_gain == rg_track_gain |
| 205 |
assert item_rg.rg_track_peak == rg_track_peak |
| 206 |
|
| 207 |
if self.has_r128_support: |
| 208 |
item_r128.load() |
| 209 |
assert item_r128.r128_track_gain == r128_track_gain |
| 210 |
|
| 211 |
def test_cli_does_not_skip_wrong_tag_type(self): |
| 212 |
"""Check that items that have tags of the wrong type won't be skipped.""" |
| 213 |
if not self.has_r128_support: |
| 214 |
# This test is a lot less interesting if the backend cannot write |
| 215 |
# both tag types. |
| 216 |
pytest.skip( |
| 217 |
f"r128 tags for opus not supported on backend {self.backend}" |
| 218 |
) |
| 219 |
|
| 220 |
album_rg = self._add_album(1) |
| 221 |
item_rg = album_rg.items()[0] |
| 222 |
|
| 223 |
album_r128 = self._add_album(1, ext="opus") |
| 224 |
item_r128 = album_r128.items()[0] |
| 225 |
|
| 226 |
item_rg.r128_track_gain = 0.0 |
| 227 |
item_rg.store() |
| 228 |
|
| 229 |
item_r128.rg_track_gain = 0.0 |
| 230 |
item_r128.rg_track_peak = 42.0 |
| 231 |
item_r128.store() |
| 232 |
|
| 233 |
self.run_command("replaygain") |
| 234 |
item_rg.load() |
| 235 |
item_r128.load() |
| 236 |
|
| 237 |
assert item_rg.rg_track_gain is not None |
| 238 |
assert item_rg.rg_track_peak is not None |
| 239 |
# FIXME: Should the plugin null this field? |
| 240 |
# assert item_rg.r128_track_gain is None |
| 241 |
|
| 242 |
assert item_r128.r128_track_gain is not None |
| 243 |
# FIXME: Should the plugin null these fields? |
| 244 |
# assert item_r128.rg_track_gain is None |
| 245 |
# assert item_r128.rg_track_peak is None |
| 246 |
|
| 247 |
def test_cli_saves_album_gain_to_file(self): |
| 248 |
self._add_album(2) |
| 249 |
|
| 250 |
for item in self.lib.items(): |
| 251 |
mediafile = MediaFile(item.path) |
| 252 |
assert mediafile.rg_album_peak is None |
| 253 |
assert mediafile.rg_album_gain is None |
| 254 |
|
| 255 |
self.run_command("replaygain", "-a") |
| 256 |
|
| 257 |
peaks = [] |
| 258 |
gains = [] |
| 259 |
for item in self.lib.items(): |
| 260 |
mediafile = MediaFile(item.path) |
| 261 |
peaks.append(mediafile.rg_album_peak) |
| 262 |
gains.append(mediafile.rg_album_gain) |
| 263 |
|
| 264 |
# Make sure they are all the same |
| 265 |
assert max(peaks) == min(peaks) |
| 266 |
assert max(gains) == min(gains) |
| 267 |
|
| 268 |
assert max(gains) != 0.0 |
| 269 |
assert max(peaks) != 0.0 |
| 270 |
|
| 271 |
def test_cli_writes_only_r128_tags(self): |
| 272 |
if not self.has_r128_support: |
| 273 |
pytest.skip( |
| 274 |
f"r128 tags for opus not supported on backend {self.backend}" |
| 275 |
) |
| 276 |
|
| 277 |
album = self._add_album(2, ext="opus") |
| 278 |
|
| 279 |
self.run_command("replaygain", "-a") |
| 280 |
|
| 281 |
for item in album.items(): |
| 282 |
mediafile = MediaFile(item.path) |
| 283 |
# does not write REPLAYGAIN_* tags |
| 284 |
assert mediafile.rg_track_gain is None |
| 285 |
assert mediafile.rg_album_gain is None |
| 286 |
# writes R128_* tags |
| 287 |
assert mediafile.r128_track_gain is not None |
| 288 |
assert mediafile.r128_album_gain is not None |
| 289 |
|
| 290 |
def test_targetlevel_has_effect(self): |
| 291 |
album = self._add_album(1) |
| 292 |
item = album.items()[0] |
| 293 |
|
| 294 |
def analyse(target_level): |
| 295 |
self.config["replaygain"]["targetlevel"] = target_level |
| 296 |
self.run_command("replaygain", "-f") |
| 297 |
item.load() |
| 298 |
return item.rg_track_gain |
| 299 |
|
| 300 |
gain_relative_to_84 = analyse(84) |
| 301 |
gain_relative_to_89 = analyse(89) |
| 302 |
|
| 303 |
assert gain_relative_to_84 != gain_relative_to_89 |
| 304 |
|
| 305 |
def test_r128_targetlevel_has_effect(self): |
| 306 |
if not self.has_r128_support: |
| 307 |
pytest.skip( |
| 308 |
f"r128 tags for opus not supported on backend {self.backend}" |
| 309 |
) |
| 310 |
|
| 311 |
album = self._add_album(1, ext="opus") |
| 312 |
item = album.items()[0] |
| 313 |
|
| 314 |
def analyse(target_level): |
| 315 |
self.config["replaygain"]["r128_targetlevel"] = target_level |
| 316 |
self.run_command("replaygain", "-f") |
| 317 |
item.load() |
| 318 |
return item.r128_track_gain |
| 319 |
|
| 320 |
gain_relative_to_84 = analyse(84) |
| 321 |
gain_relative_to_89 = analyse(89) |
| 322 |
|
| 323 |
assert gain_relative_to_84 != gain_relative_to_89 |
| 324 |
|
| 325 |
def test_per_disc(self): |
| 326 |
# Use the per_disc option and add a little more concurrency. |
| 327 |
album = self._add_album(track_count=4, disc_count=3) |
| 328 |
self.config["replaygain"]["per_disc"] = True |
| 329 |
self.run_command("replaygain", "-a") |
| 330 |
|
| 331 |
# FIXME: Add fixtures with known track/album gain (within a suitable |
| 332 |
# tolerance) so that we can actually check per-disc operation here. |
| 333 |
for item in album.items(): |
| 334 |
assert item.rg_track_gain is not None |
| 335 |
assert item.rg_album_gain is not None |
| 336 |
|
| 337 |
def test_clears_wrong_tag_type(self): |
| 338 |
"""Check that items that have tags of the wrong type won't be skipped.""" |
| 339 |
if not self.has_r128_support: |
| 340 |
pytest.skip( |
| 341 |
f"r128 tags for opus not supported on backend {self.backend}" |
| 342 |
) |
| 343 |
|
| 344 |
album_rg = self._add_album(1) |
| 345 |
item_rg = album_rg.items()[0] |
| 346 |
|
| 347 |
album_r128 = self._add_album(1, ext="opus") |
| 348 |
item_r128 = album_r128.items()[0] |
| 349 |
|
| 350 |
item_r128.r128_track_gain = 0.0 |
| 351 |
item_r128.store() |
| 352 |
|
| 353 |
item_rg.rg_track_gain = 0.0 |
| 354 |
item_rg.rg_track_peak = 42.0 |
| 355 |
item_rg.store() |
| 356 |
|
| 357 |
self.run_command("replaygain") |
| 358 |
item_rg.load() |
| 359 |
item_r128.load() |
| 360 |
|
| 361 |
assert item_rg.rg_track_gain is not None |
| 362 |
assert item_rg.rg_track_peak is not None |
| 363 |
assert item_rg.r128_track_gain is None |
| 364 |
|
| 365 |
assert item_r128.r128_track_gain is not None |
| 366 |
assert item_r128.rg_track_gain is None |
| 367 |
assert item_r128.rg_track_peak is None |
| 368 |
|
| 369 |
|
| 370 |
@pytest.mark.skipif(not GST_AVAILABLE, reason="gstreamer cannot be found") |
| 371 |
class TestReplayGainGstCli( |
| 372 |
ReplayGainCliTest, ReplayGainPluginHelper, GstBackendMixin |
| 373 |
): |
| 374 |
FNAME = "full" # file contains only silence |
| 375 |
|
| 376 |
|
| 377 |
@pytest.mark.skipif(not GAIN_PROG, reason="no *gain command found") |
| 378 |
class TestReplayGainCmdCli( |
| 379 |
ReplayGainCliTest, ReplayGainPluginHelper, CmdBackendMixin |
| 380 |
): |
| 381 |
FNAME = "full" # file contains only silence |
| 382 |
|
| 383 |
|
| 384 |
@pytest.mark.skipif(not FFMPEG_AVAILABLE, reason="ffmpeg cannot be found") |
| 385 |
class TestReplayGainFfmpegCli( |
| 386 |
ReplayGainCliTest, ReplayGainPluginHelper, FfmpegBackendMixin |
| 387 |
): |
| 388 |
FNAME = "full" # file contains only silence |
| 389 |
|
| 390 |
|
| 391 |
@pytest.mark.skipif(not FFMPEG_AVAILABLE, reason="ffmpeg cannot be found") |
| 392 |
class TestReplayGainFfmpegNoiseCli( |
| 393 |
ReplayGainCliTest, ReplayGainPluginHelper, FfmpegBackendMixin |
| 394 |
): |
| 395 |
FNAME = "whitenoise" |
| 396 |
|
| 397 |
|
| 398 |
@pytest.mark.skipif(not METAFLAC_AVAILABLE, reason="metaflac cannot be found") |
| 399 |
class TestReplayGainMetaflacCli( |
| 400 |
ReplayGainCliTest, ReplayGainPluginHelper, MetaflacBackendMixin |
| 401 |
): |
| 402 |
FNAME = "whitenoise" |
| 403 |
|
| 404 |
def _add_album(self, *args, **kwargs): |
| 405 |
kwargs.setdefault("ext", "flac") |
| 406 |
return super()._add_album(*args, **kwargs) |
| 407 |
|
| 408 |
|
| 409 |
def test_metaflac_backend_parses_replaygain_tags(): |
| 410 |
output = ( |
| 411 |
b"REPLAYGAIN_TRACK_GAIN=-11.55 dB\nREPLAYGAIN_TRACK_PEAK=0.99998772\n" |
| 412 |
) |
| 413 |
tags = MetaflacBackend._parse_tags(output) |
| 414 |
assert MetaflacBackend._parse_gain(tags["REPLAYGAIN_TRACK_GAIN"]) == ( |
| 415 |
pytest.approx(-11.55) |
| 416 |
) |
| 417 |
assert float(tags["REPLAYGAIN_TRACK_PEAK"]) == pytest.approx(0.99998772) |
| 418 |
assert MetaflacBackend._parse_gain("+4.56 dB") == pytest.approx(4.56) |
| 419 |
|
| 420 |
|
| 421 |
class ImportTest(AsIsImporterMixin): |
| 422 |
def test_import_converted(self): |
| 423 |
self.run_asis_importer() |
| 424 |
for item in self.lib.items(): |
| 425 |
# FIXME: Add fixtures with known track/album gain (within a |
| 426 |
# suitable tolerance) so that we can actually check correct |
| 427 |
# operation here. |
| 428 |
assert item.rg_track_gain is not None |
| 429 |
assert item.rg_album_gain is not None |
| 430 |
|
| 431 |
|
| 432 |
@pytest.mark.skipif(not GST_AVAILABLE, reason="gstreamer cannot be found") |
| 433 |
class TestReplayGainGstImport( |
| 434 |
ImportTest, ReplayGainPluginHelper, GstBackendMixin |
| 435 |
): |
| 436 |
pass |
| 437 |
|
| 438 |
|
| 439 |
@pytest.mark.skipif(not GAIN_PROG, reason="no *gain command found") |
| 440 |
class TestReplayGainCmdImport( |
| 441 |
ImportTest, ReplayGainPluginHelper, CmdBackendMixin |
| 442 |
): |
| 443 |
pass |
| 444 |
|
| 445 |
|
| 446 |
@pytest.mark.skipif(not FFMPEG_AVAILABLE, reason="ffmpeg cannot be found") |
| 447 |
class TestReplayGainFfmpegImport( |
| 448 |
ImportTest, ReplayGainPluginHelper, FfmpegBackendMixin |
| 449 |
): |
| 450 |
pass |
| 451 |
|
| 452 |
|
| 453 |
@pytest.mark.skipif(not FFMPEG_AVAILABLE, reason="ffmpeg cannot be found") |
| 454 |
class TestReplayGainFfmpegThreadedImport( |
| 455 |
ThreadedImportMixin, ImportTest, ReplayGainPluginHelper, FfmpegBackendMixin |
| 456 |
): |
| 457 |
pass |