2005-11-29 11:11:26 +00:00
|
|
|
;;; -*- mode: lisp -*-
|
|
|
|
;;;
|
|
|
|
;;; an implementation of the OSC (Open Sound Control) protocol
|
|
|
|
;;;
|
|
|
|
;;; copyright (C) 2004 FoAM vzw.
|
|
|
|
;;;
|
|
|
|
;;; You are granted the rights to distribute and use this software
|
|
|
|
;;; under the terms of the Lisp Lesser GNU Public License, known
|
|
|
|
;;; as the LLGPL. The LLGPL consists of a preamble and the LGPL.
|
|
|
|
;;; Where these conflict, the preamble takes precedence. The LLGPL
|
|
|
|
;;; is available online at http://opensource.franz.com/preamble.html
|
|
|
|
;;; and is distributed with this code (see: LICENCE and LGPL files)
|
|
|
|
;;;
|
|
|
|
;;; authors
|
|
|
|
;;;
|
|
|
|
;;; nik gaffney <nik@f0.am>
|
|
|
|
;;;
|
|
|
|
;;; requirements
|
|
|
|
;;;
|
|
|
|
;;; dependent on sbcl, cmucl or openmcl for float encoding, other suggestions
|
|
|
|
;;; welcome.
|
|
|
|
;;;
|
|
|
|
;;; commentary
|
|
|
|
;;;
|
|
|
|
;;; this is a partial implementation of the OSC protocol which is used
|
|
|
|
;;; for communication mostly amongst music programs and their attatched
|
|
|
|
;;; musicians. eg. sc3, max/pd, reaktor/traktorska etc+. more details
|
|
|
|
;;; of the protocol can be found at the open sound control pages -=>
|
|
|
|
;;; http://www.cnmat.berkeley.edu/OpenSoundControl/
|
|
|
|
;;;
|
|
|
|
;;; - doesnt send nested bundles or timetags later than 'now'
|
|
|
|
;;; - 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
|
|
|
(defpackage :osc
|
|
|
|
(:use :cl)
|
|
|
|
(:documentation "OSC aka the 'open sound control' protocol")
|
|
|
|
(:export :encode-message
|
2005-03-14 14:05:05 +00:00
|
|
|
:encode-bundle
|
|
|
|
:decode-message
|
|
|
|
:decode-bundle))
|
2005-02-10 13:32:51 +00:00
|
|
|
|
|
|
|
(in-package :osc)
|
2005-03-06 12:25:01 +00:00
|
|
|
|
2006-02-10 23:38:40 +00:00
|
|
|
;(declaim (optimize (speed 3) (safety 1) (debug 3)))
|
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
|
|
|
|
2006-02-10 23:38:40 +00:00
|
|
|
(defun encode-bundle (data &optional timetag)
|
2005-03-14 14:05:05 +00:00
|
|
|
"will encode an osc message, or list of messages as a bundle
|
2006-02-10 23:38:40 +00:00
|
|
|
with an optional timetag (symbol or 64bit int).
|
|
|
|
doesnt handle nested bundles"
|
2005-03-14 14:05:05 +00:00
|
|
|
(cat '(35 98 117 110 100 108 101 0) ; #bundle
|
2006-02-10 23:38:40 +00:00
|
|
|
(if timetag
|
|
|
|
(encode-timetag timetag)
|
|
|
|
(encode-timetag :now))
|
2005-03-14 14:05:05 +00:00
|
|
|
(if (listp (car data))
|
|
|
|
(apply #'cat (mapcar #'encode-bundle-elt data))
|
|
|
|
(encode-bundle-elt data))))
|
|
|
|
|
|
|
|
(defun encode-bundle-elt (data)
|
|
|
|
(let ((message (apply #'encode-message data)))
|
|
|
|
(cat (encode-int32 (length message)) message)))
|
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun encode-message (address &rest data)
|
2005-01-26 13:52:24 +00:00
|
|
|
"encodes an osc message with the given address and data."
|
2005-03-06 12:25:01 +00:00
|
|
|
(concatenate '(vector (unsigned-byte 8))
|
2005-02-10 13:32:51 +00:00
|
|
|
(encode-address address)
|
|
|
|
(encode-typetags data)
|
|
|
|
(encode-data data)))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun encode-address (address)
|
2005-01-26 13:52:24 +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. ..
|
|
|
|
|
2005-03-14 14:05:05 +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
|
2005-08-11 09:02:31 +00:00
|
|
|
b => #(98) => blob
|
2015-08-25 19:54:38 +00:00
|
|
|
h => #(104) => int64
|
2005-08-11 09:02:31 +00:00
|
|
|
and considers non int/float/string data to be a blob."
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-03-06 12:25:01 +00:00
|
|
|
(let ((lump (make-array 0 :adjustable t
|
2005-11-29 11:11:26 +00:00
|
|
|
: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 #\,)
|
|
|
|
(dolist (x data)
|
|
|
|
(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))
|
2005-08-11 09:02:31 +00:00
|
|
|
(t (write-to-vector #\b)))))
|
2005-11-29 11:11:26 +00:00
|
|
|
(cat lump
|
|
|
|
(pad (padding-length (length lump))))))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun encode-data (data)
|
2005-01-26 13:52:24 +00:00
|
|
|
"encodes data in a format suitable for an OSC message"
|
|
|
|
(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)))))
|
|
|
|
(dolist (x data)
|
|
|
|
(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))
|
2005-08-11 09:02:31 +00:00
|
|
|
(t (enc encode-blob))))
|
2005-03-02 17:23:56 +00:00
|
|
|
lump)))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
|
2005-01-26 13:52:24 +00:00
|
|
|
;;;;;; ; ;; ; ; ; ; ; ; ;
|
|
|
|
;;
|
|
|
|
;; decoding OSC messages
|
|
|
|
;;
|
|
|
|
;;; ;; ;; ; ; ; ; ; ;
|
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
(defun decode-bundle (data)
|
|
|
|
"decodes an osc bundle into a list of decoded-messages, which has
|
|
|
|
an osc-timetagas its first element"
|
|
|
|
(let ((contents '()))
|
|
|
|
(if (equalp 35 (elt data 0)) ; a bundle begins with '#'
|
|
|
|
(let ((timetag (subseq data 8 16))
|
|
|
|
(i 16)
|
|
|
|
(bundle-length (length data)))
|
|
|
|
(loop while (< i bundle-length)
|
|
|
|
do (let ((mark (+ i 4))
|
|
|
|
(size (decode-int32
|
|
|
|
(subseq data i (+ i 4)))))
|
|
|
|
(if (eq size 0)
|
|
|
|
(setf bundle-length 0)
|
|
|
|
(push (decode-bundle
|
|
|
|
(subseq data mark (+ mark size)))
|
|
|
|
contents))
|
|
|
|
(incf i (+ 4 size))))
|
|
|
|
(push timetag contents))
|
|
|
|
(decode-message data))))
|
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun decode-message (message)
|
2005-03-14 14:05:05 +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)))
|
2005-03-02 17:23:56 +00:00
|
|
|
(if (eq x NIL)
|
2005-03-06 12:25:01 +00:00
|
|
|
(format t "message contains no data.. ")
|
|
|
|
(cons (decode-address (subseq message 0 x))
|
|
|
|
(decode-taged-data (subseq message x))))))
|
2005-03-14 14:05:05 +00:00
|
|
|
|
2005-02-10 13:32:51 +00:00
|
|
|
(defun decode-address (address)
|
2005-03-06 12:25:01 +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...
|
2005-03-14 14:05:05 +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)))
|
|
|
|
(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
|
|
|
|
((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)))
|
2015-08-25 19:25:05 +00:00
|
|
|
(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!
|
2005-08-11 09:02:31 +00:00
|
|
|
(push (decode-blob (subseq acc 0 end))
|
|
|
|
result)
|
|
|
|
(setf acc (subseq acc end))))
|
2015-08-25 19:59:32 +00:00
|
|
|
(t (error "unrecognised typetag ~a" x))))
|
|
|
|
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
|
|
|
;;
|
|
|
|
;; timetags
|
|
|
|
;;
|
2006-02-10 23:38:40 +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 anyt receiver) to be created.
|
2005-03-14 14:05:05 +00:00
|
|
|
;;
|
2006-02-10 23:38:40 +00:00
|
|
|
;; - note: not well tested, and probably not accurate enough for syncronisation.
|
|
|
|
;; see also: CLHS 25.1.4 Time, and the ntp timestamp format. also needs to
|
|
|
|
;; convert from 2 32bit ints to 64bit fixed point value.
|
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
|
|
|
;;;; ;; ; ;
|
|
|
|
|
|
|
|
(defconstant +unix-epoch+ (encode-universal-time 0 0 0 1 1 1970 0))
|
|
|
|
|
2006-02-10 23:38:40 +00:00
|
|
|
(defun encode-timetag (utime &optional subseconds)
|
|
|
|
"encodes an osc timetag from a universal-time and 32bit 'sub-second' part.
|
|
|
|
for an 'instantaneous' timetag use (encode-timetag :now)
|
|
|
|
for a timetag with the current time use (encode-timetag :time)"
|
|
|
|
(cond
|
2007-02-26 15:20:53 +00:00
|
|
|
;; a 1bit timetag will be interpreted as 'imediately'
|
2006-02-10 23:38:40 +00:00
|
|
|
((equalp utime :now)
|
|
|
|
#(0 0 0 0 0 0 0 1))
|
|
|
|
;; converts seconds since 19000101 to seconds since 19700101
|
|
|
|
;; note: fractions of a second is accurate, but not syncronised.
|
|
|
|
((equalp utime :time)
|
|
|
|
(cat (encode-int32 (- (get-universal-time) +unix-epoch+))
|
|
|
|
(encode-int32
|
2006-02-10 23:44:10 +00:00
|
|
|
(round (* internal-time-units-per-second
|
|
|
|
(second (multiple-value-list
|
|
|
|
(floor (/ (get-internal-real-time)
|
|
|
|
internal-time-units-per-second)))))))))
|
2006-02-10 23:38:40 +00:00
|
|
|
((integerp utime)
|
|
|
|
(cat (encode-int32 (+ utime +unix-epoch+))
|
|
|
|
(encode-int32 subseconds)))
|
|
|
|
(t (error "the time or subsecond given is not an integer"))))
|
2005-03-14 14:05:05 +00:00
|
|
|
|
|
|
|
(defun decode-timetag (timetag)
|
2006-02-10 23:38:40 +00:00
|
|
|
"decomposes a timetag into unix-time and a subsecond,. . ."
|
2005-03-14 14:05:05 +00:00
|
|
|
(list
|
|
|
|
(decode-int32 (subseq timetag 0 4))
|
|
|
|
(decode-int32 (subseq timetag 4 8))))
|
|
|
|
|
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
|
|
|
|
;; particulaly portable, but 'works for now'.
|
|
|
|
|
2005-01-26 13:52:24 +00:00
|
|
|
(defun encode-float32 (f)
|
2005-03-02 17:23:56 +00:00
|
|
|
"encode an ieee754 float as a 4 byte vector. currently sbcl/cmucl specifc"
|
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)
|
|
|
|
(+ (ash x 16) y)))
|
|
|
|
#-(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))
|
|
|
|
(ldb (byte 16 0) (decode-int32 s)))
|
|
|
|
#-(or sbcl cmucl openmcl allegro) (error "cant decode floats using this implementation"))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
|
|
|
(defun decode-int32 (s)
|
2005-11-29 11:11:26 +00:00
|
|
|
"4 byte -> 32 bit int -> two's compliment (in network byte order)"
|
2005-01-26 13:52:24 +00:00
|
|
|
(let ((i (+ (ash (elt s 0) 24)
|
|
|
|
(ash (elt s 1) 16)
|
|
|
|
(ash (elt s 2) 8)
|
|
|
|
(elt s 3))))
|
|
|
|
(if (>= i #x7fffffff)
|
|
|
|
(- 0 (- #x100000000 i))
|
2005-03-06 12:47:28 +00:00
|
|
|
i)))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2005-12-05 19:07:01 +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
|
|
|
|
for n below num-of-octets
|
|
|
|
collect `(setf (aref ,buf ,n)
|
|
|
|
(ldb (byte 8 (* 8 (- (1- ,num-of-octets) ,n)))
|
|
|
|
,int)))
|
|
|
|
,buf))))
|
2005-01-26 13:52:24 +00:00
|
|
|
|
2019-03-24 21:00:00 +00:00
|
|
|
(defint-encoder 4 "Convert an integer into a sequence of 4 bytes in network byte order.")
|
|
|
|
(defint-encoder 8 "Convert an integer into a sequence of 8 bytes in network byte order.")
|
2015-08-25 19:54:38 +00:00
|
|
|
|
|
|
|
(defun decode-uint64 (s)
|
|
|
|
"8 byte -> 64 bit unsigned int"
|
|
|
|
(let ((i (+ (ash (elt s 0) 56)
|
|
|
|
(ash (elt s 1) 48)
|
|
|
|
(ash (elt s 2) 40)
|
|
|
|
(ash (elt s 3) 32)
|
|
|
|
(ash (elt s 4) 24)
|
|
|
|
(ash (elt s 5) 16)
|
|
|
|
(ash (elt s 6) 8)
|
|
|
|
(elt s 7))))
|
|
|
|
i))
|
|
|
|
|
|
|
|
(defun decode-int64 (s)
|
|
|
|
"8 byte -> 64 bit int -> two's compliment (in network byte order)"
|
|
|
|
(let ((i (+ (ash (elt s 0) 56)
|
|
|
|
(ash (elt s 1) 48)
|
|
|
|
(ash (elt s 2) 40)
|
|
|
|
(ash (elt s 3) 32)
|
|
|
|
(ash (elt s 4) 24)
|
|
|
|
(ash (elt s 5) 16)
|
|
|
|
(ash (elt s 6) 8)
|
|
|
|
(elt s 7))))
|
|
|
|
(if (>= i #x7fffffffffffffff)
|
|
|
|
(- 0 (- #x10000000000000000 i))
|
|
|
|
i)))
|
|
|
|
|
2005-03-14 14:05:05 +00:00
|
|
|
;; osc-strings are unsigned bytes, padded to a 4 byte boundary
|
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)))
|
|
|
|
|
|
|
|
(defun encode-string (string)
|
|
|
|
"encodes a string as a vector of character-codes, padded to 4 byte boundary"
|
|
|
|
(cat (map 'vector #'char-code string)
|
2005-03-14 14:05:05 +00:00
|
|
|
(string-padding string)))
|
|
|
|
|
|
|
|
;; 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 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
|
|
|
(defun encode-blob (blob)
|
|
|
|
"encodes a blob from a given vector"
|
|
|
|
(let ((bl (length blob)))
|
|
|
|
(cat (encode-int32 bl) blob
|
2015-08-25 19:25:05 +00:00
|
|
|
(pad (mod (- 4 bl) 4))))) ; NOTE: cannot use (padding-length bl), as it is not the same algorithm. Blobs of 4, 8, 12 etc bytes should not be padded!
|
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)
|
|
|
|
(apply #'concatenate '(vector *) 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"
|
2005-03-06 12:25:01 +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)
|
2005-03-02 17:23:56 +00:00
|
|
|
;; end
|