Class: MVLC::MIDI::MessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mvlc/midi/message_handler.rb

Overview

Directs what should happen when messages are received

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessageHandler

Returns a new instance of MessageHandler.



9
10
11
12
13
14
15
# File 'lib/mvlc/midi/message_handler.rb', line 9

def initialize
  @callback = {
    :cc => {},
    :note => {},
    :system => {}
  }
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



7
8
9
# File 'lib/mvlc/midi/message_handler.rb', line 7

def callback
  @callback
end

Instance Method Details

#add_callback(type, key, &callback) ⇒ Hash

Add a callback for a given MIDI message type

Parameters:

  • type (Symbol)

    The MIDI message type (eg :note, :cc)

  • key (Integer, String)

    The ID of the message eg note number/cc index

  • callback (Proc)

    The callback to execute when the given MIDI command is received

Returns:

  • (Hash)


22
23
24
25
# 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) ⇒ Hash

Add a callback for a given MIDI note

Parameters:

  • type (Symbol)

    The MIDI message type (eg :note, :cc)

  • note (Integer, String)
  • callback (Proc)

    The callback to execute when the given MIDI command is received

Returns:

  • (Hash)


32
33
34
35
# 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) ⇒ Boolean?

Find and call a cc received callback if it exists

Parameters:

  • message (MIDIMessage)

Returns:

  • (Boolean, nil)


60
61
62
63
# 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) ⇒ Boolean?

Find and call a channel message callback if it exists for the given message and channel

Parameters:

  • channel (Integer, nil)
  • message (MIDIMessage)

Returns:

  • (Boolean, nil)


77
78
79
80
81
82
83
84
# 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) ⇒ Boolean?

Find and call a note received callback if it exists

Parameters:

  • message (MIDIMessage)

Returns:

  • (Boolean, nil)


52
53
54
55
# 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) ⇒ Boolean?

Process a message for the given channel

Parameters:

  • channel (Integer, nil)
  • message (MIDIMessage)

Returns:

  • (Boolean, nil)


41
42
43
44
45
46
47
# 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) ⇒ Boolean?

Find and call a system message callback if it exists

Parameters:

  • message (MIDIMessage)

Returns:

  • (Boolean, nil)


68
69
70
71
# File 'lib/mvlc/midi/message_handler.rb', line 68

def system_message(message)
  name = message.name.downcase.to_sym
  call_callback(:system, name)
end