Sha256: 52f708a27d3175d2bc77a1c72c12a983b322fbb67f6601e9e934914b2fb3a05e

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

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

  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_up
    pitches_up.map(&:pitch_class).map do |pitch_class|
      pitch_class.smart_spelling(max_sharps_in_major_key_signature: 7)
    end
  end

  def spellings_down
    pitches_down.map(&:pitch_class).map do |pitch_class|
      pitch_class.smart_spelling(max_sharps_in_major_key_signature: 4)
    end
  end

  def pitches_down
    @pitches_down ||= [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

  private_class_method :new
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
head_music-7.0.5 lib/head_music/circle.rb
head_music-7.0.4 lib/head_music/circle.rb
head_music-7.0.3 lib/head_music/circle.rb
head_music-7.0.2 lib/head_music/circle.rb
head_music-7.0.1 lib/head_music/circle.rb
head_music-7.0.0 lib/head_music/circle.rb
head_music-6.0.1 lib/head_music/circle.rb
head_music-6.0.0 lib/head_music/circle.rb
head_music-5.0.0 lib/head_music/circle.rb