class OSC::Packet

Public Class Methods

decode(packet) click to toggle source
# File lib/osc.rb, line 165
def self.decode(packet)
  list = []
  decode2(nil, packet, list)
  list
end

Private Class Methods

decode2(time, packet, list) click to toggle source
# File lib/osc.rb, line 115
def self.decode2(time, packet, list)
  io = PO.new(packet)
  id = decode_string(io)
  if id =~ /\A\#/
    if id == '#bundle'
      t1, t2 = decode_timetag(io)
      if t1 == 0 && t2 == 1
        time = nil
      else
        time = t1 + t2.to_f / (2**32)
      end
      until io.eof?
        l = io.getn(4).unpack('N')[0]
        s = io.getn(l)
        decode2(time, s, list)
      end
    end
  elsif id =~ /\//
    address = id
    if io.getc == ?,.ord
      tags = decode_string(io)
      args = []
      tags.scan(/./) do |t|
        case t
        when 'i'
          i = decode_int32(io)
          args << OSCInt32.new(i)
        when 'f'
          f = decode_float32(io)
          args << OSCFloat32.new(f)
        when 's'
          s = decode_string(io)
          args << OSCString.new(s)
        when 'b'
          b = decode_blob(io)
          args << OSCBlob.new(b)
        when /[htd]/; io.read(8)
        when 'S'; decode_string(io)
        when /[crm]/; io.read(4)
        when /[TFNI\[\]]/;
        end
      end
    end
    list << [time, Message.new(address, nil, *args)]
  end
end
decode_blob(io) click to toggle source
# File lib/osc.rb, line 102
def self.decode_blob(io)
  l = io.getn(4).unpack('N')[0]
  b = io.getn(l)
  io.skip_padding
  b
end
decode_float32(io) click to toggle source
# File lib/osc.rb, line 88
def self.decode_float32(io)
  f = io.getn(4).unpack('g')[0]
  f
end
decode_int32(io) click to toggle source
# File lib/osc.rb, line 82
def self.decode_int32(io)
  i = io.getn(4).unpack('N')[0]
  i -= 2**32 if i > (2**31-1)
  i
end
decode_string(io) click to toggle source
# File lib/osc.rb, line 93
def self.decode_string(io)
  s = ''
  until (c = io.getc) == 0
    s << c
  end
  io.skip_padding
  s
end
decode_timetag(io) click to toggle source
# File lib/osc.rb, line 109
def self.decode_timetag(io)
  t1 = io.getn(4).unpack('N')[0]
  t2 = io.getn(4).unpack('N')[0]
  [t1, t2]
end