-
Notifications
You must be signed in to change notification settings - Fork 1
/
one_hot_to_midi.py
45 lines (36 loc) · 1.04 KB
/
one_hot_to_midi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Command:
python one_hot_to_midi.py <input numpy file> <output midi file>
"""
from mido import Message, MidiFile, MidiTrack
import numpy as np
import sys
index_to_pitch_map = {
0 : 36,
1 : 38,
2 : 50,
3 : 47,
4 : 43,
5 : 46,
6 : 42,
7 : 49,
8 : 51,
9 : 0 # value 0 is not defined on channel 9 so it's just silence (channel 9 is for drums)
}
def index_to_pitch(index):
return index_to_pitch_map[index]
def one_hot_to_midi(one_hot, midi_filename = 'song.mid'):
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
pitch = 36
duration = 128
velocity = 64
track.append(Message('program_change', program=12, time=0))
#for sb in one_hot:
for i in range(one_hot.shape[1]):
sb = one_hot[:,i]
pitch = int(index_to_pitch(np.argmax(sb)))
track.append(Message('note_on', channel=9, note=pitch, velocity=64, time=duration))
track.append(Message('note_off', channel=9, note=pitch, velocity=127, time=duration))
mid.save(midi_filename)