Class: MTK::Events::Rest

Inherits:
Event
  • Object
show all
Defined in:
lib/mtk/events/rest.rb

Overview

Note:

Because a negative durations indicate rests, other Event classes may represent rests too. Therefore, you should always determine if an Event is a rest via the Event#rest? method, instead of seeing if the class is an MTK::Events::Rest

An interval of silence.

By convention, MTK uses Core::Durations with negative values for rests. This class forces the Event#duration to always have a negative value.

Instance Attribute Summary

Attributes inherited from Event

#channel, #duration, #number, #type, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Event

#==, #duration_in_pulses, #instantaneous?, #length, #rest?, #to_h

Constructor Details

#initialize(duration, channel = nil) ⇒ Rest

Returns a new instance of Rest.



15
16
17
18
# File 'lib/mtk/events/rest.rb', line 15

def initialize(duration, channel=nil)
  super :rest, duration:duration, channel:channel
  self.duration = @duration # force to be a rest
end

Class Method Details

.from_h(hash) ⇒ Object



20
21
22
# File 'lib/mtk/events/rest.rb', line 20

def self.from_h(hash)
  new(hash[:duration], hash[:channel])
end

Instance Method Details

#duration=(duration) ⇒ Object

Assign the duration, forcing to a negative value to indicate this is a rest.



25
26
27
28
# File 'lib/mtk/events/rest.rb', line 25

def duration= duration
  super
  @duration = -@duration unless @duration.rest? # force to be a rest
end

#inspectObject



44
45
46
47
# File 'lib/mtk/events/rest.rb', line 44

def inspect
  "#<#{self.class}:#{object_id} @duration=#{@duration.inspect}" +
      if @channel then ", @channel=#{@channel}>" else '>' end
end

#midi_valueObject

Rests don’t have a corresponding value in MIDI, so this is nil

Returns:

  • nil



32
33
34
# File 'lib/mtk/events/rest.rb', line 32

def midi_value
  nil
end

#midi_value=(value) ⇒ Object

Rests don’t have a corresponding value in MIDI, so this is a no-op



37
38
# File 'lib/mtk/events/rest.rb', line 37

def midi_value= value
end

#to_sObject



40
41
42
# File 'lib/mtk/events/rest.rb', line 40

def to_s
  "Rest(#{@duration})"
end