-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumpy2midi.py
147 lines (120 loc) · 3.32 KB
/
numpy2midi.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import mido
import random
import math
msg_list = []
def read():
mid = mido.MidiFile("D:/MyPrograms/midilstm/songs/song0.mid")
for msg in mid:
#if msg.is_meta:
print(msg)
def denormalize_time(t):
return (t * 35756)
def load_list():
global msg_list
msg_list = np.load("data/messages.npy")
print(len(msg_list))
def print_times():
f= open("times.txt", "a")
max_1 = 0
d = dict()
for msg in msg_list:
#f.write(str(msg[-1])+"\n")
t = msg[-1]
if t in d:
d[t] += 1
else:
d[t] = 1
if msg[-1] > max_1:
max_1 = msg[-1]
print("max", max_1)
for el in d:
print(d[el])
def npy2msg(arr):
oct = np.argmax(arr[2:13])
no = np.argmax(arr[13:25])
n = 12*oct + no
if n>127:
n = n -12
t = int(denormalize_time(arr[-1]))
m_type = "note_on"
if arr[1] == 0:
m_type = "note_off"
msg = mido.Message(m_type, note=n, velocity=127, time=t)
if arr[0] == 1:
msg = mido.MetaMessage("end_of_track", time=0)
return msg
def npy2msg_c(n, t):
# chars: note-on[0-127] note-off[128-255] pause[256-264] eof[265] 1, 5, 15, 30, 60, 120, 240, 480, 960
n = int(n)
if n <128:
return mido.Message(type="note_on", note=n, velocity=127, time=t)
else:
return mido.Message(type="note_off", note=n-128, velocity=0, time=t)
def convert(arr, name):
mid = mido.MidiFile()
mid.ticks_per_beat = 960
track = mido.MidiTrack()
mid.tracks.append(track)
track.append(mido.MetaMessage("time_signature", numerator=4, denominator=4))
track.append(mido.MetaMessage("set_tempo", tempo = 600000))
i = 0
first = 1
for el in arr:
#print(el)
if el[0] == 1:
track.append(npy2msg(el))
mid.save('songs/'+name+str(i)+'.mid')
i += 1
first = 1
elif first == 1:
first = 0
mid = mido.MidiFile()
mid.ticks_per_beat = 960
track = mido.MidiTrack()
mid.tracks.append(track)
track.append(mido.MetaMessage("time_signature", numerator=4, denominator=4))
track.append(mido.MetaMessage("set_tempo", tempo = 600000))
track.append(npy2msg(el))
else:
track.append(npy2msg(el))
mid.save('songs/'+name+str(i)+'.mid')
def convert_c(arr, name):
mid = mido.MidiFile()
mid.ticks_per_beat = 960
track = mido.MidiTrack()
mid.tracks.append(track)
track.append(mido.MetaMessage("time_signature", numerator=4, denominator=4))
track.append(mido.MetaMessage("set_tempo", tempo = 600000))
#960,480,240,120,60,30,15,5,1
char2time = dict([(256, 960),(257, 480),(258, 240),(259, 120),(260, 60),(261, 30),(262, 15),(263, 5),(264, 1)])
i = 0
first = 1
msg_time = 0
for el in arr:
#print(el)
#if el > 256: print(el)
if first == 1:
first = 0
msg_time = 0
mid = mido.MidiFile()
mid.ticks_per_beat = 960
track = mido.MidiTrack()
mid.tracks.append(track)
track.append(mido.MetaMessage("time_signature", numerator=4, denominator=4))
track.append(mido.MetaMessage("set_tempo", tempo = 600000))
if el == 265:
track.append(mido.MetaMessage("end_of_track", time=0))
mid.save('songs/'+name+str(i)+'.mid')
i += 1
first = 1
elif el in char2time:
#print(el)
msg_time += char2time[el]
else:
track.append(npy2msg_c(el,msg_time))
msg_time = 0
mid.save('songs/'+name+str(i)+'.mid')
#load_list()
#convert_c(msg_list,"song")
#read()