forked from zzkt/osc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osc-examples.lisp
92 lines (86 loc) · 3.03 KB
/
osc-examples.lisp
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
;; -*- mode: lisp -*-
;;
;; Examples of how to send OSC messages. ..
;;
;; Copyright (C) 2004 FoAM vzw
;;
;; Authors
;; - nik gaffney <[email protected]>
;;
;;;;; ;; ; ; ;; ; ;
;;;;;::::: : : : ; ;; ; ; ; ; ; ;
;;
;; Commentry
;;
;; These examples are currently sbcl specific, but should be easily ported to
;; work with trivial-sockets, acl-compat or something similar. They should be
;; able to explain enough to get you started. ..
;;
;; eg. listen on port 6667 for incoming msgs
;;
;; (osc-receive-test 6667)
;; eg. send a test message to localhost port 6668
;;
;; (osc-send-test #(127 0 0 1) 6668)
;;
;; eg. listen on port 6667 and send to 10.0.89:6668
;; note the ip# is formatted as a vector
;;
;; (osc-reflector-test 6667 #(10 0 0 89) 6668)
;;
;;;;;:::;;: ; ; ;::: ; ;; ;; ; ;; ;
(eval-when (:compile-toplevel :load-toplevel)
(ql:quickload :osc)
(ql:quickload :usocket)
(defpackage osc-examples (:use :cl :osc :usocket)))
(in-package :osc-examples)
(defun osc-receive-test (port)
"a basic test function which attempts to decode an osc message on given port.
note ip#s need to be in the format #(127 0 0 1) for now.. ."
(let ((s (socket-connect nil nil
:local-port port
:local-host #(127 0 0 1)
:protocol :datagram
:element-type '(unsigned-byte 8)))
(buffer (make-sequence '(vector (unsigned-byte 8)) 1024)))
(format t "listening on localhost port ~A~%~%" port)
(unwind-protect
(loop do
(socket-receive s buffer (length buffer))
(format t "received -=> ~S~%" (osc:decode-bundle buffer)))
(when s (socket-close s)))))
(defun osc-send-test (host port)
"a basic test function which sends osc test message to a given port/hostname.
note ip#s need to be in the format #(127 0 0 1) for now.. ."
(let ((s (socket-connect host port
:protocol :datagram
:element-type '(unsigned-byte 8)))
(b (osc:encode-message "/foo/bar" "baz" 1 2 3 (coerce PI 'single-float))))
(format t "sending to ~a on port ~A~%~%" host port)
(unwind-protect
(socket-send s b (length b))
(when s (socket-close s)))))
(defun osc-reflector-test (listen-port send-host send-port)
"reflector.. . listens on a given port and sends out on another
note ip#s need to be in the format #(127 0 0 1) for now.. ."
(let ((in (socket-connect nil nil
:local-port listen-port
:local-host #(127 0 0 1)
:protocol :datagram
:element-type '(unsigned-byte 8)))
(out (socket-connect send-host send-port
:protocol :datagram
:element-type '(unsigned-byte 8)))
(buffer (make-sequence '(vector (unsigned-byte 8)) 1024)))
(unwind-protect
(loop do
(socket-receive in buffer (length buffer))
(format t "glonked -=> message: ~{~A, ~}~%"
(osc:decode-bundle buffer))
(let ((mess (apply #'osc:encode-message
(cons "/echo"
(osc:decode-message buffer)))))
(socket-send out mess (length mess))))
(when in (socket-close in))
(when out (socket-close out)))))
;end