Class: Webmidi::Message::Base

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

Direct Known Subclasses

Channel::Base, System::Base, UMP::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp: nil) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/webmidi/message/base.rb', line 8

def initialize(timestamp: nil)
  @timestamp = timestamp || Process.clock_gettime(Process::CLOCK_MONOTONIC)
  freeze
end

Instance Attribute Details

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/webmidi/message/base.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
# File 'lib/webmidi/message/base.rb', line 29

def ==(other)
  other.is_a?(self.class) && same_bytes?(other)
end

#channelObject



25
26
27
# File 'lib/webmidi/message/base.rb', line 25

def channel
  nil
end

#deconstructObject



55
56
57
# File 'lib/webmidi/message/base.rb', line 55

def deconstruct
  to_bytes
end

#deconstruct_keys(keys) ⇒ Object



59
60
61
# File 'lib/webmidi/message/base.rb', line 59

def deconstruct_keys(keys)
  {}
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/webmidi/message/base.rb', line 33

def eql?(other)
  self == other
end

#hashObject



37
38
39
# File 'lib/webmidi/message/base.rb', line 37

def hash
  [self.class, to_bytes].hash
end

#same_bytes?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/webmidi/message/base.rb', line 41

def same_bytes?(other)
  other.respond_to?(:to_bytes) && to_bytes == other.to_bytes
end

#same_event?(other) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/webmidi/message/base.rb', line 45

def same_event?(other)
  other.is_a?(self.class) && same_bytes?(other) && timestamp == other.timestamp
end

#to_binaryObject



21
22
23
# File 'lib/webmidi/message/base.rb', line 21

def to_binary
  to_bytes.pack("C*").b
end

#to_bytesObject

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/webmidi/message/base.rb', line 13

def to_bytes
  raise NotImplementedError, "#{self.class}#to_bytes must be implemented"
end

#to_hexObject



17
18
19
# File 'lib/webmidi/message/base.rb', line 17

def to_hex
  to_bytes.map { |b| format("%02X", b) }.join(" ")
end

#with(**changes) ⇒ Object



49
50
51
52
53
# File 'lib/webmidi/message/base.rb', line 49

def with(**changes)
  changes = changes.dup
  next_timestamp = changes.key?(:timestamp) ? changes.delete(:timestamp) : @timestamp
  self.class.new(**deconstruct_keys(nil).merge(changes), timestamp: next_timestamp)
end