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)
import os import sys import numpy as np import matplotlib.pyplot as plt from pypianoroll import Multitrack from utils.pypianoroll_utils import pianoroll_to_multitrack rootpath = os.path.abspath('../') sys.path.append(rootpath) TRACK_NAMES = ['drums', 'piano', 'guitar', 'bass', 'strings'] NPZ_FILE = '../data/sample_data/508448c8e2c465d9237dfca9a4c9a265.npz' # Get Multitrack object and its piano roll mt = Multitrack(NPZ_FILE, beat_resolution=24) mt.binarize() pr = mt.get_stacked_pianorolls() # shape=(num_time_step, 128, num_track) # F.Y.I num_time_step = self.beat_resolution * num_beat # pypianoroll/../multitrack.py line 744 print("Stacked piano-roll matrix: ", np.shape(pr)) # Get piano roll matrix of each track track_list = mt.tracks for track in track_list: _pr = track.pianoroll print("%s of shape: %s" % (track.name, str(list(np.shape(_pr))))) # print(" active length of this track: ", track.get_active_length()) # Plot the multi-track piano-roll fig, axs = mt.plot() plt.show()
# Iterate through all the MIDI files for filename in FILENAMES: # Parse the MIDI file into multitrack pianoroll try: multitrack = Multitrack(filename, beat_resolution=BEAT_RESOLUTION) except: continue # Pad to multtple multitrack.pad_to_multiple(4 * BEAT_RESOLUTION) # Binarize the pianoroll multitrack.binarize() # Sort the tracks according to program number multitrack.tracks.sort(key=lambda x: x.program) # Bring the drum track to the first track multitrack.tracks.sort(key=lambda x: ~x.is_drum) # Get the stacked pianoroll pianoroll = multitrack.get_stacked_pianorolls() # Check length if pianoroll.shape[0] < 4 * 4 * BEAT_RESOLUTION: continue # Keep only the mid-range pitches