-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendbox.py
336 lines (249 loc) · 8.46 KB
/
sendbox.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Bob Smith Charanga
# 2016-09
# theremin.py
"""MIDI based theremin using a Raspberry Pi and a sonar HC-SR04 as pitch control"""
"""based on code from derickdeleon.com"""
import RPi.GPIO as GPIO
import ZeroSeg.led as led
import pygame
import pygame.midi
import time
#display related methods
device = led.sevensegment(cascaded=2)
def writeKey(keyID = 0):
print('writeKey',keyID)
keyArray = [0x00000C,0x0000Db,0x00000D,0x0000Eb,0x00000E,0x00000F,0x00009b,0x000009,0x0000Ab,0x00000A,0x0000Bb,0x00000B,0x00000C,0x000DDD]
device.write_number(deviceId=1, value=keyArray[keyID], base=16, leftJustify=False)
def writeMIDIPatch(midP = 0):
#device.letter(device, 4, int(midP / 100)) # Tens
counter = 8
for i in str(midP):
device.letter(1, counter, i) # Tens
counter = counter -1
def writeNote(noP = 0):
#device.letter(device, 4, int(midP / 100)) # Tens
counter = 5
for i in str(noP):
device.letter(1, counter, i) # Tens
counter = counter -1
# Distance Related Methods
def prepare(GPIO_ECHO, GPIO_TRIGGER):
""" Initialize the Raspberry Pi GPIO """
# Set pins as output and input
print(""" Initialize the Raspberry Pi GPIO """)
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(fswitch1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(fswitch2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Allow module to settle
time.sleep(0.5)
def get_distance(GPIO_ECHO, GPIO_TRIGGER):
""" get the distance from the sensor, echo - the input from sensor """
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
# Taking time
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300
# That was the distance there and back so halve the value
distance = distance / 2
return round(distance,2)
def get_switch(GPI_BUTTON):
""" get the state on any switch """
input_state = GPIO.input(GPI_BUTTON)
#print('Button Checked',GPI_BUTTON,input_state)
if input_state == False:
print('Button Pressed',GPI_BUTTON,input_state)
return True
return False
def conf_midi():
""" Initialize MIDI component """
pygame.init()
pygame.midi.init()
port = 0
global midiOutput # It is used in other methods
midiOutput = pygame.midi.Output(port, 9)
set_instrument(1)
#print_device_info()
def set_instrument(inst):
print("set_instrument",inst)
midiOutput.set_instrument(inst)
def play_midi(note, b4note, volume):
""" Play a new MIDI note and turn off the last one """
# use the last note to compare
if (note != b4note):
""" To-Do: smoother transitions between notes, use pitch bend. """
midiOutput.note_off(b4note,volume,channel)
midiOutput.note_on(note,volume,channel)
print(note,channel)
# help to not consume all resources
time.sleep(.2)
def get_note(dist=0):
""" Compute the note based on the distance measurements, get percentages of each scale and compare """
# Config
# you can play with these settings
#scale = [-1, -5, 60, 60, -5 ,62, 62, -5, 64, 64, -5, 67, 67, -5, 69, 69, -5, 72, -5, -1]
#distances = [38, 29, 28, 24, 21, 21, 19, 17, 16, 13, 12, 11, 9, 7, 6, 5, 3, 2, 1, 0]
'''scale = [-1, -5, 60, -5 , 62, -5, 64, -5, 67, -5, 69, -5, 72, -5, -1]
distances = [40, 35, 24, 21, 19, 17, 13, 10, 9, 7, 5, 4, 2, .5, 0]'''
scale = [-1, -5, 60, -5 , 67, -5, 69, -5, 72, -5, -1]
distances = [40, 35, 21, 18, 16, 9, 6, 4, 2, .5, 0]
for index in range(len(distances)):
if dist>distances[index]:
return int(scale[index])
'''
offset = 0
minDist = 3 # Distance Scale
maxDist = 30
octaves = 1
minNote = 48 # c4 middle c
maxNote = minNote + 12*octaves
#print("dist",dist)
if dist>38:
return int(-1)
if dist>27:
return int(60+offset)
if dist>20:
return int(62+offset)
if dist>15:
return int(64+offset)
if dist>10:
return int(67+offset)
if dist>5:
return int(69+offset)
if dist>3:
return int(72+offset)
return int(-1)
# Percentage formula
fup = (dist - minDist)*(maxNote-minNote)
fdown = (maxDist - minDist)
note = minNote + fup/fdown
""" To-do: Add in scales """
return int(note)
'''
# MAIN
GPIO.setwarnings(False)
# The pin number is the actual pin number
GPIO.setmode(GPIO.BCM)
# Set up the GPIO channels
trigger = 23
echo = 24
button = 12
button1 = 21 #button 1 on device controls midi
button2 = 20 #button 2 on device controls key
button3 = 19
button4 = 4
fswitch1 = 17
fswitch2 = 26
# channel is to swap between 9 and 0 if 9 we are in drum mode if 0 instrument mode
channel =9
prepare(echo, trigger)
note = 0
conf_midi()
volume = 127
instrument = 1
offset=-21
device.clear()
delay = .5
writeKey(13)
time.sleep(delay)
writeMIDIPatch(instrument)
lastDistance = 0
offCounter = 0
try:
while True:
""" to-do this is broke """
b4note = note
# get distance
d = get_distance(echo, trigger)
# calculate note
'''
if abs(d-lastDistance)>500:
print "weirdRange",d
offCounter = offCounter+1;
if offCounter<3:
d=lastDistance
else:
offCounter=0'''
'''d = lastDistance + (d-lastDistance)/1.1
print d , lastDistance
lastDistance = d'''
note = get_note(d)
if note>0:
note = note+offset
switch = get_switch(button)
switch1 = get_switch(button1)
switch2 = get_switch(button2)
switch3 = get_switch(button3)
switch4 = get_switch(button4)
switch5 = get_switch(fswitch1)
switch6 = get_switch(fswitch2)
#print("switches",switch,switch1,switch2,switch3,switch4,switch5,switch6)
# to-do, take a number of sample notes and average them, or any other statistical function. eg. Play just minor notes, ponderate average, etc.
# play the note
bright = 10
volume = 80
if switch:
volume = 110
bright = 15
if switch3:
note = 60+offset
#print("note",note)
if note != -5:
play_midi(note, b4note, volume)
if note!=-1:
device.brightness(bright)
writeNote(note)
else:
device.brightness(6)
if note == -5:
note = b4note
if switch1:
print ("Button 1 pressed",offset)
offset = offset +1
if offset>12:
offset = 0
elif offset<0:
offset=0
if offset==12:
writeKey(offset+1)
writeMIDIPatch(instrument)
channel=9
print "DRUMS setting standard offset to be hand clap"
offset=-21
else:
print"NotDrums"
channel=0
writeKey(offset)
writeMIDIPatch(instrument)
time.sleep(0.4)
if switch2:
print "Button 2 pressed"
instrument = instrument +1
if instrument>=128:
instrument = 1
set_instrument(instrument)
writeKey(offset)
writeMIDIPatch(instrument)
except KeyboardInterrupt:
GPIO.cleanup()
del midiOutput
pygame.midi.quit()
GPIO.cleanup()
pygame.midi.quit()