'Finds hash matches that align in time with other matches and finds
consensus about which hashes are "true" signal from the audio.
Returns a dictionary with match information.'

def align_matches(self, matches):

    diff_counter = {}
    largest = 0
    largest_count = 0
    song_id = (-1)
    for tup in matches:
        (sid, diff) = tup
        if (diff not in diff_counter):
            diff_counter[diff] = {}
        if (sid not in diff_counter[diff]):
            diff_counter[diff][sid] = 0
        diff_counter[diff][sid] += 1
        if (diff_counter[diff][sid] > largest_count):
            largest = diff
            largest_count = diff_counter[diff][sid]
            song_id = sid
    song = self.db.get_song_by_id(song_id)
    if song:
        songname = song.get(Dejavu.SONG_NAME, None)
    else:
        return None
    nseconds = round((((float(largest) / fingerprint.DEFAULT_FS) * fingerprint.DEFAULT_WINDOW_SIZE) * fingerprint.DEFAULT_OVERLAP_RATIO), 5)
    song = {Dejavu.SONG_ID: song_id, Dejavu.SONG_NAME: songname, Dejavu.CONFIDENCE: largest_count, Dejavu.OFFSET: int(largest), Dejavu.OFFSET_SECS: nseconds, Database.FIELD_FILE_SHA1: song.get(Database.FIELD_FILE_SHA1, None)}
    return song
