Class: MicroMIDI::Instructions::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/micromidi/instructions/input.rb,
lib/micromidi/instructions/shorthand.rb

Overview

Commands dealing with MIDI input

Instance Method Summary collapse

Constructor Details

#initialize(state, &thru_action) ⇒ Input

Returns a new instance of Input.

Parameters:

  • state (State)
  • thru_action (Proc)

    Output module to send thru messages to



10
11
12
13
# File 'lib/micromidi/instructions/input.rb', line 10

def initialize(state, &thru_action)
  @state = state
  @thru_action = thru_action
end

Instance Method Details

#joinBoolean Also known as: j

Join the listener thread

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/micromidi/instructions/input.rb', line 97

def join
  loop { wait_for_input }
  true
end

#receive(*args, &callback) ⇒ Boolean Also known as: gets, handle, listen, listen_for, when_receive, rc

Bind an event that will be fired when a message is received

Parameters:

  • args (*Object)

    Types of messages to filter on eg :note_on, :control_change

  • callback (Proc)

    The event callback

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# 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

#receive_unless(*args, &callback) ⇒ Boolean Also known as: handle_unless, listen_unless, listen_for_unless, unless_receive, rcu

Bind an event that will be fired when a message is received

Parameters:

  • args (*Object)

    Types of messages to filter out eg :note_on, :control_change

  • callback (Proc)

    The event callback

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# 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

#thruObject Also known as: t

Send input messages thru to the outputs



54
55
56
# File 'lib/micromidi/instructions/input.rb', line 54

def thru
  thru_if
end

#thru_except(*args, &callback) ⇒ Boolean Also known as: te

Similar to Input#thru_unless except a callback can be passed that will be fired when notes specified arrive

Parameters:

  • args (*Object)
  • callback (Proc)

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/micromidi/instructions/input.rb', line 79

def thru_except(*args, &callback)
  thru_unless(*args)
  receive(*args, &callback)
end

#thru_if(*args) ⇒ Boolean

Send input messages thru to the outputs if they have a specified class

Parameters:

  • args (*Object)

Returns:

  • (Boolean)


61
62
63
64
65
# 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

#thru_unless(*args) ⇒ Boolean Also known as: tu

Send input messages thru to the outputs unless they’re of the specified class

Parameters:

  • args (*Object)

Returns:

  • (Boolean)


70
71
72
73
# 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(options = {}) ⇒ Boolean Also known as: w

Wait for input on the last input passed in (blocking) Can pass the option :from => [an input] to specify which one to wait on

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :from (UniMIDI::Input)

Returns:

  • (Boolean)


89
90
91
92
93
# 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