class Patch::IO::MIDI::Input

MIDI Input functions

Attributes

device[R]
id[R]
listener[R]

Public Class Methods

new(id, device, options = {}) click to toggle source

@param [Fixnum] id @param [String, UniMIDI::Input] device @param [Hash] options @option options [Log] :log

# File lib/patch/io/midi/input.rb, line 16
def initialize(id, device, options = {})
  @log = options[:log]
  @id = id
  @device = get_input(device)
  @listener = MIDIEye::Listener.new(@device) unless @device.nil?
end

Public Instance Methods

active?() click to toggle source

Is the input active? @return [Boolean]

# File lib/patch/io/midi/input.rb, line 36
def active?
  @listener.running?
end
disable(patch) click to toggle source

Clear message handlers @return [Boolean]

# File lib/patch/io/midi/input.rb, line 53
def disable(patch)
  @listener.event.clear
  true
end
listen(patch, &callback) click to toggle source

Specify a mpatch context and handler callback to use when messages are received @param [::Patch::Patch] patch @param [Proc] callback @return [Boolean] Whether adding the callback was successful

# File lib/patch/io/midi/input.rb, line 62
def listen(patch, &callback)
  if !@listener.nil?
    @listener.listen_for(:class => [MIDIMessage::ControlChange]) do |event|
      handle_event_received(patch, event, &callback)
    end
    true
  else
    false
  end
end
start() click to toggle source

Start listening for MIDI messages @return [Boolean] Whether the listener was started

# File lib/patch/io/midi/input.rb, line 25
def start
  if !@listener.nil?
    @listener.run(:background => true)
    true
  else
    false
  end
end
stop() click to toggle source

Stop the MIDI listener @return [Boolean]

# File lib/patch/io/midi/input.rb, line 42
def stop
  if !@listener.nil?
    @listener.stop
    true
  else
    false
  end
end

Private Instance Methods

get_input(device) click to toggle source

Initialize the input device using the given string or input. If the device is the string “choose”, the user is prompted to select an available MIDI input. @param [String, UniMIDI::Input, nil] device @return [UniMIDI::Input, nil]

# File lib/patch/io/midi/input.rb, line 91
def get_input(device)
  if device.kind_of?(String)
    if device == "choose"
      UniMIDI::Input.gets
    else
      UniMIDI::Input.find_by_name(device)
    end
  elsif device.respond_to?(:gets)
    device.open if device.kind_of?(UniMIDI::Input)
    device
  end
end
handle_event_received(patch, event) { |patch_messages| ... } click to toggle source

Handle receiving new MIDI messages from the input @param [::Patch::Patch] patch @param [Hash] event @param [Proc] callback @return [Array<::Patch::Message>]

# File lib/patch/io/midi/input.rb, line 80
def handle_event_received(patch, event, &callback)
  messages = event[:message]
  patch_messages = ::Patch::IO::MIDI::Message.to_patch_messages(patch, messages)
  yield(patch_messages) if block_given?
  patch_messages
end