-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrommidi.py
More file actions
58 lines (53 loc) · 2.05 KB
/
frommidi.py
File metadata and controls
58 lines (53 loc) · 2.05 KB
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
#!/usr/bin/python
from mido import MidiFile
import sys
if len(sys.argv) != 2:
print "Usage: {0} <midifile>".format(sys.argv[0])
sys.exit(2)
midifile = sys.argv[1]
print "# Translating {}".format(midifile)
msgqueue = MidiFile(midifile)
timenow = 0.1
timeoffset = -1
channelmap = [2, 1, 0, 3, 4]
channels = ["pedal", "great", "swell", "choir", "posit"]
notes_on = [0, 0, 0, 0, 0]
notes_off = [0, 0, 0, 0, 0]
outputlist = []
for msg in msgqueue:
timenow += msg.time
if msg.type == "note_on" and msg.velocity > 0:
if timeoffset < 0:
timeoffset = timenow - 0.1
payload = "N {} {} 1 ".format(channelmap[msg.channel], msg.note)
target = "{:09.3f}:{}".format(timenow-timeoffset, channels[channelmap[msg.channel]])
outputlist.append((target, payload))
notes_on[channelmap[msg.channel]] += 1
elif msg.type == "note_off" or (msg.type == "note_on" and msg.velocity == 0):
if timeoffset < 0:
timeoffset = timenow - 0.1
payload = "N {} {} 0 ".format(channelmap[msg.channel], msg.note)
target = "{:09.3f}:{}".format(timenow-timeoffset, channels[channelmap[msg.channel]])
outputlist.append((target, payload))
notes_off[channelmap[msg.channel]] += 1
elif msg.type == "control_change":
if msg.control == 11:
if timeoffset < 0:
timeoffset = timenow - 0.1
payload = "V {}".format(msg.value)
target = "{:09.3f}:{}".format(timenow-timeoffset, channels[channelmap[msg.channel]])
outputlist.append((target, payload))
#else:
# print "# {:09.3f}:{}".format(timenow-timeoffset, msg)
outputlist.append(("END", "END"))
print "# Notes pressed = {}".format(notes_on)
print "# Notes released = {}".format(notes_off)
(prevtarget, payload) = outputlist[0]
for i in range(1, len(outputlist)):
(newtarget, newpayload) = outputlist[i]
if newtarget == prevtarget:
payload += newpayload
else:
print "{}:{}".format(prevtarget, payload)
prevtarget = newtarget
payload = newpayload