aboutsummaryrefslogtreecommitdiff
path: root/guile/starlet/open-sound-control/utils.scm
blob: d89665c52bf540069fd1627d4569121507a7cca8 (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
;;
;; starlet/open-sound-control/utils.scm
;;
;; Copyright © 2020-2023 Thomas White <taw@bitwiz.org.uk>
;;
;; This file is part of Starlet.
;;
;; Starlet is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
(define-module (starlet open-sound-control utils)
  #:use-module (starlet attributes)
  #:use-module (starlet playback)
  #:use-module (starlet selection)
  #:use-module (starlet fixture)
  #:use-module (starlet engine)
  #:use-module (starlet state)
  #:use-module (starlet utils)
  #:use-module (open-sound-control client)
  #:use-module (open-sound-control server-thread)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:export (osc-playback-controls
            osc-select-button
            osc-parameter-encoder
            osc-smart-potentiometer
            osc-state-fader
            send-selection-updates-to))


(define* (osc-playback-controls pb server addr go-button stop-button back-button
                                #:key (min-time-between-presses 0.2))

  (let ((time-last-press 0))
    (add-osc-method
      server
      (string-append go-button "/press")
      ""
      (lambda ()
        (let ((time-this-press (hirestime)))
          (if (> time-this-press (+ time-last-press min-time-between-presses))
            (go! pb)
            (display "Too soon after last press!\n"))
          (set! time-last-press time-this-press)))))

  (add-osc-method server (string-append stop-button "/press") "" (lambda () (stop! pb)))
  (add-osc-method server (string-append back-button "/press") "" (lambda () (back! pb)))

  ;; LEDs
  (osc-send addr (string-append back-button "/set-led") 'green)

  (add-and-run-hook!
    (state-change-hook pb)
    (lambda (new-state)

      (if (eq? new-state 'running)
        (osc-send addr (string-append stop-button "/set-led") 'green)
        (osc-send addr (string-append stop-button "/set-led") 'off))

      (cond
        ((eq? new-state 'pause)
         (osc-send addr (string-append go-button "/set-led") 'orange))
        ((eq? new-state 'ready)
         (osc-send addr (string-append go-button  "/set-led") 'green))
        ((eq? new-state 'running)
         (osc-send addr (string-append go-button  "/set-led") 'green))
        (else
          (osc-send addr (string-append go-button  "/set-led") 'off))))

    (playback-state pb)))


(define (osc-select-button fix server addr button)

  (add-osc-method
    server
    (string-append button "/press")
    ""
    (lambda ()
      (toggle-sel fix)))

  (add-and-run-hook!
    selection-hook
    (lambda (sel)
      (if (selected? fix)
        (osc-send addr (string-append button "/set-led") 'orange)
        (osc-send addr (string-append button "/set-led") 'red)))
    (get-selection)))


(define (encoder-inc attr-id n)
  (for-each
    (lambda (fix)
      (let ((attr (find-attr fix attr-id))
            (cval (current-value fix attr-id)))
        (cond
          ((eq? 'continuous (get-attr-type attr))
           (at fix attr-id (+ cval n)))
          ((eq? 'list (get-attr-type attr))
           (if (> n 0)
             (at fix attr-id (next-attr-item attr cval))
             (at fix attr-id (prev-attr-item attr cval)))))))
    (get-selection)))


(define (osc-parameter-encoder attr server addr encoder)

  (add-osc-method server (string-append encoder "/inc") ""
                  (lambda () (encoder-inc attr 3)))

  (add-osc-method server (string-append encoder "/dec") ""
                  (lambda () (encoder-inc attr -3)))

  (add-osc-method server (string-append encoder "/inc-fine") ""
                  (lambda () (encoder-inc attr 1)))

  (add-osc-method server (string-append encoder "/dec-fine") ""
                  (lambda () (encoder-inc attr -1)))

  (add-and-run-hook!
    selection-hook
    (lambda (sel)
      (if (any
            (lambda (fix)
              (fixture-has-attr? fix attr))
            (get-selection))
        (osc-send addr (string-append encoder "/set-led") 'green)
        (osc-send addr (string-append encoder "/set-led") 'off)))
    (get-selection)))


(define (ccval->percent n)
  (/ (* n 100) 127))


(define (osc-state-fader server addr fader state)
  (let ((fader-val 0))
    (register-state!
      (lighting-state
        (state-for-each
          (lambda (fix attr val)
            (at fix attr
                (lambda ()

                  (if (intensity? attr)

                    ;; Intensity parameters get scaled according to the fader
                    (* 0.01 val (ccval->percent fader-val))

                    ;; Non-intensity parameters just get set in our new state,
                    ;; but only if the fader is up!
                    (if (> fader-val 0)
                      val
                      'no-value)))))
          state)))

    (osc-send addr (string-append fader "/enable"))
    (osc-send addr (string-append fader "/set-pickup") 0)
    (add-osc-method server (string-append fader "/value-change") "i"
                    (lambda (v) (set! fader-val v)))))


(define (send-selection-updates-to addr)
  (add-hook!
    selection-hook
    (lambda (sel)
      (osc-send
        addr
        "/starlet/selection/update"
        (get-selection-as-string)))))


(define (fader-congruent vals attrs)
  (mean (map (lambda (val attr)
               (scale-to-range val (get-attr-range attr) '(0 127)))
             vals attrs)))


(define (fader-up-gradients initial-vals
                            attrs
                            congruent-val)
  (map (lambda (initial-val attr)
         (let ((attr-max (cadr (get-attr-range attr))))
           (if (< congruent-val 127)
               (/ (- attr-max initial-val)
                  (- 127 congruent-val))
               0)))
       initial-vals
       attrs))


(define (fader-down-gradients initial-vals
                              attrs
                              congruent-val)
  (map (lambda (initial-val attr)
         (let ((attr-min (car (get-attr-range attr))))
           (if (> congruent-val 0)
               (/ (- initial-val attr-min)
                  congruent-val)
               0)))

       initial-vals
       attrs))


(define (fixtures-with-attr fixture-list attr-name)
  (let ((attrs (map (cut find-attr <> attr-name) fixture-list)))
    (fold (lambda (fix attr old)
            (if attr
              (cons (cons fix (car old))
                    (cons attr (cdr old)))
              old))
          (cons '() '())
          fixture-list attrs)))


(define (current-values fixture-list attr-name)
  (map (lambda (fix)
         (current-value fix attr-name))
       fixture-list))


(define (apply-fader cc-offset
                     attr-name
                     gradients
                     initial-vals
                     fixtures
                     label)
  (for-each
    (lambda (fix initial-val gradient)
      (set-in-state! programmer-state
                     fix
                     attr-name
                     (+ initial-val
                        (* gradient cc-offset))
                     label))
    fixtures
    initial-vals
    gradients))


(define (osc-smart-potentiometer attr-name
                                 server
                                 addr
                                 potentiometer)

  (let ((fixtures '())
        (attrs '())
        (initial-vals '())
        (congruent-val 0)
        (up-gradients '())
        (dn-gradients '()))

    (let ((set-gradients
            (lambda ()
              (unless (nil? fixtures)
                (set! initial-vals (current-values fixtures attr-name))
                (set! congruent-val (fader-congruent initial-vals attrs))
                (set! up-gradients (fader-up-gradients initial-vals attrs congruent-val))
                (set! dn-gradients (fader-down-gradients initial-vals attrs congruent-val))
                (osc-send addr (string-append potentiometer "/set-pickup") congruent-val)))))

      (add-and-run-hook!
        selection-hook
        (lambda (selection)
          (let ((fixtures-attrs (fixtures-with-attr selection attr-name)))
            (if (null? (car fixtures-attrs))
              (osc-send addr (string-append potentiometer "/disable"))
              (begin
                (set! fixtures (car fixtures-attrs))
                (set! attrs (cdr fixtures-attrs))
                (set-gradients)
                (osc-send addr (string-append potentiometer "/enable"))))))
        (get-selection))

      (add-osc-method
        server
        (string-append potentiometer "/value-change")
        "i"
        (lambda (new-cc-value)
          (cond
            ((> new-cc-value congruent-val)
             (apply-fader (- new-cc-value congruent-val)
                          attr-name
                          up-gradients
                          initial-vals
                          fixtures
                          potentiometer))
            ((<= new-cc-value congruent-val)
             (apply-fader (- new-cc-value congruent-val)
                          attr-name
                          dn-gradients
                          initial-vals
                          fixtures
                          potentiometer)))))

      (add-update-hook!
        programmer-state
        (lambda (source)
          (unless (eq? source potentiometer)
            (set-gradients)))))))