Skip to content

Commit

Permalink
replaygain: R128_ALBUM_GAIN tag format (Q7.8)
Browse files Browse the repository at this point in the history
Store `R129_ALBUM_GAIN`'s value as a Q7.8 fixed-point number as required by IETF
RFC 7845 "Ogg Encapsulation for the Opus Audio Codec" 5.2.1. Tag Definitions.
  • Loading branch information
zsinskri committed Jun 17, 2019
1 parent 8a2bd2f commit f3f75b7
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions beetsplug/replaygain.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def lufs_to_db(db):
return db + 107


def gain_to_q78(gain):
"""Generate a Q7.8 fixed-point gain.
Convert a `Gain` object to the representation used e.g. in OPUS tags.
"""
return int(round(gain.gain * pow(2, 8)))


# Backend base and plumbing classes.

# gain: in LU to reference level
Expand Down Expand Up @@ -1191,25 +1199,23 @@ def store_track_gain(self, item, track_gain):
item.rg_track_gain = track_gain.gain
item.rg_track_peak = track_gain.peak
item.store()

self._log.debug(u'applied track gain {0}, peak {1}',
item.rg_track_gain, item.rg_track_peak)

def store_track_r128_gain(self, item, track_gain):
item.r128_track_gain = int(round(track_gain.gain * pow(2, 8)))
item.store()

self._log.debug(u'applied r128 track gain {0}', item.r128_track_gain)

def store_album_gain(self, item, album_gain):
item.rg_album_gain = album_gain.gain
item.rg_album_peak = album_gain.peak
item.store()
self._log.debug(u'applied album gain {0}, peak {1}',
item.rg_album_gain, item.rg_album_peak)

def store_track_r128_gain(self, item, track_gain):
item.r128_track_gain = gain_to_q78(track_gain)
item.store()
self._log.debug(u'applied r128 track gain {0}', item.r128_track_gain)

def store_album_r128_gain(self, item, album_gain):
item.r128_album_gain = album_gain.gain
item.r128_album_gain = gain_to_q78(album_gain)
item.store()
self._log.debug(u'applied r128 album gain {0}',
item.r128_album_gain)
Expand Down

0 comments on commit f3f75b7

Please sign in to comment.