def grid_plot(ppr, bar_range=None, pitch_range='auto', beats_in_bar=4, beat_resolution=24, show_white_key_ticks=False, figsize=[21, 10]): """ pretty ploting for pypianoroll """ orgSize = rcParams['figure.figsize'] rcParams['figure.figsize'] = figsize if isinstance(ppr, Track): downbeat = list(range(ppr.pianoroll.shape[0])) ppr = Multitrack(tracks=[ppr], downbeat=downbeat, beat_resolution=beat_resolution) beat_res = ppr.beat_resolution bar_res = beats_in_bar * beat_res downbeat = ppr.downbeat ppr.downbeat = np.zeros_like(ppr.downbeat, dtype=bool) major_scale = [0, 2, 4, 5, 7, 9, 11] major_scale_name = ['C', 'D', 'E', 'F', 'G', 'A', 'B'] major_color = [ 'red', 'orange', 'yellow', 'green', 'cyan', 'mediumblue', 'magenta' ] major = list(zip(major_scale, major_color)) fig, axs = ppr.plot(xtick="beat") for a, ax in enumerate(axs): ax.grid(False) ax.xaxis.set_ticks_position('none') ax.set_xticklabels([], minor=True) ax.set_xticklabels(range(len(ppr.downbeat) // beat_res), minor=False) # pretty_midiに合わせてC-1を0とする if show_white_key_ticks: ax.set_yticks([k + 12 * i for i in range(11) for k in major_scale][:75]) ax.set_yticklabels([ k + str(i - 1) for i in range(11) for k in major_scale_name ][:75]) else: ax.set_yticklabels([f'C{i - 1}' for i in range(11)]) xlim = ax.get_xlim() if bar_range: xlim = (bar_range[0] * bar_res, bar_range[1] * bar_res - 0.5) ax.set_xlim(*xlim) if pitch_range == 'auto': try: low, high = ppr.tracks[a].get_active_pitch_range() except ValueError: low, high = 66, 66 ax.set_ylim(max(0, low - 6), min(high + 6, 127)) elif pitch_range: pr = np.array(pitch_range) if pr.ndim == 1: ax.set_ylim(pr[0], pr[1]) elif pr.ndim == 2: ax.set_ylim(pr[a][0], pr[a][1]) ylim = ax.get_ylim() for bar_step in range(int(xlim[0]), int(xlim[1]) + 1, bar_res): ax.vlines(bar_step - 0.5, 0, 127) for beat in range(1, 4): ax.vlines(bar_step + beat_res * beat - 0.5, 0, 127, linestyles='dashed') for k, color in major: linewidth = 2.0 if k == 0 else 1.0 for h in range(int(ylim[0]), int(ylim[1])): if h % 12 == k: ax.hlines(h, xlim[0], xlim[1], linestyles='-', linewidth=linewidth, color=color) ppr.downbeat = downbeat rcParams['figure.figsize'] = orgSize
# 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() # Convert piano-rolls to Multitrack object program_nums = [ 118, 0, 25, 33, 49 ] # ref: http://www.360doc.com/content/11/0401/13/3416571_106375312.shtml is_drums = [True, False, False, False, False] print(pr.dtype) mt_out = pianoroll_to_multitrack(stacked_pianorolls=pr, program_nums=program_nums, is_drums=is_drums, track_names=TRACK_NAMES) # Write Multitrack to midi file file_path = '../data/sample_data/out_pypianoroll.mid'
np.all(temp1[:, :-1] == temp2) # %% np.all(temp1 == temp2) # %% np.argwhere(temp1[:, :-1] != temp2) # %% temp1[31, 568] # %% temp2[31, 568] # %% test_multitrack = Multitrack("Data/Train/chpn-p1.mid") # %% test_multitrack.plot() # %% temp0.write("r2p.mid") # %% midi_data.write("p.mid") # %% test_multitrack.write("r.mid") # %%