Class: OSC::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-osc/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address = '', *args) ⇒ Message

Returns a new instance of Message.

Raises:

  • (TypeError)


5
6
7
8
9
10
# File 'lib/ruby-osc/message.rb', line 5

def initialize address = '', *args
  args.collect! { |arg| OSC.coerce_argument arg }
  args.flatten! # won't harm we're not accepting arrays anyway, in case an custom coerced arg coerces to Array eg. Hash
  raise(TypeError, "Expected address to be a string") unless String === address
  @address, @args = address, args
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/ruby-osc/message.rb', line 3

def address
  @address
end

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/ruby-osc/message.rb', line 3

def args
  @args
end

#timeObject

Returns the value of attribute time.



3
4
5
# File 'lib/ruby-osc/message.rb', line 3

def time
  @time
end

Class Method Details

.decode(string) ⇒ Object



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
# File 'lib/ruby-osc/message.rb', line 32

def self.decode string
  scanner        = StringScanner.new string
  address, tags  = (1..2).map do
    string       = scanner.scan(/[^\000]+\000/)
    scanner.pos += OSC.padding_size(string.size)
    string.chomp("\000")
  end

  args = []
  tags.scan(/\w/) do |tag|
    case tag
    when 'i'
      int = scanner.scan(/.{4}/nm).unpack('N').first
      args.push( int > (2**31-1) ? int - 2**32 : int )
    when 'f'
      args.push scanner.scan(/.{4}/nm).unpack('g').first
    when 's'
      str = scanner.scan(/[^\000]+\000/)
      scanner.pos += OSC.padding_size(str.size)
      args.push str.chomp("\000")
    when 'b'
      size = scanner.scan(/.{4}/).unpack('N').first
      str  = scanner.scan(/.{#{ size }}/nm)
      scanner.pos += OSC.padding_size(size + 4)
      args.push Blob.new(str)
    else
      raise DecodeError, "#{ t } is not a known tag"
    end
  end

  new address, *args
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
# File 'lib/ruby-osc/message.rb', line 25

def == other
  self.class == other.class and to_a == other.to_a
end

#encodeObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby-osc/message.rb', line 12

def encode
  objs, tags, dirs = @args.collect { |arg| OSC.encoding_directive arg }.transpose
  dirs ||= [] and objs ||= []

  [",#{ tags and tags.join }", @address].each do |str|
    obj, tag, dir = OSC.encoding_directive str
    objs.unshift obj
    dirs.unshift dir
  end

  objs.flatten.compact.pack dirs.join
end

#to_aObject



29
# File 'lib/ruby-osc/message.rb', line 29

def to_a; @args.dup.unshift(@address) end

#to_sObject



30
# File 'lib/ruby-osc/message.rb', line 30

def to_s; "OSC::Message(#{ args.join(', ') })" end