def pitch_class(pr): nb_class = 12 nb_pitch = pr.shape[1] if not nb_pitch == 128: raise Exception('Pitch dimension should be equal to 128') pr_red = np.zeros((pr.shape[0], nb_class)) for p in range(nb_pitch): c = p % nb_class pr_red[:, c] += pr[:, p] # Binary pr_red_bin = (pr_red > 0).astype(int) return pr_red_bin def extract_pianoroll_part(pianoroll, start_time, end_time): new_pr = {} # Start and end time are given in discrete frames for k, v in pianoroll.items(): new_pr[k] = v[start_time:end_time] return new_pr if __name__ == "__main__": from acidano.data_processing.midi.read_midi import Read_midi song_path = 'test.mid' midifile = Read_midi(song_path, 8) midifile.read_file() pr = midifile.pianoroll pr_class = pitch_class(pr)