-
Notifications
You must be signed in to change notification settings - Fork 0
/
ybot_serial.xtm
284 lines (245 loc) · 9.94 KB
/
ybot_serial.xtm
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
(sys:load-preload-check 'ybot_serial)
(define *xtmlib-ybot_serial-loaded* #f)
(impc:aot:suppress-aot-do
(sys:load "libs/core/scheduler.xtm")
(sys:load "libs/contrib/ybot/ybot_adt.xtm")
(sys:load "libs/contrib/librs232.xtm")
;(sys:load "libs/contrib/ybot/ybot_dynamic_buffer.xtm")
)
(impc:aot:insert-forms
(sys:load "libs/core/scheduler.xtm")
(sys:load "libs/contrib/ybot/ybot_adt.xtm" 'quiet)
(sys:load "libs/contrib/librs232.xtm" 'quiet)
;(sys:load "libs/contrib/ybot/ybot_dynamic_buffer.xtm" 'quiet)
)
;;;;;;;;;;;;; Serial Port management ;;;;;;;;;;;;
(bind-func list-ports:[i64]*
(lambda ()
(let ((n:i64 (i32toi64 (comEnumerate)))
(loop:[i64,i64]*
(lambda (i:i64)
(cond
((< i n)
(printf "Port %d:\t%s\n" i (comGetPortName (i64toi32 i)))
(loop (+ i 1)))
(else n)))))
(loop 0))))
(bind-func serial_port_id:[i64,i8*]*
(lambda (port_name)
(let* ((n:i64 (i32toi64 (comEnumerate)))
(loop:[i64,i64]*
(lambda (i:i64)
(cond
((and (<= 0 i) (< i n))
(let* ((name:i8* (comGetPortName (i64toi32 i)))
(a (strlen name)) (b (strlen port_name)) (c (if (< a b) a b)))
(cond
((= 0:i32 (strncmp name port_name c)) i)
(else
(loop (+ i 1))))))
(else
(printf "No serial port found matching name %s\n" port_name)
-1)))))
(loop 0))))
(bind-func openSerialPort
(lambda (index:i64)
(comOpen (i64toi32 index) (i64toi32 115200))))
;;;;;; Serial data management ;;;;;;
;; Serial data queue
(bind-type YbotSerialQueue <i8*,i64> (printer? . #f) (constructor? . #f))
(bind-func enqueue_serial_char
(let ((tail 0) (head 0))
(lambda (q:XTM_SERIAL_QUEUE c:i8)
(pset! (aref-ptr XTM_SERIAL_EVENT_QUEUE (% tail XTM_MIDI_EVENT_QUEUE_SIZE))
(Pm_Message_Type msg) (Pm_Message_Data1 msg) (Pm_Message_Data2 msg) (Pm_Message_Channel msg)
timestamp (clock_clock))
(set! tail (+ tail 1))
#t)))
(bind-func dequeue_midi_event
(let ((head 0) (tail 0))
(lambda ()
(set! tail (enqueue_midi_event.tail:i64))
(if (< (+ head XTM_MIDI_EVENT_QUEUE_SIZE) tail)
(begin (println "Warning: MIDI overrun (head:" head "tail:" tail ") events dropped")
(set! head tail)))
(if (< head tail)
(let ((res (aref-ptr XTM_MIDI_EVENT_QUEUE (% head XTM_MIDI_EVENT_QUEUE_SIZE))))
(set! head (+ head 1))
res)
null)))) ;; underrun
(bind-func available_midi_events
(lambda ()
(let ((head (dequeue_midi_event.head:i64))
(tail (enqueue_midi_event.tail:i64)))
(if (< (+ head XTM_MIDI_EVENT_QUEUE_SIZE) tail)
(begin (println "Warning: MIDI overrun - dropping old events")
(dequeue_midi_event.head:i64 tail)
0)
(- tail head)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Scheme listener code
;;
(define *run-scheme-midi-listener* #t)
;; should be redefined to 'do' something
(define midi-note-on
(lambda (timestamp pitch volume chan)
(println 'note-on ': pitch volume chan 'timestamp: timestamp)))
;; should be redefined to 'do' something
(define midi-note-off
(lambda (timestamp pitch volume chan)
(println 'note-off: pitch volume chan 'timestamp: timestamp)))
;; should be redefined to 'do' something
(define midi-cc
(lambda (timestamp controller value chan)
(println 'midi-cc ': controller value chan 'timestamp: timestamp)))
;; scheme midi event listener
(define scheme-midi-listener
(lambda (beat dur)
(dotimes (i (available_midi_events))
(let* ((evt (dequeue_midi_event))
(type (get_midi_evt_type evt))
(a (get_midi_evt_a evt))
(b (get_midi_evt_b evt))
(chan (get_midi_evt_channel evt))
(timestamp (get_midi_evt_timestamp evt)))
(cond ((= type #x90)
(midi-note-on timestamp a b chan))
((= type #x80)
(midi-note-off timestamp a b chan))
((= type #xB0)
(midi-cc timestamp a b chan))
(else #f))))
(callback (*metro* (+ beat (* .5 dur))) 'scheme-midi-listener (+ beat dur) dur)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Some helpful functions for using MIDI devices by name ;;;
(bind-func midi_input_port_id:[i32,i8*]*
(lambda (port_name)
(let* ((n:i32 (Pm_CountDevices))
(loop:[i32,i32]*
(lambda (i:i32)
(cond
((and (<= (i64toi32 0) i) (< i n))
(let* ((info:PmDeviceInfo* (Pm_GetDeviceInfo i))
(name:i8* (tref info 2))
(input:bool (= (i64toi32 1) (tref info 3)))
(a (strlen name)) (b (strlen port_name)) (c (if (< a b) a b)))
(cond
((and input (= (i64toi32 0) (strncmp name port_name c))) i)
(else
(loop (+ i (i64toi32 1)))))))
(else
(printf "No MIDI input port found with name %s\n" port_name)
(i64toi32 -1))))))
(loop (i64toi32 0)))))
(bind-func midi_output_port_id:[i32,i8*]*
(lambda (port_name)
(let* ((n:i32 (Pm_CountDevices))
(loop:[i32,i32]*
(lambda (i:i32)
(cond
((and (<= (i64toi32 0) i) (< i n))
(let* ((info:PmDeviceInfo* (Pm_GetDeviceInfo i))
(name:i8* (tref info 2))
(output:bool (= (i64toi32 1) (tref info 4)))
(a (strlen name)) (b (strlen port_name)) (c (if (< a b) a b)))
(cond
((and output (= (i64toi32 0) (strncmp name port_name c))) i)
(else
(loop (+ i (i64toi32 1)))))))
(else
(printf "No MIDI output port found with name %s\n" port_name)
(i64toi32 -1))))))
(loop (i64toi32 0)))))
;; first off create a native 'clock' scheduler
;; update rate of 200hz
(bind-val clock_sched [void]* (clock_scheduler 200.0))
(pm_initialize)
(pm_print_devices)
;; start with a default midi stream
(bind-val midi_in PmStream* (pm_create_input_stream 0))
;; OVERRIDE default midi in
(bind-func set_midi_in
(lambda (idx)
(set! midi_in (pm_create_input_stream idx))
void))
(bind-func midi_note_on
(lambda (timestamp:i32 pitch:i32 volume:i32 chan:i32)
(println "NOTE_ON :" pitch volume chan "timestamp:" timestamp)
void))
(bind-func midi_note_off
(lambda (timestamp:i32 pitch:i32 volume:i32 chan:i32)
(println "NOTE_OFF:" pitch volume chan "timestamp:" timestamp)
void))
(bind-func midi_cc
(lambda (timestamp:i32 controller:i32 value:i32 chan:i32)
(println "MIDI_CC :" controller value chan "timestamp:" timestamp)
void))
;; by default don't print non note/cc MIDI messages
(bind-func midi_msg
(lambda (timestamp:i32 type:i32 a:i32 b:i32 chan:i32)
;; (println "MIDIMSG: " type a b chan)
void))
(bind-func midi_read_msg
(let ((buffer_length 1024)
(input_buffer:PmEvent* (zalloc buffer_length))
(msg:PmMessage 0)
(timestamp:i32 0)
(read_count 0)
(i:i32 0))
(lambda ()
(set! read_count 0)
;; if you have more than one midi input device, you should call
;; Pm_Read on all your devices here
(set! read_count (Pm_Read midi_in input_buffer buffer_length))
(if (> read_count 0)
(begin
(dotimes (i read_count)
(set! msg (tref (pref-ptr input_buffer i) 0))
(set! timestamp (tref (pref-ptr input_buffer i) 1))
(cond ((= (Pm_Message_Type msg) MIDI_NOTE_ON)
(enqueue_midi_event msg timestamp)
(if (= (Pm_Message_Data2 msg) 0)
(midi_note_off timestamp
(Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg))
(midi_note_on timestamp
(Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg))))
((= (Pm_Message_Type msg) MIDI_NOTE_OFF)
(enqueue_midi_event msg timestamp)
(midi_note_off timestamp
(Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg)))
((= (Pm_Message_Type msg) MIDI_CONTROL_CHANGE)
(if XTM_MIDI_CC_QUEUEING (enqueue_midi_event msg timestamp))
(midi_cc timestamp
(Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg)))
(else
(midi_msg timestamp
(Pm_Message_Type msg)
(Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg))))
void)
void)
void))))
;; poll for midi messages at 100hz
(bind-func midi_input_polling
(lambda (time)
(midi_read_msg)
(sched clock_sched (+ time 0.01) midi_input_polling:[void,double]*)))
(xtmX (midi_input_polling (+ (clock_sched.time) 1.0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; uncomment and call the following line
;; to start listening on your preferred midi input device
;;
;; (set_midi_in 3) ;; i.e. input device 3
;; uncomment and call this to start the scheme midi listener
;; (scheme-midi-listener (*metro* 'get-beat 4) 1/24))