Class: MTK::Events::Event Abstract

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

Overview

This class is abstract.

An abstract musical event

Direct Known Subclasses

Note, Parameter, Rest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options = {}) ⇒ Event

Returns a new instance of Event.



40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

#channelObject

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



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

def channel
  @channel
end

#durationObject

Duration of the Event in beats (e.g. 1.0 is a quarter note in 4/4 time signatures)



28
29
30
# File 'lib/mtk/events/event.rb', line 28

def duration
  @duration
end

#numberObject

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.



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

def number
  @number
end

#typeObject (readonly)

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



10
11
12
# File 'lib/mtk/events/event.rb', line 10

def type
  @type
end

#valueObject

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.



21
22
23
# File 'lib/mtk/events/event.rb', line 21

def value
  @value
end

Class Method Details

.from_h(hash) ⇒ Object



49
50
51
# File 'lib/mtk/events/event.rb', line 49

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

Instance Method Details

#==(other) ⇒ Object



101
102
103
104
105
106
107
# 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_in_pulses(pulses_per_beat) ⇒ Object

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



97
98
99
# File 'lib/mtk/events/event.rb', line 97

def duration_in_pulses(pulses_per_beat)
  (length.to_f * pulses_per_beat).round
end

#inspectObject



113
114
115
# File 'lib/mtk/events/event.rb', line 113

def inspect
  "Event(#@type" + (@number ? "[#@number]" : '') + ", #@value, #{@duration.to_f})"
end

#instantaneous?Boolean

By convention, any events with 0 duration are instantaneous

Returns:

  • (Boolean)


92
93
94
# File 'lib/mtk/events/event.rb', line 92

def instantaneous?
  @duration.nil? or @duration == 0
end

#lengthObject

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

See Also:



81
82
83
# File 'lib/mtk/events/event.rb', line 81

def length
  @duration.length
end

#midi_valueObject



62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
# File 'lib/mtk/events/event.rb', line 74

def midi_value= value
  @value = value/127.0
end

#rest?Boolean

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/mtk/events/event.rb', line 87

def rest?
  @duration.rest?
end

#to_hObject



53
54
55
56
57
58
59
60
# 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_sObject



109
110
111
# File 'lib/mtk/events/event.rb', line 109

def to_s
  "Event(#@type" + (@number ? "[#@number]" : '') + ", #{sprintf '%.2f',@value}, #{sprintf '%.2f',@duration})"
end