Class: Webmidi::Middleware::Panic

Inherits:
Base
  • Object
show all
Defined in:
lib/webmidi/middleware/panic.rb

Constant Summary collapse

DEFAULT_CONTROLS =
[:all_sound_off, :all_notes_off].freeze
DEFAULT_TRIGGER =
Message::System::SystemReset

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, channels: 0..15, controls: DEFAULT_CONTROLS, trigger: DEFAULT_TRIGGER, pass_trigger: false, **options) ⇒ Panic

Returns a new instance of Panic.



9
10
11
12
13
14
15
16
# File 'lib/webmidi/middleware/panic.rb', line 9

def initialize(app, channels: 0..15, controls: DEFAULT_CONTROLS, trigger: DEFAULT_TRIGGER,
  pass_trigger: false, **options)
  super(app, **options)
  @channels = self.class.send(:normalize_channels, channels)
  @controls = self.class.send(:normalize_controls, controls)
  @trigger = trigger
  @pass_trigger = pass_trigger
end

Class Method Details

.all_notes_off(channels: 0..15, timestamp: nil) ⇒ Object



27
28
29
# File 'lib/webmidi/middleware/panic.rb', line 27

def self.all_notes_off(channels: 0..15, timestamp: nil)
  messages(channels: channels, controls: [:all_notes_off], timestamp: timestamp)
end

.messages(channels: 0..15, controls: DEFAULT_CONTROLS, timestamp: nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/webmidi/middleware/panic.rb', line 31

def self.messages(channels: 0..15, controls: DEFAULT_CONTROLS, timestamp: nil)
  normalize_channels(channels).flat_map do |channel|
    normalize_controls(controls).map do |control|
      Message.control_change(control, 0, channel: channel, timestamp: timestamp)
    end
  end
end

Instance Method Details

#call(message) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/webmidi/middleware/panic.rb', line 18

def call(message)
  return @app.call(message) unless trigger?(message)

  results = self.class.messages(channels: @channels, controls: @controls, timestamp: message.timestamp)
    .filter_map { |panic_message| @app.call(panic_message) }
  results << @app.call(message) if @pass_trigger
  results.compact
end