Class: MTK::Groups::Chord

Inherits:
Melody
  • Object
show all
Defined in:
lib/mtk/groups/chord.rb

Overview

A sorted collection of distinct Pitches.

The “vertical” (simultaneous) pitch collection.

See Also:

Instance Attribute Summary

Attributes inherited from Melody

#pitches

Instance Method Summary collapse

Methods inherited from Melody

#==, #=~, #elements, from_a, from_pitch_classes, #pitch_classes, #to_pitch_class_set, #to_s

Methods included from PitchCollection

#invert, #transpose

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

Note:

duplicate pitches will be removed. See #Melody if you want to maintain duplicates.

Returns a new instance of Chord.

Parameters:

  • pitches (#to_a)

    the collection of pitches



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