def write_tempo(tempi, filename, mirex=False): """ Write the most dominant tempi and the relative strength to a file. :param tempi: array with the detected tempi and their strengths :param filename: output file name or file handle :param mirex: report the lower tempo first (as required by MIREX) :return: the most dominant tempi and the relative strength """ from madmom.utils import open # default values t1, t2, strength = 0., 0., 1. # only one tempo was detected if len(tempi) == 1: t1 = tempi[0][0] # generate a fake second tempo # the boundary of 68 bpm is taken from Tzanetakis 2013 ICASSP paper if t1 < 68: t2 = t1 * 2. else: t2 = t1 / 2. # consider only the two strongest tempi and strengths elif len(tempi) > 1: t1, t2 = tempi[:2, 0] strength = tempi[0, 1] / sum(tempi[:2, 1]) # for MIREX, the lower tempo must be given first if mirex and t1 > t2: t1, t2, strength = t2, t1, 1. - strength # write to output with open(filename, 'wb') as f: f.write("%.2f\t%.2f\t%.2f\n" % (t1, t2, strength)) # also return the tempi & strength return t1, t2, strength
def load_notes(filename): """ Load the target notes from a file. :param filename: input file name or file handle :return: numpy array with notes """ with open(filename, 'rb') as f: return np.loadtxt(f)
def write_notes(notes, filename, sep='\t'): """ Write the detected notes to a file. :param notes: list with notes :param filename: output file name or file handle :param sep: separator for the fields [default='\t'] """ # write the notes to the output if filename is not None: with open(filename, 'wb') as f: for note in notes: f.write(sep.join([str(x) for x in note]) + '\n') # also return them return notes
def write_frequencies(notes, filename, note_length=0.6): """ Write the frequencies of the notes to file (i.e. MIREX format). :param notes: detected notes :param filename: output filename :param note_length: default length of the notes :return: notes """ from madmom.audio.filters import midi2hz # MIREX format: onset \t offset \t frequency with open(filename, 'wb') as f: for note in notes: onset, midi_note = note offset = onset + note_length frequency = midi2hz(midi_note) f.write('%.2f\t%.2f\t%.2f\n' % (onset, offset, frequency)) return notes