-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (121 loc) · 3.51 KB
/
main.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
import subprocess
import math
import time
from os import system
def noOverlap(notes):
print('')
gap2 = math.floor(24 / const)
print(const)
times = []
for n in notes:
times.append(n[1])
for i in range(len(times)-1):
x = times[i+1:]
t = times[i]
while t in x:
e = x.index(t)
x[e] += 2
times[i+1+e] += gap2
print("{} / {}".format(i + 1, len(times) - 1), end='\r')
print('')
for i in range(len(notes)):
if times[i] != notes[i][1]:
notes[i][1] = times[i]
if times[i] > notes[i][2]: notes[i][2] = times[i] + 1
print("{} / {}".format(i + 1, len(notes) - 1), end='\r')
def startTime(l): return l[1]
def beep(freq, length, start, t):
while (time.time() - start) * 1000 < t: i = 0
subprocess.Popen(['./beep/beep', '-f', str(freq), '-l', str(length)])
csv = open('input.csv', 'r').readlines()
split = []
notes = []
curNotes = []
noteNums = []
noteLines = []
playNotes = []
track = -1 # -1 = all
exclude = []
tracks = [track]
if track == -1: tracks = list(range(1, 129))
for i in range(len(tracks) - 1, 0, -1):
if tracks[i] in exclude: del tracks[i]
for i in range(len(tracks)): tracks[i] = str(tracks[i])
for l in csv:
split.append(l.split(', '))
if split[len(split) - 1][2] == 'Tempo':
tempo = int(split[len(split) - 1][3])
ppqn = int(split[0][5])
bpm = 60000000 / tempo
const = 60000 / (bpm * ppqn)
gap = 16
for i in range(len(split)):
print("{} / {}".format(i + 1, len(split)), end='\r')
if split[i][0] in tracks and split[i][2] in ['Note_on_c', 'Note_off_c']:
# if int(split[i][5]) == 0:
if split[i][2] == 'Note_off_c':
j = noteNums.index(int(split[i][4]))
curNotes[j].append(int(split[i][1]))
notes.append(curNotes[j])
noteLines[j].append(i)
del curNotes[j]
del noteNums[j]
else:
curNotes.append([
int(split[i][4]),
int(split[i][1])
])
noteNums.append(int(split[i][4]))
noteLines.append([i])
notes.sort(key=startTime)
noOverlap(notes)
header = [
"0, 0, Header, 1, 3, {}\n".format(ppqn),
"1, 0, Start_track\n",
"1, 0, End_track\n",
"2, 0, Start_track\n",
"2, 0, Title_t, \"Export\"\n",
"2, 0, Tempo, {}\n".format(tempo),
"2, 0, Instrument_name_t, \"Piano\"\n",
"2, 0, MIDI_port, 0\n"
]
outCsv = open('output.csv', 'w')
csvNotes = []
notesOn = []
notesOff = []
for i in range(len(notes)):
notesOn.append((
notes[i][0],
notes[i][1],
1
))
notesOff.append((
notes[i][0],
max((notes[i][1] + gap) - gap, gap + notes[i][1]),
0
))
csvNotes = notesOn + notesOff
csvNotes.sort(key = startTime)
footer = [
"2, {}, End_track\n".format(csvNotes[len(csvNotes) - 1][1]),
"0, 0, End_of_file"
]
outCsv.writelines(header)
for n in csvNotes:
outCsv.write("2, {}, Note_on_c, 1, {}, {}\n".format(n[1], n[0], n[2] * 100))
outCsv.writelines(footer)
outCsv.close()
system("csvmidi output.csv output.mid")
print("Outputed MIDI!")
for n in notes:
playNotes.append((
(440 / 32) * (2 ** ((n[0] - 9) / 12)),
max(math.floor(n[2] * const) - math.floor(n[1] * const) - gap, gap),
math.floor(n[1] * const)
))
print("Number of notes: {}".format(len(notes)))
input("Ready!")
start = time.time()
for i in range(len(playNotes)):
beep(playNotes[i][0], playNotes[i][1], start, playNotes[i][2])
print(notes[i])