lib/patchmaster/connection.rb in patchmaster-0.0.0 vs lib/patchmaster/connection.rb in patchmaster-0.0.1
- old
+ new
@@ -1,12 +1,13 @@
require 'patchmaster/consts'
module PM
-# A Connection connects an InputDevice to an OutputDevice. Whenever MIDI
-# data arrives at the InputDevice it is optionally modified or filtered,
-# then the remaining modified data is sent to the OutputDevice.
+# A Connection connects an InputInstrument to an OutputInstrument. Whenever
+# MIDI data arrives at the InputInstrument it is optionally modified or
+# filtered, then the remaining modified data is sent to the
+# OutputInstrument.
class Connection
attr_accessor :input, :input_chan, :output, :output_chan,
:pc_prog, :zone, :xpose, :filter
@@ -22,23 +23,24 @@
@input_chan -= 1 if @input_chan
@output_chan -= 1 if @output_chan
end
def start(start_bytes=nil)
- midi_out(@output, start_bytes) if start_bytes
- midi_out(@output, [PROGRAM_CHANGE + @output_chan, @pc_prog]) if pc?
+ midi_out(start_bytes) if start_bytes
+ midi_out([PROGRAM_CHANGE + @output_chan, @pc_prog]) if pc?
@input.add_connection(self)
end
- def stop
+ def stop(stop_bytes=nil)
+ midi_out(stop_bytes) if stop_bytes
@input.remove_connection(self)
end
def accept_from_input?(bytes)
return true if @input_chan == nil
- return true if bytes[0] >= 0xf0
- (bytes[0] & 0xf0) >= 0x80 && (bytes[0] & 0x0f) == @input_chan
+ return true unless bytes.channel?
+ bytes.note? && bytes.channel == @input_chan
end
# Returns true if the +@zone+ is nil (allowing all notes throught) or if
# +@zone+ is a Range and +note+ is inside +@zone+.
def inside_zone?(note)
@@ -46,12 +48,12 @@
end
def midi_in(bytes)
return unless accept_from_input?(bytes)
- # TODO handle running bytes
- high_nibble = bytes[0] & 0xf0
+ # TODO handle running bytes if needed
+ high_nibble = bytes.high_nibble
case high_nibble
when NOTE_ON, NOTE_OFF, POLY_PRESSURE
return unless inside_zone?(bytes[1])
bytes[0] = high_nibble + @output_chan
bytes[1] = ((bytes[1] + @xpose) & 0xff) if @xpose
@@ -59,15 +61,15 @@
bytes[0] = high_nibble + @output_chan
end
bytes = @filter.call(self, bytes) if @filter
if bytes && bytes.size > 0
- midi_out(@output, bytes)
+ midi_out(bytes)
end
end
- def midi_out(device, bytes)
- device.midi_out(bytes)
+ def midi_out(bytes)
+ @output.midi_out(bytes)
end
def pc?
@pc_prog != nil
end