Skip to content

Commit

Permalink
Merge pull request #103 from mdedwards/string-to-osc
Browse files Browse the repository at this point in the history
OSC-CALL: convert to string and back to parse multiple input formats (where :csound is t)
  • Loading branch information
mdedwards authored Oct 19, 2024
2 parents 7e25a08 + 21815fe commit 94c69e9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
68 changes: 42 additions & 26 deletions src/osc-sc-bsd.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,25 @@
+osc-sc-in-socket+ nil
+osc-sc-out-socket+ nil))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; LF 2024-10-19 added to keep osc-call clean
;;; parse a message of style ("/osc-sc something (+ 1 1)")
;;; => ("/osc-sc" "something" "(+" 1 "1")
(defun parse-one-string-message (message)
(loop for i in (first (sc::string-to-list (sc::list-to-string message)))
collect (write-to-string i) into result
finally (progn (setf (first result) (format nil "(~a" (first result)))
(setf (first (last result))
(format nil "~a)" (first (last result))))
(return result))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The main function we call to process lisp code and other messages sent via
;;; OSC. Listens on a given port and sends out on another. NB ip#s need to
;;; be in the format #(127 0 0 1) for now.

(defun osc-call (listen-port send-ip send-port print)
(defun osc-call (listen-port send-ip send-port print csound)
;; (let ((buffer (make-sequence '(vector (unsigned-byte 8)) 512)))
;; MDE Mon Apr 11 11:15:47 2016 -- increased buffer size
;; MDE Tue Mar 31 15:07:24 2020 -- increased again
Expand All @@ -87,31 +100,34 @@
(unwind-protect
(format t "~&All good. Awaiting osc messages.~%")
(loop with happy = t while happy do
;; need this otherwise messages only get printed when we quit
(finish-output t)
(socket-receive +osc-sc-in-socket+ buffer nil)
(let* ((oscuff (osc:decode-bundle buffer))
;; here: check if there's an opening (
(soscuff
(progn
(unless (third oscuff)
(error "osc-sc::osc-call: Couldn't decode buffer: ~a"
buffer))
(if (char= #\( (elt (third oscuff) 0))
'lisp
(read-from-string (third oscuff))))))
(when print ; MDE Mon May 26 10:39:48 2014
(format t "~&osc-->message: ~a" oscuff))
(finish-output t)
(case (sc::rm-package soscuff :sb-bsd-sockets)
;; test
(int (handle-number +osc-sc-output-stream+ (second oscuff)))
(quit (setf happy nil))
;; saw opening ( so evaluate lisp code
(lisp (osc-eval +osc-sc-output-stream+ oscuff print))
(t (warn "osc-sc::osc-call: Don't understand ~a. Ignoring."
soscuff)
(finish-output)))))))
;; need this otherwise messages only get printed when we quit
(finish-output t)
(socket-receive +osc-sc-in-socket+ buffer nil)
;; LF 2024-10-19, added csound argument and parsing
(let* ((oscuff (if csound
(parse-one-string-message (osc:decode-bundle buffer))
(osc:decode-bundle buffer)))
;; here: check if there's an opening (
(soscuff
(progn
(unless (third oscuff)
(error "osc-sc::osc-call: Couldn't decode buffer: ~a"
buffer))
(if (char= #\( (elt (third oscuff) 0))
'lisp
(read-from-string (third oscuff))))))
(when print ; MDE Mon May 26 10:39:48 2014
(format t "~&osc-->message: ~a" oscuff))
(finish-output t)
(case (sc::rm-package soscuff :sb-bsd-sockets)
;; test
(int (handle-number +osc-sc-output-stream+ (second oscuff)))
(quit (setf happy nil))
;; saw opening ( so evaluate lisp code
(lisp (osc-eval +osc-sc-output-stream+ oscuff print))
(t (warn "osc-sc::osc-call: Don't understand ~a. Ignoring."
soscuff)
(finish-output)))))))
(osc-cleanup-sockets)
t)

Expand Down
9 changes: 7 additions & 2 deletions src/osc-sc.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
;;; Default = #(127 0 0 1))
;;; - :send-port. The UDP port to send messages back out on. Default = 8091.
;;; - :print. Print messages as they arrive. Default = NIL.
;;; - :csound. Default = NIL, meaning incomming messages should
;;; be formatted similar to ("/osc-sc" "something" "(+" 1 "1"). When set to T,
;;; messages can also have this format: ("/osc-sc something (+ 1 1)").
;;;
;;; RETURN VALUE
;;; T
Expand All @@ -104,9 +107,11 @@
(listen-port 8090)
(send-ip #(127 0 0 1))
(print nil)
(send-port 8091))
(send-port 8091)
csound)
;;; ****
(sb-bsd-sockets::osc-call listen-port send-ip send-port print))
;; LF 2024-10-19, added csound argument.
(sb-bsd-sockets::osc-call listen-port send-ip send-port print csound))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand Down

0 comments on commit 94c69e9

Please sign in to comment.