Class: MTK::Groups::Chord
Overview
A sorted collection of distinct Pitches.
The “vertical” (simultaneous) pitch collection.
Instance Attribute Summary
Attributes inherited from Melody
Instance Method Summary collapse
-
#initialize(pitches) ⇒ Chord
constructor
A new instance of Chord.
-
#inversion(number) ⇒ Object
Generate a chord inversion (positive numbers move the lowest notes up an octave, negative moves the highest notes down).
-
#nearest(pitch_class) ⇒ Object
Transpose the chord so that it’s lowest pitch is the given pitch class.
Methods inherited from Melody
#==, #=~, #elements, from_a, from_pitch_classes, #pitch_classes, #to_pitch_class_set, #to_s
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) ⇒ Chord
duplicate pitches will be removed. See #Melody if you want to maintain duplicates.
Returns a new instance of Chord.
16 17 18 19 20 21 |
# File 'lib/mtk/groups/chord.rb', line 16 def initialize(pitches) pitches = pitches.to_a.clone pitches.uniq! pitches.sort! @pitches = pitches.freeze end |
Instance Method Details
#inversion(number) ⇒ Object
Generate a chord inversion (positive numbers move the lowest notes up an octave, negative moves the highest notes down)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mtk/groups/chord.rb', line 24 def inversion(number) number = number.to_i pitch_set = Array.new(@pitches.uniq.sort) if number > 0 number.times do |count| index = count % pitch_set.length pitch_set[index] += 12 end else number.abs.times do |count| index = -(count + 1) % pitch_set.length # count from -1 downward to go backwards through the list starting at the end pitch_set[index] -= 12 end end self.class.new pitch_set.sort end |
#nearest(pitch_class) ⇒ Object
Transpose the chord so that it’s lowest pitch is the given pitch class.
42 43 44 |
# File 'lib/mtk/groups/chord.rb', line 42 def nearest(pitch_class) self.transpose @pitches.first.pitch_class.distance_to(pitch_class) end |