-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwizrealtime-party.py
213 lines (180 loc) · 6.01 KB
/
wizrealtime-party.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
from os import system
from pywizlight import wizlight, PilotBuilder, discovery
from random import random
import asyncio
import aubio
import audioop
import math
import numpy as np
import pyaudio
import time
BULBS = []
SAMPLES = []
VOLUMES = []
RECORDING = [SAMPLES, VOLUMES]
def gen_color(volume, max=100):
threshold = (80/100)*max
br = 100 + int((int(volume)*155)/int(threshold))
if br > 255:
br = 255
case = int(random()*3)
if case == 0:
r= 255
g= random()*24
b= random()*24
elif case == 1:
r= random()*24
g= 255
b= random()*24
elif case == 2:
r= random()*24
g= random()*24
b= 255
return (r,g,b, br)
def beat_detection(data, volume, threshold=50, max=100):
if volume > threshold:
case = int(random()*3)
color = gen_color(volume, max)
bulb_index = int(random()*len(BULBS))
return{
"case" : case ,
"bulb" : BULBS[bulb_index],
"color": color
}
else:
return None
def get_volume(data):
# Calculate the volume in dB
try:
volume = audioop.rms(data, 2)
# volume = 20 * math.log10(volume)
return volume
except:
return 0
async def light_controller(audio_input):
# configuration
# in seconds
record_for = 5
bulb_lock_for = 0.3 # 0.3 -> 200 bpm
refresh_rate = 0.001
min_br = 50
syncronized = True
# variables
# initially, it is set to 100000, it will
# be then set automatically depending on
# how many seconds of samples you want to0
# have record of
_max_sample_count = 1000000
_tared = False
_tareting = False
_tareting_started_at = 0
_vols_count = 0
_max_volume = 0
_average_volume = 0
_min_volume = 0
stop = False
while not stop:
data = audio_input.read(1024)
# while tareting, nothing will happen
if not _tared:
if not _tareting:
_tareting_started_at = time.time()
_tareting = True
if time.time() - _tareting_started_at >= record_for:
_tareting = False
_tared = True
_max_sample_count = len(SAMPLES)
SAMPLES.append(data)
VOLUMES.append(get_volume(data))
if _tareting:
system("clear")
print("Tareting")
continue
for recorder in RECORDING:
if len(recorder) > _max_sample_count:
recorder.pop(0)
_max_volume = np.max(VOLUMES)
_average_volume = np.average(VOLUMES)
_min_volume = np.min(VOLUMES)
system("clear")
print("=======================")
print(f"= Volume: {int(VOLUMES[-1])}")
print(f"= Max: {int(_max_volume)}")
print(f"= Average: {int(_average_volume)}")
print(f"= Min: {int(_min_volume)}")
print(f"= Count: {len(SAMPLES)}")
print(f"= Tarato: {_tared}")
print("=======================")
beat = beat_detection(data, VOLUMES[-1], _average_volume, _max_volume)
if beat is not None:
if syncronized:
for on_bulb in BULBS:
# set bulb to new beat values
if on_bulb["lock_for"] < time.time() - on_bulb["last_lock"]:
on_bulb["lock"] = False
if not on_bulb["lock"]:
on_bulb["br"] = beat["color"][3]
on_bulb["r"] = beat["color"][0]
on_bulb["g"] = beat["color"][1]
on_bulb["b"] = beat["color"][2]
on_bulb["last_lock"] = time.time()
on_bulb["lock"] = True
else:
on_bulb = beat["bulb"]
# set bulb to new beat values
if on_bulb["lock_for"] < time.time() - on_bulb["last_lock"]:
on_bulb["lock"] = False
if not on_bulb["lock"]:
on_bulb["br"] = beat["color"][3]
on_bulb["r"] = beat["color"][0]
on_bulb["g"] = beat["color"][1]
on_bulb["b"] = beat["color"][2]
on_bulb["last_lock"] = time.time()
on_bulb["lock"] = True
for bulb in BULBS:
if bulb["br"] > min_br:
bulb["is_on"] = True
bulb["br"] -= (256-bulb["br"])
await bulb["light"].turn_on(PilotBuilder(
speed = 200,
rgb = (bulb["r"], bulb["g"], bulb["b"]),
brightness = bulb["br"]
))
else:
if bulb["is_on"]:
bulb["lock"] = False
bulb["is_on"] = False
await bulb["light"].turn_off()
time.sleep(refresh_rate)
def main():
# configuration
bulbips = [
"192.168.1.25",
"192.168.1.29",
]
input_rate = 44100
input_channels = 1
input_frames = 1024
# bulb init
for ip in bulbips:
BULBS.append({
"light": wizlight(ip),
"br": 255,
"r": 255, "g": 255, "b": 255,
"lock_for": 0,
"last_lock": 0,
"lock": False,
"is_on": False
})
# microphone socket init
audio_input = pyaudio.PyAudio().open(
format = pyaudio.paInt16,
rate = input_rate,
channels = input_channels,
frames_per_buffer = input_frames,
input = True,
)
loop = asyncio.get_event_loop()
loop.run_until_complete(light_controller(audio_input))
if __name__ == "__main__":
main()