Class: Webmidi::Port::Input

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/webmidi/port/input.rb

Instance Attribute Summary

Attributes inherited from Base

#id, #manufacturer, #name, #type, #version

Instance Method Summary collapse

Methods inherited from Base

#close, #connected?, #connection, #disconnect, #on_state_change, #open, #open?, #state, #sysex_enabled?

Constructor Details

#initialize(**kwargs) ⇒ Input

Returns a new instance of Input.



8
9
10
11
12
13
14
# File 'lib/webmidi/port/input.rb', line 8

def initialize(**kwargs)
  @error_policy = kwargs.delete(:error_policy) || :notify
  super(**kwargs, type: :input)
  @message_callbacks = []
  @typed_callbacks = []
  @error_callbacks = []
end

Instance Method Details

#dispatch(bytes) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/webmidi/port/input.rb', line 100

def dispatch(bytes)
  return unless open?

  Message.parse_many(bytes).each { |msg| dispatch_message(msg) }
rescue InvalidMessageError => e
  handle_parse_error(e, bytes)
end

#each(timeout: 0.1, stop_when: nil, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/webmidi/port/input.rb', line 68

def each(timeout: 0.1, stop_when: nil, &block)
  return enum_for(:each, timeout: timeout, stop_when: stop_when) unless block_given?

  open unless open?
  while open?
    break if stop_when&.call

    bytes = @transport_handle.read(timeout: timeout)
    next unless bytes

    begin
      Message.parse_many(bytes).each do |msg|
        next if masked_sysex?(msg)

        block.call(msg)
      end
    rescue InvalidMessageError => e
      handle_parse_error(e, bytes)
    end
  end
end

#messages(**kwargs) ⇒ Object



90
91
92
# File 'lib/webmidi/port/input.rb', line 90

def messages(**kwargs)
  each(**kwargs).lazy
end

#on_clock(&block) ⇒ Object



50
51
52
# File 'lib/webmidi/port/input.rb', line 50

def on_clock(&block)
  register_typed_callback(Message::System::Clock, &block)
end

#on_control_change(&block) ⇒ Object



34
35
36
# File 'lib/webmidi/port/input.rb', line 34

def on_control_change(&block)
  register_typed_callback(Message::Channel::ControlChange, &block)
end

#on_error(&block) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
# File 'lib/webmidi/port/input.rb', line 58

def on_error(&block)
  raise ArgumentError, "on_error requires a block" unless block

  open unless open?
  @mutex.synchronize { @error_callbacks << block }
  CallbackSubscription.new do
    @mutex.synchronize { @error_callbacks.delete(block) }
  end
end

#on_message(&block) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
# File 'lib/webmidi/port/input.rb', line 16

def on_message(&block)
  raise ArgumentError, "on_message requires a block" unless block

  open unless open?
  @mutex.synchronize { @message_callbacks << block }
  CallbackSubscription.new do
    @mutex.synchronize { @message_callbacks.delete(block) }
  end
end

#on_note_off(&block) ⇒ Object



30
31
32
# File 'lib/webmidi/port/input.rb', line 30

def on_note_off(&block)
  register_typed_callback(Message::Channel::NoteOff, &block)
end

#on_note_on(&block) ⇒ Object



26
27
28
# File 'lib/webmidi/port/input.rb', line 26

def on_note_on(&block)
  register_typed_callback(Message::Channel::NoteOn, &block)
end

#on_pitch_bend(&block) ⇒ Object



42
43
44
# File 'lib/webmidi/port/input.rb', line 42

def on_pitch_bend(&block)
  register_typed_callback(Message::Channel::PitchBend, &block)
end

#on_program_change(&block) ⇒ Object



38
39
40
# File 'lib/webmidi/port/input.rb', line 38

def on_program_change(&block)
  register_typed_callback(Message::Channel::ProgramChange, &block)
end

#on_sysex(&block) ⇒ Object



46
47
48
# File 'lib/webmidi/port/input.rb', line 46

def on_sysex(&block)
  register_typed_callback(Message::System::SysEx, &block)
end

#on_type(matcher, &block) ⇒ Object



54
55
56
# File 'lib/webmidi/port/input.rb', line 54

def on_type(matcher, &block)
  register_typed_callback(matcher, &block)
end

#pipe(stack = nil) ⇒ Object



94
95
96
97
98
# File 'lib/webmidi/port/input.rb', line 94

def pipe(stack = nil)
  require_relative "../middleware/pipeline"

  Middleware::Pipeline.new(self, stack)
end