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(tonal_context) ⇒ Object

Returns the element value for a tonal context, or nil where there is none.

is optional in MusicXML, so a signature carrying no interpretation renders as alone rather than as an invented major.



34
35
36
37
38
39
40
41
# File 'lib/head_music/notation/music_xml/key_mapper.rb', line 34

def self.mode(tonal_context)
  return nil if tonal_context.nil?

  identifier = tonal_context.is_a?(String) ? tonal_context : tonal_context.name
  scale_type = HeadMusic::Rudiment::KeySignature.get(identifier).scale_type
  MODE_NAMES_BY_SCALE_TYPE[scale_type.name.to_s] ||
    raise_render_error("Cannot render scale type #{scale_type} in a MusicXML <key> element")
end

.raise_render_error(message) ⇒ Object (private)



43
44
45
# File 'lib/head_music/notation/music_xml/key_mapper.rb', line 43

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