forked from Drachenkaetzchen/nocturn-game
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Midder.py
46 lines (36 loc) · 1.29 KB
/
Midder.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
#!/usr/bin/python2
import pypm
DEBUG = False
class Midder(object):
"""Virtualization layer for Midi communication."""
CC_OFFSET = 0xb0
def __init__(self, inDev, outDev, latency=5, channel=0):
self.inDev = inDev
self.outDev = outDev
self.latency = latency
self.channel = channel
#Initialize pypm - I'm told bad things will happen otherwise
pypm.Initialize()
self.MidiOut = pypm.Output(self.outDev,self.latency)
self.MidiIn = pypm.Input(self.inDev)
def send(self,cc, val):
try:
if DEBUG:
print ("writing midi message " + str(Midder.CC_OFFSET+self.channel) + " " +
str(cc) + " " + str(val))
self.MidiOut.WriteShort(Midder.CC_OFFSET+self.channel,cc,val)
except Exception as e:
print type(e)
print e.args
print e
def recv(self):
data = self.MidiIn.Read(1)
if data != [] and not (data[0][0][0] < 0xb0 or data[0][0][0] > 0xbf):
return [data[0][0][0]-Midder.CC_OFFSET,data[0][0][1],data[0][0][2]]
else:
return None
def setChannel( self, channel ):
self.channel = channel
midder = Midder( 1, 0 )
def getMidder( ):
return midder