aboutsummaryrefslogtreecommitdiff
path: root/extensions/gui/ix/osc.tcl
blob: e5830ec427444b321a73c2e9d682a01f4f3c66f7 (plain)
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
#OSC tcl 2005 ix
package require udp
namespace eval osc {
    proc encode {type data} {
	switch $type {
	    i {set format I }
	    f {set format R }
	    s {set len [string length $data]
		set format a[expr $len + 4 - $len % 4]}
	}
	return [binary format $format $data]
    }
    proc message {args} {
	set path [encode s [lindex $args 0]]
	set typetags ","
	set body ""
	foreach arg [lrange $args 1 end] {
	    if [string is integer $arg] {
		set type i
	    } elseif [string is double $arg] {
		set type f
	    } else {
		set type s
	    }
	    append typetags $type
	    append body [encode $type $arg]
	}
	set typetags [encode s $typetags]
	return $path$typetags$body
    }
    proc connect {host port} {
	set s [udp_open]
	fconfigure $s -remote [list $host $port] -buffering none -translation binary
	return $s
    }
    proc disconnect {socket} {
	close $socket
    }
    proc send {socket msg} {
	puts -nonewline $socket $msg
    }

}