aboutsummaryrefslogtreecommitdiff
path: root/guile/starlet/midi-control/base.scm
blob: 08310ae7acea3d21535885d1f5ae16d6d2723dba (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
;;
;; 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 (make-midi-controller
            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-class <midi-control-surface> (<object>)
  (cc-values
    #:init-form (make-vector 128 #f)
    #:getter get-cc-values)

  (channel
    #:init-form (error "MIDI channel must be specified for controller")
    #:init-keyword #:channel
    #:getter get-channel)

  (callbacks
    #:init-form (make-atomic-box '())
    #:getter get-callbacks)

  (send-queue
    #:init-form (make-atomic-box '())
    #:getter get-send-queue))


(define-class <midi-callback> (<object>)
  (type
   #:init-keyword #:type
   #:getter get-type)

  (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 controller cc-number)
  (filter (lambda (a)
            (and (eq? cc-number (get-note-or-cc-number a))
                 (eq? 'cc (get-type a))))
          (atomic-box-ref (get-callbacks controller))))


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


(define (remove-midi-callback! controller callback)
  (when controller
    (atomic-box-set! (get-callbacks controller)
                     (delq callback
                           (atomic-box-ref (get-callbacks controller))))))


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


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


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


(define enqueue-midi-bytes!
  (lambda (controller . bytes)
    (let* ((send-queue (get-send-queue controller))
           (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! (cons controller bytes))))))


(define* (send-note-on controller note)
  (when (and controller note)
    (enqueue-midi-bytes! controller
                         (+ #b10010000 (get-channel controller))
                         note
                         127)))


(define* (send-note-off controller note)
  (when (and controller note)
    (enqueue-midi-bytes! controller
                         (+ #b10000000 (get-channel controller))
                         note
                         0)))


(define (all-notes-off! controller)
  (for-each (lambda (l)
              (enqueue-midi-bytes! controller
                                   (+ #b10000000 (get-channel controller))
                                   l
                                   0))
            (iota 128)))


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


(define (handle-cc-change! controller cc-number value)
  (let* ((ccvals (get-cc-values controller))
         (old-value (vector-ref ccvals cc-number)))
    (vector-set! ccvals cc-number value)
    (check-cc-callbacks controller cc-number old-value value)))


(define* (get-cc-value controller cc-number)
  (if controller
    (vector-ref (get-cc-values controller) cc-number)
    #f))


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


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


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


(define (make-midi-controller-real device-name channel)
  (let ((controller (make <midi-control-surface>
                          #:channel 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 controller note)))

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

                (yield)
                (again))))))

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

      (all-notes-off! controller)
      controller)))


(define* (make-midi-controller device-name channel)
  (with-exception-handler

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

    (lambda ()
      (make-midi-controller-real device-name channel))

    #:unwind? #t))