blob: 70ec741fd93e6c26e8fe547df67af3a4638b2842 (
plain)
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
(define-module (starlet base)
#:use-module (starlet utils)
#:use-module (starlet colours)
#:use-module (starlet guile-ola)
#:use-module (oop goops)
#:use-module (ice-9 threads)
#:use-module (ice-9 atomic)
#:use-module (ice-9 receive)
#:use-module (ice-9 pretty-print)
#:use-module (ice-9 exceptions)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:export (<fixture>
patch-fixture!
get-attributes
get-fixture-name
find-attr
fixture?
scanout-fixture
<fixture-attribute>
attr-continuous
attr-list
attr-colour
get-attr-type
get-attr-range
get-attr-name
get-attr-home-val
intensity?
continuous-attribute?
colour-attribute?
<starlet-state>
make-empty-state
state-for-each
state-map
clear-state!
print-state
state-source
set-in-state!
state-find
current-state
at
apply-state
show-state
lighting-state
blackout
register-state!
start-ola-output
scanout-freq
percent->dmxval8
percent->dmxval16
hirestime
value->number
programmer-state
sel
current-value
selection-hook
scale-to-range))
(define-class <fixture-attribute> (<object>)
(name
#:init-form (error "Attribute name must be specified")
#:init-keyword #:name
#:getter get-attr-name)
(range
#:init-value '()
#:init-keyword #:range
#:getter get-attr-range)
(type
#:init-value 'continuous
#:init-keyword #:type
#:getter get-attr-type)
(home-value
#:init-value 0
#:init-keyword #:home-value
#:getter attr-home-value))
(define-class <fixture> (<object>)
(name
#:init-form (error "Fixture name must be specified")
#:init-keyword #:name
#:getter get-fixture-name)
(universe
#:init-value #f
#:init-keyword #:uni
#:getter get-fixture-universe
#:setter set-fixture-universe!)
(start-addr
#:init-value #f
#:init-keyword #:sa
#:getter get-fixture-addr
#:setter set-fixture-addr!)
(friendly-name
#:init-value "Fixture"
#:init-keyword #:friendly-name
#:getter get-fixture-friendly-name
#:setter set-fixture-friendly-name!)
(scanout-func
#:init-value (lambda (universe start-addr value set-dmx) #f)
#:init-keyword #:scanout-func
#:getter get-scanout-func))
(define (get-attributes f)
(slot-ref f 'attributes))
(define (fixture? f)
(is-a? f <fixture>))
;; A "state" is just a thin wrapper around a hash table
;; of (fixture . attribute) --> value
(define-class <starlet-state> (<object>)
(hash-table
#:init-form (make-hash-table)
#:getter get-state-hash-table)
(name
#:init-value #f
#:init-keyword #:name
#:getter get-state-name
#:setter set-state-name!))
(define (find-colour state fix)
(let ((col (state-find fix 'colour state)))
(if (eq? 'no-value col)
(let ((home-col (get-attr-home-val fix 'colour)))
(if (eq? 'fixture-does-not-have-attribute home-col)
(raise-exception (make-exception
(make-exception-with-message
"Fixture doesn't have colour attribute")
(make-exception-with-irritants fix)))
home-col))
col)))
(define-method (set-in-state! (state <starlet-state>)
(fix <fixture>)
(attr <colour-component-id>)
new-val)
(let ((current-colour (find-colour state fix))
(colour-component (get-colour-component attr)))
(cond
((eq? colour-component 'cyan)
(let ((orig-colour (colour-as-cmy current-colour)))
(set-in-state! state fix 'colour
(make-colour-cmy new-val
(magenta orig-colour)
(yellow orig-colour)))))
((eq? colour-component 'magenta)
(let ((orig-colour (colour-as-cmy current-colour)))
(set-in-state! state fix 'colour
(make-colour-cmy (cyan orig-colour)
new-val
(yellow orig-colour)))))
((eq? colour-component 'yellow)
(let ((orig-colour (colour-as-cmy current-colour)))
(set-in-state! state fix 'colour
(make-colour-cmy (cyan orig-colour)
(magenta orig-colour)
new-val))))
((eq? colour-component 'red)
(let ((orig-colour (colour-as-rgb current-colour)))
(set-in-state! state fix 'colour
(make-colour-rgb new-val
(green orig-colour)
(blue orig-colour)))))
((eq? colour-component 'green)
(let ((orig-colour (colour-as-rgb current-colour)))
(set-in-state! state fix 'colour
(make-colour-rgb (red orig-colour)
new-val
(blue orig-colour)))))
((eq? colour-component 'blue)
(let ((orig-colour (colour-as-rgb current-colour)))
(set-in-state! state fix 'colour
(make-colour-rgb (red orig-colour)
(green orig-colour)
new-val)))))))
(define-method (set-in-state! (state <starlet-state>)
(fix <fixture>)
(attr <symbol>)
value)
(hash-set! (get-state-hash-table state)
(cons fix attr)
value))
;; List of fixtures and home state (must remain consistent)
(define fixtures (make-atomic-box '()))
;; List of states being scanned out
(define state-list (make-atomic-box '()))
;; The state used to build a new scene for recording
(define programmer-state (make <starlet-state>))
(define (make-empty-state)
(make <starlet-state>))
(define-method (find-attr (fix <fixture>) (attr-name <symbol>))
(find (lambda (a)
(eq? (get-attr-name a)
attr-name))
(slot-ref fix 'attributes)))
(define-method (find-attr (fix <fixture>) (attr-name <colour-component-id>))
(find-attr fix 'colour))
(define-method (get-attr-home-val (fix <fixture>) (attr <symbol>))
(let ((attr-obj (find-attr fix attr)))
(if attr-obj
(attr-home-value attr-obj)
'fixture-does-not-have-attribute)))
(define-method (get-attr-home-val (fix <fixture>) (attr <colour-component-id>))
(extract-colour-component
(get-attr-home-val fix 'colour)
attr))
(define (blackout state)
(state-for-each
(lambda (fix attr val)
(when (intensity? attr)
(set-in-state! state fix attr 0.0)))
state))
;; Set a single attribute to home position
(define (home-attr! state fix attr)
(set-in-state! state
fix
attr
(get-attr-home-val fix attr)))
(define (copy-state state)
(let ((new-state (make-empty-state)))
(state-for-each (lambda (fix attr val)
(set-in-state! new-state
fix
attr
val))
state)
new-state))
(define (intensity? a)
(eq? 'intensity a))
(define (continuous-attribute? aobj)
(eq? 'continuous
(get-attr-type aobj)))
(define (colour-attribute? aobj)
(eq? 'colour
(get-attr-type aobj)))
(define (append-or-replace-named-state orig-list name new-state)
(let ((new-list (map (lambda (st)
(if (eq? (get-state-name st) name)
(begin
new-state)
st))
orig-list)))
;; If there is no state with this name in the list,
;; the replacement above will have no effect.
;; Check again and add in the normal way if so.
(if (find (lambda (st) (eq? (get-state-name st)
name))
new-list)
new-list
(append orig-list (list new-state)))))
(define* (register-state! new-state
#:key (unique-name #f))
(if unique-name
(begin (set-state-name! new-state unique-name)
(atomic-box-set! state-list
(append-or-replace-named-state (atomic-box-ref state-list)
unique-name
new-state)))
(atomic-box-set! state-list
(append (atomic-box-ref state-list)
(list new-state)))))
;; Patch a new fixture
(define* (patch-real name
class
start-addr
#:key (universe 0) (friendly-name "Fixture"))
(let ((new-fixture (make class
#:name name
#:sa start-addr
#:uni universe
#:friendly-name friendly-name)))
(atomic-box-set! fixtures (cons new-fixture
(atomic-box-ref fixtures)))
new-fixture))
(define-syntax patch-fixture!
(syntax-rules ()
((_ name stuff ...)
(define name (patch-real (quote name) stuff ...)))))
;; Helper functions for scanout functions
(define (round-dmx a)
(inexact->exact
(min 255 (max 0 (round a)))))
(define (scale-to-range val orig-range dest-range)
(define (range r)
(- (cadr r) (car r)))
(+ (car dest-range)
(* (range dest-range)
(/ (- val (car orig-range))
(range orig-range)))))
(define (percent->dmxval8 val)
(round-dmx
(scale-to-range val '(0 100) '(0 255))))
(define (percent->dmxval16 val)
(scale-to-range val '(0 100) '(0 65535)))
(define (msb val)
(round-dmx (euclidean-quotient val 256)))
(define (lsb val)
(round-dmx (euclidean-remainder val 256)))
(define (state-for-each func state)
(hash-for-each (lambda (key value)
(func (car key)
(cdr key)
value))
(get-state-hash-table state)))
(define-method (state-find (fix <fixture>)
(attr <symbol>)
(state <starlet-state>))
(hash-ref (get-state-hash-table state)
(cons fix attr)
'no-value))
(define-method (state-find (fix <fixture>)
(attr <colour-component-id>)
(state <starlet-state>))
(let ((col (state-find fix 'colour state)))
(if (eq? 'no-value col)
'no-value
(extract-colour-component col attr))))
(define (state-map func state)
(hash-map->list (lambda (key value)
(func (car key)
(cdr key)
value))
(get-state-hash-table state)))
(define (apply-state state)
"Apply the contents of 'state' to the current state, on top of the \
pre-existing contents."
(state-for-each at state))
(define (show-state state)
"Clear the current state, and apply the contents of 'state'"
(clear-state! (current-state))
(state-for-each at state))
;; Coerce something from a state object into a number for scanout
(define (value->number val time)
(if (procedure? val)
(value->number (val time) time)
val))
(define (clear-state! state)
(hash-clear! (get-state-hash-table state)))
;; Scanout
(define (bytevec->string bv)
(string-join
(map
number->string
(u8vector->list bv))
","))
(define (send-to-ola ola-client universe-buffer-pair)
(let ((uni (car universe-buffer-pair))
(buf (cdr universe-buffer-pair)))
(send-streaming-dmx-data! ola-client uni buf)))
(define (hirestime)
(let ((a (gettimeofday)))
(+ (car a)
(/ (cdr a)
1000000))))
(define (ensure-number value irritating)
(unless (number? value)
(raise-exception (make-exception
(make-exception-with-message "Value is not a number")
(make-exception-with-irritants irritating)))))
(define scanout-freq 0)
(define-generic scanout-fixture)
(define (scanout-loop ola-client start-time count previous-universes)
(let ((universes '()))
;; Helper function for scanout functions to set individual DMX values
(define (set-dmx universe addr value)
(ensure-number value (list universe addr value))
;; Create DMX array for universe if it doesn't exist already
(unless (assq universe universes)
(set! universes (acons universe
(make-ola-dmx-buffer)
universes)))
(set-ola-dmx-buffer! (assq-ref universes universe)
(- addr 1) ; OLA indexing starts from zero
(round-dmx value)))
(for-each
(lambda (fix)
(let ((univ (get-fixture-universe fix))
(addr (get-fixture-addr fix)))
;; Helper function to get a value for this
;; fixture in the current state
(define (get-attr attr-name)
(current-value fix attr-name (hirestime)))
;; Helper function to set 8-bit DMX value
(define (set-chan relative-channel-number value)
(ensure-number value (list fix relative-channel-number value))
(set-dmx univ (+ addr relative-channel-number -1) value))
;; Helper function to set 16-bit DMX value
(define (set-chan-16bit relative-channel-number value)
(ensure-number value (list fix relative-channel-number value))
(set-chan relative-channel-number (msb value))
(set-chan (+ relative-channel-number 1) (lsb value)))
(scanout-fixture fix get-attr set-chan set-chan-16bit)))
(atomic-box-ref fixtures))
;; Send everything to OLA
(for-each (lambda (uni-buf-pair)
(let ((uni (car uni-buf-pair))
(buf (cdr uni-buf-pair)))
(let ((prev-buf (assv-ref previous-universes uni)))
;; Do not send exactly the same data every time,
;; but do send an update once every 100 loops, just to
;; make sure OLA does not forget about us.
(unless (and prev-buf
(ola-dmx-buffers-equal? buf prev-buf)
(not (= count 0)))
(send-streaming-dmx-data! ola-client uni buf)))))
universes)
(usleep 10000)
;; Update scanout rate every 1000 cycles
(if (eq? count 100)
(begin
(set! scanout-freq
(exact->inexact (/ 100
(- (hirestime) start-time))))
(scanout-loop ola-client (hirestime) 0 universes))
(scanout-loop ola-client start-time (+ count 1) universes))))
(define ola-thread #f)
(define (start-ola-output)
(unless ola-thread
(let* ((ola-client (make-ola-streaming-client))
(start-time (hirestime)))
(set! ola-thread
(begin-thread
(with-exception-handler
(lambda (exn)
(display "Error in OLA output thread:\n")
(set! ola-thread #f)
(backtrace)
(raise-exception exn))
(lambda ()
(scanout-loop ola-client start-time 0 '()))
#:unwind? #f))))))
(define (state-has-fix-attr fix attr tnow state)
(let ((val (state-find fix attr state)))
(if (eq? 'no-value val)
#f
(not (eq? 'no-value (value->number val tnow))))))
(define (first-val fix attr tnow state-list)
(let ((first-state (find (lambda (state)
(state-has-fix-attr fix attr tnow state))
state-list)))
(if first-state
(state-find fix attr first-state)
'no-value)))
(define-method (current-value (fix <fixture>) (attr-name <symbol>) tnow)
(let ((programmer-val (state-find fix attr-name programmer-state)))
(if (eq? 'no-value programmer-val)
;; Look in the states
(if (intensity? attr-name)
;; HTP for intensity
(fold (lambda (state prev)
(let ((val (state-find fix attr-name state)))
(if (eq? 'no-value val)
prev
(let ((real-val (value->number val tnow)))
(if (eq? 'no-value real-val)
prev
(max real-val prev))))))
0.0
(atomic-box-ref state-list))
;; Priority order for everything else
(let ((val (first-val fix attr-name tnow (atomic-box-ref state-list))))
(if (eq? 'no-value val)
(get-attr-home-val fix attr-name)
(value->number val tnow))))
;; Use programmer value, if we have it
(value->number programmer-val tnow))))
(define-method (current-value (fix <fixture>) (attr-name <colour-component-id>) tnow)
(let ((colour (current-value fix 'colour tnow)))
(extract-colour-component colour attr-name)))
(define-syntax attr-continuous
(syntax-rules ()
((_ attr-name attr-range attr-home-value)
(make <fixture-attribute>
#:name attr-name
#:range attr-range
#:type 'continuous
#:home-value attr-home-value))))
(define-syntax attr-list
(syntax-rules ()
((_ attr-name attr-allowed-values attr-home-value)
(make <fixture-attribute>
#:name attr-name
#:range attr-allowed-values
#:type 'list
#:home-value attr-home-value))))
(define-syntax attr-colour
(syntax-rules ()
((_ attr-name attr-home-value)
(make <fixture-attribute>
#:name attr-name
#:type 'colour
#:home-value attr-home-value))))
(define current-state (make-parameter programmer-state))
(define-syntax lighting-state
(syntax-rules ()
((_ body ...)
(parameterize ((current-state (make-empty-state)))
body ...
(current-state)))))
(define (partition3 pred1 pred2 input)
(receive (output1 others)
(partition pred1 input)
(receive (output2 others)
(partition pred2 others)
(values output1 output2 others))))
(define (set-fixtures fixtures attr-name value)
(for-each (lambda (fix)
(set-in-state! (current-state)
fix
(car attr-name)
(car value)))
fixtures))
;; (at <fixtures/groups> [<attribute>] <level> [<attribute> <level>...])
;; (at fix1 100) <-- Set intensity of single fixture
;; (at fix1 'intensity 100) <-- Explicit attribute name
;; (at fix1 fix2 100) <-- Multiple fixtures
;; (at fix1 fix2 'pan 36) <-- Multiple fixtures + explicit attribute
;; (at group1 fix1 'intensity 100) <-- Groups can be used instead of fixtures
;; (at fix1 100 'pan 36) <-- Set multiple attributes
;; NB Can't set multiple fixtures and attributes: (at fix1 'pan 35 fix2 'tilt 22)
(define (at . args)
(receive (fixtures attr-name value)
(partition3 fixture? symbol? (flatten-sublists args))
(cond
((nil? value)
(error "at: Value not specified"))
((or (more-than-one value)
(more-than-one attr-name))
(error "at: Only one attribute or value name"))
((and (nil? fixtures)
(nil? attr-name))
(if (nil? selection)
'no-fixtures-selected
(set-fixtures selection '(intensity) value)))
((nil? attr-name)
(set-fixtures fixtures '(intensity) value))
((nil? fixtures)
(if (nil? selection)
'no-fixtures-selected
(set-fixtures selection attr-name value)))
(else
(set-fixtures fixtures attr-name value)))))
(define selection-hook (make-hook 1))
(define selection '())
(define (sel . fixture-list)
(if (nil? fixture-list)
(set! selection '())
(if (not (car fixture-list))
(set! selection '())
(set! selection (flatten-sublists fixture-list))))
(run-hook selection-hook selection))
(define (print-state a)
(pretty-print (state-source a)))
(define (state-source a)
(cons 'lighting-state
(state-map (lambda (fix attr val)
(list 'at
(get-fixture-name fix)
(list 'quote attr)
val))
a)))
|