aboutsummaryrefslogtreecommitdiff
path: root/guile/starlet/midi-control/base.scm
blob: f0947aa8a219cc956133baef0bbcdd5df9f67d53 (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
;;
;; starlet/midi-control/base.scm
;;
;; Copyright © 2020-2021 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 midi-control base)
  #:use-module (oop goops)
  #:use-module (ice-9 atomic)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 binary-ports)
  #:use-module (srfi srfi-1)
  #:export (start-midi-control
            get-cc-value
            ccval->percent
            percent->ccval
            send-note-on
            send-note-off
            register-midi-note-callback!
            register-midi-cc-callback!
            remove-midi-callback!))


(define cc-arrays (make-atomic-box '()))
(define callback-list (make-atomic-box '()))
(define send-queue (make-atomic-box '()))


(define-class <midi-callback> (<object>)

  (type
   #:init-keyword #:type
   #:getter get-type)

  (channel
   #:init-keyword #:channel
   #:getter get-channel)

  (note-or-cc-number
   #:init-keyword #:note-or-cc-number
   #:getter get-note-or-cc-number)

  (callback
   #:init-keyword #:func
   #:getter get-callback-func))

(define (find-cc-callbacks channel cc-number)
  (filter (lambda (a)
            (and (eq? cc-number (get-note-or-cc-number a))
                 (eq? channel (get-channel a))
                 (eq? 'cc (get-type a))))
          (atomic-box-ref callback-list)))


(define (find-note-callbacks channel note-number)
  (filter (lambda (a)
            (and (eq? note-number (get-note-or-cc-number a))
                 (eq? channel (get-channel a))
                 (eq? 'note (get-type a))))
          (atomic-box-ref callback-list)))


(define (remove-midi-callback! callback)
  (atomic-box-set! callback-list
                   (delq callback
                         (atomic-box-ref callback-list))))


(define (register-midi-callback! type
                                 channel
                                 note-or-cc-number
                                 func)
  (let ((new-callback (make <midi-callback>
                        #:type type
                        #:channel (if channel channel default-channel)
                        #:note-or-cc-number note-or-cc-number
                        #:func func)))
    (atomic-box-set! callback-list
                     (cons new-callback
                           (atomic-box-ref callback-list)))
    new-callback))


(define* (register-midi-note-callback!
          #:key (channel #f) (note-number 1) (func #f) (unique #t))
  (when unique
    (for-each remove-midi-callback! (find-note-callbacks
                                      (if channel channel default-channel)
                                      note-number)))
  (register-midi-callback! 'note channel note-number func))


(define* (register-midi-cc-callback!
          #:key (channel #f) (cc-number 1) (func #f) (unique #t))
  (when unique
    (for-each remove-midi-callback! (find-cc-callbacks
                                      (if channel channel default-channel)
                                      cc-number)))
  (register-midi-callback! 'cc channel cc-number func))


(define enqueue-midi-bytes!
  (lambda bytes
    (let* ((old-queue (atomic-box-ref send-queue))
           (new-queue (append old-queue bytes)))
      (unless (eq? (atomic-box-compare-and-swap! send-queue
                                                 old-queue
                                                 new-queue)
                   old-queue)
        (apply enqueue-midi-bytes! bytes)))))


(define* (send-note-on note
                       #:key (channel #f))
  (when note
    (enqueue-midi-bytes! (+ #b10010000
                            (if channel channel default-channel))
                         note
                         127)))


(define* (send-note-off note
                        #:key (channel #f))
  (when note
    (enqueue-midi-bytes! (+ #b10000000
                            (if channel channel default-channel))
                         note
                         0)))


(define (all-notes-off! channel)
  (let again ((l 0))
    (enqueue-midi-bytes! (+ #b10000000 channel) l 0)
    (unless (= l 127)
      (again (+ l 1)))))


(define (ensure-cc-array channel)
  (let ((old-list (atomic-box-ref cc-arrays)))
    (unless (assq channel old-list)
      (unless (eq?
               old-list
               (atomic-box-compare-and-swap! cc-arrays
                                             old-list
                                             (acons channel
                                                    (make-vector 128 #f)
                                                    old-list)))
        ;; CAS failed - try again
        (ensure-cc-array channel)))))


(define (check-cc-callbacks channel cc-number old-val new-val)
  (for-each (lambda (a) ((get-callback-func a) old-val new-val))
            (find-cc-callbacks channel cc-number)))


(define (handle-cc-change! channel cc-number value)
  (ensure-cc-array channel)
  (let* ((cc-array (assq-ref (atomic-box-ref cc-arrays) channel))
         (old-value (vector-ref cc-array cc-number)))
    (vector-set! cc-array cc-number value)
    (check-cc-callbacks channel cc-number old-value value)))


(define* (get-cc-value cc-number
                       #:key (channel #f))
  (let ((cc-arrays (atomic-box-ref cc-arrays)))
    (let ((ccs (assq-ref cc-arrays
                         (if channel channel default-channel))))
      (if ccs
          (vector-ref ccs cc-number)
          #f))))


(define (check-note-callbacks channel note-number)
  (for-each (lambda (a) ((get-callback-func a)))
            (find-note-callbacks channel note-number)))


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


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


(define default-channel 0)

(define (start-midi-control-real device-name channel)
  (let ((midi-port (open-file device-name "r+0b")))

    ;; Read thread
    (begin-thread
      (with-exception-handler
        (lambda (exn)
          (backtrace)
          (raise-exception exn))
        (lambda ()
          (let again ()

            (let* ((status-byte (get-u8 midi-port))
                   (channel (bit-extract status-byte 0 4))
                   (command (bit-extract status-byte 4 8)))

              (case command

                ;; Note on
                ((9) (let* ((note (get-u8 midi-port))
                            (vel (get-u8 midi-port)))
                       (check-note-callbacks channel note)))

                ;; Control value
                ((11) (let* ((cc-number (get-u8 midi-port))
                             (value (get-u8 midi-port)))
                        (handle-cc-change! channel
                                           cc-number
                                           value))))

              (yield)
              (again))))))

    ;; Write thread
    (begin-thread
      (let again ()
        (let ((bytes-to-send (atomic-box-swap! send-queue '())))
          (for-each (lambda (a)
                      (put-u8 midi-port a)
                      (usleep 1))
                    bytes-to-send)
          (usleep 1000)
          (again))))

    (all-notes-off! default-channel)))


(define midi-running #f)

(define (start-dummy-midi)
  (display "Using dummy MIDI control\n")
  (begin-thread
    (let again ()
      (let ((bytes-to-send (atomic-box-swap! send-queue '())))
        (usleep 1000)
        (again))))
  (set! midi-running #t))

(define* (start-midi-control device-name
                             #:key (channel #f))


  (if midi-running

      (format #t "MIDI already running\n")

      (begin
        (when channel
          (set! default-channel channel))

        (with-exception-handler

          (lambda (exn)
            (format #t "Couldn't start MIDI ~a\n"
                    (exception-irritants exn))
            (start-dummy-midi))

          (lambda ()
            (start-midi-control-real device-name channel)
            (set! midi-running #t))

          #:unwind? #t))))