Sha256: 370e2c6d5d37ccce46f432e3c21e0e0ed4946b944974840d7e59bf9b42fbddee

Contents?: true

Size: 971 Bytes

Versions: 22

Compression:

Stored size: 971 Bytes

Contents

# frozen_string_literal: true

module Coltrane
  module Theory
    class CircleOfFifths
      attr_reader :notes

      LETTER_SEQUENCE = %w[C G D A E B F].freeze

      def initialize(start_note = Note['C'], size = Float::INFINITY)
        index  = letters.index(start_note.name[0])
        @notes = fifths(note: start_note - Interval.perfect_fifth,
                        size: size,
                        index: (index - 1) % LETTER_SEQUENCE.size)
      end

      private

      def fifths(note:, index: nil, notes: [], size:)
        return notes if size == 0
        return notes if notes.any? && note.as(letters(index)).name == notes.first.name
        fifths note: note + Interval.perfect_fifth,
               notes: notes + [note.as(letters(index))],
               index: index + 1,
               size:  size - 1
      end

      def letters(i = nil)
        i.nil? ? LETTER_SEQUENCE : LETTER_SEQUENCE[i % LETTER_SEQUENCE.size]
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
coltrane-4.1.2 lib/coltrane/theory/circle_of_fifths.rb
coltrane-4.1.1 lib/coltrane/theory/circle_of_fifths.rb
coltrane-4.1.0 lib/coltrane/theory/circle_of_fifths.rb
coltrane-4.0.6 lib/coltrane/theory/circle_of_fifths.rb
coltrane-4.0.5 lib/coltrane/theory/circle_of_fifths.rb
coltrane-4.0.4 lib/coltrane/theory/circle_of_fifths.rb
coltrane-4.0.3 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.4.2 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.4.1 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.4.0 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.3.3 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.3.2 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.3.1 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.3.0 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.2.0 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.1.3 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.1.2 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.1.1 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.1.0 lib/coltrane/theory/circle_of_fifths.rb
coltrane-3.0.0 lib/coltrane/theory/circle_of_fifths.rb