Class: Webmidi::Message::System::SysEx

Inherits:
Base
  • Object
show all
Defined in:
lib/webmidi/message/system.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#timestamp

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #channel, #deconstruct, #eql?, #hash, #same_bytes?, #same_event?, #to_binary, #to_hex, #with

Constructor Details

#initialize(data:, timestamp: nil) ⇒ SysEx

Returns a new instance of SysEx.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/webmidi/message/system.rb', line 12

def initialize(data:, timestamp: nil)
  unless data.respond_to?(:each)
    raise InvalidMessageError, "SysEx data must be enumerable, got #{data.class}"
  end

  data.each_with_index do |byte, i|
    unless byte.is_a?(Integer) && byte.between?(0, 127)
      raise InvalidMessageError, "SysEx data byte at index #{i} must be between 0 and 127, got #{byte.inspect}"
    end
  end
  @data = data.dup.freeze
  super(timestamp: timestamp)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/webmidi/message/system.rb', line 10

def data
  @data
end

Class Method Details

.join(messages) ⇒ Object



47
48
49
50
51
52
# File 'lib/webmidi/message/system.rb', line 47

def self.join(messages)
  data = messages.flat_map do |message|
    message.is_a?(self) ? message.data : message
  end
  new(data: data)
end

.split(data, max_data_bytes:) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/webmidi/message/system.rb', line 38

def self.split(data, max_data_bytes:)
  unless max_data_bytes.is_a?(Integer) && max_data_bytes.positive?
    raise InvalidMessageError, "max_data_bytes must be a positive integer, got #{max_data_bytes.inspect}"
  end

  bytes = data.is_a?(self) ? data.data : data
  bytes.each_slice(max_data_bytes).map { |slice| new(data: slice) }
end

Instance Method Details

#chunks(max_data_bytes:) ⇒ Object



34
35
36
# File 'lib/webmidi/message/system.rb', line 34

def chunks(max_data_bytes:)
  self.class.split(self, max_data_bytes: max_data_bytes)
end

#deconstruct_keys(keys) ⇒ Object



30
31
32
# File 'lib/webmidi/message/system.rb', line 30

def deconstruct_keys(keys)
  {data: @data}
end

#to_bytesObject



26
27
28
# File 'lib/webmidi/message/system.rb', line 26

def to_bytes
  [0xF0, *@data, 0xF7]
end