class MTK::Groups::Melody
An ordered collection of {Pitch}es.
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}.
@see Chord
Attributes
pitches[R]
Public Class Methods
from_a(enumerable)
click to toggle source
# 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)
click to toggle source
# 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
new(pitches)
click to toggle source
@param pitches [#to_a] the collection of pitches @see MTK#Melody
# File lib/mtk/groups/melody.rb, line 21 def initialize(pitches) @pitches = pitches.to_a.clone.freeze end
Public Instance Methods
==(other)
click to toggle source
@param other [#pitches, Enumerable]
# 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)
click to toggle source
Compare for equality, ignoring order and duplicates @param other [#pitches, Array, to_a]
# 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()
click to toggle source
@see Helper::Collection
# File lib/mtk/groups/melody.rb, line 38 def elements @pitches end
pitch_classes()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/mtk/groups/melody.rb, line 81 def to_s '[' + @pitches.map{|pitch| pitch.to_s}.join(', ') + ']' end