2005-11-29 11:11:26 +00:00
|
|
|
;;; -*- mode: lisp -*-
|
|
|
|
;;;
|
|
|
|
;;; an implementation of the OSC (Open Sound Control) protocol
|
|
|
|
;;;
|
2015-08-25 19:59:32 +00:00
|
|
|
;;; copyright (C) 2004 FoAM vzw.
|
2005-11-29 11:11:26 +00:00
|
|
|
;;;
|
|
|
|
;;; You are granted the rights to distribute and use this software
|
2015-08-25 19:59:32 +00:00
|
|
|
;;; under the terms of the Lisp Lesser GNU Public License, known
|
|
|
|
;;; as the LLGPL. The LLGPL consists of a preamble and the LGPL.
|
2005-11-29 11:11:26 +00:00
|
|
|
;;; Where these conflict, the preamble takes precedence. The LLGPL
|
2015-08-25 19:59:32 +00:00
|
|
|
;;; is available online at http://opensource.franz.com/preamble.html
|
2005-11-29 11:11:26 +00:00
|
|
|
;;; and is distributed with this code (see: LICENCE and LGPL files)
|
|
|
|
;;;
|
2015-08-25 19:59:32 +00:00
|
|
|
;;; authors
|
2005-11-29 11:11:26 +00:00
|
|
|
;;;
|
|
|
|
;;; nik gaffney <nik@f0.am>
|
|
|
|
;;;
|
|
|
|
;;; requirements
|
|
|
|
;;;
|
|
|
|
;;; dependent on sbcl, cmucl or openmcl for float encoding, other suggestions
|
2015-08-25 19:59:32 +00:00
|
|
|
;;; welcome.
|
2005-11-29 11:11:26 +00:00
|
|
|
;;;
|
|
|
|
;;; commentary
|
|
|
|
;;;
|
|
|
|
;;; this is a partial implementation of the OSC protocol which is used
|
|
|
|
;;; for communication mostly amongst music programs and their attatched
|
2015-08-25 19:59:32 +00:00
|
|
|
;;; musicians. eg. sc3, max/pd, reaktor/traktorska etc+. more details
|
|
|
|
;;; of the protocol can be found at the open sound control pages -=>
|
2005-11-29 11:11:26 +00:00
|
|
|
;;; http://www.cnmat.berkeley.edu/OpenSoundControl/
|
2015-08-25 19:59:32 +00:00
|
|
|
;;;
|
|
|
|
;;; - doesnt send nested bundles or timetags later than 'now'
|
2005-11-29 11:11:26 +00:00
|
|
|
;;; - malformed input -> exception
|
|
|
|
;;; - int32 en/de-coding based on code (c) Walter C. Pelissero
|
|
|
|
;;; - unknown types are sent as 'blobs' which may or may not be an issue
|
|
|
|
;;;
|
|
|
|
;;; see the README file for more details...
|
|
|
|
;;;
|
|
|
|
;;; known BUGS
|
2006-02-10 23:38:40 +00:00
|
|
|
;;; - encoding a :symbol which is unbound, or has no symbol-value will cause
|
|
|
|
;;; an error
|
|
|
|
;;;
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(in-package :osc)
|
2015-08-25 19:59:32 +00:00
|
|
|
|
|
|
|
;;(declaim (optimize (speed 3) (safety 1) (debug 3)))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
|
|
|
;;;;;; ; ;; ; ; ; ; ; ; ;
|
2015-08-25 19:59:32 +00:00
|
|
|
;;
|
2005-01-26 13:52:24 +00:00
|
|
|
;; eNcoding OSC messages
|
|
|
|
;;
|
2005-11-29 11:11:26 +00:00
|
|
|
;;;; ;; ;; ; ; ;; ; ; ; ;
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
(defparameter *debug* 0
|
|
|
|
"Set debug verbosity for core library functions. Currently levels
|
|
|
|
are 0-3.")
|
|
|
|
|
|
|
|
(defgeneric encode-osc-data (data))
|
|
|
|
|
|
|
|
(defmethod encode-osc-data ((data message))
|
|
|
|
"Encode an osc message with the given address and args."
|
|
|
|
(with-slots (command args) data
|
|
|
|
(concatenate '(vector (unsigned-byte 8))
|
|
|
|
(encode-address command)
|
|
|
|
(encode-typetags args)
|
|
|
|
(encode-args args))))
|
|
|
|
|
|
|
|
(defmethod encode-osc-data ((data bundle))
|
|
|
|
"Encode an osc bundle. A bundle contains a timetag (symbol or 64bit
|
|
|
|
int) and a list of message or nested bundle elements."
|
|
|
|
(with-slots (timetag elements) data
|
|
|
|
(cat '(35 98 117 110 100 108 101 0) ; #bundle
|
|
|
|
(if timetag
|
|
|
|
(encode-timetag timetag)
|
|
|
|
(encode-timetag :now))
|
|
|
|
(apply #'cat (mapcar #'encode-bundle-elt elements)))))
|
|
|
|
|
|
|
|
(defgeneric encode-bundle-elt (data))
|
|
|
|
|
|
|
|
(defmethod encode-bundle-elt ((data message))
|
|
|
|
(let ((bytes (encode-osc-data data)))
|
|
|
|
(cat (encode-int32 (length bytes)) bytes)))
|
|
|
|
|
|
|
|
(defmethod encode-bundle-elt ((data bundle))
|
|
|
|
(let ((bytes (encode-osc-data data)))
|
|
|
|
(cat (encode-int32 (length bytes)) bytes)))
|
|
|
|
|
|
|
|
;; Auxilary functions
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun encode-address (address)
|
2015-08-25 19:59:32 +00:00
|
|
|
(cat (map 'vector #'char-code address)
|
2005-03-14 14:05:05 +00:00
|
|
|
(string-padding address)))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun encode-typetags (data)
|
|
|
|
"creates a typetag string suitable for the given data.
|
2005-01-26 13:52:24 +00:00
|
|
|
valid typetags according to the osc spec are ,i ,f ,s and ,b
|
|
|
|
non-std extensions include ,{h|t|d|S|c|r|m|T|F|N|I|[|]}
|
|
|
|
see the spec for more details. ..
|
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
NOTE: currently handles the following tags
|
2005-01-26 13:52:24 +00:00
|
|
|
i => #(105) => int32
|
|
|
|
f => #(102) => float
|
2015-08-25 19:59:32 +00:00
|
|
|
s => #(115) => string
|
2005-08-11 09:02:31 +00:00
|
|
|
b => #(98) => blob
|
2015-08-25 19:54:38 +00:00
|
|
|
h => #(104) => int64
|
2015-08-25 19:59:32 +00:00
|
|
|
and considers non int/float/string data to be a blob."
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
(let ((lump (make-array 0 :adjustable t
|
|
|
|
:fill-pointer t)))
|
2005-03-02 17:23:56 +00:00
|
|
|
(macrolet ((write-to-vector (char)
|
|
|
|
`(vector-push-extend
|
|
|
|
(char-code ,char) lump)))
|
|
|
|
(write-to-vector #\,)
|
2015-08-25 19:59:32 +00:00
|
|
|
(dolist (x data)
|
2005-03-02 17:23:56 +00:00
|
|
|
(typecase x
|
2015-08-25 19:54:38 +00:00
|
|
|
(integer (if (>= x 4294967296) (write-to-vector #\h) (write-to-vector #\i)))
|
2005-03-02 17:23:56 +00:00
|
|
|
(float (write-to-vector #\f))
|
|
|
|
(simple-string (write-to-vector #\s))
|
2015-08-25 19:59:32 +00:00
|
|
|
(keyword (write-to-vector #\s))
|
|
|
|
(t (write-to-vector #\b)))))
|
2005-11-29 11:11:26 +00:00
|
|
|
(cat lump
|
2015-08-25 19:59:32 +00:00
|
|
|
(pad (padding-length (length lump))))))
|
|
|
|
|
|
|
|
(defun encode-args (args)
|
|
|
|
"encodes args in a format suitable for an OSC message"
|
2005-01-26 13:52:24 +00:00
|
|
|
(let ((lump (make-array 0 :adjustable t :fill-pointer t)))
|
2005-03-02 17:23:56 +00:00
|
|
|
(macrolet ((enc (f)
|
|
|
|
`(setf lump (cat lump (,f x)))))
|
2015-08-25 19:59:32 +00:00
|
|
|
(dolist (x args)
|
2005-03-02 17:23:56 +00:00
|
|
|
(typecase x
|
2015-08-25 19:54:38 +00:00
|
|
|
(integer (if (>= x 4294967296) (enc encode-int64) (enc encode-int32)))
|
|
|
|
(float (enc encode-float32))
|
2005-03-14 14:05:05 +00:00
|
|
|
(simple-string (enc encode-string))
|
2015-08-25 19:59:32 +00:00
|
|
|
(t (enc encode-blob))))
|
2005-03-02 17:23:56 +00:00
|
|
|
lump)))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
|
2005-01-26 13:52:24 +00:00
|
|
|
;;;;;; ; ;; ; ; ; ; ; ; ;
|
2015-08-25 19:59:32 +00:00
|
|
|
;;
|
2005-01-26 13:52:24 +00:00
|
|
|
;; decoding OSC messages
|
|
|
|
;;
|
|
|
|
;;; ;; ;; ; ; ; ; ; ;
|
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
(defun bundle-p (buffer &optional (start 0))
|
|
|
|
"A bundle begins with '#bundle' (8 bytes). The start argument should
|
|
|
|
index the beginning of a bundle in the buffer."
|
|
|
|
(= 35 (elt buffer start)))
|
|
|
|
|
|
|
|
(defun get-timetag (buffer &optional (start 0))
|
|
|
|
"Bytes 8-15 are the bundle timestamp. The start argument should
|
|
|
|
index the beginning of a bundle in the buffer."
|
|
|
|
(decode-timetag (subseq buffer
|
|
|
|
(+ 8 start)
|
|
|
|
(+ 16 start))))
|
|
|
|
|
|
|
|
(defun get-bundle-element-length (buffer &optional (start 16))
|
|
|
|
"Bytes 16-19 are the size of the bundle element. The start argument
|
|
|
|
should index the beginning of the bundle element (length, content)
|
|
|
|
pair in the buffer."
|
|
|
|
(decode-int32 (subseq buffer start (+ 4 start))))
|
|
|
|
|
|
|
|
(defun get-bundle-element (buffer &optional (start 16))
|
|
|
|
"Bytes 20 upto to the length of the content (defined by the
|
|
|
|
preceding 4 bytes) are the content of the bundle. The start argument
|
|
|
|
should index the beginning of the bundle element (length, content)
|
|
|
|
pair in the buffer."
|
|
|
|
(let ((length (get-bundle-element-length buffer start)))
|
|
|
|
(subseq buffer
|
|
|
|
(+ 4 start)
|
|
|
|
(+ (+ 4 start)
|
|
|
|
(+ length)))))
|
|
|
|
|
|
|
|
(defun split-sequence-by-n (sequence n)
|
|
|
|
(loop :with length := (length sequence)
|
|
|
|
:for start :from 0 :by n :below length
|
|
|
|
:collecting (coerce
|
|
|
|
(subseq sequence start (min length (+ start n)))
|
|
|
|
'list)))
|
|
|
|
|
|
|
|
(defun print-buffer (buffer &optional (n 8))
|
|
|
|
(format t "~%~{~{ ~5d~}~%~}Total: ~a bytes~2%"
|
|
|
|
(split-sequence-by-n buffer n)
|
|
|
|
(length buffer)))
|
|
|
|
|
|
|
|
(defun decode-bundle (buffer &key (start 0) end)
|
|
|
|
"Decodes an osc bundle/message into a bundle/message object. Bundles
|
|
|
|
comprise an osc-timetag and a list of elements, which may be
|
|
|
|
messages or bundles recursively. An optional end argument can be
|
|
|
|
supplied (i.e. the length value returned by socket-receive, or the
|
|
|
|
element length in the case of nested bundles), otherwise the entire
|
|
|
|
buffer is decoded - in which case, if you are reusing buffers, you
|
|
|
|
are responsible for ensuring that the buffer does not contain stale
|
|
|
|
data."
|
|
|
|
(unless end
|
|
|
|
(setf end (- (length buffer) start)))
|
|
|
|
(when (>= *debug* 2)
|
|
|
|
(format t "~%Buffer start: ~a end: ~a~%" start end)
|
|
|
|
(print-buffer (subseq buffer start end)))
|
|
|
|
(if (bundle-p buffer start)
|
|
|
|
;; Bundle
|
|
|
|
(let ((timetag (get-timetag buffer start)))
|
|
|
|
(incf start (+ 8 8)) ; #bundle, timetag bytes
|
|
|
|
(loop while (< start end)
|
|
|
|
for element-length = (get-bundle-element-length
|
|
|
|
buffer start)
|
|
|
|
do (incf start 4) ; length bytes
|
|
|
|
when (>= *debug* 1)
|
|
|
|
do (format t "~&Bundle element length: ~a~%" element-length)
|
|
|
|
collect (decode-bundle buffer
|
|
|
|
:start start
|
|
|
|
:end (+ start element-length))
|
|
|
|
into elements
|
|
|
|
do (incf start (+ element-length))
|
|
|
|
finally (return
|
|
|
|
(values (make-bundle timetag elements)
|
|
|
|
timetag))))
|
|
|
|
;; Message
|
|
|
|
(let ((message
|
|
|
|
(decode-message
|
|
|
|
(subseq buffer start (+ start end)))))
|
|
|
|
(make-message (car message) (cdr message)))))
|
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun decode-message (message)
|
2015-08-25 19:59:32 +00:00
|
|
|
"reduces an osc message to an (address . data) pair. .."
|
2005-03-06 12:25:01 +00:00
|
|
|
(declare (type (vector *) message))
|
2005-01-26 13:52:24 +00:00
|
|
|
(let ((x (position (char-code #\,) message)))
|
2015-08-25 19:59:32 +00:00
|
|
|
(if (eq x nil)
|
2005-03-06 12:25:01 +00:00
|
|
|
(format t "message contains no data.. ")
|
2015-08-25 19:59:32 +00:00
|
|
|
(cons (decode-address (subseq message 0 x))
|
|
|
|
(decode-taged-data (subseq message x))))))
|
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun decode-address (address)
|
2015-08-25 19:59:32 +00:00
|
|
|
(coerce (map 'vector #'code-char
|
|
|
|
(delete 0 address))
|
|
|
|
'string))
|
2005-03-14 14:05:05 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun decode-taged-data (data)
|
2005-01-26 13:52:24 +00:00
|
|
|
"decodes data encoded with typetags...
|
2015-08-25 19:59:32 +00:00
|
|
|
NOTE: currently handles the following tags
|
2005-01-26 13:52:24 +00:00
|
|
|
i => #(105) => int32
|
|
|
|
f => #(102) => float
|
2005-03-14 14:05:05 +00:00
|
|
|
s => #(115) => string
|
2015-08-25 19:54:38 +00:00
|
|
|
b => #(98) => blob
|
|
|
|
h => #(104) => int64"
|
2005-03-14 14:05:05 +00:00
|
|
|
|
|
|
|
(let ((div (position 0 data)))
|
2015-08-25 19:59:32 +00:00
|
|
|
(let ((tags (subseq data 1 div))
|
2015-08-25 19:59:32 +00:00
|
|
|
(acc (subseq data (padded-length div)))
|
|
|
|
(result '()))
|
2005-03-14 14:05:05 +00:00
|
|
|
(map 'vector
|
2015-08-25 19:59:32 +00:00
|
|
|
#'(lambda (x)
|
|
|
|
(cond
|
2015-08-25 19:59:32 +00:00
|
|
|
((eq x (char-code #\i))
|
|
|
|
(push (decode-int32 (subseq acc 0 4))
|
|
|
|
result)
|
|
|
|
(setf acc (subseq acc 4)))
|
|
|
|
((eq x (char-code #\h))
|
|
|
|
(push (decode-uint64 (subseq acc 0 8))
|
|
|
|
result)
|
|
|
|
(setf acc (subseq acc 8)))
|
|
|
|
((eq x (char-code #\f))
|
|
|
|
(push (decode-float32 (subseq acc 0 4))
|
|
|
|
result)
|
|
|
|
(setf acc (subseq acc 4)))
|
|
|
|
((eq x (char-code #\s))
|
|
|
|
(let ((pointer (padded-length (position 0 acc))))
|
|
|
|
(push (decode-string
|
|
|
|
(subseq acc 0 pointer))
|
|
|
|
result)
|
|
|
|
(setf acc (subseq acc pointer))))
|
|
|
|
((eq x (char-code #\b))
|
|
|
|
(let* ((size (decode-int32 (subseq acc 0 4)))
|
|
|
|
(bl (+ 4 size))
|
|
|
|
(end (+ bl (mod (- 4 bl) 4))))
|
|
|
|
;; NOTE: cannot use (padded-length bl), as it is not the same algorithm.
|
|
|
|
;; Blobs of 4, 8, 12 etc bytes should not be padded!
|
|
|
|
(push (decode-blob (subseq acc 0 end))
|
|
|
|
result)
|
|
|
|
(setf acc (subseq acc end))))
|
|
|
|
(t (error "unrecognised typetag ~a" x))))
|
2015-08-25 19:59:32 +00:00
|
|
|
tags)
|
2005-03-14 14:05:05 +00:00
|
|
|
(nreverse result))))
|
|
|
|
|
|
|
|
|
2005-11-29 11:11:26 +00:00
|
|
|
;;;;;; ;; ;; ; ; ; ; ; ;; ;
|
2005-03-14 14:05:05 +00:00
|
|
|
;;
|
2015-08-25 19:59:32 +00:00
|
|
|
;; timetags
|
2005-03-14 14:05:05 +00:00
|
|
|
;;
|
2015-08-25 19:59:32 +00:00
|
|
|
;; - timetags can be encoded using a value, or the :now and :time
|
|
|
|
;; keywords. the keywords enable either a tag indicating 'immediate'
|
|
|
|
;; execution, or a tag containing the current time (which will most
|
|
|
|
;; likely be in the past of any receiver) to be created.
|
2005-03-14 14:05:05 +00:00
|
|
|
;;
|
2007-02-26 15:20:53 +00:00
|
|
|
;; - see this c.l.l thread to sync universal-time and internal-time
|
|
|
|
;; http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/c207fef63a78d720/adc7442d2e4de5a0?lnk=gst&q=internal-real-time-sync&rnum=1#adc7442d2e4de5a0
|
2005-03-14 14:05:05 +00:00
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
;; - In SBCL, using sb-ext:get-time-of-day to get accurate seconds and
|
|
|
|
;; microseconds from OS.
|
|
|
|
;;
|
|
|
|
;;;; ;; ; ;
|
|
|
|
|
|
|
|
(defun encode-timetag (timetag)
|
|
|
|
"From the spec: `Time tags are represented by a 64 bit fixed point
|
|
|
|
number. The first 32 bits specify the number of seconds since midnight
|
|
|
|
on January 1, 1900, and the last 32 bits specify fractional parts of a
|
|
|
|
second to a precision of about 200 picoseconds. This is the
|
|
|
|
representation used by Internet NTP timestamps'. For an
|
|
|
|
'instantaneous' timetag use (encode-timetag :now), and for a timetag
|
|
|
|
with the current time use (encode-timetag :time)."
|
2006-02-10 23:38:40 +00:00
|
|
|
(cond
|
2015-08-25 19:59:32 +00:00
|
|
|
((equalp timetag :now)
|
|
|
|
;; a 1 bit timetag will be interpreted as 'immediately'
|
|
|
|
#(0 0 0 0 0 0 0 1))
|
|
|
|
((equalp timetag :time)
|
|
|
|
;; encode timetag with current real time
|
|
|
|
(encode-int64 (get-current-timetag)))
|
|
|
|
((timetagp timetag)
|
|
|
|
;; encode osc timetag
|
|
|
|
(encode-int64 timetag))
|
|
|
|
(t (error "Argument given is not one of :now, :time, or timetagp."))))
|
2005-03-14 14:05:05 +00:00
|
|
|
|
|
|
|
(defun decode-timetag (timetag)
|
2015-08-25 19:59:32 +00:00
|
|
|
"Return a 64 bit timetag from a vector of 8 bytes in network byte
|
|
|
|
order."
|
|
|
|
(if (equalp timetag #(0 0 0 0 0 0 0 1))
|
|
|
|
1 ; A timetag of 1 is defined as immediately.
|
|
|
|
(decode-uint64 timetag)))
|
2007-02-26 15:20:53 +00:00
|
|
|
|
2005-03-02 17:23:56 +00:00
|
|
|
;;;;; ; ; ;; ;; ; ;
|
|
|
|
;;
|
2005-01-26 13:52:24 +00:00
|
|
|
;; dataformat en- de- cetera.
|
2005-03-02 17:23:56 +00:00
|
|
|
;;
|
|
|
|
;;; ;; ; ; ;
|
2005-11-29 11:11:26 +00:00
|
|
|
|
|
|
|
;; floats are encoded using implementation specific 'internals' which is not
|
2015-08-25 19:59:32 +00:00
|
|
|
;; particulaly portable, but 'works for now'.
|
2005-11-29 11:11:26 +00:00
|
|
|
|
2005-01-26 13:52:24 +00:00
|
|
|
(defun encode-float32 (f)
|
2015-08-25 19:59:32 +00:00
|
|
|
"encode an ieee754 float as a 4 byte vector. currently sbcl/cmucl specific"
|
2005-01-26 14:36:20 +00:00
|
|
|
#+sbcl (encode-int32 (sb-kernel:single-float-bits f))
|
2005-03-02 17:23:56 +00:00
|
|
|
#+cmucl (encode-int32 (kernel:single-float-bits f))
|
2005-11-29 11:11:26 +00:00
|
|
|
#+openmcl (encode-int32 (CCL::SINGLE-FLOAT-BITS f))
|
2007-02-26 15:20:53 +00:00
|
|
|
#+allegro (encode-int32 (multiple-value-bind (x y) (excl:single-float-to-shorts f)
|
2015-08-25 19:59:32 +00:00
|
|
|
(+ (ash x 16) y)))
|
2007-02-26 15:20:53 +00:00
|
|
|
#-(or sbcl cmucl openmcl allegro) (error "cant encode floats using this implementation"))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
|
|
|
(defun decode-float32 (s)
|
|
|
|
"ieee754 float from a vector of 4 bytes in network byte order"
|
2005-01-26 14:49:03 +00:00
|
|
|
#+sbcl (sb-kernel:make-single-float (decode-int32 s))
|
2005-03-02 17:23:56 +00:00
|
|
|
#+cmucl (kernel:make-single-float (decode-int32 s))
|
2005-12-05 19:07:01 +00:00
|
|
|
#+openmcl (CCL::HOST-SINGLE-FLOAT-FROM-UNSIGNED-BYTE-32 (decode-uint32 s))
|
2007-02-26 15:20:53 +00:00
|
|
|
#+allegro (excl:shorts-to-single-float (ldb (byte 16 16) (decode-int32 s))
|
2015-08-25 19:59:32 +00:00
|
|
|
(ldb (byte 16 0) (decode-int32 s)))
|
2007-02-26 15:20:53 +00:00
|
|
|
#-(or sbcl cmucl openmcl allegro) (error "cant decode floats using this implementation"))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2019-05-02 04:34:42 +00:00
|
|
|
(defmacro defint-decoder (num-of-octets &optional docstring)
|
|
|
|
(let ((decoder-name (intern (format nil "~:@(decode-uint~)~D" (* 8 num-of-octets))))
|
|
|
|
(seq (gensym))
|
|
|
|
(int (gensym)))
|
|
|
|
`(defun ,decoder-name (,seq)
|
|
|
|
,@(when docstring
|
|
|
|
(list docstring))
|
|
|
|
(let* ((,int 0)
|
|
|
|
,@(loop
|
|
|
|
for n below num-of-octets
|
|
|
|
collect `(,int (dpb (aref ,seq ,n) (byte 8 (* 8 (- (1- ,num-of-octets) ,n)))
|
|
|
|
,int))))
|
|
|
|
,int))))
|
2015-08-25 19:59:32 +00:00
|
|
|
|
|
|
|
(defun decode-uint32 (s)
|
|
|
|
"4 byte -> 32 bit unsigned int"
|
|
|
|
(let ((i (+ (ash (elt s 0) 24)
|
|
|
|
(ash (elt s 1) 16)
|
|
|
|
(ash (elt s 2) 8)
|
|
|
|
(elt s 3))))
|
|
|
|
i))
|
|
|
|
|
2019-03-24 21:00:00 +00:00
|
|
|
(defmacro defint-encoder (num-of-octets &optional docstring)
|
|
|
|
(let ((enc-name (intern (format nil "~:@(encode-int~)~D" (* 8 num-of-octets))))
|
|
|
|
(buf (gensym))
|
|
|
|
(int (gensym)))
|
|
|
|
`(defun ,enc-name (,int)
|
|
|
|
,@(when docstring
|
|
|
|
(list docstring))
|
|
|
|
(let ((,buf (make-array ,num-of-octets :element-type '(unsigned-byte 8))))
|
|
|
|
,@(loop
|
2019-03-24 21:57:45 +00:00
|
|
|
for n below num-of-octets
|
|
|
|
collect `(setf (aref ,buf ,n)
|
|
|
|
(ldb (byte 8 (* 8 (- (1- ,num-of-octets) ,n)))
|
|
|
|
,int)))
|
2019-03-24 21:00:00 +00:00
|
|
|
,buf))))
|
|
|
|
|
2019-03-24 21:43:20 +00:00
|
|
|
(defint-encoder 4 "Convert an integer into a sequence of 4 bytes in network byte order (32 bit).")
|
|
|
|
(defint-encoder 8 "Convert an integer into a sequence of 8 bytes in network byte order (64 bit).")
|
2015-08-25 19:54:38 +00:00
|
|
|
|
2019-03-24 21:14:43 +00:00
|
|
|
(defun decode-int32 (s)
|
|
|
|
"4 byte -> 32 bit int -> two's complement (in network byte order)"
|
|
|
|
(let ((i (decode-uint32 s)))
|
2019-03-24 21:57:45 +00:00
|
|
|
(if (>= i #.(1- (expt 2 31)))
|
|
|
|
(- (- #.(expt 2 32) i))
|
|
|
|
i)))
|
2019-03-24 21:14:43 +00:00
|
|
|
|
|
|
|
(defun decode-int64 (s)
|
|
|
|
"8 byte -> 64 bit int -> two's complement (in network byte order)"
|
|
|
|
(let ((i (decode-uint64 s)))
|
2019-03-24 21:57:45 +00:00
|
|
|
(if (>= i #.(1- (expt 2 63)))
|
|
|
|
(- (- #.(expt 2 64) i))
|
2015-08-25 19:59:32 +00:00
|
|
|
i)))
|
|
|
|
|
|
|
|
;; osc-strings are unsigned bytes, padded to a 4 byte boundary
|
2015-08-25 19:54:38 +00:00
|
|
|
|
2015-08-25 19:59:32 +00:00
|
|
|
(defun encode-string (string)
|
|
|
|
"encodes a string as a vector of character-codes, padded to 4 byte boundary"
|
|
|
|
(cat (map 'vector #'char-code string)
|
|
|
|
(string-padding string)))
|
2005-03-06 12:25:01 +00:00
|
|
|
|
2005-01-26 13:52:24 +00:00
|
|
|
(defun decode-string (data)
|
|
|
|
"converts a binary vector to a string and removes trailing #\nul characters"
|
|
|
|
(string-trim '(#\nul) (coerce (map 'vector #'code-char data) 'string)))
|
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
|
|
|
|
;; blobs are binary data, consisting of a length (int32) and bytes which are
|
|
|
|
;; osc-padded to a 4 byte boundary.
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
(defun encode-blob (blob)
|
|
|
|
"encodes a blob from a given vector"
|
|
|
|
(let ((bl (length blob)))
|
|
|
|
(cat (encode-int32 bl) blob
|
2015-08-25 19:59:32 +00:00
|
|
|
<<<<<<< HEAD
|
|
|
|
(pad (padding-length bl)))))
|
|
|
|
|
|
|
|
(defun decode-blob (blob)
|
|
|
|
"decode a blob as a vector of unsigned bytes."
|
|
|
|
(let ((size (decode-int32
|
|
|
|
(subseq blob 0 4))))
|
|
|
|
(subseq blob 4 (+ 4 size))))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
;; utility functions for osc-string/padding slonking
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-01-26 14:36:20 +00:00
|
|
|
(defun cat (&rest catatac)
|
2015-08-25 19:59:32 +00:00
|
|
|
(apply #'concatenate '(vector (unsigned-byte 8)) catatac))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun padding-length (s)
|
|
|
|
"returns the length of padding required for a given length of string"
|
2005-03-06 12:25:01 +00:00
|
|
|
(declare (type fixnum s))
|
2005-01-26 13:52:24 +00:00
|
|
|
(- 4 (mod s 4)))
|
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
(defun padded-length (s)
|
|
|
|
"returns the length of an osc-string made from a given length of string"
|
|
|
|
(declare (type fixnum s))
|
|
|
|
(+ s (- 4 (mod s 4))))
|
|
|
|
|
|
|
|
(defun string-padding (string)
|
2005-08-12 09:20:11 +00:00
|
|
|
"returns the padding required for a given osc string"
|
2015-08-25 19:59:32 +00:00
|
|
|
(declare (type simple-string string))
|
2005-11-29 11:11:26 +00:00
|
|
|
(pad (padding-length (length string))))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun pad (n)
|
2005-01-26 13:52:24 +00:00
|
|
|
"make a sequence of the required number of #\Nul characters"
|
2005-03-06 12:25:01 +00:00
|
|
|
(declare (type fixnum n))
|
2005-01-26 13:52:24 +00:00
|
|
|
(make-array n :initial-element 0 :fill-pointer n))
|
|
|
|
|
2006-04-05 10:33:33 +00:00
|
|
|
(provide :osc)
|
2019-03-24 21:43:20 +00:00
|
|
|
|
2005-03-02 17:23:56 +00:00
|
|
|
;; end
|