-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundHandler.py
110 lines (97 loc) · 3.55 KB
/
soundHandler.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
import time
import json
from logger import Logger
from sound import Sound
class SoundHandle(object):
MAX_DISTANCE = 300 #cm
MIN_DISTANCE = 15 #cm
def __init__(self, ipc):
self.ipc = ipc
self.Log = Logger("SoundHandler")
self.sound = Sound()
#add callback to ipc handler
self.ipc.add_message_callback(self.on_message)
self.ipc.start()
def on_message(self, client, userdata, message):
self.Log.log("Message Arrived")
msgRecv = message.payload.decode("utf-8")
try:
dataRecv = json.loads(msgRecv)
self.Log.log(dataRecv)
self.handleMessage(dataRecv)
except Exception as e:
self.Log.log("Failed to parse message: {}".format(e))
def handleMessage(self, data):
if data['status'] is not None:
self.handleStatus(data['status'])
if not data['status']:
return
if data['horizontal'] is not None:
self.handlePan(data['horizontal'])
if data['vertical'] is not None:
self.handleTone(data['vertical'])
if data['distance'] is not None:
self.handleDistance(data['distance'])
def handleStatus(self, status):
self.Log.log("STATUS: {}".format(status))
if status == 0:
self.sound.setStatus(False)
elif status == 1:
self.sound.setStatus(True)
else:
self.Log.log("Invalid Status")
def handlePan(self, angle):
# Receive angle
self.Log.log("Horizontal Angle: {}".format(angle))
side = "none"
if int(angle) >= -10 and int(angle) <= 10:
side = "center"
elif int(angle) > 10:
side = "right"
elif int(angle) < -10:
side = "left"
else:
self.Log.log("Invalid Angle: {}".format(angle))
return
self.Log.log("PAN: {}".format(side))
if side == "left":
self.sound.setPan(self.sound.CHANNEL_LEFT)
elif side == "right":
self.sound.setPan(self.sound.CHANNEL_RIGHT)
elif side == "center":
self.sound.setPan(self.sound.CHANNEL_CENTER)
else:
self.Log.log("Invalid Pan")
def handleTone(self, angle):
self.Log.log("Vertical Angle: {}".format(angle))
side = "none"
if int(angle) >= -10 and int(angle) <= 10:
side = "center"
elif int(angle) > 10:
side = "up"
elif int(angle) < -10:
side = "down"
else:
self.Log.log("Invalid Angle: {}".format(angle))
return
self.Log.log("PAN: {}".format(side))
if side == "up":
self.sound.setTone(self.sound.TONE_UP)
elif side == "down":
self.sound.setTone(self.sound.TONE_DOWN)
elif side == "center":
self.sound.setTone(self.sound.TONE_CENTER)
else:
self.Log.log("Invalid Pan")
def handleDistance(self, distance):
if distance > self.MAX_DISTANCE or distance < self.MIN_DISTANCE:
self.Log.log("Invalid Distance")
return
newInterval = self.mapDistanceToInterval(distance)
self.Log.log("INTERVAL: {}".format(newInterval))
self.sound.setInterval(newInterval)
def mapDistanceToInterval(self, distance):
return (distance - self.MIN_DISTANCE) * (self.sound.MAX_INTERVAL - self.sound.MIN_INTERVAL) / (self.MAX_DISTANCE - self.MIN_DISTANCE) + self.sound.MIN_INTERVAL
def Run(self):
while 1:
self.sound.Play()