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.



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

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



63
64
65
# File 'lib/coltrane/interval_class.rb', line 63

def self.[](semis)
  new semis
end

.all_full_namesObject



71
72
73
# File 'lib/coltrane/interval_class.rb', line 71

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

.method_missingObject



22
# File 'lib/coltrane/interval_class.rb', line 22

def self.method_missing; 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



91
92
93
# File 'lib/coltrane/interval_class.rb', line 91

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

#-(other) ⇒ Object



95
96
97
# File 'lib/coltrane/interval_class.rb', line 95

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

#-@Object



99
100
101
# File 'lib/coltrane/interval_class.rb', line 99

def -@
  IntervalClass[-semitones]
end

#==(other) ⇒ Object



75
76
77
# File 'lib/coltrane/interval_class.rb', line 75

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

#all_full_namesObject



67
68
69
# File 'lib/coltrane/interval_class.rb', line 67

def all_full_names
  self.class.all_full_names
end

#full_nameObject



83
84
85
# File 'lib/coltrane/interval_class.rb', line 83

def full_name
  self.class.full_name(name)
end

#full_namesObject



87
88
89
# File 'lib/coltrane/interval_class.rb', line 87

def full_names
  NAMES[name]
end

#nameObject



79
80
81
# File 'lib/coltrane/interval_class.rb', line 79

def name
  INTERVALS[semitones % 12]
end