-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_input.xtm
192 lines (166 loc) · 7.15 KB
/
midi_input.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
;;; midi_input.xtm -- a 'simple' wrapper for portmidi input
;; Author: Andrew Sorensen
;; Keywords: extempore
;; Required dylibs: portmidi
;;; Commentary:
;; Simple MIDI input loads the default MIDI input device on startup
;; It also prints the available devices on startup
;; To change from the default MIDI input simply call (set_midi_in *devicenum*)
;;
;; By default MIDI_NOTE_ON, MIDI_NOTE_OFF and MIDI_CONTROL_CHANGE messages
;; are printed to the log.
;; To start doing something with these messages, override
;; midi_note_on, midi_note_off, midi_cc
;;
;; NOTE: this lib automatically converts note-on velocity 0 into note-off.
;; i.e. you will not see note-on msg with a velocity 0.
;;
;;; Ybot hacked
(sys:load "libs/core/scheduler.xtm")
(sys:load "libs/external/portmidi.xtm")
;; 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))
(bind-type MIDIDeviceManager <PmStream**,i64,i64,PmStream**,i64,i64> (constructor? . #f) (printer? . #f))
(bind-func start_all:[MIDIDeviceManager*]*
(lambda ()
(let* ((mdm:MIDIDeviceManager* (alloc))
(ndev (Pm_CountDevices))
(numInput:i64 0)
(numOutput:i64 0)
(input_devices:PmStream** (zalloc ndev))
(output_devices:PmStream** (zalloc ndev)))
(if (= ndev 0)
(println "No MIDI devices found")
(begin
(println "\n-- MIDI input devices --\n")
(doloop (i ndev)
(if (= (tref (Pm_GetDeviceInfo i) 3) 1)
(let ((dev (pm_create_input_stream i)))
(pset! input_devices numInput dev)
(set! numInput (+ numInput 1))
(println " device id" i ":" (Pm_GetDeviceInfo i)))))
(println "\n-- MIDI output devices --\n")
(doloop (i ndev)
(if (= (tref (Pm_GetDeviceInfo i) 4) 1)
(let ((dev (pm_create_output_stream i)))
(pset! output_devices numOutput dev)
(set! numOutput (+ numOutput 1))
(println " device id" i ":" (Pm_GetDeviceInfo i)))))))
(tset! mdm 0 input_devices)
(tset! mdm 3 output_devices)
(tset! mdm 1 numInput)
(tset! mdm 4 numOutput)
mdm)))
;; 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 (pitch:i32 volume:i32 chan:i32)
(println "NOTE_ON :" pitch volume chan)
void))
(bind-func midi_note_off
(lambda (pitch:i32 volume:i32 chan:i32)
(println "NOTE_OFF:" pitch volume chan)
void))
(bind-func midi_cc
(lambda (controller:i32 value:i32 chan:i32)
(println "MIDI_CC :" controller value chan)
void))
;; by default don't print non note/cc MIDI messages
(bind-func midi_msg
(lambda (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)
;; (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))
;; (cond ((= (Pm_Message_Type msg) MIDI_NOTE_ON)
;; (if (= (Pm_Message_Data2 msg) 0)
;; (midi_note_off (Pm_Message_Data1 msg)
;; (Pm_Message_Data2 msg)
;; (Pm_Message_Channel msg))
;; (midi_note_on (Pm_Message_Data1 msg)
;; (Pm_Message_Data2 msg)
;; (Pm_Message_Channel msg))))
;; ((= (Pm_Message_Type msg) MIDI_NOTE_OFF)
;; (midi_note_off (Pm_Message_Data1 msg)
;; (Pm_Message_Data2 msg)
;; (Pm_Message_Channel msg)))
;; ((= (Pm_Message_Type msg) MIDI_CONTROL_CHANGE)
;; (midi_cc (Pm_Message_Data1 msg)
;; (Pm_Message_Data2 msg)
;; (Pm_Message_Channel msg)))
;; (else
;; (midi_msg (Pm_Message_Type msg)
;; (Pm_Message_Data1 msg)
;; (Pm_Message_Data2 msg)
;; (Pm_Message_Channel msg))))
;; void)
;; void)
;; void))))
(bind-val midiDeviceManager MIDIDeviceManager* (start_all))
(bind-func midi_read_msg
(let ((buffer_length 1024)
(input_buffer:PmEvent* (zalloc buffer_length))
(msg:PmMessage 0)
(read_count 0)
(i:i32 0) (j:i64 0)
(numInputs (tref midiDeviceManager 1)))
(lambda ()
(dotimes (j numInputs)
(set! read_count 0)
(let ((input_dev (pref (tref midiDeviceManager 0) j)))
(set! read_count (Pm_Read input_dev input_buffer buffer_length))
(if (> read_count 0)
(begin
(dotimes (i read_count)
(set! msg (tref (pref-ptr input_buffer i) 0))
(cond ((= (Pm_Message_Type msg) MIDI_NOTE_ON)
(if (= (Pm_Message_Data2 msg) 0)
(midi_note_off (Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg))
(midi_note_on (Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg))))
((= (Pm_Message_Type msg) MIDI_NOTE_OFF)
(midi_note_off (Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg)))
((= (Pm_Message_Type msg) MIDI_CONTROL_CHANGE)
(midi_cc (Pm_Message_Data1 msg)
(Pm_Message_Data2 msg)
(Pm_Message_Channel msg)))
(else
(midi_msg (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)))