forked from nvmao/bouncing-square-playing-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.py
38 lines (33 loc) · 1.42 KB
/
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
import mido
def list_notes_from_midi(file_path):
midi = mido.MidiFile(file_path)
current_instruments = {}
all_notes = []
for track in midi.tracks:
absolute_time = 0
for msg in track:
absolute_time += mido.tick2second(msg.time, midi.ticks_per_beat, mido.bpm2tempo(100)) # 187
# absolute_time += dt
if msg.type == 'program_change':
current_instruments[msg.channel] = msg.program
if msg.type == 'note_on' and msg.velocity > 0:
note_name = msg.note
instrument = current_instruments.get(msg.channel, 0) # Default to instrument 0 if not set
all_notes.append({
'name': note_name,
'start_time': absolute_time,
'instrument': instrument,
'velocity' : msg.velocity,
'play' : False
})
if msg.type == 'note_off' or (msg.type == 'note_on' and msg.velocity == 0):
note_name = msg.note
instrument = current_instruments.get(msg.channel, 0) # Default to instrument 0 if not set
all_notes.append({
'name': note_name,
'start_time': absolute_time,
'instrument': instrument,
'velocity': 0,
'play': False
})
return all_notes