Class: HeadMusic::Rudiment::ChromaticInterval
- Inherits:
-
Object
- Object
- HeadMusic::Rudiment::ChromaticInterval
- 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
-
#alias_name_keys ⇒ Object
included
from Named
readonly
Returns the value of attribute alias_name_keys.
-
#name_key ⇒ Object
included
from Named
readonly
Returns the value of attribute name_key.
-
#semitones ⇒ Object
(also: #specific_interval)
readonly
Returns the value of attribute semitones.
Class Method Summary collapse
Instance Method Summary collapse
-
#+(other) ⇒ Object
-
#-(other) ⇒ Object
-
#<=>(other) ⇒ Object
-
#compound? ⇒ Boolean
-
#diatonic_name ⇒ Object
-
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object
included
from Named
-
#initialize(identifier) ⇒ ChromaticInterval
constructor
A new instance of ChromaticInterval.
-
#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object
included
from Named
-
#localized_name_in_default_locale ⇒ Object
included
from Named
private
-
#localized_name_in_locale_matching_language(locale) ⇒ Object
included
from Named
private
-
#localized_name_in_matching_locale(locale) ⇒ Object
included
from Named
private
-
#localized_names ⇒ Object
included
from Named
Returns an array of LocalizedName instances that are synonymous with the name.
-
#name(locale_code: Locale::DEFAULT_CODE) ⇒ Object
included
from Named
-
#name=(name) ⇒ Object
included
from Named
-
#set_name ⇒ Object
-
#simple ⇒ Object
-
#simple? ⇒ Boolean
-
#to_i ⇒ Object
Constructor Details
#initialize(identifier) ⇒ ChromaticInterval
Returns a new instance of ChromaticInterval.
25 26 27 28 29 30 31 32 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 25 def initialize(identifier) if /^\D/i.match?(identifier.to_s.strip) 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
#alias_name_keys ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute alias_name_keys.
#name_key ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute name_key.
#semitones ⇒ Object (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
65 66 67 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 65 def +(other) HeadMusic::Rudiment::ChromaticInterval.get(to_i + other.to_i) end |
#-(other) ⇒ Object
69 70 71 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 69 def -(other) HeadMusic::Rudiment::ChromaticInterval.get((to_i - other.to_i).abs) end |
#<=>(other) ⇒ Object
73 74 75 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 73 def <=>(other) to_i <=> other.to_i end |
#compound? ⇒ Boolean
50 51 52 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 50 def compound? semitones > 12 end |
#diatonic_name ⇒ Object
58 59 60 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 58 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_locale ⇒ Object (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_names ⇒ Object 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_name ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 34 def set_name candidate = semitones while name.nil? && candidate > 0 self.name = NAMES[candidate] candidate -= 12 end end |
#simple ⇒ Object
42 43 44 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 42 def simple HeadMusic::Rudiment::ChromaticInterval.get(semitones % 12) end |
#simple? ⇒ Boolean
46 47 48 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 46 def simple? (0..12).cover?(semitones) end |
#to_i ⇒ Object
54 55 56 |
# File 'lib/head_music/rudiment/chromatic_interval.rb', line 54 def to_i semitones end |