class MVLC::MIDI::Wrapper

Wrapper for MIDI functionality

Attributes

channel[R]
listener[R]
message_handler[R]

Public Class Methods

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

@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/mvlc/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_cc_callback(index, &callback) click to toggle source

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/mvlc/midi/wrapper.rb, line 42
def add_cc_callback(index, &callback)
  @message_handler.add_callback(:cc, index, &callback)
end
add_note_callback(note, &callback) click to toggle source

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/mvlc/midi/wrapper.rb, line 34
def add_note_callback(note, &callback)
  @message_handler.add_note_callback(note, &callback)
end
add_system_callback(command, &callback) click to toggle source

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/mvlc/midi/wrapper.rb, line 26
def add_system_callback(command, &callback)
  @message_handler.add_callback(:system, command, &callback)
end
channel=(channel) click to toggle source

Change the subscribed MIDI channel (or nil for all) @param [Integer, nil] channel @return [Integer, nil]

# File lib/mvlc/midi/wrapper.rb, line 55
def channel=(channel)
  @listener.event.clear
  @channel = channel
  initialize_listener if @listener.running?
  @channel
end
omni?() click to toggle source

Whether the player is subscribed to all channels @return [Boolean]

# File lib/mvlc/midi/wrapper.rb, line 73
def omni?
  @channel.nil?
end
start() click to toggle source

Start the MIDI listener @return [Boolean]

# File lib/mvlc/midi/wrapper.rb, line 64
def start
  initialize_listener
  @start_time = Time.now.to_i
  @listener.start(:background => true)
  true
end
stop() click to toggle source

Stop the MIDI listener @return [Boolean]

# File lib/mvlc/midi/wrapper.rb, line 48
def stop
  @listener.stop
end

Private Instance Methods

handle_new_event(event) click to toggle source

Handle a new MIDI event received @param [Hash] event @return [Hash]

# File lib/mvlc/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
initialize_listener() click to toggle source

Populate the MIDI listener callback @return [MIDIEye::Listener]

# File lib/mvlc/midi/wrapper.rb, line 107
def initialize_listener
  @listener.on_message { |event| handle_new_event(event) }
  @listener
end
now() click to toggle source

Elapsed time since start in seconds @return [Integer]

# File lib/mvlc/midi/wrapper.rb, line 81
def now
  Time.now.to_i - @start_time
end
process_event?(event) click to toggle source

Should the given MIDI event be processed or thrown away? @param [Hash] event @return [Boolean]

# File lib/mvlc/midi/wrapper.rb, line 88
def process_event?(event)
  @buffer_length.nil? ||
    event[:timestamp].nil? ||
    event[:timestamp].to_i >= now - @buffer_length
end