def converter(filepath, datasets, midi_dict): """Save a multi-track piano-roll converted from a MIDI file to target dataset directory and update MIDI information to `midi_dict`""" try: midi_md5 = os.path.splitext(os.path.basename(filepath))[0] multitrack = Multitrack(beat_resolution=settings['beat_resolution'], name=midi_md5) pm = pretty_midi.PrettyMIDI(filepath) midi_info = get_midi_info(pm) multitrack.parse_pretty_midi(pm) if 'lpd' in datasets: dst = change_prefix(os.path.dirname(filepath), settings['lmd']['full'], settings['lpd']['full']) make_sure_path_exists(dst) multitrack.save(os.path.join(dst, midi_md5 + '.npz')) if 'lpd-5' in datasets: dst = change_prefix(os.path.dirname(filepath), settings['lmd']['full'], settings['lpd-5']['full']) merged = get_merged(multitrack) make_sure_path_exists(dst) merged.save(os.path.join(dst, midi_md5 + '.npz')) return (midi_md5, midi_info) except: return None
def converter(filepath, src, dst): """Convert a MIDI file to a multi-track piano-roll and save the resulting multi-track piano-roll to the destination directory. Return a tuple of `midi_md5` and useful information extracted from the MIDI file. """ midi_md5 = os.path.splitext(os.path.basename(filepath))[0] multitrack = Multitrack(beat_resolution=CONFIG["beat_resolution"], name=midi_md5) pm = pretty_midi.PrettyMIDI(filepath) # Merge tracks assert pm.instruments[0].name == "MELODY" assert pm.instruments[1].name == "BRIDGE" assert pm.instruments[2].name == "PIANO" pm.instruments[0].name = "MAIN" pm.instruments[0].notes = ( pm.instruments[0].notes + pm.instruments[1].notes + pm.instruments[2].notes ) del pm.instruments[2] del pm.instruments[1] multitrack.parse_pretty_midi(pm) midi_info = get_midi_info(pm) result_dir = change_prefix(os.path.dirname(filepath), src, dst) make_sure_path_exists(result_dir) multitrack.save(os.path.join(result_dir, midi_md5 + ".npz")) return (midi_md5, midi_info)
def binarizer(filepath, src, dst): """Load and binarize a multitrack pianoroll and save the resulting multitrack pianoroll to the destination directory.""" # Load and binarize the multitrack pianoroll multitrack = Multitrack(filepath) multitrack.binarize() # Save the binarized multitrack pianoroll result_path = change_prefix(filepath, src, dst) make_sure_path_exists(os.path.dirname(result_path)) multitrack.save(result_path)
def generate_multiple_tracks_from_numpy_matrices(self, num, dir, files, size, names, are_drums, tempo, downbeat, beat_resolution, programs=None, save_fig=False, save_path=None): tracks = [] for i in range(num): path = dir + files[i] name = names[i] if programs == None: program = 0 else: program = programs[i] is_drum = are_drums[i] piano_roll = np.load(path) piano_roll.resize(size) track = Track(piano_roll, program, is_drum, name) tracks.append(track) multitrack = Multitrack(tracks=tracks, tempo=tempo, downbeat=downbeat, beat_resolution=beat_resolution) multitrack.save(dir + 'multi.npz') fig, axs = pypianoroll.multitrack.plot_multitrack(multitrack, grid_linewidth=0.8, ytick='off') if save_fig: plt.savefig(save_path) else: plt.show()
def converter(filepath, src, dst): """Convert a MIDI file to a multi-track piano-roll and save the resulting multi-track piano-roll to the destination directory. Return a tuple of `midi_md5` and useful information extracted from the MIDI file. """ try: midi_md5 = os.path.splitext(os.path.basename(filepath))[0] multitrack = Multitrack(beat_resolution=CONFIG['beat_resolution'], name=midi_md5) pm = pretty_midi.PrettyMIDI(filepath) multitrack.parse_pretty_midi(pm) midi_info = get_midi_info(pm) result_dir = change_prefix(os.path.dirname(filepath), src, dst) make_sure_path_exists(result_dir) multitrack.save(os.path.join(result_dir, midi_md5 + '.npz')) return (midi_md5, midi_info) except: return None