Class: HeadMusic::Notation::MusicXML::KeyMapper

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

Overview

Converts a key signature into the fifths/mode pair MusicXML’s

element needs.

Constant Summary collapse

MODE_NAMES_BY_SCALE_TYPE =

MusicXML’s element only names these seven modes, so scale types outside this set (harmonic minor, whole tone, etc.) cannot be rendered as a element and must raise instead.

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

Class Method Summary collapse

Class Method Details

.fifths(key_signature) ⇒ Object

Returns the number of sharps (positive) or flats (negative) for the

element. Theoretical keys with double accidentals (e.g. G# major) count each double accidental twice, which is correct here.


24
25
26
27
# File 'lib/head_music/notation/music_xml/key_mapper.rb', line 24

def self.fifths(key_signature)
  key_signature = HeadMusic::Rudiment::KeySignature.get(key_signature)
  key_signature.num_sharps - key_signature.num_flats
end

.mode(key_signature) ⇒ Object

Returns the element value for a key signature.



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

def self.mode(key_signature)
  key_signature = HeadMusic::Rudiment::KeySignature.get(key_signature)
  MODE_NAMES_BY_SCALE_TYPE[key_signature.scale_type.name.to_s] ||
    raise_render_error("Cannot render scale type #{key_signature.scale_type} in a MusicXML <key> element")
end

.raise_render_error(message) ⇒ Object (private)



36
37
38
# File 'lib/head_music/notation/music_xml/key_mapper.rb', line 36

def self.raise_render_error(message)
  raise HeadMusic::Notation::MusicXML::RenderError, message
end