Class: Coltrane::RomanChord
- Inherits:
-
Object
- Object
- Coltrane::RomanChord
- Defined in:
- lib/coltrane/roman_chord.rb
Overview
This class deals with chords in roman notation. Ex: IVº.
Constant Summary collapse
- DIGITS =
%w[I II III IV V VI VII].freeze
- NOTATION_REGEX =
%r{ (?<degree>b?[ivIV]*) (?<quality>.*) }x- NOTATION_SUBSTITUTIONS =
[ %w[º dim], %w[o dim], %w[ø m7b5]
Instance Method Summary collapse
- #chord ⇒ Object
- #degree ⇒ Object
-
#initialize(notation = nil, chord: nil, key: nil, scale: nil) ⇒ RomanChord
constructor
A new instance of RomanChord.
- #major? ⇒ Boolean
- #minor? ⇒ Boolean
- #notation ⇒ Object
- #quality ⇒ Object
- #quality_name ⇒ Object
- #roman_numeral ⇒ Object
- #root_note ⇒ Object
- #upcase? ⇒ Boolean
Constructor Details
#initialize(notation = nil, chord: nil, key: nil, scale: nil) ⇒ RomanChord
Returns a new instance of RomanChord.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/coltrane/roman_chord.rb', line 18 def initialize(notation=nil, chord: nil, key: nil, scale: nil) if notation.nil? && chord.nil? || key.nil? && scale.nil? raise WrongKeywordsError, '[notation, [scale: || key:]] '\ '[chord:, [scale: || key:]] '\ end @scale = scale || Scale.from_key(key) if !notation.nil? @notation = notation elsif !chord.nil? @chord = chord.is_a?(String) ? Chord.new(name: chord) : chord end end |
Instance Method Details
#chord ⇒ Object
49 50 51 52 |
# File 'lib/coltrane/roman_chord.rb', line 49 def chord @chord ||= Chord.new root_note: root_note, quality: quality end |
#degree ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/coltrane/roman_chord.rb', line 32 def degree return @scale.degree_of_note(root_note) unless @chord.nil? d = regexed_notation['degree'] @flats = d.count('b') d = d.delete('b') @degree ||= DIGITS.index(d.upcase) + 1 end |
#major? ⇒ Boolean
58 59 60 |
# File 'lib/coltrane/roman_chord.rb', line 58 def major? quality.has_major_third? end |
#minor? ⇒ Boolean
54 55 56 |
# File 'lib/coltrane/roman_chord.rb', line 54 def minor? quality.has_minor_third? end |
#notation ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/coltrane/roman_chord.rb', line 80 def notation q = case quality_name when 'm', 'M' then '' when 'm7', 'M' then '7' else quality_name end @notation ||= [ roman_numeral, q, ].join end |
#quality ⇒ Object
70 71 72 73 |
# File 'lib/coltrane/roman_chord.rb', line 70 def quality return @chord.quality unless @chord.nil? ChordQuality.new(name: quality_name) if quality_name end |
#quality_name ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/coltrane/roman_chord.rb', line 62 def quality_name return @chord.quality.name unless @chord.nil? q = normalize_quality_name(regexed_notation['quality']) minor = 'm' if !q.match?((/dim|m7b5/)) && !upcase? q = [minor, q].join q.empty? ? 'M' : q end |
#roman_numeral ⇒ Object
40 41 42 43 |
# File 'lib/coltrane/roman_chord.rb', line 40 def roman_numeral r = DIGITS[degree] minor? ? r.downcase : r end |
#root_note ⇒ Object
75 76 77 78 |
# File 'lib/coltrane/roman_chord.rb', line 75 def root_note return @chord.root_note unless @chord.nil? @scale[degree] - @flats end |
#upcase? ⇒ Boolean
45 46 47 |
# File 'lib/coltrane/roman_chord.rb', line 45 def upcase? !!(regexed_notation['degree'][0].match /[[:upper:]]/) end |