Class: OSC::Message

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/osc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, types = nil, *args) ⇒ Message

address

The OSC address (a String)

types

The OSC type tags string

args

arguments. must match type tags in arity

Example:

Message.new('/foo','ff', Math::PI, Math::E)

Arguments will be coerced as indicated by the type tags. If types is nil, type tags will be inferred from arguments.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/osc.rb', line 100

def initialize(address, types=nil, *args)
  if types and types.size != args.size
    raise ArgumentError, 'type/args arity mismatch'
  end

  @address = address
  @args = []

  if types
    args.each_with_index do |arg, i|
      case types[i]
      when ?i; @args << arg.to_i
      when ?f; @args << arg.to_f
      when ?s; @args << arg.to_s
      when ?b; @args << Blob.new(arg)
      else
        raise ArgumentError, "unknown type tag '#{@types[i].inspect}'"
      end
    end
  else
    args.each do |arg|
      case arg
      when Fixnum,Float,String,TimeTag,Blob
        @args << arg
      else
        raise ArgumentError, "Object has unknown OSC type: '#{arg}'"
      end
    end
  end
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



86
87
88
# File 'lib/osc.rb', line 86

def address
  @address
end

#argsObject

Returns the value of attribute args.



86
87
88
# File 'lib/osc.rb', line 86

def args
  @args
end

#sourceObject

The source of this message, usually something like [“AF_INET”, 50475, ‘localhost’,‘127.0.0.1’]



89
90
91
# File 'lib/osc.rb', line 89

def source
  @source
end

Instance Method Details

#encodeObject

Encode this message for transport



137
138
139
# File 'lib/osc.rb', line 137

def encode
  Packet.encode(self)
end

#to_sObject

string representation. not the raw representation, for that use encode.



142
143
144
# File 'lib/osc.rb', line 142

def to_s
  "#{address},#{types},#{args.collect{|a| a.to_s}.join(',')}"
end

#to_yamlObject



145
146
147
# File 'lib/osc.rb', line 145

def to_yaml
  {'address'=>address, 'types'=>types, 'args'=>args}.to_yaml
end

#typesObject Also known as: typetag



131
132
133
# File 'lib/osc.rb', line 131

def types
  @args.collect {|a| Packet.tag a}.join
end