Class: Musicality::Key

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

Constant Summary collapse

FLAT =
:flat
SHARP =
:sharp
ACCIDENTAL_TYPES =
[FLAT, SHARP]
MAJOR =
:major
MINOR =
:minor
TRIAD_TYPES =
[MAJOR, MINOR]
TONICS =
{
  MAJOR => {
    FLAT => [PitchClasses::F, PitchClasses::Bb, PitchClasses::Eb,
      PitchClasses::Ab, PitchClasses::Db, PitchClasses::Gb, PitchClasses::Cb],
    SHARP => [PitchClasses::G,PitchClasses::D,PitchClasses::A,
      PitchClasses::E,PitchClasses::B,PitchClasses::Fs,PitchClasses::Cs]
  },
  MINOR => {
    FLAT => [PitchClasses::D,PitchClasses::G,PitchClasses::C,
      PitchClasses::F,PitchClasses::Bb,PitchClasses::Eb,PitchClasses::Ab],
    SHARP => [PitchClasses::E,PitchClasses::B,PitchClasses::Fs,
      PitchClasses::Cs,PitchClasses::Gs,PitchClasses::Ds,PitchClasses::As]
  }
}
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]
}

Constants included from Packable

Packable::PACKED_CLASS_KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Packable

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

Constructor Details

#initialize(tonic_pc, triad: MAJOR, accidental_pref: FLAT) ⇒ Key

Returns a new instance of Key.



38
39
40
41
42
# File 'lib/musicality/notation/model/key.rb', line 38

def initialize tonic_pc, triad: MAJOR, accidental_pref: FLAT
  self.tonic_pc = tonic_pc
  self.triad = triad
  self.accidental_pref = accidental_pref
end

Instance Attribute Details

#accidental_prefObject

Returns the value of attribute accidental_pref.



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

def accidental_pref
  @accidental_pref
end

#tonic_pcObject

Returns the value of attribute tonic_pc.



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

def tonic_pc
  @tonic_pc
end

#triadObject

Returns the value of attribute triad.



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

def triad
  @triad
end

Class Method Details

.major_flat(tonic_pc) ⇒ Object



44
45
46
# File 'lib/musicality/notation/model/key.rb', line 44

def self.major_flat tonic_pc
  Key.new(tonic_pc, triad: MAJOR, accidental_pref: FLAT)
end

.major_sharp(tonic_pc) ⇒ Object



48
49
50
# File 'lib/musicality/notation/model/key.rb', line 48

def self.major_sharp tonic_pc
  Key.new(tonic_pc, triad: MAJOR, accidental_pref: SHARP)
end

.minor_flat(tonic_pc) ⇒ Object



52
53
54
# File 'lib/musicality/notation/model/key.rb', line 52

def self.minor_flat tonic_pc
  Key.new(tonic_pc, triad: MINOR, accidental_pref: FLAT)
end

.minor_sharp(tonic_pc) ⇒ Object



56
57
58
# File 'lib/musicality/notation/model/key.rb', line 56

def self.minor_sharp tonic_pc
  Key.new(tonic_pc, triad: MINOR, accidental_pref: SHARP)
end

Instance Method Details

#==(other) ⇒ Object



97
98
99
100
# File 'lib/musicality/notation/model/key.rb', line 97

def ==(other)
  return @tonic_pc == other.tonic_pc && @triad == other.triad && 
    @accidental_pref == other.accidental_pref
end

#accidental_free?Boolean

Returns:

  • (Boolean)


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

def accidental_free?
  (major? && @tonic_pc == PitchClasses::C) || (minor? && @tonic_pc == PitchClasses::A)
end

#accidental_typeObject



118
119
120
121
122
123
124
# File 'lib/musicality/notation/model/key.rb', line 118

def accidental_type
  if accidental_free? || TONICS[@triad][@accidental_pref].include?(@tonic_pc)
    @accidental_pref
  else
    (@accidental_pref == FLAT) ? SHARP : FLAT
  end
end

#accidentalsObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/musicality/notation/model/key.rb', line 106

def accidentals
  unless @accidentals
    @accidentals = []
    unless accidental_free?
      acc_type = accidental_type
      idx = TONICS[@triad][acc_type].index(@tonic_pc)
      @accidentals = ACCIDENTALS[acc_type].take(idx+1)
    end
  end
  return @accidentals
end

#cloneObject



102
103
104
# File 'lib/musicality/notation/model/key.rb', line 102

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

#flat?Boolean

Returns:

  • (Boolean)


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

def flat?; accidental_type == FLAT; end

#major?Boolean

Returns:

  • (Boolean)


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

def major?; @triad == MAJOR; end

#minor?Boolean

Returns:

  • (Boolean)


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

def minor?; @triad == MINOR; end

#sharp?Boolean

Returns:

  • (Boolean)


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

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



126
127
128
129
130
# File 'lib/musicality/notation/model/key.rb', line 126

def transpose interval
  new_key = self.clone
  new_key.tonic_pc += interval
  return new_key
end