Class: Cyclotone::Backends::OSCBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclotone/backends/osc_backend.rb

Defined Under Namespace

Classes: Blob, Double

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "127.0.0.1", port: 57_120, address: "/dirt/play", socket: nil, socket_factory: nil, retries: 1) ⇒ OSCBackend

Returns a new instance of OSCBackend.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cyclotone/backends/osc_backend.rb', line 23

def initialize(
  host: "127.0.0.1",
  port: 57_120,
  address: "/dirt/play",
  socket: nil,
  socket_factory: nil,
  retries: 1
)
  @host = host
  @port = port
  @address = address
  @socket_factory = socket_factory || proc { UDPSocket.new }
  @retries = retries.to_i
  @socket = socket || build_socket
  @closed = false
rescue StandardError => error
  raise ConnectionError, error.message
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



11
12
13
# File 'lib/cyclotone/backends/osc_backend.rb', line 11

def address
  @address
end

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/cyclotone/backends/osc_backend.rb', line 11

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/cyclotone/backends/osc_backend.rb', line 11

def port
  @port
end

Class Method Details

.blob(bytes) ⇒ Object



18
19
20
# File 'lib/cyclotone/backends/osc_backend.rb', line 18

def blob(bytes)
  Blob.new(bytes: bytes.to_s.b)
end

.double(value) ⇒ Object



14
15
16
# File 'lib/cyclotone/backends/osc_backend.rb', line 14

def double(value)
  Double.new(value: value.to_f)
end

Instance Method Details

#build_bundle(events, at:, cps: nil, timetag: at) ⇒ Object



56
57
58
59
# File 'lib/cyclotone/backends/osc_backend.rb', line 56

def build_bundle(events, at:, cps: nil, timetag: at)
  messages = Array(events).map { |event| build_message(event, at: at, cps: cps) }
  encode_bundle(messages, timetag: timetag)
end

#build_message(event, at:, cps: nil) ⇒ Object



52
53
54
# File 'lib/cyclotone/backends/osc_backend.rb', line 52

def build_message(event, at:, cps: nil)
  encode_message(address, payload_for(event, at: at, cps: cps))
end

#closeObject



78
79
80
81
82
# File 'lib/cyclotone/backends/osc_backend.rb', line 78

def close
  close_socket
  @closed = true
  self
end

#flushObject



70
71
72
# File 'lib/cyclotone/backends/osc_backend.rb', line 70

def flush
  self
end

#panicObject



74
75
76
# File 'lib/cyclotone/backends/osc_backend.rb', line 74

def panic
  self
end

#payload_for(event, at:, cps: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/cyclotone/backends/osc_backend.rb', line 42

def payload_for(event, at:, cps: nil)
  values = event.value.is_a?(Hash) ? event.value : { value: event.value }

  [
    "when", at.to_f,
    "onset", absolute_onset(event, at),
    "offset", absolute_offset(event, at, cps)
  ].compact + flatten_hash(values)
end

#send_event(event, at: Time.now.to_f, cps: nil, **_options) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/cyclotone/backends/osc_backend.rb', line 61

def send_event(event, at: Time.now.to_f, cps: nil, **_options)
  reopen_if_closed!
  with_retry do
    @socket.send(build_message(event, at: at, cps: cps), 0, host, port)
  end
rescue StandardError => error
  raise ConnectionError, error.message
end