Class: MMPlayer::MIDI::MessageHandler
- Inherits:
-
Object
- Object
- MMPlayer::MIDI::MessageHandler
- Defined in:
- lib/mmplayer/midi/message_handler.rb
Overview
Directs what should happen when messages are received
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
Instance Method Summary collapse
-
#add_callback(type, key, &callback) ⇒ Hash
Add a callback for a given MIDI message type.
-
#add_note_callback(note, &callback) ⇒ Hash
Add a callback for a given MIDI note.
-
#cc_message(message) ⇒ Boolean?
Find and call a cc received callback if it exists.
-
#channel_message(channel, message) ⇒ Boolean?
Find and call a channel message callback if it exists for the given message and channel.
-
#initialize ⇒ MessageHandler
constructor
A new instance of MessageHandler.
-
#note_message(message) ⇒ Boolean?
Find and call a note received callback if it exists.
-
#process(channel, message) ⇒ Boolean?
Process a message for the given channel.
-
#system_message(message) ⇒ Boolean?
Find and call a system message callback if it exists.
Constructor Details
#initialize ⇒ MessageHandler
Returns a new instance of MessageHandler.
9 10 11 12 13 14 15 |
# File 'lib/mmplayer/midi/message_handler.rb', line 9 def initialize @callback = { :cc => {}, :note => {}, :system => {} } end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
7 8 9 |
# File 'lib/mmplayer/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
22 23 24 25 |
# File 'lib/mmplayer/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
32 33 34 35 |
# File 'lib/mmplayer/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
60 61 62 63 |
# File 'lib/mmplayer/midi/message_handler.rb', line 60 def () call_callback(:cc, .index, .value) | call_catch_all_callback(:cc, ) end |
#channel_message(channel, message) ⇒ Boolean?
Find and call a channel message callback if it exists for the given message and channel
77 78 79 80 81 82 83 84 |
# File 'lib/mmplayer/midi/message_handler.rb', line 77 def (channel, ) if channel.nil? || .channel == channel case when MIDIMessage::NoteOn then () when MIDIMessage::ControlChange then () end end end |
#note_message(message) ⇒ Boolean?
Find and call a note received callback if it exists
52 53 54 55 |
# File 'lib/mmplayer/midi/message_handler.rb', line 52 def () call_callback(:note, .note, .velocity) | call_catch_all_callback(:note, ) end |
#process(channel, message) ⇒ Boolean?
Process a message for the given channel
41 42 43 44 45 46 47 |
# File 'lib/mmplayer/midi/message_handler.rb', line 41 def process(channel, ) case when MIDIMessage::SystemCommon, MIDIMessage::SystemRealtime then () else (channel, ) end end |
#system_message(message) ⇒ Boolean?
Find and call a system message callback if it exists
68 69 70 71 |
# File 'lib/mmplayer/midi/message_handler.rb', line 68 def () name = .name.downcase.to_sym call_callback(:system, name) end |