Class: MTK::Events::Note

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

Overview

A musical Event defined by a Core::Pitch, Core::Intensity, and Core::Duration

Constant Summary collapse

DEFAULT_DURATION =
MTK::Core::Duration[1]
DEFAULT_INTENSITY =
MTK::Core::Intensity[0.75]

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, #midi_value, #midi_value=, #rest?

Constructor Details

#initialize(pitch, duration = DEFAULT_DURATION, intensity = DEFAULT_INTENSITY, channel = nil) ⇒ Note

Returns a new instance of Note.



23
24
25
# File 'lib/mtk/events/note.rb', line 23

def initialize(pitch, duration=DEFAULT_DURATION, intensity=DEFAULT_INTENSITY, channel=nil)
  super :note, number:pitch, duration:duration, value:intensity, channel:channel
end

Class Method Details

.from_h(hash) ⇒ Object



27
28
29
# File 'lib/mtk/events/note.rb', line 27

def self.from_h(hash)
  new(hash[:pitch]||hash[:number], hash[:duration], hash[:intensity]||hash[:value], hash[:channel])
end

.from_midi(pitch, velocity, duration_in_beats, channel = 0) ⇒ Object



35
36
37
# File 'lib/mtk/events/note.rb', line 35

def self.from_midi(pitch, velocity, duration_in_beats, channel=0)
  new( MTK::Lang::Pitches::PITCHES[pitch.to_i], MTK::Core::Duration[duration_in_beats], MTK::Core::Intensity[velocity/127.0], channel )
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
# File 'lib/mtk/events/note.rb', line 53

def ==(other)
  ( other.respond_to? :pitch and pitch == other.pitch and
    other.respond_to? :intensity and intensity == other.intensity and
    other.respond_to? :duration and duration == other.duration
  ) or super
end

#inspectObject



64
65
66
# File 'lib/mtk/events/note.rb', line 64

def inspect
  "#<#{self.class}:#{object_id} @pitch=#{@number.inspect}, @duration=#{@duration.inspect}, @intensity=#{@value.inspect}>"
end

#invert(around_pitch) ⇒ Object



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

def invert(around_pitch)
  self.pitch = self.pitch.invert(around_pitch)
  self
end

#midi_pitchObject



39
40
41
# File 'lib/mtk/events/note.rb', line 39

def midi_pitch
  pitch.to_i
end

#to_hObject



31
32
33
# File 'lib/mtk/events/note.rb', line 31

def to_h
  super.merge({ pitch: @number, intensity: @value })
end

#to_sObject



60
61
62
# File 'lib/mtk/events/note.rb', line 60

def to_s
  "Note(#{@number}, #{@duration}, #{@value.to_percent}%)"
end

#transpose(interval) ⇒ Object



43
44
45
46
# File 'lib/mtk/events/note.rb', line 43

def transpose(interval)
  self.pitch += interval
  self
end