Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaygain2: support writing both standard and R128 tags to Opus files #381

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions plugins/replaygain2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,9 +81,6 @@
"a"
)

OPUS_MODE_STANDARD = 0
OPUS_MODE_R128 = 1

SUPPORTED_FORMATS = (
AiffFile,
ASFFile,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -251,15 +256,19 @@ 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(
file.metadata,
results[i],
album_result,
isinstance(track, NonAlbumTrack),
opus_r128 and isinstance(file, OggOpusFile)
opus_mode
)


Expand Down Expand Up @@ -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)
]

Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions plugins/replaygain2/ui_options_replaygain2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugins/replaygain2/ui_options_replaygain2.ui
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
<number>-1</number>
</property>
<property name="maxCount">
<number>2</number>
<number>3</number>
</property>
</widget>
</item>
Expand Down
Loading