-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanim_beatdetect.py
executable file
·227 lines (169 loc) · 5.88 KB
/
anim_beatdetect.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
#!/usr/bin/env python3
import time
import math
import random
import threading
import aubio
import numpy as np
import sounddevice as sd
from dmx import *
from gamepad import *
from beatserver import *
class Animation:
"""
Store animation state and update the animation
"""
def __init__(self, dmx, pad):
self.head1 = MiniGobo9CH(name="head1", chan_no=32)
self.head2 = MiniGobo9CH(name="head2", chan_no=64)
self.heads = [self.head1, self.head2]
self.fix1 = RGB36(name="fix1", chan_no=100)
self.fix2 = RGB36(name="fix2", chan_no=110)
self.fix3 = RGB36(name="fix3", chan_no=120)
self.fix4 = RGB36(name="fix4", chan_no=130)
self.fixs = [self.fix1, self.fix2, self.fix3, self.fix4]
# Blacklight fixture
self.uv = RGBW54(name="uv", chan_no=10)
self.strip = LedStrip4CH(name="strip", chan_no=256)
for head in self.heads:
dmx.add_device(head)
for fix in self.fixs:
dmx.add_device(fix)
dmx.add_device(self.uv)
dmx.add_device(self.strip)
self.pad = pad
self.mode = 'normal'
def gen_sequence(self, num_steps, num_elems):
"""
Ideally what we want is a vector
[num_steps, num_elems, 4]
We could start by just genetating colors and then masking
"""
seq = np.zeros(shape=(num_steps, num_elems, 4))
for step in range(0, num_steps):
# Pick one color for this step
color = random_rgbw()
# Generate a mask with at least one nonzero element
while True:
mask = np.random.choice(a=[0, 1], size=(num_elems,), p=[0.5, 0.5])
if mask.any():
break
for elem in range(0, num_elems):
seq[step, elem] = color * mask[elem]
return seq
def update(self, beat, beat_no):
"""
Update the animation
"""
t = time.time()
if self.pad.event_avail():
type, number, value = self.pad.read_event()
#print('type={}, value={}, number={}'.format(type, value, number))
#time.sleep(0.2)
if type == 'button':
if number == 0: # Green
self.mode = 'purple'
if number == 1: # Red
self.mode = 'red'
if number == 2 and value == 1: # Blue
self.uv.rgbw = np.array([1, 1, 1, 1])
if self.uv.dimming:
self.uv.dimming = 0
else:
self.uv.dimming = 1
if number == 3: # Yellow
self.mode = 'normal'
# Right shoulder:
# Strobe until released
if number == 5 and value == 1:
self.mode = 'strobe'
self.start_beat = beat_no
if number == 5 and value == 0:
self.mode = 'normal'
# Strobe mode
if self.mode == 'strobe':
for head in self.heads:
head.speed = 1
head.dimming = 1
head.gobo = 2
head.color = 0
head.strobe = 1
self.heads[0].pan = 0.30
self.heads[0].tilt = 0.00
self.heads[1].pan = 0.37
self.heads[1].tilt = 0.00
self.uv.dimming = 0
self.strip.ch1 = 0
for fix in self.fixs:
fix.rgb = np.array([1, 1, 1])
fix.strobe = 0.2
return
if beat:
rgb = random_rgb()
if self.mode == 'red':
rgb = np.array([1, 0, 0])
if self.mode == 'purple':
rgb = np.array([1, 0, 1])
fixs = random.choices(self.fixs, k = random.randint(1, 4))
for fix in fixs:
fix.rgb = rgb
fix.strobe = 0
for head in self.heads:
head.dimming = 1
head.speed = 0.01
head.strobe = 0
if beat_no % 4 == 0:
head.color = random.randint(0, 7)
head.gobo = random.randint(0, 7)
if self.mode == 'red':
head.color = 1
if self.mode == 'purple':
head.color = 7
if beat_no % 4 == 0:
head.pan = random.uniform(0.5, 0.9)
head.tilt = random.uniform(0.7, 1.0)
self.strip.ch1 = 0.7
else:
# Decay
for fix in self.fixs:
fix.rgb = fix.rgb * 0.86
self.strip.ch1 = self.strip.ch1 * 0.83
#############################################################################
dmx = DMXUniverse()
pad = GamePad()
beatserver = BeatServer()
anim = Animation(dmx, pad)
dmx.start_dmx_thread()
samplerate = 44100
win_s = 1024 # fft size
hop_s = win_s // 2 # hop size
a_tempo = aubio.tempo("default", win_s, hop_s, samplerate)
stream = sd.InputStream(samplerate=samplerate, blocksize=400, channels=1, dtype=np.float32, latency='low')
stream.start()
beat_no = 0
loud_vals = []
while True:
beat_detected = False
samples, overflowed = stream.read(hop_s)
samples = samples.squeeze()
# Amplify the volume a bit
samples = samples * 5
loudness = np.std(samples)
loud_vals.append(loudness)
if (len(loud_vals) > 500):
loud_vals.pop(0)
print(loudness)
# Loudness threshold for beat detection
# Stops beats when the music stops
if max(loud_vals[-10:]) < 0.05:
beat = False
else:
# Note: we can call o.get_last_s() to get the sample where the beat occurred
beat = a_tempo(samples)
anim.update(beat, beat_no)
if beat:
print('|' * 40)
beatserver.beat()
beat_no += 1
stream.end()
server.stop()