class TransposeChords::Chord

Constants

FLATS
SHARPS

Public Class Methods

new(keys) click to toggle source
# File lib/transpose_chords.rb, line 10
def initialize(keys)
  @keys = keys
  @scale = FLATS
end
transpose(keys) click to toggle source
# File lib/transpose_chords.rb, line 6
def self.transpose(keys)
  new(keys)
end

Public Instance Methods

capo(fret) click to toggle source
# File lib/transpose_chords.rb, line 21
def capo(fret)
  @keys.map { |k| transpose_chord(k, fret) }
end
to(key) click to toggle source
# File lib/transpose_chords.rb, line 15
def to(key)
  @scale = key.include?('b') || key.start_with?('F') ? FLATS : SHARPS
  intervals = index_of(key)
  @keys.map { |k| transpose_chord(k, intervals) }
end

Private Instance Methods

index_of(chord_name) click to toggle source
# File lib/transpose_chords.rb, line 35
def index_of(chord_name)
  index = FLATS.index(chord_name) || SHARPS.index(chord_name)
  raise "Cannot recognize chord name '#{chord_name}'" unless index
  index
end
transpose_chord(chord, intervals) click to toggle source
# File lib/transpose_chords.rb, line 27
def transpose_chord(chord, intervals)
  chord.gsub /([A-G][#b]*)/ do
    chord_name, complement = $~.captures
    new_chord_name = @scale[(index_of(chord_name) + intervals) % @scale.size]
    "#{new_chord_name}#{complement}"
  end
end