branch: master commit 4db95ab266983bf89adcd17d17b91aee1a1b43b4 Author: Mario Lang <ml...@blind.guru> Commit: Mario Lang <ml...@blind.guru>
Concatenate messages instead of using a temp-buffer and buffer-string * packages/osc/osc.el: (osc-float32, osc-int32, osc-string): New functions. * (osc-insert-float32, osc-insert-int32, osc-insert-string): Remove. * (osc-send-message): Adjust. --- packages/osc/osc.el | 59 +++++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/packages/osc/osc.el b/packages/osc/osc.el index 316b532..e88f135 100644 --- a/packages/osc/osc.el +++ b/packages/osc/osc.el @@ -51,10 +51,11 @@ (require 'cl-lib) -(defun osc-insert-string (string) - (insert string 0 (make-string (- 3 (% (length string) 4)) 0))) +(defun osc-string (string) + (setq string (encode-coding-string string 'binary)) + (concat string (make-string (1+ (- 3 (% (length string) 4))) 0))) -(defun osc-insert-float32 (value) +(defun osc-float32 (value) (let (s (e 0) f) (cond ((= value 0.0) @@ -73,18 +74,17 @@ (if (= e 0) (while (< (* f (expt 2.0 e)) 1.0) (setq e (1+ e)))) (setq f (round (* (1- (* f (expt 2.0 e))) (expt 2 23))) e (+ (* -1 e) 127)))) - (insert (+ (lsh s 7) (lsh (logand e #XFE) -1)) - (+ (lsh (logand e #X01) 7) (lsh (logand f #X7F0000) -16)) - (lsh (logand f #XFF00) -8) - (logand f #XFF)))) + (unibyte-string (+ (lsh s 7) (lsh (logand e #XFE) -1)) + (+ (lsh (logand e #X01) 7) (lsh (logand f #X7F0000) -16)) + (lsh (logand f #XFF00) -8) + (logand f #XFF)))) -(defun osc-insert-int32 (value) +(defun osc-int32 (value) (let (bytes) (dotimes (i 4) (push (% value 256) bytes) (setq value (/ value 256))) - (dolist (byte bytes) - (insert byte)))) + (apply 'unibyte-string bytes))) ;;;###autoload (defun osc-make-client (host port) @@ -99,23 +99,28 @@ ;;;###autoload (defun osc-send-message (client path &rest args) "Send an OSC message from CLIENT to the specified PATH with ARGS." - (with-temp-buffer - (set-buffer-multibyte nil) - (osc-insert-string path) - (osc-insert-string - (apply 'concat "," (mapcar (lambda (arg) - (cond - ((floatp arg) "f") - ((integerp arg) "i") - ((stringp arg) "s") - (t (error "Invalid argument: %S" arg)))) - args))) - (dolist (arg args) - (cond - ((floatp arg) (osc-insert-float32 arg)) - ((integerp arg) (osc-insert-int32 arg)) - ((stringp arg) (osc-insert-string arg)))) - (process-send-string client (buffer-string)))) + (process-send-string + client + (apply + 'concat + (osc-string path) + (osc-string + (apply + 'concat "," + (mapcar (lambda (arg) + (cond + ((floatp arg) "f") + ((integerp arg) "i") + ((stringp arg) "s") + (t (error "Invalid argument: %S" arg)))) + args))) + (mapcar + (lambda (arg) + (cond + ((floatp arg) (osc-float32 arg)) + ((integerp arg) (osc-int32 arg)) + ((stringp arg) (osc-string arg)))) + args)))) (defun osc-read-string () (let ((pos (point)) string)