class MicroMIDI::Instructions::Input
Commands dealing with MIDI
input
Public Class Methods
@param [State] state @param [Proc] thru_action Output
module to send thru messages to
# File lib/micromidi/instructions/input.rb, line 10 def initialize(state, &thru_action) @state = state @thru_action = thru_action end
Public Instance Methods
Join the listener thread @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 97 def join loop { wait_for_input } true end
Bind an event that will be fired when a message is received @param [*Object] args Types of messages to filter on eg :note_on, :control_change @param [Proc] callback The event callback @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 19 def receive(*args, &callback) args = args.dup options = args.last.kind_of?(Hash) ? args.pop : {} unless args.empty? match = { :class => message_classes(args) } end listener(match, options) do |event| yield(event[:message], event[:timestamp]) end true end
Bind an event that will be fired when a message is received @param [*Object] args Types of messages to filter out eg :note_on, :control_change @param [Proc] callback The event callback @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 40 def receive_unless(*args, &callback) args = args.dup options = args.last.kind_of?(Hash) ? args.pop : {} match = message_classes(args) listener(nil, options) do |event| yield(event[:message], event[:timestamp]) unless match.include?(event[:message].class) end end
Send input messages thru to the outputs
# File lib/micromidi/instructions/input.rb, line 54 def thru thru_if end
Similar to Input#thru_unless
except a callback can be passed that will be fired when notes specified arrive @param [*Object] args @param [Proc] callback @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 79 def thru_except(*args, &callback) thru_unless(*args) receive(*args, &callback) end
Send input messages thru to the outputs if they have a specified class @param [*Object] args @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 61 def thru_if(*args) receive_options = thru_arguments(args) receive(*receive_options) { |message, timestamp| @thru_action.call(message) } true end
Send input messages thru to the outputs unless they’re of the specified class @param [*Object] args @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 70 def thru_unless(*args) receive_options = thru_arguments(args) receive_unless(*receive_options) { |message, timestamp| @thru_action.call(message) } end
Wait for input on the last input passed in (blocking) Can pass the option :from => [an input] to specify which one to wait on @param [Hash] options @option options [UniMIDI::Input] :from @return [Boolean]
# File lib/micromidi/instructions/input.rb, line 89 def wait_for_input(options = {}) listener = options[:from] || @state.listeners.last || @state.thru_listeners.last listener.join true end
Protected Instance Methods
Initialize input event listeners for the given inputs and options @param [Hash] match @param [Hash] options option options [Array<UniMIDI::Input>, UniMIDI::Input] :from option options [Boolean] :start option options [Boolean] :thru
# File lib/micromidi/instructions/input.rb, line 110 def listener(match, options = {}, &block) inputs = options[:from] || @state.inputs inputs = [inputs].flatten do_thru = options.fetch(:thru, false) should_start = options.fetch(:start, true) match ||= {} listeners = inputs.map { |input| initialize_listener(input, match, do_thru, &block) } if should_start listeners.each { |listener| listener.start(:background => true) } end end
Private Instance Methods
Initialize an input event listener @param [UniMIDI::Input] input @param [Hash] match @param [Boolean] do_thru @param [Proc] callback @return [MIDIEye::Listener]
# File lib/micromidi/instructions/input.rb, line 131 def initialize_listener(input, match, do_thru, &callback) listener = MIDIEye::Listener.new(input) listener.listen_for(match, &callback) if do_thru @state.thru_listeners.each(&:stop) @state.thru_listeners.clear @state.thru_listeners << listener else @state.listeners << listener end listener end
Get the MIDIMessage class for the given note name @param [Array<String, Symbol>] type_list @return [Array<Class>]
# File lib/micromidi/instructions/input.rb, line 160 def message_classes(type_list) classes = type_list.map do |type| case type.to_sym when :aftertouch, :pressure, :aft then [MIDIMessage::ChannelAftertouch, MIDIMessage::PolyphonicAftertouch] when :channel_aftertouch, :channel_pressure, :ca, :cp then MIDIMessage::ChannelAftertouch when :control_change, :cc, :c then MIDIMessage::ControlChange when :note, :n then [MIDIMessage::NoteOn, MIDIMessage::NoteOff] when :note_on, :nn then MIDIMessage::NoteOn when :note_off, :no, :off then MIDIMessage::NoteOff when :pitch_bend, :pb then MIDIMessage::PitchBend when :polyphonic_aftertouch, :poly_aftertouch, :poly_pressure, :polyphonic_pressure, :pa, :pp then MIDIMessage::PolyphonicAftertouch when :program_change, :pc, :p then MIDIMessage::ProgramChange end end classes.flatten.compact end
The arguments for using thru @param [Array<Object>] args @return [Array<Object, Hash>]
# File lib/micromidi/instructions/input.rb, line 147 def thru_arguments(args) receive_options = args.dup if receive_options.last.kind_of?(Hash) receive_options.last[:thru] = true else receive_options << { :thru => true } end receive_options end