lib/coltrane/roman_chord.rb in coltrane-1.0.24 vs lib/coltrane/roman_chord.rb in coltrane-1.0.26
- old
+ new
@@ -1,24 +1,34 @@
# frozen_string_literal: true
module Coltrane
attr_reader :degree, :quality
- # It deals with chords in roman notation
+ # This class deals with chords in roman notation. Ex: IVº.
class RomanChord
DIGITS = %w[I II III IV V VI VII].freeze
NOTATION_REGEX = %r{
(?<degree>b?[ivIV]*)
(?<quality>.*)
}x
- def initialize(scale, notation)
- @scale = scale
- @notation = notation.match(NOTATION_REGEX).named_captures
- @notation['quality'] = @notation['quality']
- .gsub('o', 'dim')
- .gsub('ø', 'm7b5')
+ NOTATION_SUBSTITUTIONS = [
+ %w[º dim],
+ %w[o dim],
+ %w[ø m7b5]
+ ]
+
+ def initialize(notation, key: nil, scale: nil)
+ @scale = scale || Scale.from_key(key)
+ notation = notation.match(NOTATION_REGEX).named_captures
+ notation['quality'] =
+ NOTATION_SUBSTITUTIONS.reduce(notation['quality']) do |memo, subs|
+ break memo if memo.empty?
+ memo.gsub(*subs)
+ end
+
+ @notation = notation
end
def degree
d = @notation['degree']
@flats = d.count('b')
@@ -50,10 +60,9 @@
q = quality_name
ChordQuality.new(name: (q.size.zero? ? 'M' : q))
end
def root_note
- binding.pry if @scale[degree] - @flats == Note['A#']
@scale[degree] - @flats
end
end
end