Class: HeadMusic::ChromaticInterval

Inherits:
Object
  • Object
show all
Includes:
Comparable, Named
Defined in:
lib/head_music/chromatic_interval.rb

Overview

A chromatic interval is the distance between two pitches measured in half-steps.

Constant Summary collapse

NAMES =
%w[
  perfect_unison minor_second major_second minor_third major_third perfect_fourth tritone perfect_fifth
  minor_sixth major_sixth minor_seventh major_seventh perfect_octave
].freeze

Instance Attribute Summary collapse

Attributes included from Named

#alias_name_keys, #name_key

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Named

#ensure_localized_name, included, #localized_name, #localized_names, #name, #name=

Constructor Details

#initialize(identifier) ⇒ ChromaticInterval

Returns a new instance of ChromaticInterval.



24
25
26
27
28
29
30
31
# File 'lib/head_music/chromatic_interval.rb', line 24

def initialize(identifier)
  if identifier.to_s.strip =~ /^\D/i
    candidate = identifier.to_s.downcase.gsub(/\W+/, "_")
    semitones = NAMES.index(candidate) || identifier.to_i
  end
  @semitones = semitones || identifier.to_i
  set_name
end

Instance Attribute Details

#semitonesObject (readonly) Also known as: specific_interval

Returns the value of attribute semitones.



15
16
17
# File 'lib/head_music/chromatic_interval.rb', line 15

def semitones
  @semitones
end

Class Method Details

.get(identifier) ⇒ Object



17
18
19
20
21
22
# File 'lib/head_music/chromatic_interval.rb', line 17

def self.get(identifier)
  @intervals ||= {}
  candidate = identifier.to_s.downcase.gsub(/\W+/, "_")
  semitones = NAMES.index(candidate) || identifier.to_i
  @intervals[semitones] ||= new(semitones.to_i)
end

Instance Method Details

#+(other) ⇒ Object



64
65
66
# File 'lib/head_music/chromatic_interval.rb', line 64

def +(other)
  HeadMusic::ChromaticInterval.get(to_i + other.to_i)
end

#-(other) ⇒ Object



68
69
70
# File 'lib/head_music/chromatic_interval.rb', line 68

def -(other)
  HeadMusic::ChromaticInterval.get((to_i - other.to_i).abs)
end

#<=>(other) ⇒ Object



72
73
74
# File 'lib/head_music/chromatic_interval.rb', line 72

def <=>(other)
  to_i <=> other.to_i
end

#compound?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/head_music/chromatic_interval.rb', line 49

def compound?
  semitones > 12
end

#diatonic_nameObject



57
58
59
# File 'lib/head_music/chromatic_interval.rb', line 57

def diatonic_name
  NAMES[simple.semitones].tr("_", " ")
end

#set_nameObject



33
34
35
36
37
38
39
# File 'lib/head_music/chromatic_interval.rb', line 33

def set_name
  candidate = semitones
  while name.nil? && candidate > 0
    self.name = NAMES[candidate]
    candidate -= 12
  end
end

#simpleObject



41
42
43
# File 'lib/head_music/chromatic_interval.rb', line 41

def simple
  HeadMusic::ChromaticInterval.get(semitones % 12)
end

#simple?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/head_music/chromatic_interval.rb', line 45

def simple?
  (0..12).cover?(semitones)
end

#to_iObject



53
54
55
# File 'lib/head_music/chromatic_interval.rb', line 53

def to_i
  semitones
end