From 0ec78c25c8d6daee2858725d6619dc89283a099b Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Sun, 8 Sep 2024 13:33:11 +0200 Subject: [PATCH] replaygain2: support writing both standard and R128 tags to Opus files --- plugins/replaygain2/__init__.py | 32 ++++++++++++------- plugins/replaygain2/ui_options_replaygain2.py | 4 +-- plugins/replaygain2/ui_options_replaygain2.ui | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/plugins/replaygain2/__init__.py b/plugins/replaygain2/__init__.py index d82811a7..e84cb4d4 100755 --- a/plugins/replaygain2/__init__.py +++ b/plugins/replaygain2/__init__.py @@ -30,11 +30,12 @@ This plugin is based on the original ReplayGain plugin by Philipp Wolfer and Sophist. ''' -PLUGIN_VERSION = "1.6" +PLUGIN_VERSION = "1.7" PLUGIN_API_VERSIONS = ["2.0"] PLUGIN_LICENSE = "GPL-2.0" PLUGIN_LICENSE_URL = "https://www.gnu.org/licenses/gpl-2.0.html" +from enum import IntEnum from functools import partial import subprocess # nosec: B404 import shutil @@ -80,9 +81,6 @@ "a" ) -OPUS_MODE_STANDARD = 0 -OPUS_MODE_R128 = 1 - SUPPORTED_FORMATS = ( AiffFile, ASFFile, @@ -123,6 +121,13 @@ "r128_track_gain" ) + +class OpusMode(IntEnum): + STANDARD = 0 + R128 = 1 + BOTH = 2 + + # Make sure the rsgain executable exists def rsgain_found(rsgain_command, window): if not os.path.exists(rsgain_command) and shutil.which(rsgain_command) is None: @@ -161,18 +166,18 @@ def format_r128(result, config): gain += float(-23 - config.setting["target_loudness"]) return str(int(round(gain * 256.0))) -def update_metadata(metadata, track_result, album_result, is_nat, r128_tags): +def update_metadata(metadata, track_result, album_result, is_nat, opus_mode): for tag in TAGS: metadata.delete(tag) # Opus R128 tags - if r128_tags: + if opus_mode in (OpusMode.R128, OpusMode.BOTH): metadata.set("r128_track_gain", format_r128(track_result, config)) if album_result is not None: metadata.set("r128_album_gain", format_r128(album_result, config)) # Standard ReplayGain tags - else: + if opus_mode in (OpusMode.STANDARD, OpusMode.BOTH): metadata.set("replaygain_track_gain", track_result["gain"] + " dB") metadata.set("replaygain_track_peak", track_result["peak"]) if config.setting["album_tags"]: @@ -251,7 +256,11 @@ def calculate_replaygain(tracks, options): results.append(result) # Update track metadata with results - opus_r128 = config.setting["opus_mode"] == OPUS_MODE_R128 + if isinstance(file, OggOpusFile): + opus_mode = config.setting["opus_mode"] + else: + opus_mode = OpusMode.STANDARD + for i, track in enumerate(valid_tracks): for file in track.files: update_metadata( @@ -259,7 +268,7 @@ def calculate_replaygain(tracks, options): results[i], album_result, isinstance(track, NonAlbumTrack), - opus_r128 and isinstance(file, OggOpusFile) + opus_mode ) @@ -366,7 +375,7 @@ class ReplayGain2OptionsPage(OptionsPage): IntOption("setting", "target_loudness", -18), IntOption("setting", "clip_mode", CLIP_MODE_POSITIVE), IntOption("setting", "max_peak", 0), - IntOption("setting", "opus_mode", OPUS_MODE_STANDARD), + IntOption("setting", "opus_mode", OpusMode.STANDARD), BoolOption("setting", "opus_m23", False) ] @@ -381,7 +390,8 @@ def __init__(self, parent=None): ]) self.ui.opus_mode.addItems([ "Write standard ReplayGain tags", - "Write R128_*_GAIN tags" + "Write R128_*_GAIN tags", + "Write both standard and R128 tags" ]) self.ui.rsgain_command_browse.clicked.connect(self.rsgain_command_browse) diff --git a/plugins/replaygain2/ui_options_replaygain2.py b/plugins/replaygain2/ui_options_replaygain2.py index 2ab5a9ea..ace7e5fc 100755 --- a/plugins/replaygain2/ui_options_replaygain2.py +++ b/plugins/replaygain2/ui_options_replaygain2.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'plugins/replaygain2/ui_options_replaygain2.ui' # -# Created by: PyQt5 UI code generator 5.15.7 +# Created by: PyQt5 UI code generator 5.15.10 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. @@ -130,7 +130,7 @@ def setupUi(self, ReplayGain2OptionsPage): sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.opus_mode.sizePolicy().hasHeightForWidth()) self.opus_mode.setSizePolicy(sizePolicy) - self.opus_mode.setMaxCount(2) + self.opus_mode.setMaxCount(3) self.opus_mode.setObjectName("opus_mode") self.vboxlayout1.addWidget(self.opus_mode) self.opus_m23 = QtWidgets.QCheckBox(self.replay_gain) diff --git a/plugins/replaygain2/ui_options_replaygain2.ui b/plugins/replaygain2/ui_options_replaygain2.ui index 68876577..0f5f0d3c 100755 --- a/plugins/replaygain2/ui_options_replaygain2.ui +++ b/plugins/replaygain2/ui_options_replaygain2.ui @@ -242,7 +242,7 @@ -1 - 2 + 3