class MMPlayer::MIDI::Wrapper
Attributes
Public Class Methods
@param [UniMIDI::Input, Array<UniMIDI::Input>] input @param [Hash] options @option options [Integer] :buffer_length Length of MIDI
message buffer in seconds @option options [Integer] :receive_channel A MIDI
channel to subscribe to. By default, responds to all
# File lib/mmplayer/midi/wrapper.rb, line 14 def initialize(input, options = {}) @buffer_length = options[:buffer_length] @channel = options[:receive_channel] @message_handler = MessageHandler.new @listener = MIDIEye::Listener.new(input) end
Public Instance Methods
Add a callback for a given MIDI
control change @param [Integer, nil] index The MIDI
control change index to add a callback for eg 10 @param [Proc] callback The callback to execute when the given MIDI
control change is received @return [Hash]
# File lib/mmplayer/midi/wrapper.rb, line 42 def add_cc_callback(index, &callback) @message_handler.add_callback(:cc, index, &callback) end
Add a callback for a given MIDI
note @param [Integer, String, nil] note The MIDI
note to add a callback for eg 64 “E4” @param [Proc] callback The callback to execute when the given MIDI
note is received @return [Hash]
# File lib/mmplayer/midi/wrapper.rb, line 34 def add_note_callback(note, &callback) @message_handler.add_note_callback(note, &callback) end
Add a callback for a given MIDI
system message @param [String, Symbol] command The MIDI
system command eg :start, :stop @param [Proc] callback The callback to execute when the given MIDI
command is received @return [Hash]
# File lib/mmplayer/midi/wrapper.rb, line 26 def add_system_callback(command, &callback) @message_handler.add_callback(:system, command, &callback) end
Change the subscribed MIDI
channel (or nil for all) @param [Integer, nil] channel @return [Integer, nil]
# File lib/mmplayer/midi/wrapper.rb, line 55 def channel=(channel) @listener.event.clear @channel = channel initialize_listener if @listener.running? @channel end
Whether the player is subscribed to all channels @return [Boolean]
# File lib/mmplayer/midi/wrapper.rb, line 73 def omni? @channel.nil? end
Start the MIDI
listener @return [Boolean]
# File lib/mmplayer/midi/wrapper.rb, line 64 def start initialize_listener @start_time = Time.now.to_i @listener.start(:background => true) true end
Stop the MIDI
listener @return [Boolean]
# File lib/mmplayer/midi/wrapper.rb, line 48 def stop @listener.stop end
Private Instance Methods
Handle a new MIDI
event received @param [Hash] event @return [Hash]
# File lib/mmplayer/midi/wrapper.rb, line 97 def handle_new_event(event) if process_event?(event) message = event[:message] @message_handler.process(@channel, message) event end end
Populate the MIDI
listener callback @return [MIDIEye::Listener]
# File lib/mmplayer/midi/wrapper.rb, line 107 def initialize_listener @listener.on_message { |event| handle_new_event(event) } @listener end
Elapsed time since start in seconds @return [Integer]
# File lib/mmplayer/midi/wrapper.rb, line 81 def now Time.now.to_i - @start_time end
Should the given MIDI
event be processed or thrown away? @param [Hash] event @return [Boolean]
# File lib/mmplayer/midi/wrapper.rb, line 88 def process_event?(event) @buffer_length.nil? || event[:timestamp].nil? || event[:timestamp].to_i >= now - @buffer_length end