Class: Webmidi::Network::OSC::Bridge
- Inherits:
-
Object
- Object
- Webmidi::Network::OSC::Bridge
- Defined in:
- lib/webmidi/network/osc.rb
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#reverse_mapping ⇒ Object
readonly
Returns the value of attribute reverse_mapping.
Instance Method Summary collapse
- #custom_mapping(&block) ⇒ Object
-
#initialize(midi_input: nil, midi_output: nil, osc_host: "127.0.0.1", osc_port: 9000, mapping: :default, reverse_mapping: :default) ⇒ Bridge
constructor
A new instance of Bridge.
- #osc_to_midi(address, args) ⇒ Object
- #receive_osc(data) ⇒ Object
- #send_osc(message) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(midi_input: nil, midi_output: nil, osc_host: "127.0.0.1", osc_port: 9000, mapping: :default, reverse_mapping: :default) ⇒ Bridge
Returns a new instance of Bridge.
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/webmidi/network/osc.rb', line 121 def initialize(midi_input: nil, midi_output: nil, osc_host: "127.0.0.1", osc_port: 9000, mapping: :default, reverse_mapping: :default) @midi_input = midi_input @midi_output = midi_output @osc_host = osc_host @osc_port = osc_port @mapping = (mapping == :default) ? DEFAULT_MAPPINGS.dup : mapping @reverse_mapping = (reverse_mapping == :default) ? DEFAULT_REVERSE_MAPPINGS.dup : reverse_mapping @socket = nil @running = false @subscription = nil end |
Instance Attribute Details
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
119 120 121 |
# File 'lib/webmidi/network/osc.rb', line 119 def mapping @mapping end |
#reverse_mapping ⇒ Object (readonly)
Returns the value of attribute reverse_mapping.
119 120 121 |
# File 'lib/webmidi/network/osc.rb', line 119 def reverse_mapping @reverse_mapping end |
Instance Method Details
#custom_mapping(&block) ⇒ Object
175 176 177 178 |
# File 'lib/webmidi/network/osc.rb', line 175 def custom_mapping(&block) block.call(@mapping) self end |
#osc_to_midi(address, args) ⇒ Object
170 171 172 173 |
# File 'lib/webmidi/network/osc.rb', line 170 def osc_to_midi(address, args) mapper = @reverse_mapping[address] mapper&.call(args) end |
#receive_osc(data) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/webmidi/network/osc.rb', line 163 def receive_osc(data) address, args = Encoder.(data) = osc_to_midi(address, args) @midi_output&.send() if end |
#send_osc(message) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/webmidi/network/osc.rb', line 152 def send_osc() return unless @running && @socket address = @mapping[.class] return unless address args = midi_to_osc_args() data = Encoder.(address, *args) @socket.send(data, 0, @osc_host, @osc_port) end |
#start ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/webmidi/network/osc.rb', line 134 def start return self if @running @socket = UDPSocket.new @running = true @subscription = @midi_input&. { |msg| send_osc(msg) } self end |
#stop ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/webmidi/network/osc.rb', line 143 def stop @running = false @subscription&.unsubscribe @subscription = nil @socket&.close @socket = nil self end |