-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
236 lines (215 loc) · 8.27 KB
/
main.rkt
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
#lang racket
(require ffi/unsafe
ffi/unsafe/atomic
ffi/unsafe/define
ffi/unsafe/define/conventions)
(provide RtMidiApi
RtMidiErrorType
rtmidi-list-compiled-apis
rtmidi-get-compiled-api
rtmidi-api-name
rtmidi-api-display-name
rtmidi-compiled-api-by-name
rtmidi-list-port-names
rtmidi-list-all-port-names
rtmidi-open-port
rtmidi-open-virtual-port
rtmidi-close-port
rtmidi-get-port-count
rtmidi-get-port-name
rtmidi-in-create-default
rtmidi-in-create
rtmidi-in-free
rtmidi-in-get-current-api
rtmidi-in-set-callback
rtmidi-in-cancel-callback
rtmidi-in-ignore-types
rtmidi-in-get-message
rtmidi-out-create-default
rtmidi-out-create
rtmidi-out-free
rtmidi-out-get-current-api
rtmidi-out-send-message)
; TODO: Adapt this to load both on Linux, OSX and Windows
(define rtmidi-lib (ffi-lib "librtmidi" '("5" #f)))
(define-ffi-definer define-rtmidi
rtmidi-lib
#:make-c-id convention:hyphen->underscore)
(define-cstruct _RtMidiWrapper ([ptr _pointer]
[data _pointer]
[ok _bool]
[msg _string]))
(define _RtMidiPtr _RtMidiWrapper-pointer)
(define _RtMidiInPtr _RtMidiWrapper-pointer)
(define _RtMidiOutPtr _RtMidiWrapper-pointer)
(define RtMidiApiList '(RTMIDI_API_UNSPECIFIED
RTMIDI_API_MACOSX_CORE
RTMIDI_API_LINUX_ALSA
RTMIDI_API_UNIX_JACK
RTMIDI_API_WINDOWS_MM
RTMIDI_API_RTMIDI_DUMMY
RTMIDI_API_NUM))
(define RtMidiApi (_enum RtMidiApiList))
(define RtMidiErrorType
(_enum '(RTMIDI_ERROR_WARNING
RTMIDI_ERROR_DEBUG_WARNING
RTMIDI_ERROR_UNSPECIFIED
RTMIDI_ERROR_NO_DEVICES_FOUND
RTMIDI_ERROR_INVALID_DEVICE
RTMIDI_ERROR_MEMORY_ERROR
RTMIDI_ERROR_INVALID_PARAMETER
RTMIDI_ERROR_INVALID_USE
RTMIDI_ERROR_DRIVER_ERROR
RTMIDI_ERROR_SYSTEM_ERROR
RTMIDI_ERROR_THREAD_ERROR)))
; Helper function to help retrieve compiled apis
(define (rtmidi-list-compiled-apis)
(let ((apis-bytes (make-bytes 6 0)))
(rtmidi-get-compiled-api apis-bytes 6)
(let ((apis-list
(map (lambda (e)
(list-ref RtMidiApiList e))
(filter positive? (bytes->list apis-bytes)))))
apis-list)))
; rtmidi_c exported functions related to compiled APIs
(define-rtmidi rtmidi-get-compiled-api (_fun _bytes _int -> _int))
(define-rtmidi rtmidi-api-name (_fun RtMidiApi -> _string))
(define-rtmidi rtmidi-api-display-name (_fun RtMidiApi -> _string))
(define-rtmidi rtmidi-compiled-api-by-name (_fun _string -> RtMidiApi))
; Helper functions to list midi ports
(define (rtmidi-list-port-names api)
(let ([total-ports (rtmidi-get-port-count api)])
(let append-port ([l '()] [n (- total-ports 1)])
(if (natural? n)
(append-port
(append (list (list n (rtmidi-get-port-name api n))) l)
(- n 1))
l))))
(define (rtmidi-list-all-port-names)
(let ([midi-in (rtmidi-in-create-default)]
[midi-out (rtmidi-out-create-default)])
(let ([insert-inout
(lambda (dir)
(lambda (port)
(append
(list dir (car port))
(cdr port))))])
(let ([ports-list
(append
(map (insert-inout 'in)
(rtmidi-list-port-names midi-in))
(map (insert-inout 'out)
(rtmidi-list-port-names midi-out)))])
(rtmidi-in-free midi-in)
(rtmidi-out-free midi-out)
ports-list))))
; rtmidi_c exported functions related to generic port operations
(define-rtmidi rtmidi-open-port (_fun _RtMidiPtr _int _string -> _void))
(define-rtmidi rtmidi-open-virtual-port (_fun _RtMidiPtr _string -> _void))
(define-rtmidi rtmidi-close-port (_fun _RtMidiPtr -> _void))
(define-rtmidi rtmidi-get-port-count (_fun _RtMidiPtr -> _int))
(define-rtmidi rtmidi-get-port-name (_fun _pointer _int -> _string))
; rtmidi_c exported functions related to midi input specifically
(define-rtmidi rtmidi-in-create-default (_fun -> _RtMidiInPtr))
(define-rtmidi rtmidi-in-create (_fun RtMidiApi _string _int -> _RtMidiInPtr))
(define-rtmidi rtmidi-in-free (_fun _RtMidiInPtr -> _void))
(define-rtmidi rtmidi-in-get-current-api (_fun _RtMidiPtr -> RtMidiApi))
; MIDI In callback multi thread wrapper (not exported)
; This is needed because RtMidi creates a thread for each Midi Api
; instantiated and callback functions need to run inside those
; threads and so data received has to be sent to Racket main
; scheduler thread.
; Taken from https://gist.github.com/dchest/718858
; This part is the most complex of the ffi definition and requires
; an explanation:
; RtMidi starts a new thread for each midi api instantiated, and
; callback function sent from Racket's side will be called from that
; thread. That generates an error, as Racket expects that function
; to be called only from its own thread.
; What it's done here is that Racket allows us to set a proxy
; callback function with #:async-apply enqueue in _RtMidiCCallback
; definition.
(define sema (make-semaphore))
(define queue null)
(define (enqueue thunk)
(set! queue (append queue (list thunk)))
(semaphore-post sema))
(define (dequeue)
(semaphore-wait sema)
(start-atomic)
(let ([v (car queue)])
(set! queue (cdr queue))
(end-atomic)
v))
; rtmidi_c callback function definition modified to use a proxy
; function, enqueue, that is called with racket's side defined
; callback. Enqueue pushes that callback function to a queue
; that will be called when
(define _RtMidiCCallback
(_fun #:async-apply enqueue
_double _pointer _size _racket -> _void))
; These two functions are the real rtmidi_c functions but are
; used internally, not exported.
(define rtmidi_in_set_callback
(get-ffi-obj
"rtmidi_in_set_callback" rtmidi-lib
(_fun _RtMidiInPtr _RtMidiCCallback _racket -> _void)))
(define rtmidi_in_cancel_callback
(get-ffi-obj
"rtmidi_in_cancel_callback" rtmidi-lib
(_fun _RtMidiInPtr -> _void)))
; Callbacks are registered when calling rtmidi-in-set-callback
; to allow creating a background thread when a callback is
; inserted, and killing that thread when no more callbacks are
; registered.
(define receiver-thread null)
(define setted-callbacks null)
(define (rtmidi-in-set-callback midi-ptr callback user-data)
(when (member midi-ptr setted-callbacks)
(rtmidi-in-cancel-callback midi-ptr))
(rtmidi_in_set_callback
midi-ptr
;callback
(lambda (ts msg size udata)
(let ((bs (make-bytes size)))
(let copy-bytes ([offset 0])
(when (< offset size)
(bytes-set! bs offset (ptr-ref msg _byte offset))
(copy-bytes (+ offset 1))))
(callback ts bs size udata)))
user-data)
(set! setted-callbacks (append setted-callbacks (list midi-ptr)))
; If this is the first callback set, start the receiver thread
(when (= (length setted-callbacks) 1)
(begin
(set! receiver-thread
(thread
(lambda ()
(let loop ()
(let ((thunk (dequeue)))
(thunk)
(loop)))))))))
(define (rtmidi-in-cancel-callback midi-ptr)
(rtmidi_in_cancel_callback midi-ptr)
(set! setted-callbacks (remove midi-ptr setted-callbacks))
; If this was the last callback, stop the receiver thread
(when (= (length setted-callbacks) 0)
(when (thread? receiver-thread) (kill-thread receiver-thread))))
; exported rtmidi_c function to ignore certain message types
; in registered callback for that midi api pointer
(define-rtmidi rtmidi-in-ignore-types (_fun _RtMidiInPtr _bool _bool _bool -> _void))
; for getting a midi message, a 1024 bytes buffer is created and
; only the filled part (indicated by len) is returned
(define-rtmidi rtmidi-in-get-message
(_fun (in-ptr) ::
(in-ptr : _RtMidiInPtr)
(msg : (_bytes o 1024))
(len : (_ptr o _size))
-> (res : _double)
-> (list (subbytes msg 0 len) res)))
; rtmidi_c exported functions related to midi out api
(define-rtmidi rtmidi-out-create-default (_fun -> _RtMidiOutPtr))
(define-rtmidi rtmidi-out-create (_fun RtMidiApi _string -> _RtMidiOutPtr))
(define-rtmidi rtmidi-out-free (_fun _RtMidiOutPtr -> _void))
(define-rtmidi rtmidi-out-get-current-api (_fun _RtMidiPtr -> RtMidiApi))
(define-rtmidi rtmidi-out-send-message (_fun _RtMidiOutPtr _bytes _int -> _int))