class Coltrane::Theory::IntervalClass

Interval class here is not related to the Object Oriented Programming context but to the fact that there is a class of intervals that can all be categorized as having the same quality.

This class in specific still takes into account the order of intervals. C to D is a major second, but D to C is a minor seventh.

Constants

ALTERATIONS
COMPOUND_DISTANCES_NAMES
DISTANCES_NAMES
QUALITY_NAMES
QUALITY_SEQUENCE
SINGLE_DISTANCES_NAMES

Public Class Methods

all() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 93
def all
  @all ||= names.map { |n| IntervalClass[n] }
end
all_names_including_compound() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 85
def all_names_including_compound
  @all_names_including_compound ||= names + compound_names
end
compound_names() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 81
def compound_names
  @compound_names ||= all.map(&:compound_name)
end
distance_name(n) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 63
def distance_name(n)
  DISTANCES_NAMES[n - 1]
end
distances_names() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 59
def distances_names
  DISTANCES_NAMES
end
expand_name(name) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 106
def expand_name(name)
  q, n = split(name)
  (
    case name
    when /AA|dd/ then 'Double '
    when /AAA|ddd/ then 'Triple '
    else ''
    end
  ) + "#{quality_name(q)} #{distance_name(n.to_i)}"
end
full_names() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 89
def full_names
  @full_names ||= names.map { |n| expand_name(n) }
end
full_names_including_compound() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 97
def full_names_including_compound
  @full_names_including_compound ||=
    all_names_including_compound.map { |n| expand_name(n) }
end
names() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 71
def names
  @names ||= begin
    SINGLE_DISTANCES_NAMES.each_with_index.reduce([]) do |i_names, (_d, i)|
      i_names + QUALITY_SEQUENCE[i % 7].reduce([]) do |qs, q|
        qs + ["#{q}#{i + 1}"]
      end
    end
  end
end
new(arg) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 118
def initialize(arg)
  super case arg
        when FrequencyInterval then arg.semitones
        when String
          self.class.names.index(arg) ||
            self.class.full_names.index(arg) ||
            self.class.all_names_including_compound.index(arg) ||
            self.class.full_names_including_compound.index(arg)
        when Numeric then arg
        else
          raise WrongArgumentsError,
                'Provide: [interval] || [name] || [number of semitones]'
        end % 12 * 100
end
quality_name(q) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 67
def quality_name(q)
  QUALITY_NAMES[q]
end
split(interval) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 102
def split(interval)
  interval.scan(/(\w)(\d\d?)/)[0]
end

Private Class Methods

interval_by_full_name(arg) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 204
def self.interval_by_full_name(arg)
  NAMES.invert.each do |full_names, interval_name|
    return INTERVALS.index(interval_name) if full_names.include?(arg)
  end
  raise IntervalNotFoundError, arg
end

Public Instance Methods

+(other) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 190
def +(other)
  IntervalClass[semitones + other]
end
-(other) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 194
def -(other)
  IntervalClass[semitones - other]
end
-@() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 198
def -@
  IntervalClass[-semitones]
end
==(other) click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 149
def ==(other)
  return false unless other.is_a? FrequencyInterval
  (semitones % 12) == (other.semitones % 12)
end
alteration() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 154
def alteration
  name.chars.reduce(0) { |a, s| a + (ALTERATIONS[s] || 0) }
end
ascending() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 158
def ascending
  self.class[semitones.abs]
end
compound()
Alias for: compound_interval
compound_interval() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 139
def compound_interval
  Interval.new(
    letter_distance: distance,
    semitones: semitones,
    compound: true
  )
end
Also aliased as: compound
compound_name() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 178
def compound_name
  "#{quality}#{distance + 7}"
end
descending() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 162
def descending
  self.class[-semitones.abs]
end
distance() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 182
def distance
  self.class.split(name)[1].to_i
end
full_name() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 170
def full_name
  self.class.expand_name(name)
end
interval() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 135
def interval
  Interval.new(letter_distance: distance, semitones: semitones)
end
inversion() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 166
def inversion
  self.class[-semitones % 12]
end
name() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 174
def name
  self.class.names[semitones % 12]
end
quality() click to toggle source
# File lib/coltrane/theory/interval_class.rb, line 186
def quality
  self.class.split(name)[0]
end