Sha256: e91f7958a5f7c0002f6962de76f7ed37ce601d390d0eca2c4b9b960fccec4168

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents


module Coltrane
  # It describe the quality of a chord, like maj7 or dim.
  class ChordQuality
    attr_reader :name
    include Qualities

    def initialize(interval_sequence)
      if interval_sequence.class != IntervalSequence
        interval_sequence = IntervalSequence.new(interval_sequence)
      end
      @name = find_chord_name(interval_sequence)
    end

    def self.new_from_notes(notes)
      note_set = NoteSet.new(notes) unless notes.class == NoteSet
      new(note_set.interval_sequence.reordered)
    end

    def self.new_from_pitches(*pitches)
      notes = pitches.sort_by(&:number)
                     .collect(&:note)
                     .collect(&:name)
                     .uniq

      new_from_notes(notes)
    end

    def self.new_from_string(quality_string)
      new(CHORD_QUALITIES[quality_string])
    end

    def intervals
      CHORD_QUALITIES[name]
    end

    protected

    def find_chord_name(note_intervals, inversion = 0)
      inversion >= note_intervals.all.size && return
      CHORD_QUALITIES.key(note_intervals.numbers) ||
        CHORD_QUALITIES.key(note_intervals.numbers.sort) ||
        find_chord_name(note_intervals.next_inversion, inversion + 1) ||
        nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coltrane-0.0.2 lib/coltrane/chord_quality.rb