class MTK::Events::Event

An abstract musical event @abstract

Attributes

channel[RW]

The channel of the event, for multi-tracked events.

duration[R]

Duration of the Event in beats (e.g. 1.0 is a quarter note in 4/4 time signatures) @see length @see rest? @see instantaneous? @see duration_in_pulses

number[RW]

The specific element effected by this type of event, when applicable. Depends on the event type. For example, the number of a :note type Event is the pitch, and the number of a :control type Event is the controller (CC) number. This value is nil for inapplicable event types.

type[R]

The type of event: :note, :control, :pressure, :bend, or :program

value[RW]

The value of event. Depends on event type. For example, the value of a :note type Event is the intensity, and the value of a :control type Event is the controller (CC) value.

Public Class Methods

from_h(hash) click to toggle source
# File lib/mtk/events/event.rb, line 49
def self.from_h(hash)
  new(hash[:type], hash)
end
new(type, options={}) click to toggle source
# File lib/mtk/events/event.rb, line 40
def initialize(type, options={})
  @type = type
  @value = options[:value]
  @number = options[:number]
  @duration = options.fetch(:duration, 0)
  @duration = ::MTK::Core::Duration[@duration] unless @duration.is_a? ::MTK::Core::Duration
  @channel = options[:channel]
end

Public Instance Methods

==(other) click to toggle source
# File lib/mtk/events/event.rb, line 101
def == other
  other.respond_to? :type and @type == other.type and
  other.respond_to? :number and @number == other.number and
  other.respond_to? :value and @value == other.value and
  other.respond_to? :duration and @duration == other.duration and
  other.respond_to? :channel and @channel == other.channel
end
duration=(duration) click to toggle source
# File lib/mtk/events/event.rb, line 30
def duration= duration
  @duration = duration
  @duration = ::MTK::Core::Duration[@duration || 0] unless @duration.is_a? ::MTK::Core::Duration
  @duration
end
duration_in_pulses(pulses_per_beat) click to toggle source

Convert duration to an integer number of MIDI pulses, given the pulses_per_beat

# File lib/mtk/events/event.rb, line 97
def duration_in_pulses(pulses_per_beat)
  (length.to_f * pulses_per_beat).round
end
inspect() click to toggle source
# File lib/mtk/events/event.rb, line 113
def inspect
  "Event(#@type" + (@number ? "[#@number]" : '') + ", #@value, #{@duration.to_f})"
end
instantaneous?() click to toggle source

By convention, any events with 0 duration are instantaneous

# File lib/mtk/events/event.rb, line 92
def instantaneous?
  @duration.nil? or @duration == 0
end
length() click to toggle source

The magnitude (absolute value) of the duration. Indicate the “real” duration for rests. @see rest?

# File lib/mtk/events/event.rb, line 81
def length
  @duration.length
end
midi_value() click to toggle source
# File lib/mtk/events/event.rb, line 62
def midi_value
  if @value and @value.respond_to? :to_midi
    @value.to_midi
  else
    value = @value
    midi_value = (127 * (value || 0)).round
    midi_value = 0 if midi_value < 0
    midi_value = 127 if midi_value > 127
    midi_value
  end
end
midi_value=(value) click to toggle source
# File lib/mtk/events/event.rb, line 74
def midi_value= value
  @value = value/127.0
end
rest?() click to toggle source

True if this event represents a rest, false otherwise. By convention, any events with negative durations are a rest

# File lib/mtk/events/event.rb, line 87
def rest?
  @duration.rest?
end
to_h() click to toggle source
# File lib/mtk/events/event.rb, line 53
def to_h
  hash = {type: @type}
  hash[:value] = @value unless @value.nil?
  hash[:duration] = @duration unless @duration.nil?
  hash[:number] = @number unless @number.nil?
  hash[:channel] = @channel unless @channel.nil?
  hash
end
to_s() click to toggle source
# File lib/mtk/events/event.rb, line 109
def to_s
  "Event(#@type" + (@number ? "[#@number]" : '') + ", #{sprintf '%.2f',@value}, #{sprintf '%.2f',@duration})"
end