-
Notifications
You must be signed in to change notification settings - Fork 0
/
mido_lib.py
202 lines (167 loc) · 6.99 KB
/
mido_lib.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import mido
import time
class MidiNote(object):
start_timing = 0
duration = 0
end_timing = 0
note = 0
note_name = ''
score = 0
timing_interval = 0.3
note_interval = 1
time_score = 0
def __init__(self, note=0, note_name='', start=0, duration=0, ):
self.note = note
self.note_name = note_name
self.start_timing = start
self.duration = duration
self.end_timing = start + duration
def duration_append(self, duration):
self.duration += duration
self.end_timing = self.start_timing + duration
def compare(self, target_note, curr_timing):
isMatch = 0
if target_note == self.note:
self.score = 1
# if curr_timing < self.start_timing - self.timing_interval * 2 or curr_timing > self.start_timing + self.timing_interval * 2:
# return 0
# if target_note < self.note - self.note_interval * 2 or target_note > self.note + self.note_interval * 2:
# return 0
# newScore = self.note_interval * 3 - abs(target_note - self.note)
# if newScore > self.score:
# self.score = newScore
return self.score
def in_time(self, target_time, target_end_time=None, thres=0.1):
if target_end_time is not None:
return not(self.end_timing < target_time or self.start_timing > target_end_time)
return self.start_timing - thres < target_time < self.end_timing + thres
def __str__(self):
return "note_name={}[note={}]\tstart_timing={}\tend_timing={}\t[score={}]".format(self.note_name, self.note,
round(self.start_timing, 3),
round(self.end_timing, 3),
self.score)
class IvyMidi(object):
secPerTick = 0
secPerBeat = 0
tempo = 0
tickPerBeat = 0
curr_note = set()
mid = None
noteNames = []
noteDicts = {}
note_list = []
curr_timing = 0
start_timing = 0
end_timeing = 0
ivy_notes = []
scores = {}
score = 0
def __init__(self):
self.midi_labels()
def __len__(self):
return len(self.ivy_notes)
def score_init(self):
self.scores = {'all': 0, 'perfect': 0, 'good': 0, 'fail': 0}
self.score = 0
def midi_labels(self):
note_name1 = ['C', 'C', 'D', 'D', 'E', 'F', 'F', 'G', 'G', 'A', 'A', 'B']
note_name2 = ['', '#', '', '#', '', '', '#', '', '#', '', '#', '']
note_name_level = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
for n in range(21, 109):
level = n // 12 - 1
noteIndex = n % 12
nn1 = note_name1[noteIndex]
nnL = note_name_level[level]
nn2 = note_name2[noteIndex]
noteString = nn1 + nn2 + nnL
self.noteNames.append(noteString)
self.noteDicts[n] = noteString
print(self.noteNames)
print(self.noteDicts)
def open(self, file_name='./music/canon.mid'):
self.mid = mido.MidiFile(file_name)
self.tickPerBeat = self.mid.ticks_per_beat
for msg in self.mid.tracks[0]:
if msg.type == 'set_tempo':
self.tempo = msg.tempo
break
self.secPerTick = 1 * (self.tempo * 1e-6 / self.tickPerBeat)
self.secPerBeat = self.secPerTick * self.tickPerBeat
for i, track in enumerate(self.mid.tracks):
print(track.name)
print(self)
t = 0
c_notes = {}
self.ivy_notes.clear()
for i, track in enumerate(self.mid.tracks):
if 'cello' in track.name.lower() or 'solo' in track.name.lower():
print('Track {}: {} msg count: {}'.format(i, track.name,len(track)))
for msg in track:
if msg.type == 'note_on':
# print(self.noteDicts[msg.note])
time_duration = msg.time * self.secPerTick
if msg.velocity > 0 and not c_notes.__contains__(msg.note):
c_notes[msg.note] = MidiNote(msg.note, self.noteDicts[msg.note], t, 0)
self.ivy_notes.append(c_notes[msg.note])
# 统计音符延长
for n in c_notes.values():
n.end_timing = n.end_timing+time_duration
# print(c_notes)
if msg.velocity == 0:
c_notes.pop(msg.note)
t += time_duration
if t > self.end_timeing:
self.end_timeing = t
if msg.type == 'note_off':
pass
# self.mid.curr_note.remove(msg.note)
print('ivy_notes length---------', len(self.ivy_notes))
for n in self.ivy_notes:
print(n)
def compare(self, note):
self.score = 0
for n in self.note_list:
n.compare(note, time.time() - self.start_timing)
self.score += n.score
def get_curr_notes(self, target_time):
res = []
for n in self.ivy_notes:
if n.in_time(target_time):
res.append(n)
return res
def get_nearby_notes(self,target_time,secs=5):
res = []
for n in self.ivy_notes:
if n.in_time(target_time, target_time+secs):
res.append(n)
return res
def __str__(self):
return "{}[{}] {}-{}".format(self.tempo, self.secPerTick, self.secPerBeat, self.note_list)
if __name__ == '__main__':
mid = IvyMidi()
#mid.open()
print(mid)
# for n in mid.ivy_notes:
# print(n)
# for i, track in enumerate(mid.mid.tracks):
# print(track.name)
#
# for i, track in enumerate(mid.mid.tracks):
# if 'cello' in track.name.lower():
# print('Track {}: {}'.format(i, track.name))
# for msg in track:
# print(msg)
# # print(msg.type)
# if msg.type == 'note_on':
# # print('----note=',msg.note)
# # print('----velocity=',msg.velocity)
# print(mid.noteDicts[msg.note])
#
# if msg.velocity > 0:
# mid.curr_note.add(msg.note)
# elif msg.velocity == 0:
# mid.curr_note.remove(msg.note)
# # if len(curr_note)>0:
# # print('{',curr_note,'}')
# if msg.type == 'note_off':
# mid.curr_note.remove(msg.note)