-
Notifications
You must be signed in to change notification settings - Fork 1
/
huelloween.py
257 lines (212 loc) · 7.66 KB
/
huelloween.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Huelloween by Mark Hurley - huelloween.py
https://github.com/markph0204/huelloween
<<license>>
<<description>>
"""
import argparse
import logging
from code import InteractiveConsole
import audioop
import random
import wave
import time
import yaml
import pyaudio # from http://people.csail.mit.edu/hubert/pyaudio/
from phue import Bridge, PhueRegistrationException, Light
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
__version__ = '0.1'
def print_audio_devices():
"""
Print a list of available audio devices (microphones)
:return:
"""
p = pyaudio.PyAudio()
i = 0
n = p.get_device_count()
while i < n:
dev = p.get_device_info_by_index(i)
if dev['maxInputChannels'] > 0:
print(str(i) + '. ' + dev['name'])
i += 1
def find_light_by_name(name: str, bridge: Bridge) -> Light:
"""
Query the bridge finding a matching light by name
:param name: Name of light to match
:param bridge: The Hue bridge
:return:
"""
for light in bridge.lights:
if light.name == name:
logger.info("Found light: " + light.name)
return light
return None
def hue_bridge_init(host: str) -> Bridge:
"""
Initializes the Hue Bridge
Prompts to press Bridge button if first time
:param host: String as dotted quad
:return Bridge:
"""
while True:
try:
return Bridge(host)
except PhueRegistrationException as e:
InteractiveConsole().raw_input('Press button on Bridge then hit Enter to try again')
except Exception as e:
exit("Failed to run " + str(e))
def hued_mic_input(input_device: int, bridge: Bridge, light: Light):
"""
Hue light responds to mic audio input
:param input_device:
:param bridge:
:param light:
:return:
"""
chunk = 2048 # Change if too fast/slow, never less than 1024
scale = 100 # Change if too dim/bright
exponent = 4 # Change if too little/too much difference between loud and quiet sounds
# CHANGE THIS TO CORRECT INPUT DEVICE
# Enable stereo mixing in your sound card
# to make you sound output an input
# Use list_devices() to list all your input devices
device = input_device
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=chunk,
input_device_index=device)
# b = Bridge('10.0.1.2')
# b.set_light(4, 'on', False)
bridge.set_light(light.light_id, {'on': True, 'bri': 100, 'transitiontime': 0})
print("Starting, use Ctrl+C to stop")
try:
last_val = 0
while True:
data = stream.read(chunk)
rms = audioop.rms(data, 2)
level = min(rms / (2.0 ** 16) * scale, 1.0)
level = level ** exponent
level = int(level * 255)
# print(level)
bright = level if (level > 0 and level <= 255) else 1
ttime = 4
# bright = 128
# print('{0} | {1}'.format(level, bright))
if last_val != bright:
stream.stop_stream()
bridge.set_light(light.light_id,
{'bri': bright, 'transitiontime': ttime, 'sat': random.randint(0, 128)})
stream.start_stream()
last_val = bright
except KeyboardInterrupt:
pass
finally:
print("\nStopping")
stream.close()
p.terminate()
bridge.set_light(4, 'on', False)
def hued_wav_file(sound_file: str, bridge: Bridge, light: Light):
"""
Hue light responds to WAV sound file
:param sound_file:
:param bridge:
:param light:
:return:
"""
chunk = 2048 # Change if too fast/slow, never less than 1024
scale = 10 # Change if too dim/bright 50
exponent = 2 # Change if too little/too much difference between loud and quiet sounds
bridge.set_light(light.light_id,
{'on': True, 'bri': 0, 'sat': 0, 'transitiontime': 0})
wf = wave.open(sound_file, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(chunk)
old_bright = 0
try:
while len(data) != 0:
stream.write(data)
rms = audioop.rms(data, 2)
level = min(rms / (2.0 ** 16) * scale, 1.0)
level = level ** exponent
level = int(level * 254)
# print(level)
# bright = level if (level >= 0 and level <= 254) else 1
bright = level
# print("%s | %s" % (level, bright))
# print("bright: %s" % bright)
diff = old_bright - bright
if abs(diff) > 5:
# print("-> update hue: (%s-%s) %s" % (old_bright, bright, diff))
bridge.set_light(light.light_id,
{'bri': bright, 'transitiontime': 0})
old_bright = bright
# read in the next chunk of data
data = wf.readframes(chunk)
finally:
stream.stop_stream()
stream.close()
p.terminate()
wf.close()
bridge.set_light(light.light_id,
{'on': False, 'bri': 0, 'sat': 0, 'transitiontime': 0})
def load_config():
with open("config.yml", "r") as ymlfile:
cfg = yaml.load(ymlfile)
return cfg
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--list-audio-devices', help='List audio devices',
dest='print_audio_devices', action='store_true')
parser.add_argument('--test-mic', action='store_true',
help='Test using microphone and one light')
parser.add_argument('--test-sound', action='store_true',
help='Test using audio file and one light')
args = parser.parse_args()
if args.print_audio_devices:
print_audio_devices()
exit(0)
config = load_config()
# print(yaml.dump(config))
host = config['host']
sounds = config['sounds']
lights = config['lights']
logger.info("Attempting to set up your Phillips Hue Bridge")
bridge = hue_bridge_init(host)
assert bridge is not None, "bridge cannot be found"
if args.test_mic:
test_config = config['test_mode']
light = find_light_by_name(test_config['light'], bridge)
audio_input = test_config['audio_device']
hued_mic_input(audio_input, bridge, light)
exit(0)
if args.test_sound:
test_config = config['test_mode']
light = find_light_by_name(test_config['light'], bridge)
sound_input = test_config['sound']
hued_wav_file(sound_input, bridge, light)
exit(0)
assert host is not None, "no host specified - must configure"
assert sounds is not None and len(sounds) > 0, "no sounds specified - need at least one"
assert lights is not None and len(lights) > 0, "no lights specified - need at least one"
# -------------- NORMAL START ---------------
RUNNING = True
while RUNNING:
random_light = random.randint(0, len(lights) - 1)
light = find_light_by_name(lights[random_light], bridge)
random_sound = random.randint(0, len(sounds) - 1)
sound = sounds[random_sound]
logger.info("Play %s on %s", sound, light)
hued_wav_file(sound, bridge, light)
sleep = 10
logger.info("Sleeping %s secs", sleep)
time.sleep(sleep)