aboutsummaryrefslogtreecommitdiff
path: root/guile/starlet/midi-control/faders.scm
blob: dd37986e617477b5cc0729fac8bc438067c540c6 (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
(define-module (starlet midi-control faders)
  #:use-module (starlet midi-control base)
  #:use-module (starlet base)
  #:use-module (ice-9 receive)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-1)
  #:export (state-on-fader
            sel))


(define (put-state-on-fader cc-number
                            channel
                            state)
  (register-state!
    (lighting-state
      (state-for-each
        (lambda (fix attr val)
          (if (intensity? attr)

              ;; Intensity parameters get scaled according to the fader
              (at fix attr (lambda (time)
                             (let ((cc-val (get-cc-value cc-number
                                                         #:channel channel)))
                               (if cc-val
                                   (* 0.01 val (ccval->percent cc-val))
                                   0))))

              ;; Non-intensity parameters just get set in our new state
              (at fix attr val)))

        state))))


(define* (state-on-fader cc-number state
                         #:key (channel #f))
  (register-midi-cc-callback!
    #:cc-number cc-number
    #:func (lambda (old-val new-val)
             (when (or (eqv? old-val 0)
                       (and (not old-val)
                            (< new-val 10)))
               (put-state-on-fader cc-number channel state)))))


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


(define (fixtures-with-attr fixture-list attr-name)
  (filter (lambda (fix) (find-attr fix attr-name))
          fixture-list))


(define* (at-midi-jogwheel fixture-list attr cc-number
                           #:key (led #f))

  (define (ccval->offset a)
    (if (eq? a 127)
        -1
        1))

  (let ((fixtures (fixtures-with-attr fixture-list attr)))
    (unless (null? fixtures)

      (when led
        (send-note-on led))

      (let ((old-vals (current-values fixtures attr))
            (offset 0))
        (register-midi-cc-callback!
         #:cc-number cc-number
         #:func (lambda (prev-cc-val new-cc-value)
                  (set! offset (+ offset (ccval->offset new-cc-value)))
                  (for-each (lambda (fix old-val)
                              (set-attr! programmer-state
                                         fix
                                         attr
                                         (+ old-val offset)))
                            fixtures old-vals)))))))


(define (in-range a val1 val2)
  (or
   (and (>= a val1)
        (<= a val2))
   (and (>= a val2)
        (<= a val1))))


;; Returns a pair of (low . high), which are the amount of fader
;; space required in the downward and upward directions respectively
(define (fader-space fixtures attr-name)

  (define (attr-max-value attr)
    (cadr (get-attr-range attr)))

  (define (attr-min-value attr)
    (car (get-attr-range attr)))

  (define (distance-above-min fix attr)
    (- (current-value fix (get-attr-name attr))
       (attr-min-value attr)))

  (define (distance-below-max fix attr)
    (- (attr-max-value attr)
       (current-value fix (get-attr-name attr))))

  (fold (lambda (fix prev)
          (let ((attr (find-attr fix attr-name)))
            (cons (max (distance-above-min fix attr)
                       (car prev))
                  (max (distance-below-max fix attr)
                       (cdr prev)))))
        (cons 0 0)
        fixtures))


(define space-down car)
(define space-up cdr)

(define (space-span r)
  (+ (space-down r)
     (space-up r)))

(define (fader-space->congruence r)
  (inexact->exact
    (round
      (* 127 (/ (space-down r)
                (space-span r))))))


(define (range-scale cspace)
  (/ (+ (space-up cspace)
        (space-down cspace))
     127))


(define (conv-fader orig-cc
                    new-cc
                    initial-val
                    control-space)
  (+ initial-val
     (* (range-scale control-space)
        (- new-cc orig-cc))))


(define* (at-midi-fader fixture-list
                        attr-name
                        cc-number
                        #:key
                        (led-incongruent #f)
                        (led #f))

  (let ((fixtures (fixtures-with-attr fixture-list attr-name)))
    (unless (null? fixtures)
      (let* ((control-space (fader-space fixtures attr-name))
             (congruent-val (fader-space->congruence control-space))
             (cc-val (get-cc-value cc-number))
             (congruent (and cc-val (= cc-val congruent-val)))
             (initial-vals (current-values fixture-list attr-name)))

        (if congruent
            (send-note-on led)
            (send-note-on led-incongruent))

        (register-midi-cc-callback!
         #:cc-number cc-number
         #:func (lambda (prev-cc-val new-cc-value)

                  (when congruent
                    (for-each (lambda (fix initial-val)
                                (set-attr! programmer-state
                                           fix
                                           attr-name
                                           (conv-fader congruent-val
                                                       new-cc-value
                                                       initial-val
                                                       control-space)))
                              fixture-list
                              initial-vals))


                  (when (or (and (not prev-cc-val)
                                 (= new-cc-value congruent-val))
                            (and prev-cc-val new-cc-value
                                 (in-range congruent-val
                                           prev-cc-val
                                           new-cc-value)))
                    (set! congruent #t)
                    (send-note-on led))))))))


(define control-map
  '((intensity jogwheel 21 98)
    (pan       jogwheel 0  124)
    (tilt      jogwheel 1  125)
    (cyan      fader    4  (120 84))
    (magenta   fader    5  (121 85))
    (yellow    fader    6  (122 86))
    (cto       fader    7  (123 87))
    (iris      fader    8  (116 80))
    (zoom      fader    9  (117 81))
    (focus     fader    10 (118 82))))


(define (midi-control-attr control-spec fixture-list)
  (cond

   ((eq? 'jogwheel (cadr control-spec))
    (at-midi-jogwheel fixture-list
                      (car control-spec)
                      (caddr control-spec)
                      #:led (cadddr control-spec)))

   ((eq? 'fader (cadr control-spec))
    (at-midi-fader fixture-list
                   (car control-spec)
                   (caddr control-spec)
                   #:led (car (cadddr control-spec))
                   #:led-incongruent (cadr (cadddr control-spec))))))


;; Stuff to clear up when we're done with selected fixtures
(define midi-callbacks '())

(define (flatten-sublists l)
  (fold (lambda (a prev)
          (if (list? a)
              (append a prev)
              (cons a prev)))
        '() l))


(define (sel . fixture-list)

  (define (led-off leds)
    (cond
      ((list? leds)
       (for-each send-note-off leds))
      ((number? leds)
       (send-note-off leds))))

  (for-each remove-midi-callback! midi-callbacks)

  (for-each (lambda (control-spec)
              (led-off (cadddr control-spec)))
            control-map)

  (set! midi-callbacks '())

  (when (car fixture-list)
    (set! midi-callbacks
      (map (lambda (control-spec)
             (midi-control-attr control-spec
                                (flatten-sublists fixture-list)))
           control-map))))