Class: MTK::Groups::Melody
- Inherits:
-
Object
- Object
- MTK::Groups::Melody
- Includes:
- PitchCollection
- Defined in:
- lib/mtk/groups/melody.rb
Overview
An ordered collection of Pitches.
The “horizontal” (sequential) pitch collection.
Unlike the strict definition of melody, this class is fairly abstract and only models a succession of pitches. To create a true, playable melody one must combine an MTK::Melody and rhythms into a Events::Timeline.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#pitches ⇒ Object
readonly
Returns the value of attribute pitches.
Class Method Summary collapse
- .from_a(enumerable) ⇒ Object
- .from_pitch_classes(pitch_classes, start = MTK::Lang::Pitches::C4, max_distance = 12) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#=~(other) ⇒ Object
Compare for equality, ignoring order and duplicates.
- #elements ⇒ Object
-
#initialize(pitches) ⇒ Melody
constructor
A new instance of Melody.
- #pitch_classes ⇒ Object
- #to_pitch_class_set(remove_duplicates = true) ⇒ Object
- #to_s ⇒ Object
Methods included from PitchCollection
Methods included from Collection
#[], #clone, #concat, #each, #empty?, #enumerable_map, #first, #last, #map, #partition, #permute, #repeat, #reverse, #rotate, #size, #to_a
Constructor Details
#initialize(pitches) ⇒ Melody
Returns a new instance of Melody.
21 22 23 |
# File 'lib/mtk/groups/melody.rb', line 21 def initialize(pitches) @pitches = pitches.to_a.clone.freeze end |
Instance Attribute Details
#pitches ⇒ Object (readonly)
Returns the value of attribute pitches.
16 17 18 |
# File 'lib/mtk/groups/melody.rb', line 16 def pitches @pitches end |
Class Method Details
.from_a(enumerable) ⇒ Object
46 47 48 |
# File 'lib/mtk/groups/melody.rb', line 46 def self.from_a enumerable new enumerable end |
.from_pitch_classes(pitch_classes, start = MTK::Lang::Pitches::C4, max_distance = 12) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mtk/groups/melody.rb', line 25 def self.from_pitch_classes(pitch_classes, start=MTK::Lang::Pitches::C4, max_distance=12) pitch = start pitches = [] pitch_classes.each do |pitch_class| pitch = pitch.nearest(pitch_class) pitch -= 12 if pitch > start+max_distance # keep within max_distance of start (default is one octave) pitch += 12 if pitch < start-max_distance pitches << pitch end new pitches end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/mtk/groups/melody.rb', line 59 def == other if other.respond_to? :pitches @pitches == other.pitches elsif other.is_a? Enumerable @pitches == other.to_a else @pitches == other end end |
#=~(other) ⇒ Object
Compare for equality, ignoring order and duplicates
71 72 73 74 75 76 77 78 79 |
# File 'lib/mtk/groups/melody.rb', line 71 def =~ other @normalized_pitches ||= @pitches.uniq.sort @normalized_pitches == case when other.respond_to?(:pitches) then other.pitches.uniq.sort when (other.is_a? Array and other.frozen?) then other when other.respond_to?(:to_a) then other.to_a.uniq.sort else other end end |
#elements ⇒ Object
38 39 40 |
# File 'lib/mtk/groups/melody.rb', line 38 def elements @pitches end |
#pitch_classes ⇒ Object
54 55 56 |
# File 'lib/mtk/groups/melody.rb', line 54 def pitch_classes @pitch_classes ||= @pitches.map{|p| p.pitch_class } end |
#to_pitch_class_set(remove_duplicates = true) ⇒ Object
50 51 52 |
# File 'lib/mtk/groups/melody.rb', line 50 def to_pitch_class_set(remove_duplicates=true) PitchClassSet.new(remove_duplicates ? pitch_classes.uniq : pitch_classes) end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/mtk/groups/melody.rb', line 81 def to_s '[' + @pitches.map{|pitch| pitch.to_s}.join(', ') + ']' end |