-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtmidi_interface.rkt
More file actions
461 lines (357 loc) · 17.3 KB
/
rtmidi_interface.rkt
File metadata and controls
461 lines (357 loc) · 17.3 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#lang racket
(require rtmidi)
(require midi-readwrite)
(require control)
(require racket/stream)
;;; For best performance, run using the non-interactive mode of the command line racket interpreter, rather than DrRacket.
;; see the racket documentation for the rtmidi package for additional install steps
;; the following are changes that need to be made to the Makefile for the rtmidi
;; package for racket.
;; system specific install commands are still needed for mac and windows
;; the following lines need to be added
;; +unix: CXXFLAGS += -D__UNIX_JACK__ -ljack
;; +unix: LDFLAGS += -ljack -lpthread
;; +unix: wrap-rtmidi.so
;; +install-unix: unix
;; + cp wrap-rtmidi.so /usr/lib/
;; +install-linux: linux
;; + cp wrap-rtmidi.so /usr/lib/
;; the following are changes that need to be made to the Makefile for RtMidi-2.1.0
;; the following line needs to be commented out
;; - install --mode=644 RtMidi.h RtError.h $(PREFIX)/include
;; until the racket library midi-readwrite is updated with my fix, the following file needs to be changed.
;;
;; /home/USERNAME/.racket/6.8/pkgs/midi-readwrite/midi-readwrite/midi-read.rkt
;;
;; the following changes need to be made
;;
;; (define channel (bitwise-and #x7 next-byte))
;; needs to be changed to
;; (define channel (bitwise-and #xf next-byte))
;;
;; and
;;
;; (define channel (bitwise-and #x7 prior-event-type-byte))
;; needs to be changed to
;; (define channel (bitwise-and #xf prior-event-type-byte))
;;
;; this is to fix an issue where midi channels 9-16 were being mapped onto midi channels 1-8
; to set up a port use the following call sequence
; (define in (make-in-port))
; (define out (make-out-port))
; (open-in-port in "MIDI-DEVICE")
; (open-out-port out "MIDI-DEVICE")
; replace "MIDI-DEVICE" with the appropriate midi device by calling the
; following procedures to find the name of the available devices
; the (open-in-port) and (open-out-port) procedures pick the first
; available port that has a name that contains "MIDI-DEVICE" as a substring
; (list-in-ports in)
; (list-out-ports out)
; the usb to midi cable I have is shown as
; "a2j:Turtle Beach USB MIDI 1x1 [20] (capture): Turtle Beach USB MIDI 1x1 MIDI "
; and
; "a2j:Turtle Beach USB MIDI 1x1 [20] (playback): Turtle Beach USB MIDI 1x1 MIDI "
; for more information about the midi specifications, see www.midi.org/specifications
; refer to the General midi specification level 1
; for info on setting up midi devices and software on linux, refer to
; http://tedfelix.com/linux/linux-midi.html
; for info on setting up midi devices and software on windows, refer to
; http://donyaquick.com/midi-on-windows/
; makes the following procedures available to code that require this file
(provide make-in-port
make-out-port
list-in-ports
list-out-ports
open-in-port
open-out-port
close-in-port
close-out-port
send-midi-message
read-midi-message
note-off
note-on
play-note
poly-key-pressure
control-change
program-change
channel-pressure
pitch-bend
bank-select
channel-volume-change
set-pan
set-expression-controller
set-sustain
set-sostenuto
set-soft-pedal
set-local-control
play-midi-file
play-midi-data
play-midi-track
play-midi-stream
delay-of
type-of
channel-of
first-data-byte-of
second-data-byte-of
channel-all-notes-off
channel-reset-all-controllers
device-all-notes-off
device-reset-all-controllers
; Still need to implement the following:
record-midi-in-to-file
record-midi-out-to-file
echo-midi-out-to-console
echo-midi-in-to-console
echo-midi-in-to-out)
;
;others?
; Create RtMidiIn and RtMidiOut
(define (make-in-port) (make-rtmidi-in))
(define (make-out-port) (make-rtmidi-out))
;list input and output ports
(define (list-in-ports in) (rtmidi-ports in))
(define (list-out-ports out) (rtmidi-ports out))
; Open the specified midi ports for input and output
(define (in-ports-length in) (length (list-in-ports in)))
(define (out-ports-length out) (length (list-out-ports out)))
(define (open-in-port in-port name)
(define i 0)
(for ([j (in-range (in-ports-length in-port))])
#:break (string-contains? (list-ref (list-in-ports in-port) i) name)
(set! i j))
(if (not (= i (in-ports-length in-port)))
(rtmidi-open-port in-port i)
(printf "Port ~a not found" name)))
(define (open-out-port out-port name)
(define i 0)
(for ([j (in-range (out-ports-length out-port))])
#:break (string-contains? (list-ref (list-out-ports out-port) i) name)
(set! i j))
(if (not (= i (out-ports-length out-port)))
(rtmidi-open-port out-port i)
(printf "Port ~a not found" name)))
; Close the in port
(define (close-in-port in-port) (rtmidi-close-port in-port))
; Close the out port
(define (close-out-port out-port) (rtmidi-close-port out-port))
; Sends a midi message to the port
(define (send-midi-message port status data1 data2)
(rtmidi-send-message port (list status data1 data2)))
; Read a midi message from the port
(define (read-midi-message port)
(sync port))
;; accessors for midi events
(define (delay-of midi-event)
(car midi-event))
(define (type-of midi-event)
(list-ref (cadr midi-event) 0))
(define (channel-of midi-event)
(list-ref (cadr midi-event) 1))
(define (first-data-byte-of midi-event)
(list-ref (cadr midi-event) 2))
(define (second-data-byte-of midi-event)
(list-ref (cadr midi-event) 3))
;; all values start from 0
;; channel 1 is channel number 0, channel 10 (purcusion) is channel number 9
; Procedures for sending midi messages
(define (note-off port channel note) (send-midi-message port (+ 128 channel) note 0))
(define (note-on port channel note velocity) (send-midi-message port (+ 144 channel) note velocity))
; plays a note for a specified length of time in seconds, length can have arbitrary precision
; 24 seems to be the lowest note
; 96 seems to be the highest note
(define (play-note port channel note velocity length)
(thread (lambda () (note-on port channel note velocity)(sleep length)(note-off port channel note))))
; polyphonic key pressure (aftertouch)
(define (poly-key-pressure port channel note pressure) (send-midi-message port (+ 160 channel) note pressure))
(define (control-change port channel controller value) (send-midi-message port (+ 176 channel) controller value))
; program change is tone select
(define (program-change port channel program) (send-midi-message port (+ 192 channel) program 0))
; channel-pressure (aftertouch)
(define (channel-pressure port channel pressure) (send-midi-message port (+ 208 channel) pressure 0))
; pitch bend value is a 14 bit value, center (no change) is 8192 or lsb=0 msb=40
(define (pitch-bend port channel lsb msb) (send-midi-message port (+ 224 channel) lsb msb))
; control changes and mode changes
(define (channel-all-notes-off port channel)
(send-midi-message port (+ 176 channel) 123 0))
(define (channel-reset-all-controllers port channel)
(send-midi-message port (+ 176 channel) 121 0))
(define (device-all-notes-off port)
(for ([i (in-range 0 15)])
(channel-all-notes-off port i)))
(define (device-reset-all-controllers port)
(for ([i (in-range 0 15)])
(channel-reset-all-controllers port i)))
(define (bank-select port channel bank tone)
(thread (lambda ()
(send-midi-message port (+ 176 channel) 0 bank)
(send-midi-message port (+ 176 channel) 32 0)
(program-change port channel tone))))
(define (channel-volume-change port channel volume)
(send-midi-message port (+ 176 channel) 7 volume))
;left-right balance
(define (set-pan port channel pan)
(send-midi-message port (+ 176 channel) 10 pan))
; expression controller
(define (set-expression-controller port channel expression-value)
(send-midi-message port (+ 176 channel) 11 expression-value))
; hold1 (sustain) pedal. set on to #t to enable sustain, set on argument to #f to turn off
(define (set-sustain port channel on)
(send-midi-message port (+ 176 channel) 64 (cond ((eq? on #f) 0)
((eq? on #t) 64)
(else on))))
;(if on 64 0)))
; Sostenuto pedal
(define (set-sostenuto port channel on)
(send-midi-message port (+ 176 channel) 66 (cond ((eq? on #f) 0)
((eq? on #t) 64)
(else on))))
;(if on 64 0)))
; Soft pedal
(define (set-soft-pedal port channel on)
(send-midi-message port (+ 176 channel) 67 (cond ((eq? on #f) 0)
((eq? on #t) 64)
(else on))))
;(if on 64 0)))
; local control on/off (should the keyboard make sounds when keys are pressed?)
; The piano we will be using for the demo doesn't recognize this message
(define (set-local-control port channel on)
(send-midi-message port (+ 176 channel) 122 (cond ((eq? on #f) 0)
((eq? on #t) 127)
(else on))))
;(if on 127 0)))
; plays a midi track with the given tempo
; tempo is microseconds per quarter beat, or bpm * 60,000,000.
; PPQN is pulses per quarter note, or the resolution of the midi-track (also called ticks per quarter note)
; track is a midi track structure from midi-readwrite
; port is an out-port
(define (play-midi-track BPM PPQN track port)
(thread (λ () (play-midi-track-iter BPM PPQN track port 0))))
(define (play-midi-track-iter BPM PPQN track port time)
(let ([secondsPerTick (/ 60 (* BPM PPQN))])
(if (not (null? track))
(if (= time (caar track))
(cond ((ChannelMessage? (cadar track))
(cond ((equal? (ChannelMessage-kind (cadar track)) 'note-off)
(note-off port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track)))))
((equal? (ChannelMessage-kind (cadar track)) 'note-on)
(note-on port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track))) (cadr (ChannelMessage-operands (cadar track)))))
((equal? (ChannelMessage-kind (cadar track)) 'aftertouch)
(poly-key-pressure port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track))) (cadr (ChannelMessage-operands (cadar track)))))
((equal? (ChannelMessage-kind (cadar track)) 'control-change)
(control-change port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track))) (cadr (ChannelMessage-operands (cadar track)))))
((equal? (ChannelMessage-kind (cadar track)) 'program-change)
(program-change port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track)))))
((equal? (ChannelMessage-kind (cadar track)) 'channel-aftertouch)
(channel-pressure port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track)))))
((equal? (ChannelMessage-kind (cadar track)) 'pitch-bend)
(pitch-bend port (ChannelMessage-channel (cadar track)) (car (ChannelMessage-operands (cadar track))) (cadr (ChannelMessage-operands (cadar track))))))
(sleep 0)
(play-midi-track-iter BPM PPQN (cdr track) port time))
; System Exclusive messages need to be handled
((SysexMessage? (cadar track))
(play-midi-track-iter BPM PPQN (cdr track) port time))
; Midi File Meta messages need to be handled
((MetaMessage? (cadar track))
(cond ((equal? 'set-tempo (MetaMessage-content (cadar track)))
(play-midi-track-iter (/ 60000000 (cadr (MetaMessage-content (cadar track)))) PPQN (cdr track) port time))
(else (play-midi-track-iter BPM PPQN (cdr track) port time))))
(else (play-midi-track-iter BPM PPQN (cdr track) port time)))
(begin
(sleep secondsPerTick)
(play-midi-track-iter BPM PPQN track port (+ time 1))))
0)))
(define (get-tempo-from-meta-track track)
(define tempo 0)
(while (= tempo 0)
(begin
; (pretty-print (cadar track))
(cond ((MetaMessage? (cadar track))
(if (equal? 'set-tempo (car (MetaMessage-content (cadar track))))
(set! tempo (cadr (MetaMessage-content (cadar track))))
0))
((SysexMessage? (cadar track))
0)
((ChannelMessage? (cadar track))
0))
(set! track (cdr track))))
; (pretty-print tempo)
tempo)
; path is a the file path to a standard midi formated midi file, *.mid
(define (play-midi-file path out-port)
(play-midi-data (midi-file-parse path) out-port))
; midi-data is a midi-file structure from midi-readwrite
; port should be an out port
(define (play-midi-data midi-data out-port)
; (thread (λ ()
(device-all-notes-off out-port)
(device-reset-all-controllers out-port)
(define midi-worker-threads '())
(let* ([format (MIDIFile-format midi-data)]
[division (MIDIFile-division midi-data)]
[tracks (MIDIFile-tracks midi-data)]
[BPM (/ 60000000 (get-tempo-from-meta-track (list-ref tracks 0)))])
(for ([i (in-range 0 (length tracks))])
;(pretty-print (if (> i 0) (ChannelMessage-channel (cadadr (list-ref tracks i))) -1))
(set! midi-worker-threads (append midi-worker-threads (list (play-midi-track BPM (TicksPerQuarter-ticks division) (list-ref tracks i) out-port))))))
midi-worker-threads)
; midi-stream is a stream that gives a sequence of lists that contain a delay from the previous event, measured in seconds,
; and a list
; (list 0.5 (list 'event-type channel data-byte-1 data-byte-2))
; where
(define (play-midi-stream out-port midi-stream)
(thread (λ () (play-midi-stream-iter out-port midi-stream))))
(define (play-midi-stream-iter out-port midi-stream)
(if (not (stream-empty? midi-stream))
(let ([midi (stream-first midi-stream)])
;(pretty-print (stream-ref midi-stream 0))
(cond ((null? midi) 0)
((equal? (type-of midi) 'note-off)
(sleep (delay-of midi))
(note-off out-port (channel-of midi) (first-data-byte-of midi)))
((equal? (type-of midi) 'note-on)
(sleep (delay-of midi))
(note-on out-port (channel-of midi) (first-data-byte-of midi) (second-data-byte-of midi)))
((equal? (type-of midi) 'aftertouch)
(sleep (delay-of midi))
(poly-key-pressure out-port (channel-of midi) (first-data-byte-of midi) (second-data-byte-of midi)))
((equal? (type-of midi) 'control-change)
(sleep (delay-of midi))
(control-change out-port (channel-of midi) (first-data-byte-of midi) (second-data-byte-of midi)))
((equal? (type-of midi) 'program-change)
(sleep (delay-of midi))
(program-change out-port (channel-of midi) (first-data-byte-of midi) (second-data-byte-of midi)))
((equal? (type-of midi) 'channel-aftertouch)
(sleep (delay-of midi))
(channel-pressure out-port (channel-of midi) (first-data-byte-of midi) (second-data-byte-of midi)))
((equal? (type-of midi) 'pitch-bend)
(sleep (delay-of midi))
(pitch-bend out-port (channel-of midi) (first-data-byte-of midi) (second-data-byte-of midi)))
; System Exclusive messages need to be handled
; Midi File Meta messages need to be handled
(else (sleep 0))))
(play-midi-stream-iter out-port (stream-rest midi-stream))))
; Placeholder implementation, call sequences subject to change
(define (record-midi-in-to-file port-in file-path)
0) ;TODO
(define (record-midi-out-to-file port-out file-path)
0) ;TODO
(define (echo-midi-out-to-console port-out)
0) ;TODO
(define (echo-midi-in-to-console port-in in-device-name)
0) ;TODO
(define (echo-midi-in-to-out in-port out-port in-device-name out-device-name)
0) ;TODO
;(define (listen-midi-events)
; Read incoming messages until break
;(let loop ()
; (pretty-print (sync in))
; (play-note)
; (loop)))
;(define listenthread (thread listen-midi-events))
;(sleep 60)
;(kill-thread listenthread)
;(define (echo-midi-events)
; (let loop ()
; ((lambda (evnt) (pretty-print evnt)(sleep (car evnt))(send-midi-message (cadr evnt) (caddr evnt) (cadddr evnt))) (sync in))
; (loop)))
;(define echothread (thread echo-midi-events))