一、对音符的pianoroll
midi = pm.PrettyMIDI(midi_file)
note_instrument = midi.instruments[0]
onset_instrument = on_midi.instruments[0]
offset_instrument = off_midi.instruments[0]
for note, onset_note, offset_note in zip(note_instrument.notes, onset_instrument.notes, offset_instrument.notes):
note_length = offset_note.end - offset_note.start #end-start=音符时值
onset_note.end = onset_note.start + min(note_length, unit_time)
offset_note.end += unit_time
offset_note.start = offset_note.end - min(note_length, unit_time)
pianoroll = note_instrument.get_piano_roll(fs=frame_per_second)
#frame_per_second = 8,unit_time = 1/8 = 0.125s(16分音符
onset_roll = onset_instrument.get_piano_roll(fs=frame_per_second)
offset_roll = offset_instrument.get_piano_roll(fs=frame_per_second)
print(pianoroll[60:80,10:20])
print(pianoroll.shape) #(128, 326)
用pretty_midi读取的(note,onset_note,offset_note)
三种一模一样的音符
1 Note(start=1.500000, end=1.625000, pitch=67, velocity=115) Note(start=1.500000, end=1.625000, pitch=67, velocity=115) Note(start=1.500000, end=1.625000, pitch=67, velocity=115)
2 Note(start=1.750000, end=1.875000, pitch=72, velocity=118) Note(start=1.750000, end=1.875000, pitch=72, velocity=118) Note(start=1.750000, end=1.875000, pitch=72, velocity=118)
3 Note(start=1.875000, end=2.000000, pitch=74, velocity=112) Note(start=1.875000, end=2.000000, pitch=74, velocity=112) Note(start=1.875000, end=2.000000, pitch=74, velocity=112)
4 Note(start=2.000000, end=2.125000, pitch=76, velocity=112) Note(start=2.000000, end=2.125000, pitch=76, velocity=112) Note(start=2.000000, end=2.125000, pitch=76, velocity=112)
5 Note(start=2.250000, end=2.500000, pitch=72, velocity=112) Note(start=2.250000, end=2.500000, pitch=72, velocity=112) Note(start=2.250000, end=2.500000, pitch=72, velocity=112)
6 Note(start=2.500000, end=2.750000, pitch=69, velocity=118) Note(start=2.500000, end=2.750000, pitch=69, velocity=118) Note(start=2.500000, end=2.750000, pitch=69, velocity=118)
因为pitch range选择的是人声的歌唱范围,48-95(c3-b5)
所以做了4个八度的移调
取出pianoroll[60:80,10:20],如下图所示
怎么阅读note的pianoroll呢?
1、矩阵的行意义为128种音高范围,因为我们的音高是48-96,所以只在这个范围内有值。
2、矩阵的列意义为时间步,往右走时间步增加,因为每秒是8帧,所以假如Note(start=1.500000, end=1.625000, pitch=67, velocity=115),即它在1.5*8=12帧,所以可以看到在坐标(67,12)的位置有值115
偏移处理后
所以40.758 = 326
39.8758 = 319
40.875*8 = 327
pianoroll.shape = (128,326)
onset_roll.shape = (128,319)
offset_roll.shape = (128,327)