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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#!/usr/bin/env python
# vim: set expandtab ts=4:
import sys
import array, os, os.path
import colorsys
import traceback
from time import time, sleep
from math import sin, cos, pi
from ola.ClientWrapper import ClientWrapper
wrapper = None
loop_count = 0
TICK_INTERVAL = 10 # in ms
universenum = 2
enable = '--enable' in sys.argv[1:]
phase = 0.0
bpm = 120.0
tl = time()
beatsep = 0
def ctlpath(path):
return os.path.join('/tmp', path)
# seamless restart
try:
phase = float(open(ctlpath('.dmxeffects'), 'r').read())
except Exception, e:
traceback.print_exc()
def DmxSent(state):
if not state.Succeeded():
wrapper.Stop()
#
# minimal abstraction... not really used much
#
class DMXTarget(object):
pass
class DMXLEDPAR(DMXTarget):
# NB: relative color adjustments hardcoded here!
def __init__(self, baseaddr, mult = 1.0, max_r = 255, max_g = 144, max_b = 180):
self.baseaddr = baseaddr
self.r = 0.
self.g = 0.
self.b = 0.
self.max_r = max_r
self.max_g = max_g
self.max_b = max_b
self.mult = mult
def getdata(self):
return (self.baseaddr, [0,
max(min(int(self.r * self.mult * self.max_r), 255), 0),
max(min(int(self.g * self.mult * self.max_g), 255), 0),
max(min(int(self.b * self.mult * self.max_b), 255), 0),
0])
class DMXUniverse(list):
def __init__(self, uniid):
list.__init__(self)
self.uniid = uniid
def mergedata(self):
data = array.array('B')
for dev in self:
devdata = dev.getdata()
lastpos = devdata[0] + len(devdata[1])
if len(data) < lastpos:
data.extend([0] * (lastpos - len(data)))
for i in range(devdata[0], lastpos):
data[i] = devdata[1][i - devdata[0]]
return data[1:]
def send(self, wrapper):
md = self.mergedata()
if enable:
wrapper.Client().SendDmx(self.uniid, md, DmxSent)
else:
print ' '.join(['%02x' % i for i in md])
#
# actual effect code
#
class EffectRGBFadeJump(object):
def __init__(self, target, fadespeed = 1./120., phasejump = 0.0):
self.target = target
self.phasejump = phasejump
self.fadespeed = fadespeed
self.control = 1.0
self.camount = 0.0
self.cphase = 0.0
self.jumpv = 0.0
self.jumpdecayset = 0.0
self.jumpdecayfall = 0.9
self.r_lastphase = 0
self.r_phasejump = 0.0
self.r_jumpdecay = 0.0
self.hsvmap = lambda arg, h, s, v: (h, s, v)
def update(self, timestamp):
curphase = beatsep # int(timestamp)
if self.r_lastphase != curphase:
self.r_phasejump += self.phasejump
self.r_lastphase = curphase
self.r_jumpdecay = self.jumpdecayset
phase = (self.r_phasejump + timestamp * self.fadespeed) % 1
mixcorr = (cos((phase * 3) * 2*pi) - 1. / 2.) * 0.33
h, s, v = (phase,
1.0 - self.r_jumpdecay,
(0.8 + mixcorr) * self.control + self.r_jumpdecay * self.jumpv)
h, s, v = self.hsvmap(phase, h, s, v)
(self.target.r, self.target.g, self.target.b) = \
colorsys.hsv_to_rgb(h, s, v)
self.r_jumpdecay *= self.jumpdecayfall
class EffectStaticHSV(object):
def __init__(self, target):
self.target = target
self.h = 0.0
self.s = 0.0
self.v = 0.0
self.r_phasejump = 0.0
self.cphase = 0.0
self.camount = 0.0
def update(self, timestamp):
(self.target.r, self.target.g, self.target.b) = \
colorsys.hsv_to_rgb(self.h, self.s, self.v)
class EffectEffectCross(object):
def __init__(self, group, fadespeed = 1./2., phasejump = 0.0):
self.group = group
self.phasejump = phasejump
self.fadespeed = fadespeed
self.control = 1.0
self.r_lastphase = 0
self.r_phasejump = 0.0
def update(self, timestamp):
curphase = beatsep # int(timestamp)
if self.r_lastphase != curphase:
self.r_phasejump += self.phasejump
self.r_lastphase = curphase
phase = (self.r_phasejump + timestamp * self.fadespeed) % 1
for e in self.group:
e.control = sin((phase + e.cphase) * 2 * pi) * e.camount + 1.0
#
# deployed setup
#
universe = DMXUniverse(universenum)
universe.append(DMXLEDPAR( 1, 0.55))
universe.append(DMXLEDPAR( 6, 0.55))
universe.append(DMXLEDPAR(11, 0.55))
universe.append(DMXLEDPAR(16, 0.55))
universe.append(DMXLEDPAR(21, 0.55))
effects = [
EffectRGBFadeJump(universe[0]),
EffectStaticHSV(universe[1]),
EffectRGBFadeJump(universe[2]), # , fadespeed = 1./12., phasejump = 0.1),
EffectRGBFadeJump(universe[3]),
EffectStaticHSV(universe[4]),
]
effects.append(
EffectEffectCross([effects[0], effects[1], effects[2], effects[3], effects[4]])
)
#
# control loop
#
lastbeat = 0.0
freerun = False
nobeat = True
lastphase = 0.0
bpmhist = []
prevrms = 0.0
def SendDMXFrame():
global phase, tl, beatsep, lastbeat, bpm, bpmhist, freerun, nobeat, lastphase, prevrms
wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)
t = time()
dt = t - tl
tl = t
phase += dt * bpm / 60.
beat = False
try:
if os.access(ctlpath('.dmxbeat'), os.R_OK):
os.unlink(ctlpath('.dmxbeat'))
freerun = False
nobeat = False
beatsep += 1
dbeat = t - lastbeat
if 0.20 < dbeat < 0.90:
nowbpm = 60. / dbeat
bpmhist.insert(0, nowbpm)
bpmhist = bpmhist[:10]
bpmavg = sum(bpmhist) / len(bpmhist)
print 'bpm: %6.1f ' % (bpmavg), ', '.join(['%6.1f' % i for i in bpmhist])
bpm = bpmavg
lastbeat = t
lastphase = phase
beat = True
phasephase = phase - int(phase)
if phasephase > 0.5:
phasephase -= 1.0
print 'phph', phasephase
phase -= phasephase * 0.05
except Exception, e:
traceback.print_exc()
try:
if os.access(ctlpath('.dmxfreebeat'), os.R_OK):
os.unlink(ctlpath('.dmxfreebeat'))
freerun = True
except Exception, e:
traceback.print_exc()
try:
if os.access(ctlpath('.dmxnobeat'), os.R_OK):
os.unlink(ctlpath('.dmxnobeat'))
nobeat = True
freerun = False
except Exception, e:
traceback.print_exc()
if t - lastbeat > (60. / bpm) * 1.8 and not nobeat:
pass
#if not freerun:
# print 'freerun'
#freerun = True
if freerun:
if phase - lastphase >= 1.0:
lastphase = phase
beatsep += 1
beat = True
print 'freebeat', bpm
try:
execfile('dmxcontrol.py')
except Exception, e:
traceback.print_exc()
for e in effects:
e.update(phase)
universe.send(wrapper)
class DummyVerse(object):
def __init__(self):
pass
def Client(self):
return self
def SendDmx(self, uniid, data, cb):
print uniid, data
def Run(self):
while True:
sleep(1./30.)
self.func()
def AddEvent(self, ignore, func):
self.func = func
#wrapper = DummyVerse()
wrapper = ClientWrapper()
wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)
try:
wrapper.Run()
except KeyboardInterrupt:
file(ctlpath('.dmxeffects'), 'w').write(str(phase))
|