Sha256: c46423f988627a2d9eb1835eadc0d37fdb3ec3e871add38901d5abaed9fc2e7b

Contents?: true

Size: 1.28 KB

Versions: 12

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'head_music/interval_cycle'

# A Circle of Fifths or Fourths shows relationships between pitch classes
class HeadMusic::Circle < HeadMusic::IntervalCycle
  def self.of_fifths
    get(:perfect_fifth)
  end

  def self.of_fourths
    get(:perfect_fourth)
  end

  def self.get(interval = :perfect_fifth)
    @circles ||= {}
    diatonic_interval = HeadMusic::DiatonicInterval.get(interval)
    @circles[interval] ||= new(interval: diatonic_interval, starting_pitch: 'C4')
  end

  def index(pitch_class)
    pitch_classes.index(HeadMusic::Spelling.get(pitch_class).pitch_class)
  end

  alias spellings_up spellings

  def key_signatures_up
    spellings_up.map { |spelling| HeadMusic::KeySignature.new(spelling) }
  end

  def key_signatures_down
    spellings_down.map { |spelling| HeadMusic::KeySignature.new(spelling) }
  end

  def spellings_down
    pitches_down.map(&:spelling)
  end

  def pitches_down
    @pitches_down ||= begin
      [starting_pitch].tap do |list|
        loop do
          next_pitch = list.last - interval
          next_pitch += octave while starting_pitch - next_pitch > 12
          break if next_pitch.pitch_class == list.first.pitch_class

          list << next_pitch
        end
      end
    end
  end

  private_class_method :new
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
head_music-0.27.0 lib/head_music/circle.rb
head_music-0.26.3 lib/head_music/circle.rb
head_music-0.26.2 lib/head_music/circle.rb
head_music-0.26.1 lib/head_music/circle.rb
head_music-0.26.0 lib/head_music/circle.rb
head_music-0.25.0 lib/head_music/circle.rb
head_music-0.24.5 lib/head_music/circle.rb
head_music-0.24.4 lib/head_music/circle.rb
head_music-0.24.3 lib/head_music/circle.rb
head_music-0.24.2 lib/head_music/circle.rb
head_music-0.24.1 lib/head_music/circle.rb
head_music-0.24.0 lib/head_music/circle.rb