/
rtmidi_test.py
executable file
·187 lines (150 loc) · 5.22 KB
/
rtmidi_test.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
import re
import sys
import time
import rtmidi
def mapping(group, effect, value):
return 3 * (group * 4 + effect) + value
class Midi:
midiout = rtmidi.MidiOut()
def __init__(self):
available_ports = self.midiout.get_ports()
if available_ports:
if len(available_ports) > 1:
print "Available ports:", available_ports
portNum = 1
else:
portNum = 0
self.midiout.open_port(portNum)
print "Using the port '", available_ports[portNum], "'"
else:
print "Using VirtualRtMidiPort."
self.midiout.open_virtual_port("VirtualRtMidiPort")
def note_on(self, channel, note, velocity):
channelON = 0x90 | channel
note_on = [channelON, note, velocity]
self.midiout.send_message(note_on)
def note_off(self, channel, note):
channelOFF = 0x80 | channel
note_off = [channelOFF, note, 0]
self.midiout.send_message(note_off)
def cc(self, channel, number, value):
print 'control_change<{}:{}:{}>'.format(channel, number, value)
assert(channel < 16)
channelCC = 0xB0 | channel
cc = [channelCC, number, value]
self.midiout.send_message(cc)
class Controller (object):
def __init__(self, midi):
self.midi = midi
class Transport (Controller):
def __init__(self, midi):
super(Transport, self).__init__(midi)
def play(self):
self.midi.cc(15, 15, 127)
def stop(self):
self.midi.cc(15, 14, 127)
def record(self):
self.midi.cc(15, 13, 127)
class Group (Controller):
def __init__(self, id_, midi):
super(Group, self).__init__(midi)
self.id = id_
self.effects = []
def effect(self, effect):
return self.effects[effect]
class Effect (Controller):
def __init__(self, id_, midi, group):
super(Effect, self).__init__(midi)
self.group = group
self.id = id_
def control(self, offset):
return mapping(self.group.id, self.id, offset)
def enable(self, active=True):
control = self.control(0)
self.midi.cc(1, control, 127 if active else 0)
def disable(self):
self.enable(False)
class Effect1D (Effect):
def __init__(self, id_, midi, group):
super(Effect1D, self).__init__(id_, midi, group)
def set_value(self, value):
control = self.control(1)
self.midi.cc(1, control, value)
def set_value_from_accelerometer(self, x, y, z):
self.set_value(z)
class Effect2D (Effect):
def __init__(self, id_, midi, group):
super(Effect2D, self).__init__(id_, midi, group)
def set_value(self, x, y):
control = self.control(1)
self.midi.cc(1, control, x)
control = self.control(2)
self.midi.cc(1, control, y)
def set_value_from_accelerometer(self, x, y, z):
self.set_value(x, y)
class Parser:
def __init__(self):
self.midi = Midi()
self.groups = []
for i in range(0, 5):
group = Group(i, self.midi)
for j in range(0, 4):
group.effects.append(Effect2D(j, self.midi, group))
self.groups.append(group)
self.transport = Transport(self.midi)
def parse(self):
while True:
line = sys.stdin.readline()
if not line:
break
cmd = line[0]
if cmd == 'B': # Begin note
note = int(line[1:])
self.midi.note_on(0, note, 127)
elif cmd == 'F': # Finish note
note = int(line[1:])
self.midi.note_off(0, note)
elif cmd == 'E': # Enable effect
group = 0 # int(line[1])
effect = 0 # int(line[2])
self.groups[group].effect(effect).enable()
elif cmd == 'M': # Modulate effect
group = 0 # int(line[1])
effect = 0 # int(line[2])
x, y, z = [int(v) for v in line[3:].split(':')]
self.groups[group].effect(effect).set_value_from_accelerometer(x, y, z)
elif cmd == 'D': # Disable effect
group = 0 # int(line[1])
effect = 0 # int(line[2])
self.groups[group].effect(effect).disable()
elif cmd == 'P': # Play
self.transport.play()
elif cmd == 'S': # Stop
self.transport.stop()
elif cmd == 'R': # Record
self.transport.record()
def main():
parser = Parser()
parser.parse()
if __name__ == '__main__':
main()
# # Midi Learn code:
# # comment the main call on top!
# midi = Midi()
# for group in range(0, 5):
# for effect in range(0, 4):
# for i, value in zip(range(0, 3), ['active', 'x', 'y']):
# cc = mapping(group, effect, i)
# print 'group:{} effect:{} {}'.format(group, effect, value)
# raw_input()
# midi.cc(1, cc, 127)
# print '\n'
# # next and previous scene commands:
# transport = Transport(self.midi)
# print 'prev'
# raw_input()
# transport.prev_scene()
# print 'next'
# raw_input()
# transport.next_scene()
# print '\n'