Class: HeadMusic::ChromaticInterval
- Inherits:
-
Object
- Object
- HeadMusic::ChromaticInterval
- Includes:
- Comparable
- 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 =
TODO: include the Named module
%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
-
#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
-
#initialize(semitones) ⇒ ChromaticInterval
constructor
A new instance of ChromaticInterval.
- #simple ⇒ Object
- #simple? ⇒ Boolean
- #to_i ⇒ Object
Constructor Details
#initialize(semitones) ⇒ ChromaticInterval
Returns a new instance of ChromaticInterval.
24 25 26 |
# File 'lib/head_music/chromatic_interval.rb', line 24 def initialize(semitones) @semitones = semitones end |
Instance Attribute Details
#semitones ⇒ Object (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
51 52 53 |
# File 'lib/head_music/chromatic_interval.rb', line 51 def +(other) HeadMusic::ChromaticInterval.get(to_i + other.to_i) end |
#-(other) ⇒ Object
55 56 57 |
# File 'lib/head_music/chromatic_interval.rb', line 55 def -(other) HeadMusic::ChromaticInterval.get((to_i - other.to_i).abs) end |
#<=>(other) ⇒ Object
59 60 61 |
# File 'lib/head_music/chromatic_interval.rb', line 59 def <=>(other) to_i <=> other.to_i end |
#compound? ⇒ Boolean
36 37 38 |
# File 'lib/head_music/chromatic_interval.rb', line 36 def compound? semitones > 12 end |
#diatonic_name ⇒ Object
44 45 46 |
# File 'lib/head_music/chromatic_interval.rb', line 44 def diatonic_name NAMES[simple.semitones].tr("_", " ") end |
#simple ⇒ Object
28 29 30 |
# File 'lib/head_music/chromatic_interval.rb', line 28 def simple HeadMusic::ChromaticInterval.get(semitones % 12) end |
#simple? ⇒ Boolean
32 33 34 |
# File 'lib/head_music/chromatic_interval.rb', line 32 def simple? (0..12).cover?(semitones) end |
#to_i ⇒ Object
40 41 42 |
# File 'lib/head_music/chromatic_interval.rb', line 40 def to_i semitones end |