-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiano.py
executable file
·143 lines (118 loc) · 4.07 KB
/
piano.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
#!/usr/bin/env python
try:
from ConfigParser import SafeConfigParser
import time
import os
from time import sleep
import rtmidi_python as rtmidi
import serial
except ImportError, e:
raise ImportError (str(e) + " found, please install")
global PORT_COUNT
PORT_COUNT = 0
def serial_setup(port):
global PORT_COUNT
PORT_COUNT += 1
print("Attempting to use port " + port + " (Device #" + str(PORT_COUNT) + ")")
try:
fp = open(port)
except IOError, errormsg:
print "Could not write to port %s: %s" % (port, errormsg)
raise
else:
try:
ser = serial.Serial(
port=str(port),
baudrate=9600,
timeout=0
)
print("Connected Device " + str(PORT_COUNT) + " port: " + ser.portstr)
ser.write("M" + str(d1Modes) + "\\r")
ser.write(d1Levels + "\\r")
ser.write("C" + str(d1AFreq) + "\\r")
ser.write("D" + str(d1BFreq) + "\\r")
except serial.SerialException:
print("Device " + str(PORT_COUNT) + " is not connected")
print("\n")
def MidiCallback(message, time_stamp):
messagetype = message[0] >> 4
note = message[1] if len(message) > 1 else None
if messagetype == 9:
keystate = "down"
elif messagetype == 8:
keystate = "up"
else:
pass
if note == 48 and keystate == "down":
ser1.write(d1AOn)
print(d1AOn)
sleep(1)
elif keystate == "up":
ser1.write(d1AOff)
print(d1AOff)
else:
pass
print("keystate is %s " % (keystate))
print("note is %s" % (str(note)))
if __name__ == "__main__":
config = SafeConfigParser()
try:
config.read('settings.txt')
except ConfigParser.ParsingError, err:
print 'Could not parse:', err
'''
Settings for Device 1
'''
d1On = config.getboolean('device1', 'on')
d1Box = config.get('device1', 'box')
d1Port = config.get('device1', 'port')
d1Mode = config.get('device1', 'mode')
d1Level = config.get('device1', 'level')
d1AInt = config.get('device1', 'a-intensity')
d1AFreq = config.get('device1', 'a-frequency')
d1BInt = config.get('device1', 'b-intensity')
d1BFreq = config.get('device1', 'b-frequency')
'''
Settings for Device 2
'''
d2On = config.getboolean('device2', 'on')
d2Box = config.get('device2', 'box')
d2Port = config.get('device2', 'port')
d2Mode = config.get('device2', 'mode')
d2Level = config.get('device2', 'level')
d2AInt = config.get('device2', 'a-intensity')
d2AFreq = config.get('device2', 'a-frequency')
d2BInt = config.get('device2', 'b-intensity')
d2BFreq = config.get('device2', 'b-frequency')
'''
Settings conversion
'''
modes = {"pulse": 0, "bounce": 1, "continuous": 2, "a-split": 3, "b-split": 4, "wave": 5, "waterfall": 6, "squeeze": 7, "milk": 8, "throb": 9, "thrust": 10, "random": 11, "step": 12, "training": 13}
d1Modes = modes[d1Mode.lower()]
d2Modes = modes[d2Mode.lower()]
levels = {"high": "H", "low": "L"}
d1Levels = levels[d1Level.lower()]
d2Levels = levels[d2Level.lower()]
channels = {"AInt": "A", "BInt": "B", "AFreq": "C", "BFreq": "D"}
lineEnd = "\r"
channelOff = "0"
'''
Sets the on/off levels for the Channels, to be passed into the MIDICallback.
'''
d1AOn = "A%s%s" % (str(d1AInt), lineEnd)
d1BOn = "B%s%s" % (str(d1BInt), lineEnd)
d1AOff = "A%s%s" % (channelOff, lineEnd)
d1BOff = "B%s%s" % (channelOff, lineEnd)
serial_setup(d1Port)
serial_setup(d2Port)
midi_in = [rtmidi.MidiIn()]
previous = []
while True:
for port in midi_in[0].ports:
if port not in previous and 'Midi Through' not in port:
midi_in.append(rtmidi.MidiIn())
midi_in[-1].callback = MidiCallback
midi_in[-1].open_port(port)
print("Connected MIDI device: " + port)
previous = midi_in[0].ports
time.sleep(0.5)