-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_sound_multi.py
143 lines (120 loc) · 4.08 KB
/
run_sound_multi.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
import sys
sys.path.append("..")
import time
import serial
import serial.tools.list_ports
import threading
import threaded_serial
#https://stackoverflow.com/questions/6487180/synthesize-musical-notes-with-piano-sounds-in-python
from mingus.core import notes, chords
from mingus.containers import *
from mingus.midi import fluidsynth
class InputInterface():
def __init__(self):
self.serial_port = threaded_serial.SerialPort()
self.data = []
self.input_callback = None
def open(self, input_callback, port_name=None, baud=38400):
self.input_callback = input_callback
if not port_name:
port_list = serial.tools.list_ports.comports()
print port_list
for p in port_list:
print p.description
if "USBUART" in p.description:
port_name = p.device
self.serial_port.open(port_name, baud, self.serial_receive)
def serial_receive(self, data):
self.data = self.data + data
while len(self.data) >= 7:
if (self.data[0] == 0xF0) and (self.data[6] == 0x0F):
in_data = {
'touch': self.data[1],
'gpio': self.data[2],
'dist1': self.data[3],
'dist2': self.data[4],
'dist3': self.data[5]
}
print in_data
if self.input_callback:
self.input_callback(in_data)
self.data = []
else:
self.data = self.data[1:]
def close(self):
self.serial_port.close()
class SoundMachine(object):
last_touch = 0
def Open(self):
return fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2', "alsa")
def on_input(self, in_dict):
# get touch keys
touch = in_dict['touch']
delta_touch = touch^self.last_touch
self.last_touch = touch
if (delta_touch & 0x01):
if (touch & 0x01):
fluidsynth.play_Note(ord('A'), 0, 100)
else:
fluidsynth.stop_Note(ord('A'), 0)
if (delta_touch & 0x02):
if (touch & 0x02):
fluidsynth.play_Note(ord('B'), 0, 100)
else:
fluidsynth.stop_Note(ord('B'), 0)
if (delta_touch & 0x04):
if (touch & 0x04):
fluidsynth.play_Note(ord('C'), 0, 100)
else:
fluidsynth.stop_Note(ord('C'), 0)
if (delta_touch & 0x08):
if (touch & 0x08):
fluidsynth.play_Note(ord('D'), 0, 100)
else:
fluidsynth.stop_Note(ord('D'), 0)
if (delta_touch & 0x10):
if (touch & 0x10):
fluidsynth.play_Note(ord('E'), 0, 100)
else:
fluidsynth.stop_Note(ord('E'), 0)
if (delta_touch & 0x20):
if (touch & 0x20):
fluidsynth.play_Note(ord('F'), 0, 100)
else:
fluidsynth.stop_Note(ord('F'), 0)
if (delta_touch & 0x40):
if (touch & 0x40):
fluidsynth.play_Note(ord('G'), 0, 100)
else:
fluidsynth.stop_Note(ord('G'), 0)
dist1 = in_dict['dist3']
if (dist1 < 180):
dist1_note = Note();
dist1_note.from_int(in_dict['dist3']/2)
fluidsynth.play_Note(dist1_note,1, 20)
def run(self):
#takes the main loop and runs background tasks
insturment = 1
while 1:
time.sleep(2)
fluidsynth.set_instrument(0,insturment)
insturment+=1
if (insturment>50):
return
def test_input(in_dict):
print in_dict
def main():
if 0:
input = InputInterface()
input.open(test_input)
time.sleep(120)
else:
sound_machine = SoundMachine()
sound_machine.Open()
input = InputInterface()
input.open(sound_machine.on_input)
sound_machine.run()
#time.sleep(60)
input.close()
if __name__ == "__main__":
main()