Files @ 84a9527508c6
Branch filter:

Location: light9/light9/effect/effecteval.py

drewp@bigasterisk.com
some new effects
Ignore-this: 16a5592769171a2426c0e317b1e0401a
  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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
from rdflib import Literal, URIRef, Namespace
from light9.namespaces import L9, DEV
from webcolors import rgb_to_hex, hex_to_rgb
from colorsys import hsv_to_rgb
import math
from noise import pnoise1
import logging
from light9.effect.settings import DeviceSettings
from light9.effect.scale import scale
from typing import Dict, Tuple, Any
from PIL import Image
import random


SKY = Namespace('http://light9.bigasterisk.com/theater/skyline/device/')

random.seed(0)

log = logging.getLogger('effecteval')
log.info("reload effecteval")


def literalColor(rnorm, gnorm, bnorm):
    return Literal(
        rgb_to_hex([int(rnorm * 255),
                    int(gnorm * 255),
                    int(bnorm * 255)]))


def literalColorHsv(h, s, v):
    return literalColor(*hsv_to_rgb(h, s, v))


def nsin(x):
    return (math.sin(x * (2 * math.pi)) + 1) / 2


def ncos(x):
    return (math.cos(x * (2 * math.pi)) + 1) / 2


def nsquare(t, on=.5):
    return (t % 1.0) < on


def lerp(a, b, t):
    return a + (b - a) * t


def noise(t):
    return pnoise1(t % 1000.0, 2)


def clamp(lo, hi, x):
    return max(lo, min(hi, x))


def clamp255(x):
    return min(255, max(0, x))


def _8bit(f):
    if not isinstance(f, (int, float)):
        raise TypeError(repr(f))
    return clamp255(int(f * 255))

class EffectEval(object):
    """
    runs one effect's code to turn effect attr settings into output
    device settings. No state; suitable for reload().
    """

    def __init__(self, graph, effect, simpleOutputs):
        self.graph = graph
        self.effect = effect
        self.simpleOutputs = simpleOutputs

    def outputFromEffect(self, effectSettings, songTime, noteTime):
        """
        From effect attr settings, like strength=0.75, to output device
        settings like light1/bright=0.72;light2/bright=0.78. This runs
        the effect code.
        """
        # both callers need to apply note overrides
        effectSettings = dict(
            effectSettings
        )  # we should make everything into nice float and Color objects too

        strength = float(effectSettings[L9['strength']])
        if strength <= 0:
            return DeviceSettings(self.graph, []), {'zero': True}

        report = {}
        out: Dict[Tuple[URIRef, URIRef], Any] = {}  # (dev, attr): value

        out.update(
            self.simpleOutputs.values(
                self.effect, strength,
                effectSettings.get(L9['colorScale'], None)))

        if self.effect.startswith(L9['effect/']):
            tail = 'effect_' + self.effect[len(L9['effect/']):]
            try:
                func = globals()[tail]
            except KeyError:
                report['error'] = 'effect code not found for %s' % self.effect
            else:
                out.update(func(effectSettings, strength, songTime, noteTime))

        outList = [(d, a, v) for (d, a), v in out.items()]
        return DeviceSettings(self.graph, outList), report


def effect_Curtain(effectSettings, strength, songTime, noteTime):
    return {(L9['device/lowPattern%s' % n], L9['color']):
            literalColor(strength, strength, strength)
            for n in range(301, 308 + 1)}


def effect_animRainbow(effectSettings, strength, songTime, noteTime):
    out = {}
    tint = effectSettings.get(L9['tint'], '#ffffff')
    tintStrength = float(effectSettings.get(L9['tintStrength'], 0))
    tr, tg, tb = hex_to_rgb(tint)
    for n in range(1, 5 + 1):
        scl = strength * nsin(songTime + n * .3)**3
        col = literalColor(
            scl * lerp(nsin(songTime + n * .2), tr / 255, tintStrength),
            scl * lerp(nsin(songTime + n * .2 + .3), tg / 255, tintStrength),
            scl * lerp(nsin(songTime + n * .3 + .6), tb / 255, tintStrength))

        dev = L9['device/aura%s' % n]
        out.update({
            (dev, L9['color']): col,
            (dev, L9['zoom']): .9,
        })
        ang = songTime * 4
        out.update({
            (dev, L9['rx']):
            lerp(.27, .7, (n - 1) / 4) + .2 * math.sin(ang + n),
            (dev, L9['ry']):
            lerp(.46, .52, (n - 1) / 4) + .5 * math.cos(ang + n),
        })
    return out


def effect_auraSparkles(effectSettings, strength, songTime, noteTime):
    out = {}
    tint = effectSettings.get(L9['tint'], '#ffffff')
    print(effectSettings)
    tr, tg, tb = hex_to_rgb(tint)
    for n in range(1, 5 + 1):
        scl = strength * ((int(songTime * 10) % n) < 1)
        col = literalColorHsv((songTime + (n / 5)) % 1, 1, scl)

        dev = L9['device/aura%s' % n]
        out.update({
            (dev, L9['color']): col,
            (dev, L9['zoom']): .95,
        })
        ang = songTime * 4
        out.update({
            (dev, L9['rx']):
            lerp(.27, .8, (n - 1) / 4) + .2 * math.sin(ang + n),
            (dev, L9['ry']):
            lerp(.46, .52, (n - 1) / 4) + .4 * math.cos(ang + n),
        })
    return out


def effect_qpan(effectSettings, strength, songTime, noteTime):
    dev = L9['device/q2']
    dur = 4
    col = scale(scale('#ffffff', strength),
                effectSettings.get(L9['colorScale']) or '#ffffff')
    return {
        (dev, L9['color']): col,
        (dev, L9['focus']): 0.589,
        (dev, L9['rx']): lerp(0.778, 0.291, clamp(0, 1, noteTime / dur)),
        (dev, L9['ry']): 0.5,
        (dev, L9['zoom']): 0.714,
    }


def effect_pulseRainbow(effectSettings, strength, songTime, noteTime):
    out = {}
    tint = effectSettings.get(L9['tint'], '#ffffff')
    tintStrength = float(effectSettings.get(L9['tintStrength'], 0))
    tr, tg, tb = hex_to_rgb(tint)
    for n in range(1, 5 + 1):
        scl = strength
        col = literalColor(
            scl * lerp(nsin(songTime + n * .2), tr / 255, tintStrength),
            scl * lerp(nsin(songTime + n * .2 + .3), tg / 255, tintStrength),
            scl * lerp(nsin(songTime + n * .3 + .6), tb / 255, tintStrength))

        dev = L9['device/aura%s' % n]
        out.update({
            (dev, L9['color']): col,
            (dev, L9['zoom']): .5,
        })
        out.update({
            (dev, L9['rx']): lerp(.27, .7, (n - 1) / 4),
            (dev, L9['ry']): lerp(.46, .52, (n - 1) / 4),
        })
    return out


def effect_aurawash(effectSettings, strength, songTime, noteTime):
    out = {}
    scl = strength
    period = float(effectSettings.get(L9['period'], 125 / 60 / 4))
    if period < .05:
        quantTime = songTime
    else:
        quantTime = int(songTime / period) * period
    noisePos = quantTime * 6.3456

    col = literalColorHsv(noise(noisePos), 1, scl)
    col = scale(col, effectSettings.get(L9['colorScale']) or '#ffffff')

    print(songTime, quantTime, col)

    for n in range(1, 5 + 1):
        dev = L9['device/aura%s' % n]
        out.update({
            (dev, L9['color']): col,
            (dev, L9['zoom']): .5,
        })
        out.update({
            (dev, L9['rx']): lerp(.27, .7, (n - 1) / 4),
            (dev, L9['ry']): lerp(.46, .52, (n - 1) / 4),
        })
    return out


def effect_qsweep(effectSettings, strength, songTime, noteTime):
    out = {}
    period = float(effectSettings.get(L9['period'], 2))

    col = effectSettings.get(L9['colorScale'], '#ffffff')
    col = scale(col, effectSettings.get(L9['strength'], 1))

    for n in range(1, 3 + 1):
        dev = L9['device/q%s' % n]
        out.update({
            (dev, L9['color']): col,
            (dev, L9['zoom']): effectSettings.get(L9['zoom'], .5),
        })
        out.update({
            (dev, L9['rx']):
            lerp(.3, .8, nsin(songTime / period + n / 4)),
            (dev, L9['ry']):
            effectSettings.get(L9['ry'], .2),
        })
    return out


def effect_qsweepusa(effectSettings, strength, songTime, noteTime):
    out = {}
    period = float(effectSettings.get(L9['period'], 2))

    colmap = {
        1: '#ff0000',
        2: '#998888',
        3: '#0050ff',
    }

    for n in range(1, 3 + 1):
        dev = L9['device/q%s' % n]
        out.update({
            (dev, L9['color']):
            scale(colmap[n], effectSettings.get(L9['strength'], 1)),
            (dev, L9['zoom']):
            effectSettings.get(L9['zoom'], .5),
        })
        out.update({
            (dev, L9['rx']):
            lerp(.3, .8, nsin(songTime / period + n / 4)),
            (dev, L9['ry']):
            effectSettings.get(L9['ry'], .5),
        })
    return out


chase1_members = [
    DEV['backlight1'],
    DEV['lip1'],
    DEV['backlight2'],
    DEV['down2'],
    DEV['lip2'],
    DEV['backlight3'],
    DEV['down3'],
    DEV['lip3'],
    DEV['backlight4'],
    DEV['down4'],
    DEV['lip4'],
    DEV['backlight5'],
    DEV['down5Edge'],
    DEV['lip5'],
    #DEV['upCenter'],
]
chase2_members = chase1_members * 10
random.shuffle(chase2_members)


def effect_chase1(effectSettings, strength, songTime, noteTime):
    members = chase1_members + chase1_members[-2:0:-1]

    out = {}
    period = float(effectSettings.get(L9['period'], 2 / len(members)))

    for i, dev in enumerate(members):
        cursor = (songTime / period) % float(len(members))
        dist = abs(i - cursor)
        radius = 3
        if dist < radius:
            col = effectSettings.get(L9['colorScale'], '#ffffff')
            col = scale(col, effectSettings.get(L9['strength'], 1))
            col = scale(col, (1 - dist / radius))

            out.update({
                (dev, L9['color']): col,
            })
    return out


def effect_chase2(effectSettings, strength, songTime, noteTime):
    members = chase2_members

    out = {}
    period = float(effectSettings.get(L9['period'], 0.3))

    for i, dev in enumerate(members):
        cursor = (songTime / period) % float(len(members))
        dist = abs(i - cursor)
        radius = 3
        if dist < radius:
            col = effectSettings.get(L9['colorScale'], '#ffffff')
            col = scale(col, effectSettings.get(L9['strength'], 1))
            col = scale(col, (1 - dist / radius))

            out.update({
                (dev, L9['color']): col,
            })
    return out


def effect_whirlscolor(effectSettings, strength, songTime, noteTime):
    out = {}

    col = effectSettings.get(L9['colorScale'], '#ffffff')
    col = scale(col, effectSettings.get(L9['strength'], 1))

    for n in (1, 3):
        dev = L9['device/q%s' % n]
        scl = strength
        col = literalColorHsv(((songTime / 5) + (n / 5)) % 1, 1, scl)
        out.update({
            (dev, L9['color']): col,
        })

    return out


def effect_orangeSearch(effectSettings, strength, songTime, noteTime):
    dev = L9['device/auraStage']
    return {
        (dev, L9['color']): '#a885ff',
        (dev, L9['rx']): lerp(.65, 1, nsin(songTime / 2.0)),
        (dev, L9['ry']): .6,
        (dev, L9['zoom']): 1,
    }


def effect_Strobe(effectSettings, strength, songTime, noteTime):
    rate = 2
    duty = .3
    offset = 0
    f = (((songTime + offset) * rate) % 1.0)
    c = (f < duty) * strength
    col = rgb_to_hex([int(c * 255), int(c * 255), int(c * 255)])
    return {(L9['device/colorStrip'], L9['color']): Literal(col)}


def effect_lightning(effectSettings, strength, songTime, noteTime):
    devs = [
        L9['device/veryLow1'], L9['device/veryLow2'], L9['device/veryLow3'],
        L9['device/veryLow4'], L9['device/veryLow5'], L9['device/backlight1'],
        L9['device/backlight2'], L9['device/backlight3'],
        L9['device/backlight4'], L9['device/backlight5'], L9['device/down2'],
        L9['device/down3'], L9['device/down4'], L9['device/hexLow3'],
        L9['device/hexLow5'], L9['device/postL1'], L9['device/postR1']
    ]
    out = {}
    col = rgb_to_hex([int(255 * strength)] * 3)
    for i, dev in enumerate(devs):
        n = noise(songTime * 8 + i * 6.543)
        if n > .4:
            out[(dev, L9['color'])] = col
    return out


def sample(img, x, y, repeat=False):
    if 0 <= x < img.width:
        return img.getpixel((x, y))
    elif not repeat:
        return (0, 0, 0)
    else:
        return img.getpixel((x % img.width, y))


def effect_image(effectSettings, strength, songTime, noteTime):
    out = {}
    imgPath = f'cur/anim/{effectSettings[L9["image"]]}'
    t_offset = effectSettings.get(L9['tOffset'], 0)
    pxPerSec = effectSettings.get(L9['pxPerSec'], 30)
    img = Image.open(imgPath)
    x = (noteTime * pxPerSec)

    scl = effectSettings.get(L9['strength'], 1)
    for dev, y in [(SKY['strip1'], 0),
                   (SKY['strip2'], 1),
                   (SKY['strip3'], 2)]:
        color = sample(img, x, y, effectSettings.get(L9['repeat'], False))
        out[(dev, L9['color'])] = scale(rgb_to_hex(color), scl)
    return out

def effect_cyc(effectSettings, strength, songTime, noteTime):
    colorScale = effectSettings.get(L9['colorScale'], '#ffffff')
    r, g, b = map(lambda x: x / 255, hex_to_rgb(colorScale))

    out ={
        (SKY['cycRed1'], L9['brightness']): r,
        (SKY['cycRed2'], L9['brightness']): r,
        (SKY['cycRed3'], L9['brightness']): r,
        (SKY['cycRed4'], L9['brightness']): r,
        (SKY['cycGreen1'], L9['brightness']): g,
        (SKY['cycGreen2'], L9['brightness']): g,
        (SKY['cycGreen3'], L9['brightness']): g,
        (SKY['cycGreen4'], L9['brightness']): g,
        (SKY['cycBlue1'], L9['brightness']): b,
        (SKY['cycBlue2'], L9['brightness']): b,
        (SKY['cycBlue3'], L9['brightness']): b,
        (SKY['cycBlue4'], L9['brightness']): b,
         
        }

    return out

cycChase1_members = [
       SKY['cycRed1'], 
       SKY['cycRed2'], 
       SKY['cycRed3'], 
       SKY['cycRed4'], 
       SKY['cycGreen1'], 
       SKY['cycGreen2'], 
       SKY['cycGreen3'], 
       SKY['cycGreen4'], 
       SKY['cycBlue1'], 
       SKY['cycBlue2'], 
       SKY['cycBlue3'], 
       SKY['cycBlue4'],
    ]
cycChase1_members = cycChase1_members * 20
random.shuffle(cycChase1_members)

def effect_cycChase1(effectSettings, strength, songTime, noteTime):
    colorScale = effectSettings.get(L9['colorScale'], '#ffffff')
    r, g, b = map(lambda x: x / 255, hex_to_rgb(colorScale))
    tintAmount = {'Red': r, 'Green': g, 'Blue': b}

    members = cycChase1_members

    out = {}
    period = float(effectSettings.get(L9['period'], 6 / len(members)))

    for i, dev in enumerate(members):
        cursor = (songTime / period) % float(len(members))
        dist = abs(i - cursor)
        radius = 7
        if dist < radius:
            colorFromUri = str(dev).split('/')[-1].split('cyc')[1][:-1]
            scale = strength * tintAmount[colorFromUri]
            out.update({
                (dev, L9['brightness']): (1 - dist / radius) * scale,
            })
    return out


def effect_parNoise(effectSettings, strength, songTime, noteTime):
    
    colorScale = effectSettings.get(L9['colorScale'], '#ffffff')
    r, g, b = map(lambda x: x / 255, hex_to_rgb(colorScale))
    out = {}
    speed = 10
    gamma = .6
    for dev in [SKY['strip1'], SKY['strip2'], SKY['strip3']]:
        out[(dev, L9['color'])] = scale(rgb_to_hex(
            (_8bit(r * math.pow(max(.01, noise(speed * songTime)), gamma)),
             _8bit(g * math.pow(max(.01, noise(speed * songTime + 10)), gamma)),
             _8bit(b * math.pow(max(.01, noise(speed * songTime + 20)), gamma)))), strength)

    return out