class Musicality::Key

Constants

ACCIDENTALS
ACCIDENTAL_TYPES
CONVERSION_METHOD
FLAT
MAJOR
MINOR
NONE
PARSER
SHARP
TONIC_PCS
TRIAD_TYPES

Attributes

accidental_type[R]
accidentals[R]
tonic_pc[R]
triad_type[R]

Public Class Methods

major(tonic_pc) click to toggle source
# File lib/musicality/notation/model/key.rb, line 61
def self.major tonic_pc
  Key.new(tonic_pc, triad_type: MAJOR)
end
minor(tonic_pc) click to toggle source
# File lib/musicality/notation/model/key.rb, line 65
def self.minor tonic_pc
  Key.new(tonic_pc, triad_type: MINOR)
end
new(tonic_pc, triad_type: MAJOR) click to toggle source
# File lib/musicality/notation/model/key.rb, line 39
def initialize tonic_pc, triad_type: MAJOR
  raise ArgumentError, "Unknown triad type #{triad_type}" unless TRIAD_TYPES.include?(triad_type)
  @triad_type = triad_type
  @tonic_pc = PitchClass.from_i(tonic_pc)

  if (@triad_type == MAJOR && @tonic_pc == PitchClasses::C) ||
      (@triad_type == MINOR && @tonic_pc == PitchClasses::A)
    @accidentals = []
    @accidental_type = NONE
  else
    if TONIC_PCS[@triad_type][FLAT].include?(@tonic_pc)
      @accidental_type = FLAT
    elsif TONIC_PCS[@triad_type][SHARP].include?(@tonic_pc)
      @accidental_type = SHARP
    else
      raise ArgumentError, "unknown tonic PC #{@tonic_pc}"
    end
    i = TONIC_PCS[@triad_type][@accidental_type].index(@tonic_pc)
    @accidentals = ACCIDENTALS[@accidental_type][0..i]
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/musicality/notation/model/key.rb, line 75
def ==(other)
  return @tonic_pc == other.tonic_pc && @triad_type == other.triad_type
end
clone() click to toggle source
# File lib/musicality/notation/model/key.rb, line 79
def clone
  Marshal.load(Marshal.dump(self))
end
flat?() click to toggle source
# File lib/musicality/notation/model/key.rb, line 72
def flat?; @accidental_type == FLAT; end
major?() click to toggle source
# File lib/musicality/notation/model/key.rb, line 69
def major?; @triad_type == MAJOR; end
minor?() click to toggle source
# File lib/musicality/notation/model/key.rb, line 70
def minor?; @triad_type == MINOR; end
sharp?() click to toggle source
# File lib/musicality/notation/model/key.rb, line 73
def sharp?; @accidental_type == SHARP; end
to_lilypond() click to toggle source
# File lib/musicality/printing/lilypond/key_engraving.rb, line 4
def to_lilypond
  "\\key #{PitchClass.to_lilypond(@tonic_pc, sharp?)} \\#{@triad}"
end
transpose(interval) click to toggle source
# File lib/musicality/notation/model/key.rb, line 83
def transpose interval
  Key.new(PitchClass.from_i(@tonic_pc+interval), @triad_type)
end