class MVLC::MIDI::MessageHandler

Directs what should happen when messages are received

Attributes

callback[R]

Public Class Methods

new() click to toggle source
# File lib/mvlc/midi/message_handler.rb, line 9
def initialize
  @callback = {
    :cc => {},
    :note => {},
    :system => {}
  }
end

Public Instance Methods

add_callback(type, key, &callback) click to toggle source

Add a callback for a given MIDI message type @param [Symbol] type The MIDI message type (eg :note, :cc) @param [Integer, String] key The ID of the message eg note number/cc index @param [Proc] callback The callback to execute when the given MIDI command is received @return [Hash]

# File lib/mvlc/midi/message_handler.rb, line 22
def add_callback(type, key, &callback)
  @callback[type][key] = callback
  @callback[type]
end
add_note_callback(note, &callback) click to toggle source

Add a callback for a given MIDI note @param [Symbol] type The MIDI message type (eg :note, :cc) @param [Integer, String] note @param [Proc] callback The callback to execute when the given MIDI command is received @return [Hash]

# File lib/mvlc/midi/message_handler.rb, line 32
def add_note_callback(note, &callback)
  note = MIDIMessage::Constant.value(:note, note) if note.kind_of?(String)
  add_callback(:note, note, &callback)
end
cc_message(message) click to toggle source

Find and call a cc received callback if it exists @param [MIDIMessage] message @return [Boolean, nil]

# File lib/mvlc/midi/message_handler.rb, line 60
def cc_message(message)
  call_callback(:cc, message.index, message.value) |
  call_catch_all_callback(:cc, message)
end
channel_message(channel, message) click to toggle source

Find and call a channel message callback if it exists for the given message and channel @param [Integer, nil] channel @param [MIDIMessage] message @return [Boolean, nil]

# File lib/mvlc/midi/message_handler.rb, line 77
def channel_message(channel, message)
  if channel.nil? || message.channel == channel
    case message
    when MIDIMessage::NoteOn then note_message(message)
    when MIDIMessage::ControlChange then cc_message(message)
    end
  end
end
note_message(message) click to toggle source

Find and call a note received callback if it exists @param [MIDIMessage] message @return [Boolean, nil]

# File lib/mvlc/midi/message_handler.rb, line 52
def note_message(message)
  call_callback(:note, message.note, message.velocity) |
  call_catch_all_callback(:note, message)
end
process(channel, message) click to toggle source

Process a message for the given channel @param [Integer, nil] channel @param [MIDIMessage] message @return [Boolean, nil]

# File lib/mvlc/midi/message_handler.rb, line 41
def process(channel, message)
  case message
  when MIDIMessage::SystemCommon, MIDIMessage::SystemExclusive, MIDIMessage::SystemRealtime then system_message(message)
  else
    channel_message(channel, message)
  end
end
system_message(message) click to toggle source

Find and call a system message callback if it exists @param [MIDIMessage] message @return [Boolean, nil]

# File lib/mvlc/midi/message_handler.rb, line 68
def system_message(message)
  name = message.name.downcase.to_sym
  call_callback(:system, name)
end

Private Instance Methods

call_callback(type, key, *arguments) click to toggle source

Execute the callback for the given type and key and pass it the given args @param [Symbol] type @param [Object] key @param [*Object] arguments @return [Boolean]

# File lib/mvlc/midi/message_handler.rb, line 101
def call_callback(type, key, *arguments)
  unless (callback = @callback[type][key]).nil?
    callback.call(*arguments)
    true
  end
end
call_catch_all_callback(type, message) click to toggle source

Execute the catch-all callback for the given type if it exists @param [Symbol] type @param [MIDIMessage] message @return [Boolean]

# File lib/mvlc/midi/message_handler.rb, line 92
def call_catch_all_callback(type, message)
  call_callback(type, nil, message)
end