diff --git a/src/OSC.jl b/src/OSC.jl index 366661c..7dba2b4 100644 --- a/src/OSC.jl +++ b/src/OSC.jl @@ -1,4 +1,5 @@ module OSC +import Base.show macro incfp(x) quote begin local gensym_ = $(esc(x)) $(esc(x)) = $(esc(x))+1 @@ -93,7 +94,7 @@ end function vsosc_null(address::ASCIIString, arguments::ASCIIString, args...) - pos::Int = length(address) + pos::Int = length(address)+1 pos = align(pos) pos += 1+length(arguments) pos = align(pos) @@ -119,7 +120,7 @@ function vsosc_null(address::ASCIIString, end #other args classes are ignored end - return pos; + return pos-1; end function rtosc_amessage(buffer::Array{Uint8}, @@ -196,6 +197,8 @@ function rtosc_amessage(buffer::Array{Uint8}, return pos-1; end +OscMsg(address, arguments, args...) = message(address, arguments, args...) + function message(address::ASCIIString, arguments::ASCIIString, args...) @@ -273,15 +276,15 @@ end getindex(msg::OscMsg, idx::Int) = rtosc_argument(msg, idx) -function show(msg::OscMsg) - println("OSC Message to ", stringify(msg.data)) - println(" Arguments:"); +function show(io::IO, msg::OscMsg) + println(io, "OSC Message to ", stringify(msg.data)) + println(io, " Arguments:"); for i=1:narguments(msg) - showField(msg,i) + showField(io, msg,i) end end -function showField(msg::OscMsg, arg_id) +function showField(io::IO, msg::OscMsg, arg_id) map = ['i' Int32; 'f' Float32; 's' String; 'b' :Blob; 'h' Int32; 't' Uint64; 'd' Float64; 'S' Symbol; 'c' Char; 'r' :RBG; 'm' :Midi; 'T' true; 'F' false; 'N' Nothing] @@ -292,11 +295,14 @@ function showField(msg::OscMsg, arg_id) if(issubtype(typeof(value), Array)) value = value' end - @printf(" #%2d %c:", arg_id, typeChar); + @printf(io, " #%2d %c:", arg_id, typeChar); print(dict[typeChar]," - ", value) if(!issubtype(typeof(value), Array)) println() end end + +export OscMsg + end diff --git a/test/runtests.jl b/test/runtests.jl index 128ea74..9625200 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using Base.Test require("OSC") +using OSC test_type = length(ARGS) == 1 ? ARGS[1] : "ALL" @@ -36,9 +37,8 @@ function test_it_fat() #nil #inf - msg = OSC.message("/dest", - "[ifsbhtdScrmTFNI]", i,f,s,b,h,t,d,S,c,r,m); - OSC.show(msg) + msg = OscMsg("/dest", "[ifsbhtdScrmTFNI]", i,f,s,b,h,t,d,S,c,r,m); + show(msg) #println(string(map(x->(hex(x,2)), buffer[1:len])...)) #println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...)) @@ -62,7 +62,6 @@ function test_it_fat() end function test_it_osc_spec() - buffer::Array{Uint8} = Array(Uint8, 256) println("Starting OSC Spec...") message_one::Array{Uint8} = [ 0x2f, 0x6f, 0x73, 0x63, @@ -76,37 +75,36 @@ function test_it_osc_spec() ]; message_two::Array{Uint8} = [ - 0x2f, 0x66, 0x6f, 0x6f, - 0x00, 0x00, 0x00, 0x00, + 0x2f, 0x66, 0x6f, 0x6f, #4 + 0x00, 0x00, 0x00, 0x00, #8 0x2c, 0x69, 0x69, 0x73, - 0x66, 0x66, 0x00, 0x00, + 0x66, 0x66, 0x00, 0x00, #16 0x00, 0x00, 0x03, 0xe8, 0xff, 0xff, 0xff, 0xff, 0x68, 0x65, 0x6c, 0x6c, - 0x6f, 0x00, 0x00, 0x00, - 0x3f, 0x9d, 0xf3, 0xb6, - 0x40, 0xb5, 0xb2, 0x2d, + 0x6f, 0x00, 0x00, 0x00, #32 + 0x3f, 0x9d, 0xf3, 0xb6, #36 + 0x40, 0xb5, 0xb2, 0x2d, #40 ]; - len=OSC.rtosc_amessage(buffer, 256, "/oscillator/4/frequency", "f", float32(440.0)) + osc=OscMsg("/oscillator/4/frequency", "f", float32(440.0)) - println(string(map(x->(hex(x,2)), buffer[1:len])...)) + println(string(map(x->(hex(x,2)), osc.data)...)) println(string(map(x->(hex(x,2)), message_one)...)) - println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...)) + println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), osc.data)...)) println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), message_one)...)) - @test len == length(message_one) - @test buffer[1:length(message_one)] == message_one - OSC.show(OSC.OscMsg(buffer)) + @test length(osc.data) == length(message_one) + @test osc.data == message_one + show(osc) - len = OSC.rtosc_amessage(buffer, 256, "/foo", "iisff", - int32(1000), int32(-1), "hello", float32(1.234), float32(5.678)) - println(string(map(x->(hex(x,2)), buffer[1:len])...)) + osc = OscMsg("/foo", "iisff", int32(1000), int32(-1), "hello", float32(1.234), float32(5.678)) + println(string(map(x->(hex(x,2)), osc.data)...)) println(string(map(x->(hex(x,2)), message_two)...)) - println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...)) + println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), osc.data)...)) println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), message_two)...)) - @test len == length(message_two) - @test buffer[1:len] == message_two - OSC.show(OSC.OscMsg(buffer)) + @test length(osc.data) == length(message_two) + @test osc.data == message_two + show(osc) end if test_type in ["ALL", "TEST", "INSTALL"]