Class: HeadMusic::Notation::LilyPond::KeyMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/notation/lily_pond/key_mapper.rb

Overview

Converts a key signature into a LilyPond \key command.

Constant Summary collapse

MODE_COMMANDS_BY_SCALE_TYPE =

LilyPond’s \key command names these nine modes, so scale types outside this set (harmonic minor, whole tone, etc.) cannot be rendered as a \key command and must raise instead.

{
  "major" => "\\major",
  "ionian" => "\\ionian",
  "minor" => "\\minor",
  "aeolian" => "\\aeolian",
  "dorian" => "\\dorian",
  "phrygian" => "\\phrygian",
  "lydian" => "\\lydian",
  "mixolydian" => "\\mixolydian",
  "locrian" => "\\locrian"
}.freeze

Class Method Summary collapse

Class Method Details

.mode(key_signature) ⇒ Object (private)



31
32
33
34
35
# File 'lib/head_music/notation/lily_pond/key_mapper.rb', line 31

def self.mode(key_signature)
  MODE_COMMANDS_BY_SCALE_TYPE.fetch(key_signature.scale_type.name.to_s) do
    raise RenderError, "cannot render scale type #{key_signature.scale_type} in a LilyPond \\key command"
  end
end

.token(key_signature) ⇒ Object



20
21
22
23
# File 'lib/head_music/notation/lily_pond/key_mapper.rb', line 20

def self.token(key_signature)
  key_signature = HeadMusic::Rudiment::KeySignature.get(key_signature)
  "\\key #{tonic(key_signature)} #{mode(key_signature)}"
end

.tonic(key_signature) ⇒ Object (private)



25
26
27
28
# File 'lib/head_music/notation/lily_pond/key_mapper.rb', line 25

def self.tonic(key_signature)
  spelling = key_signature.tonic_spelling
  spelling.letter_name.to_s.downcase + PitchWriter.alteration_suffix(spelling.alteration&.semitones)
end