-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
171 lines (139 loc) · 6.18 KB
/
main.py
File metadata and controls
171 lines (139 loc) · 6.18 KB
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
import numpy as np
import pyaudio
from glob import glob
from threading import Thread
from random import randrange
import simpleaudio
from time import sleep
from sys import platform
class Audioplayer:
def __init__(self, queueName, wheel):
print(f'Audioplayer {wheel} is [a]live')
self.wheel = wheel
#self.audioLib = glob("audio/*.wav")
if self.wheel == 0:
self.audioLib = glob("audio/left/*.wav")
else:
self.audioLib = glob("audio/right/*.wav")
self.numAudioFiles = len(self.audioLib)
# own queue
self.audioQueue = queueName
self.queueLock = False
# start thread
thd = Thread(target=self.makeSound)
thd.start()
def makeSound(self):
while True:
if len(self.audioQueue) > 0:
_dummy = self.audioQueue.pop(0)
rndSound = randrange(0, self.numAudioFiles)
if self.queueLock:
playing.stop()
# make new audio obj
audiofile = self.audioLib[rndSound]
player = simpleaudio.WaveObject.from_wave_file(audiofile)
playing = player.play()
self.queueLock = True
else:
sleep(0.1)
class BBBot1:
def __init__(self, robot=False):
# own the robot
self.robot = robot
if self.robot: # todo - delete this once beta tested
from robot import robot
self.jetbot = robot.Robot()
self.threshold = 1000
# set up mic listening funcs
running = True
self.CHUNK = 2 ** 11
self.RATE = 44100
p = pyaudio.PyAudio()
self.stream = p.open(format=pyaudio.paInt16, channels=2, rate=self.RATE, input=True, frames_per_buffer=self.CHUNK)
# set up audio functions
self.leftAudioQ = [] #Queue(maxsize=1)
self.leftAudioBot = Audioplayer(self.leftAudioQ, 0)
self.rightAudioQ = [] #Queue(maxsize=1)
self.rightAudioBot = Audioplayer(self.rightAudioQ, 1)
# set the ball running
self.running = True
def scale (self, x):
y1 = 0
y2 = 0.3
return y1 + (y2 - y1)*x / 45000
def main(self):
while self.running:
# set up listening stream
data = np.frombuffer(self.stream.read(self.CHUNK, exception_on_overflow=False), dtype=np.int16)
# grab the RMS for left and right channels
peakLeft = np.average(np.abs(data[0])) * 2 - self.threshold
peakRight = np.average(np.abs(data[1])) * 2 - self.threshold
if peakLeft > 0 and peakRight > 0:
if peakLeft > 0:
bars = "#" * int(50 * peakLeft / 2 ** 16)
print("%05d %s" % (peakLeft, bars))
right_speed = 0.3
left_speed = 0
self.leftAudioQ.append(right_speed)
if peakRight > 0:
bars = "=" * int(50 * peakRight / 2 ** 16)
print("%05d %s" % (peakRight, bars))
# round number to 1dp to avoid lots of
left_speed = 0.3
right_speed = 0
# add to Right wheel audio Queue
self.rightAudioQ.append(left_speed)
# change motor speed for each wheel depending on RMS
# if peakLeft > peakRight:
# bars = "#" * int(50 * peakLeft / 2 ** 16)
# print("%05d %s" % (peakLeft, bars))
# right_speed = 0.4 + self.scale(peakLeft)
# self.leftAudioQ.append(right_speed)
# bars = "=" * int(50 * peakRight / 2 ** 16)
# print("%05d %s" % (peakRight, bars))
# # round number to 1dp to avoid lots of
# left_speed = 0.4 + 0.5*self.scale(peakRight)
# # add to Right wheel audio Queue
# self.rightAudioQ.append(left_speed)
# elif peakRight > peakLeft:
# bars = "=" * int(50 * peakRight / 2 ** 16)
# print("%05d %s" % (peakRight, bars))
# # round number to 1dp to avoid lots of
# left_speed = 0.4 + self.scale(peakRight)
# # add to Right wheel audio Queue
# self.rightAudioQ.append(left_speed)
# bars = "#" * int(50 * peakLeft / 2 ** 16)
# print("%05d %s" % (peakLeft, bars))
# right_speed = 0.4 + 0.5*self.scale(peakLeft)
# self.leftAudioQ.append(right_speed)
# else:
# if peakLeft > 0:
# #2000
# bars = "#" * int(50 * peakLeft / 2 ** 16)
# print("%05d %s" % (peakLeft, bars))
# right_speed = 0.4 + self.scale(peakLeft)
# #round(peakLeft / 10000, 1)
# # add to Left wheel audio Queue
# self.leftAudioQ.append(right_speed)
# if peakRight > 0:
# bars = "=" * int(50 * peakRight / 2 ** 16)
# print("%05d %s" % (peakRight, bars))
# # round number to 1dp to avoid lots of
# left_speed = 0.4 + self.scale(peakRight)
# # add to Right wheel audio Queue
# self.rightAudioQ.append(left_speed)
else:
left_speed = 0
right_speed = 0
# send the command to the robot wheels
if self.robot:
self.jetbot.set_motors(right_speed, left_speed) # microphone directiongs get switched when it faces other way from robot
if __name__ == "__main__":
if platform == "linux" or platform == "linux2":
#linux
bot = BBBot1(robot = True)
elif platform == "darwin": # OS X
bot = BBBot1(robot = False)
else:
bot = BBBot1(robot = False) #Windows
bot.main()