Skip to content

Commit 8fdd025

Browse files
committed
opus: move EbuR128 -> ReplayGain to OpusTags
Logically, the offset belongs to the conversion of Opus Ebu R128 metadata to the internal ReplayGain structures. Movin the code does not change change the logic at all (as addition is commutative), but by moving the conversion closer to the source of the data and avoiding EBU R128-scaled data as far as possible, I hope to reduce the chance of future logic errors.
1 parent 2dbf8a4 commit 8fdd025

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/decoder/plugins/OpusDecoderPlugin.cxx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@ IsOpusTags(const ogg_packet &packet) noexcept
4747
return packet.bytes >= 8 && memcmp(packet.packet, "OpusTags", 8) == 0;
4848
}
4949

50-
/**
51-
* Convert an EBU R128 value to ReplayGain.
52-
*/
53-
static constexpr float
54-
EbuR128ToReplayGain(float ebu_r128) noexcept
55-
{
56-
/* add 5dB to compensate for the different reference levels
57-
between ReplayGain (89dB) and EBU R128 (-23 LUFS) */
58-
return ebu_r128 + 5;
59-
}
60-
6150
static bool
6251
mpd_opus_init([[maybe_unused]] const ConfigBlock &block)
6352
{
@@ -282,11 +271,12 @@ MPDOpusDecoder::HandleTags(const ogg_packet &packet)
282271

283272
if (rgi.IsDefined()) {
284273
/* submit all valid EBU R128 values with output_gain
285-
applied */
274+
applied. conversion from EBU R128 -> ReplayGain
275+
happened in ScanOpusTags already. */
286276
if (rgi.track.IsDefined())
287-
rgi.track.gain += EbuR128ToReplayGain(output_gain);
277+
rgi.track.gain += output_gain;
288278
if (rgi.album.IsDefined())
289-
rgi.album.gain += EbuR128ToReplayGain(output_gain);
279+
rgi.album.gain += output_gain;
290280
client.SubmitReplayGain(&rgi);
291281
submitted_replay_gain = true;
292282
}

src/decoder/plugins/OpusTags.cxx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717

1818
using std::string_view_literals::operator""sv;
1919

20+
/**
21+
* Convert an EBU R128 value to ReplayGain.
22+
*/
23+
static constexpr float
24+
EbuR128ToReplayGain(float ebu_r128) noexcept
25+
{
26+
/* add 5dB to compensate for the different reference levels
27+
between ReplayGain (89dB) and EBU R128 (-23 LUFS) */
28+
return ebu_r128 + 5;
29+
}
30+
2031
[[gnu::pure]]
2132
static TagType
2233
ParseOpusTagName(std::string_view name) noexcept
@@ -47,14 +58,14 @@ ScanOneOpusTag(std::string_view name, std::string_view value,
4758
dB */
4859

4960
if (const auto i = ParseInteger<int_least32_t>(value))
50-
rgi->track.gain = float(*i) / 256.0f;
61+
rgi->track.gain = EbuR128ToReplayGain(float(*i) / 256.0f);
5162
} else if (rgi != nullptr &&
5263
StringIsEqualIgnoreCase(name, "R128_ALBUM_GAIN"sv)) {
5364
/* R128_ALBUM_GAIN is a Q7.8 fixed point number in
5465
dB */
5566

5667
if (const auto i = ParseInteger<int_least32_t>(value))
57-
rgi->album.gain = float(*i) / 256.0f;
68+
rgi->album.gain = EbuR128ToReplayGain(float(*i) / 256.0f);
5869
}
5970

6071
handler.OnPair(name, value);

0 commit comments

Comments
 (0)