Class: Coltrane::IntervalClass

Inherits:
Interval
  • Object
show all
Defined in:
lib/coltrane/interval_class.rb

Overview

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.

Constant Summary collapse

INTERVALS =
%w[P1 m2 M2 m3 M3 P4 A4 P5 m6 M6 m7 M7].freeze
NAMES =

Create full names and methods such as major_third? minor_seventh? TODO: It’s a mess and it really needs a refactor someday

INTERVALS.each_with_index.each_with_object({}) do |(interval, index), memo|
  memo[interval] ||= []
  2.times do |o|
    q, i = split(interval)
    num = o * 7 + i.to_i
    prev_q = split(INTERVALS[(index - 1) % 12])[0]
    next_q = split(INTERVALS[(index + 1) % 12])[0]
    memo[interval] << full_name("#{q}#{num}")
    memo[interval] << full_name("d#{(num - 1 + 1) % 14 + 1}") if next_q.match? /m|P/
    next if q == 'A'
    memo[interval] << full_name("A#{(num - 1 - 1) % 14 + 1}") if prev_q.match? /M|P/
  end
end
ALL_FULL_NAMES =
NAMES.values.flatten

Instance Attribute Summary

Attributes inherited from Interval

#cents

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Interval

#ascending?, #descending?, #semitones

Constructor Details

#initialize(arg) ⇒ IntervalClass

Returns a new instance of IntervalClass.



51
52
53
54
55
56
57
58
59
# File 'lib/coltrane/interval_class.rb', line 51

def initialize(arg)
  super case arg
        when Interval then arg.semitones
        when String
          INTERVALS.index(arg) || self.class.interval_by_full_name(arg)
        when Numeric then arg
        else raise WrongArgumentsError
        end % 12 * 100
end

Class Method Details

.[](semis) ⇒ Object



61
62
63
# File 'lib/coltrane/interval_class.rb', line 61

def self.[](semis)
  new semis
end

.all_full_namesObject



69
70
71
# File 'lib/coltrane/interval_class.rb', line 69

def self.all_full_names
  ALL_FULL_NAMES
end

.full_name(interval) ⇒ Object



17
18
19
20
# File 'lib/coltrane/interval_class.rb', line 17

def self.full_name(interval)
  q, n = split(interval)
  "#{q.interval_quality} #{n.to_i.interval_name}"
end

.split(interval) ⇒ Object



13
14
15
# File 'lib/coltrane/interval_class.rb', line 13

def self.split(interval)
  interval.scan(/(\w)(\d\d?)/)[0]
end

Instance Method Details

#+(other) ⇒ Object



89
90
91
# File 'lib/coltrane/interval_class.rb', line 89

def +(other)
  IntervalClass[semitones + other]
end

#-(other) ⇒ Object



93
94
95
# File 'lib/coltrane/interval_class.rb', line 93

def -(other)
  IntervalClass[semitones - other]
end

#==(other) ⇒ Object



73
74
75
# File 'lib/coltrane/interval_class.rb', line 73

def ==(other)
  (cents % 12) == (other.cents % 12)
end

#all_full_namesObject



65
66
67
# File 'lib/coltrane/interval_class.rb', line 65

def all_full_names
  self.class.all_full_names
end

#full_nameObject



81
82
83
# File 'lib/coltrane/interval_class.rb', line 81

def full_name
  self.class.full_name(name)
end

#full_namesObject



85
86
87
# File 'lib/coltrane/interval_class.rb', line 85

def full_names
  NAMES[name]
end

#nameObject



77
78
79
# File 'lib/coltrane/interval_class.rb', line 77

def name
  INTERVALS[semitones % 12]
end