def track_distance(item, track_info, track_index=None, incl_artist=False): """Determines the significance of a track metadata change. Returns a float in [0.0,1.0]. `track_index` is the track number of the `track_info` metadata set. If `track_index` is provided and item.track is set, then these indices are used as a component of the distance calculation. `incl_artist` indicates that a distance component should be included for the track artist (i.e., for various-artist releases). """ # Distance and normalization accumulators. dist, dist_max = 0.0, 0.0 # Check track length. if not track_info.length: # If there's no length to check, assume the worst. dist += TRACK_LENGTH_WEIGHT else: diff = abs(item.length - track_info.length) diff = max(diff - TRACK_LENGTH_GRACE, 0.0) diff = min(diff, TRACK_LENGTH_MAX) dist += (diff / TRACK_LENGTH_MAX) * TRACK_LENGTH_WEIGHT dist_max += TRACK_LENGTH_WEIGHT # Track title. dist += string_dist(item.title, track_info.title) * TRACK_TITLE_WEIGHT dist_max += TRACK_TITLE_WEIGHT # Track artist, if included. # Attention: MB DB does not have artist info for all compilations, # so only check artist distance if there is actually an artist in # the MB track data. if incl_artist and track_info.artist: dist += string_dist(item.artist, track_info.artist) * \ TRACK_ARTIST_WEIGHT dist_max += TRACK_ARTIST_WEIGHT # Track index. if track_index and item.track: if track_index != item.track: dist += TRACK_INDEX_WEIGHT dist_max += TRACK_INDEX_WEIGHT # MusicBrainz track ID. if item.mb_trackid: if item.mb_trackid != track_info.track_id: dist += TRACK_ID_WEIGHT dist_max += TRACK_ID_WEIGHT # Plugin distances. plugin_d, plugin_dm = plugins.track_distance(item, track_info) dist += plugin_d dist_max += plugin_dm return dist / dist_max
def track_distance(item, track_info, incl_artist=False): """Determines the significance of a track metadata change. Returns a float in [0.0,1.0]. `incl_artist` indicates that a distance component should be included for the track artist (i.e., for various-artist releases). """ # Distance and normalization accumulators. dist, dist_max = 0.0, 0.0 # Check track length. # If there's no length to check, apply no penalty. if track_info.length: diff = abs(item.length - track_info.length) diff = max(diff - TRACK_LENGTH_GRACE, 0.0) diff = min(diff, TRACK_LENGTH_MAX) dist += (diff / TRACK_LENGTH_MAX) * TRACK_LENGTH_WEIGHT dist_max += TRACK_LENGTH_WEIGHT # Track title. dist += string_dist(item.title, track_info.title) * TRACK_TITLE_WEIGHT dist_max += TRACK_TITLE_WEIGHT # Track artist, if included. # Attention: MB DB does not have artist info for all compilations, # so only check artist distance if there is actually an artist in # the MB track data. if incl_artist and track_info.artist and \ item.artist.lower() not in VA_ARTISTS: dist += string_dist(item.artist, track_info.artist) * \ TRACK_ARTIST_WEIGHT dist_max += TRACK_ARTIST_WEIGHT # Track index. if track_info.index and item.track: if item.track not in (track_info.index, track_info.medium_index): dist += TRACK_INDEX_WEIGHT dist_max += TRACK_INDEX_WEIGHT # MusicBrainz track ID. if item.mb_trackid: if item.mb_trackid != track_info.track_id: dist += TRACK_ID_WEIGHT dist_max += TRACK_ID_WEIGHT # Plugin distances. plugin_d, plugin_dm = plugins.track_distance(item, track_info) dist += plugin_d dist_max += plugin_dm return dist / dist_max