cc-4105: added replaygain and a whole ton of formatting

This commit is contained in:
Rudi Grinberg 2012-08-08 11:04:56 -04:00
parent a8b9b300e5
commit 6f82fa103a
2 changed files with 22 additions and 11 deletions

View File

@ -3,6 +3,8 @@ import mutagen
import math import math
import os import os
import copy import copy
import media.update.replaygain as gain
from media.monitor.exceptions import BadSongFile from media.monitor.exceptions import BadSongFile
from media.monitor.log import Loggable from media.monitor.log import Loggable
import media.monitor.pure as mmp import media.monitor.pure as mmp
@ -16,8 +18,9 @@ list of supported easy tags in mutagen version 1.20
'titlesort', 'discsubtitle', 'website', 'musicip_fingerprint', 'conductor', 'titlesort', 'discsubtitle', 'website', 'musicip_fingerprint', 'conductor',
'compilation', 'barcode', 'performer:*', 'composersort', 'musicbrainz_discid', 'compilation', 'barcode', 'performer:*', 'composersort', 'musicbrainz_discid',
'musicbrainz_albumtype', 'genre', 'isrc', 'discnumber', 'musicbrainz_trmid', 'musicbrainz_albumtype', 'genre', 'isrc', 'discnumber', 'musicbrainz_trmid',
'replaygain_*_gain', 'musicip_puid', 'artist', 'title', 'bpm', 'musicbrainz_trackid', 'replaygain_*_gain', 'musicip_puid', 'artist', 'title', 'bpm',
'arranger', 'albumsort', 'replaygain_*_peak', 'organization'] 'musicbrainz_trackid', 'arranger', 'albumsort', 'replaygain_*_peak',
'organization']
""" """
airtime2mutagen = { airtime2mutagen = {
@ -46,10 +49,14 @@ airtime2mutagen = {
# airtime metadata object will skip the attribute outright. # airtime metadata object will skip the attribute outright.
airtime_special = { airtime_special = {
"MDATA_KEY_DURATION" : lambda m: format_length(getattr(m.info, u'length', 0.0)), "MDATA_KEY_DURATION" :
"MDATA_KEY_BITRATE" : lambda m: getattr(m.info, "bitrate", 0), lambda m: format_length(getattr(m.info, u'length', 0.0)),
"MDATA_KEY_SAMPLERATE" : lambda m: getattr(m.info, u'sample_rate', 0), "MDATA_KEY_BITRATE" :
"MDATA_KEY_MIME" : lambda m: m.mime[0] if len(m.mime) > 0 else u'', lambda m: getattr(m.info, "bitrate", 0),
"MDATA_KEY_SAMPLERATE" :
lambda m: getattr(m.info, u'sample_rate', 0),
"MDATA_KEY_MIME" :
lambda m: m.mime[0] if len(m.mime) > 0 else u'',
} }
mutagen2airtime = dict( (v,k) for k,v in airtime2mutagen.iteritems() if isinstance(v, str) ) mutagen2airtime = dict( (v,k) for k,v in airtime2mutagen.iteritems() if isinstance(v, str) )
@ -139,8 +146,9 @@ class Metadata(Loggable):
airtime_key = mutagen2airtime[muta_k] airtime_key = mutagen2airtime[muta_k]
# Apply truncation in the case where airtime_key is in our # Apply truncation in the case where airtime_key is in our
# truncation table # truncation table
muta_v = truncate_to_length(muta_v, truncate_table[airtime_key]) \ muta_v = \
if airtime_key in truncate_table else muta_v truncate_to_length(muta_v, truncate_table[airtime_key])\
if airtime_key in truncate_table else muta_v
self.__metadata[ airtime_key ] = muta_v self.__metadata[ airtime_key ] = muta_v
# Now we extra the special values that are calculated from the mutagen # Now we extra the special values that are calculated from the mutagen
# object itself: # object itself:
@ -152,6 +160,8 @@ class Metadata(Loggable):
self.__metadata = mmp.normalized_metadata(self.__metadata, fpath) self.__metadata = mmp.normalized_metadata(self.__metadata, fpath)
# Now we must load the md5: # Now we must load the md5:
self.__metadata['MDATA_KEY_MD5'] = mmp.file_md5(fpath) self.__metadata['MDATA_KEY_MD5'] = mmp.file_md5(fpath)
self.__metadata['MDATA_KEY_REPLAYGAIN'] = \
gain.calculate_replay_gain(fpath)
def is_recorded(self): def is_recorded(self):
return mmp.is_airtime_recorded( self.__metadata ) return mmp.is_airtime_recorded( self.__metadata )

View File

@ -45,9 +45,10 @@ def duplicate_file(file_path):
return fdst.name return fdst.name
def calculate_replay_gain(file_path): def calculate_replay_gain(file_path):
""" This function accepts files of type mp3/ogg/flac and returns a """
calculated ReplayGain value in dB. If the value cannot be calculated for This function accepts files of type mp3/ogg/flac and returns a calculated
some reason, then we default to 0 (Unity Gain). ReplayGain value in dB. If the value cannot be calculated for some reason,
then we default to 0 (Unity Gain).
http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification
""" """