Class: Coltrane::Theory::Voicing

Inherits:
Object
  • Object
show all
Defined in:
lib/coltrane/theory/voicing.rb

Overview

This class describes an actual implementation of a Chord, being aware of exact octaves of each pitch and even repeating pitches across octaves.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*pitch_strings, pitches: nil) ⇒ Voicing

Returns a new instance of Voicing.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/coltrane/theory/voicing.rb', line 11

def initialize(*pitch_strings, pitches: nil)
  @pitches = begin
    if pitch_strings.any?
     pitch_strings.map { |s| Pitch[s] }
    elsif pitches
     pitches
    else
     raise WrongArgumentsError
    end
  end.sort
end

Instance Attribute Details

#pitchesObject (readonly)

Returns the value of attribute pitches.



9
10
11
# File 'lib/coltrane/theory/voicing.rb', line 9

def pitches
  @pitches
end

Class Method Details

.[](*args) ⇒ Object



23
24
25
# File 'lib/coltrane/theory/voicing.rb', line 23

def self.[](*args)
  new(*args)
end

Instance Method Details

#chordObject



33
34
35
36
37
# File 'lib/coltrane/theory/voicing.rb', line 33

def chord
  @chord ||= Chord.new(notes: notes)
rescue ChordNotFoundError
  return false
end

#discontinuityObject



43
44
45
46
47
48
# File 'lib/coltrane/theory/voicing.rb', line 43

def discontinuity
  frequencies.each_with_index.reduce(0) do |max_dist, (freq, index)|
    next 0 if index.zero?
    [max_dist, freq.to_f - frequencies[index - 1].to_f].max
  end
end

#frequenciesObject



39
40
41
# File 'lib/coltrane/theory/voicing.rb', line 39

def frequencies
  @frequencies ||= pitches.map(&:frequency)
end

#pitch_classesObject Also known as: notes



27
28
29
# File 'lib/coltrane/theory/voicing.rb', line 27

def pitch_classes
  NoteSet[*pitches.map(&:pitch_class).uniq]
end