Class: HeadMusic::Rudiment::ChromaticInterval

Inherits:
Base
  • Object
show all
Includes:
Comparable, Named
Defined in:
lib/head_music/rudiment/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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ ChromaticInterval

Returns a new instance of ChromaticInterval.



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

def initialize(identifier)
  candidate = HeadMusic::Utilities::HashKey.for(identifier).to_s
  @semitones = NAMES.index(candidate) || identifier.to_i
  set_name
end

Instance Attribute Details

#alias_name_keysObject (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#name_keyObject (readonly) Originally defined in module Named

Returns the value of attribute name_key.

#semitonesObject (readonly) Also known as: specific_interval

Returns the value of attribute semitones.



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

def semitones
  @semitones
end

Class Method Details

.get(identifier) ⇒ Object



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

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



62
63
64
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 62

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

#-(other) ⇒ Object



66
67
68
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 66

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

#<=>(other) ⇒ Object



70
71
72
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 70

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

#compound?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 47

def compound?
  semitones > 12
end

#diatonic_nameObject



55
56
57
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 55

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

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named

#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#localized_name_in_default_localeObject (private) Originally defined in module Named

#localized_name_in_locale_matching_language(locale) ⇒ Object (private) Originally defined in module Named

#localized_name_in_matching_locale(locale) ⇒ Object (private) Originally defined in module Named

#localized_namesObject Originally defined in module Named

Returns an array of LocalizedName instances that are synonymous with the name.

#name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#name=(name) ⇒ Object Originally defined in module Named

#set_nameObject



31
32
33
34
35
36
37
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 31

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

#simpleObject



39
40
41
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 39

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

#simple?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 43

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

#to_iObject



51
52
53
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 51

def to_i
  semitones
end