Class: Musicality::Key

Inherits:
Object
  • Object
show all
Includes:
Parseable, Packable
Defined in:
lib/musicality/notation/model/key.rb,
lib/musicality/printing/lilypond/key_engraving.rb,
lib/musicality/notation/parsing/convenience_methods.rb

Constant Summary collapse

FLAT =
:flat
SHARP =
:sharp
NONE =
:none
ACCIDENTAL_TYPES =
[FLAT, SHARP, NONE]
MAJOR =
:major
MINOR =
:minor
TRIAD_TYPES =
[MAJOR, MINOR]
TONIC_PCS =
{
  MAJOR => {
    FLAT => [PitchClasses::F, PitchClasses::Bb, PitchClasses::Eb,
      PitchClasses::Ab, PitchClasses::Db, PitchClasses::Gb],
    SHARP => [PitchClasses::G,PitchClasses::D,PitchClasses::A,
      PitchClasses::E,PitchClasses::B]
  },
  MINOR => {
    FLAT => [PitchClasses::D,PitchClasses::G,PitchClasses::C,
      PitchClasses::F,PitchClasses::Bb,PitchClasses::Eb],
    SHARP => [PitchClasses::E,PitchClasses::B,PitchClasses::Fs,
      PitchClasses::Cs,PitchClasses::Gs]
  }
}
ACCIDENTALS =
{
  FLAT => [PitchClasses::Bb,PitchClasses::Eb,PitchClasses::Ab,PitchClasses::Db,
    PitchClasses::Gb,PitchClasses::Cb,PitchClasses::Fb],
  SHARP => [PitchClasses::Fs,PitchClasses::Cs,PitchClasses::Gs,
    PitchClasses::Ds,PitchClasses::As,PitchClasses::Es,PitchClasses::Bs]
}
PARSER =
Parsing::KeyParser.new
CONVERSION_METHOD =
:to_key

Constants included from Parseable

Parseable::DEFAULT_SPLIT_PATTERN

Constants included from Packable

Packable::PACKED_CLASS_KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parseable

included

Methods included from Packable

#class_str, included, #init_params, #pack, pack_val, recover_class, unpack_val

Constructor Details

#initialize(tonic_pc, triad_type: MAJOR) ⇒ Key

Returns a new instance of Key.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#accidental_typeObject (readonly)

Returns the value of attribute accidental_type.



37
38
39
# File 'lib/musicality/notation/model/key.rb', line 37

def accidental_type
  @accidental_type
end

#accidentalsObject (readonly)

Returns the value of attribute accidentals.



37
38
39
# File 'lib/musicality/notation/model/key.rb', line 37

def accidentals
  @accidentals
end

#tonic_pcObject (readonly)

Returns the value of attribute tonic_pc.



37
38
39
# File 'lib/musicality/notation/model/key.rb', line 37

def tonic_pc
  @tonic_pc
end

#triad_typeObject (readonly)

Returns the value of attribute triad_type.



37
38
39
# File 'lib/musicality/notation/model/key.rb', line 37

def triad_type
  @triad_type
end

Class Method Details

.major(tonic_pc) ⇒ Object



61
62
63
# 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) ⇒ Object



65
66
67
# File 'lib/musicality/notation/model/key.rb', line 65

def self.minor tonic_pc
  Key.new(tonic_pc, triad_type: MINOR)
end

Instance Method Details

#==(other) ⇒ Object



75
76
77
# File 'lib/musicality/notation/model/key.rb', line 75

def ==(other)
  return @tonic_pc == other.tonic_pc && @triad_type == other.triad_type
end

#cloneObject



79
80
81
# File 'lib/musicality/notation/model/key.rb', line 79

def clone
  Marshal.load(Marshal.dump(self))
end

#flat?Boolean

Returns:

  • (Boolean)


72
# File 'lib/musicality/notation/model/key.rb', line 72

def flat?; @accidental_type == FLAT; end

#major?Boolean

Returns:

  • (Boolean)


69
# File 'lib/musicality/notation/model/key.rb', line 69

def major?; @triad_type == MAJOR; end

#minor?Boolean

Returns:

  • (Boolean)


70
# File 'lib/musicality/notation/model/key.rb', line 70

def minor?; @triad_type == MINOR; end

#sharp?Boolean

Returns:

  • (Boolean)


73
# File 'lib/musicality/notation/model/key.rb', line 73

def sharp?; @accidental_type == SHARP; end

#to_lilypondObject



4
5
6
# File 'lib/musicality/printing/lilypond/key_engraving.rb', line 4

def to_lilypond
  "\\key #{PitchClass.to_lilypond(@tonic_pc, sharp?)} \\#{@triad}"
end

#transpose(interval) ⇒ Object



83
84
85
# File 'lib/musicality/notation/model/key.rb', line 83

def transpose interval
  Key.new(PitchClass.from_i(@tonic_pc+interval), @triad_type)
end