-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmidi.py
More file actions
73 lines (62 loc) · 1.98 KB
/
Copy pathmidi.py
File metadata and controls
73 lines (62 loc) · 1.98 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import OSC
class OSCTimeoutSignal(Exception): pass
class OSCTimeoutServer(OSC.OSCServer):
"""
Raise timeout signals so that users can tell
when there are no messages to read.
"""
def handle_timeout(self):
OSC.OSCServer.handle_timeout(self)
raise OSCTimeoutSignal
class MIDIOsc(object):
"""
A OSC server designed to recieve
midi-like messages from keyboard instruments
(probably converted via OSCulator).
Create an instance, add callbacks to
either note_handlers or noteon_handlers.
"""
def __init__(self, ip='127.0.0.1', port=57111, osc_address='midi/note/1'):
self.ip = ip
self.port = port
self.osc_address = osc_address
self.noteon_handlers = []
self.note_handlers = []
self.server = None
def connect(self):
"""
Initialize the server. Calling this is optional,
but may be useful if connecting is likely to
produce errors.
"""
self.server = OSCTimeoutServer((self.ip, self.port))
self.server.timeout = 0.01
self.server.addMsgHandler(self.osc_address, self._osc_recieve)
def _osc_recieve(self, address, format, message, hostport):
pitch = int(128 * message[0])
vel = message[1]
on = bool(message[2])
if on:
for handler in self.noteon_handlers:
handler(pitch, vel)
for handler in self.note_handlers:
handler(pitch, vel, on)
def handle(self):
"""
Handle all buffered messages and return.
"""
if not self.server:
self.connect()
while 1:
try:
self.server.handle_request()
except OSCTimeoutSignal:
return
def run(self):
"""
Block execution and handle
requests forever.
"""
if not self.server:
self.connect()
self.server.serve_forever()