[56834c]: contrib / cl-portmidi / cffi-portmidi.lisp  Maximize  Restore  History

Download this file

313 lines (261 with data), 9.4 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
;;; Copyright (c) 2013 Tito Latini
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 2.1 of the License, or (at your option) any later version.
;;;
;;; This library 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
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(in-package :portmidi)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *standard-optimize-settings*
'(optimize speed (safety 0)))
(cffi:define-foreign-library portmidi
(:unix "libportmidi.so")
(:darwin "libportmidi.dylib")
(:cygwin "cygportmidi-0.dll")
(t (:default "libportmidi")))
(defun load-portmidi-library ()
(cffi:use-foreign-library portmidi))
(unless (cffi:foreign-library-loaded-p 'portmidi)
(load-portmidi-library)))
(defstruct (stream (:constructor %make-stream)
(:copier nil))
(pointer (cffi:null-pointer) :type cffi:foreign-pointer)
(direction :all :type keyword)
(device-id 0 :type non-negative-fixnum)
(device-interf "none" :type string)
(device-name "none" :type string))
(declaim (inline make-stream))
(defun make-stream (ptr direction device-id device-interf device-name)
(declare (type cffi:foreign-pointer ptr) (type keyword direction)
(type non-negative-fixnum device-id)
(type string device-interf device-name))
(let ((obj (%make-stream :pointer ptr
:direction direction
:device-id device-id
:device-interf device-interf
:device-name device-name)))
(tg:finalize obj (lambda () (close ptr)))
obj))
(defmethod print-object ((obj stream) stream)
(format stream "#<PM:STREAM ~S \"~A - ~A\">"
(stream-direction obj) (stream-device-interf obj)
(stream-device-name obj)))
(cffi:define-foreign-type stream-type ()
()
(:actual-type :pointer)
(:simple-parser stream))
(defmethod cffi:translate-to-foreign (handle (type stream-type))
(slot-value handle 'pointer))
(cffi:defctype message :int32)
(cffi:defctype timestamp :int32)
(defmacro define-constants (bindings)
`(progn
,@(mapcar (lambda (bind)
`(define-constant ,@bind))
bindings)))
(define-constants
((default-sysex-buffer-size 1024)
(hdrlength 50)
(host-error-msg-len 256)
(no-device -1)
;; Filter bit-mask definitions
(filt-active (ash 1 #xe))
(filt-sysex (ash 1 0))
(filt-clock (ash 1 #x8))
(filt-play (logior (ash 1 #xa)
(ash 1 #xc)
(ash 1 #xb)))
(filt-tick (ash 1 #x9))
(filt-fd (ash 1 #xd))
(filt-undefined filt-fd)
(filt-reset (ash 1 #xf))
(filt-realtime (logior filt-active
filt-sysex
filt-clock
filt-play
filt-undefined
filt-reset
filt-tick))
(filt-note (logior (ash 1 #x19) (ash 1 #x18)))
(filt-channel-aftertouch (ash 1 #x1d))
(filt-poly-aftertouch (ash 1 #x1a))
(filt-aftertouch (logior filt-channel-aftertouch
filt-poly-aftertouch))
(filt-program (ash 1 #x1c))
(filt-control (ash 1 #x1b))
(filt-pitchbend (ash 1 #x1e))
(filt-mtc (ash 1 1))
(filt-song-position (ash 1 2))
(filt-song-select (ash 1 3))
(filt-tune (ash 1 6))
(filt-systemcommon (logior filt-mtc
filt-song-position
filt-song-select
filt-tune))))
(cffi:defcenum error
(:pm-no-data 0)
(:pm-no-error 0)
(:pm-got-data 1)
(:pm-host-error -10000)
:pm-invalid-device-id
:pm-insufficient-memory
:pm-buffer-too-small
:pm-buffer-overflow
:pm-bad-ptr
:pm-bad-data
:pm-internal-error
:pm-buffer-max-size)
(cffi:defcstruct device-info
(struct-version :int)
(interf :string)
(name :string)
(input :int)
(output :int)
(opened :int))
(cffi:defcstruct event
(message message)
(timestamp timestamp))
(cffi:defcfun ("Pm_Initialize" initialize) error)
(cffi:defcfun ("Pm_Terminate" terminate%) error)
(cffi:defcfun ("Pm_HasHostError" has-host-error) :int
(stream stream))
(cffi:defcfun ("Pm_GetErrorText" get-error-text) :string
(errnum error))
(cffi:defcfun ("Pm_GetHostErrorText" get-host-error-text) :void
(msg :string)
(len :unsigned-int))
(cffi:defcfun ("Pm_CountDevices" count-devices) :int)
(cffi:defcfun ("Pm_GetDefaultInputDeviceID"
get-default-input-device-id) :int)
(cffi:defcfun ("Pm_GetDefaultOutputDeviceID"
get-default-output-device-id) :int)
(cffi:defcfun ("Pm_GetDeviceInfo" get-device-info%) :pointer
(id :int))
(cffi:defcfun ("Pm_OpenInput" open-input) error
(stream :pointer)
(input-device :int)
(input-driver-info :pointer)
(buffer-size :int32)
(time-proc :pointer)
(time-info :pointer))
(cffi:defcfun ("Pm_OpenOutput" open-output) error
(stream :pointer)
(output-device :int)
(output-driver-info :pointer)
(buffer-size :int32)
(time-proc :pointer)
(time-info :pointer)
(latency :int32))
(cffi:defcfun ("Pm_SetFilter" set-filter) error
(stream stream)
(filters :int32))
(cffi:defcfun ("Pm_SetChannelMask" set-channel-mask) error
(stream stream)
(mask :int))
(cffi:defcfun ("Pm_Abort" abort) error
(stream stream))
(cffi:defcfun ("Pm_Close" close%) error
(stream stream))
(cffi:defcfun ("Pm_Synchronize" synchronize) error
(stream stream))
(cffi:defcfun ("Pm_Read" read%) :int
(stream stream)
(buffer :pointer)
(length :int32))
(cffi:defcfun ("Pm_Poll" poll) error
(stream stream))
(cffi:defcfun ("Pm_Write" write%) error
(stream stream)
(buffer :pointer)
(length :int32))
(cffi:defcfun ("Pm_WriteShort" write-short) error
(stream stream)
(when timestamp)
(msg :int32))
(cffi:defcfun ("Pm_WriteSysEx" write-sysex) error
(stream stream)
(when timestamp)
(msg :string))
(declaim (inline message))
(defun message (status data1 data2)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 8) status data1 data2))
(logior (ldb (byte 8 0) status)
(ldb (byte 16 0) (ash data1 8))
(ldb (byte 24 0) (ash data2 16))))
(declaim (inline message-status))
(defun message-status (msg)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 24) msg))
(ldb (byte 8 0) msg))
(declaim (inline message-data1))
(defun message-data1 (msg)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 24) msg))
(ldb (byte 8 8) msg))
(declaim (inline message-data2))
(defun message-data2 (msg)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 24) msg))
(ldb (byte 8 16) msg))
(declaim (inline decode-message))
(defun decode-message (msg)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 24) msg))
(let ((ash-8 (ldb (byte 16 8) msg)))
(values (ldb (byte 8 0) msg) ; status
(ldb (byte 8 0) ash-8) ; data1
(ldb (byte 8 8) ash-8)))) ; data2
(declaim (inline decode-channel-message))
(defun decode-channel-message (msg)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 24) msg))
(let* ((ash-4 (ldb (byte 20 4) msg))
(ash-8 (ldb (byte 16 4) ash-4)))
(values (ldb (byte 4 0) ash-4) ; type
(ldb (byte 4 0) msg) ; channel
(ldb (byte 8 0) ash-8) ; data1
(ldb (byte 8 8) ash-8)))) ; data2
(declaim (inline before))
(defun before (t1 t2)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 32) t1 t2))
(< t1 t2))
(declaim (inline channel))
(defun channel (chan)
(declare #.*standard-optimize-settings*
(type (unsigned-byte 4) chan)
;; Avoid the boring notes from sbcl-1.8
#+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note))
(ash 1 chan))
(in-package :porttime)
(cffi:defctype timestamp :int32)
(cffi:defcenum error
(:pt-no-error 0)
(:pt-host-error -10000)
:pt-already-started
:pt-already-stopped
:pt-insufficient-memory)
(cffi:defcfun ("Pt_Start" start%) error
(resolution :int)
(callback :pointer)
(userData :pointer))
(declaim (inline start))
(defun start (&optional (resolution 1) (callback (cffi:null-pointer))
(user-data (cffi:null-pointer)))
(declare (type unsigned-byte resolution)
(type cffi:foreign-pointer callback user-data))
(start% resolution callback user-data))
(cffi:defcfun ("Pt_Stop" stop) error)
(cffi:defcfun ("Pt_Started" started) :boolean)
(cffi:defcfun ("Pt_Time" time) timestamp)
(cffi:defcfun ("Pt_Sleep" sleep) :void (duration :int32))