Class: Stretto::MusicElements::KeySignature

Inherits:
MusicElement show all
Defined in:
lib/stretto/music_elements/key_signature.rb

Overview

A key signature indicates the channel to play in the indicated key or scale.

This increases or decreases note values for certain notes, according to music theory (see en.wikipedia.org/wiki/Key_signature). The key signature is specified by the letter K, followed by a key (for example C, D# or Eb) and the scale (maj or min)

Note that notes and chords specified with a pitch value will not be affected. That is, given the following pattern "KGmaj F [65]", the first note will raise its pitch to 66 (due to the sharp accidental in the F notes), while the second note will keep its given value of 65

Constant Summary

MODIFIERS =
{
  :major => {
    'C'   => {},
    'G'   => { 'F' => +1 },
    'D'   => { 'F' => +1, 'C' => +1 },
    'A'   => { 'F' => +1, 'C' => +1, 'G' => +1 },
    'E'   => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1 },
    'B'   => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1, 'A' => +1 },
    'F#'  => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1, 'A' => +1, 'E' => +1},
    'C#'  => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1, 'A' => +1, 'E' => +1, 'B' => +1 },

    'F'   => { 'B' => -1 },
    'Bb'  => { 'B' => -1, 'E' => -1 },
    'Eb'  => { 'B' => -1, 'E' => -1, 'A' => -1 },
    'Ab'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1 },
    'Db'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1, 'G' => -1 },
    'Gb'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1, 'G' => -1, 'C' => -1 },
    'Cb'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1, 'G' => -1, 'C' => -1, 'F' => -1},
  },
  :minor => {
    'A'   => {},
    'E'   => { 'F' => +1 },
    'B'   => { 'F' => +1, 'C' => +1 },
    'F#'  => { 'F' => +1, 'C' => +1, 'G' => +1 },
    'C#'  => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1 },
    'G#'  => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1, 'A' => +1 },
    'D#'  => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1, 'A' => +1, 'E' => +1},
    'A#'  => { 'F' => +1, 'C' => +1, 'G' => +1, 'D' => +1, 'A' => +1, 'E' => +1, 'B' => +1 },
    'D'   => { 'B' => -1 },
    'G'   => { 'B' => -1, 'E' => -1 },
    'C'   => { 'B' => -1, 'E' => -1, 'A' => -1 },
    'F'   => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1 },
    'Bb'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1, 'G' => -1 },
    'Eb'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1, 'G' => -1, 'C' => -1 },
    'Ab'  => { 'B' => -1, 'E' => -1, 'A' => -1, 'D' => -1, 'G' => -1, 'C' => -1, 'F' => -1},
  }
}
SCALES =
{ 'maj' => :major, 'min' => :minor }

Instance Attribute Summary collapse

Attributes inherited from MusicElement

#original_string, #pattern

Attributes included from Node

#next, #prev

Instance Method Summary collapse

Methods inherited from MusicElement

#build_music_string, #duration, #end_of_tie?, #start_of_tie?, #substitute_variables!, #to_s

Constructor Details

#initialize(string_or_options, pattern = nil) ⇒ KeySignature



21
22
23
24
25
26
27
28
29
# File 'lib/stretto/music_elements/key_signature.rb', line 21

def initialize(string_or_options, pattern = nil)
  token = case string_or_options
    when String then Stretto::Parser.parse_key_signature!(string_or_options)
    else string_or_options
  end
  super(token[:text_value], pattern)
  @key = normalize_keysig(token[:key])
  @scale = SCALES[token[:scale].downcase]
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key



19
20
21
# File 'lib/stretto/music_elements/key_signature.rb', line 19

def key
  @key
end

#scaleObject (readonly)

Returns the value of attribute scale



19
20
21
# File 'lib/stretto/music_elements/key_signature.rb', line 19

def scale
  @scale
end

Instance Method Details

#modifier_for(note_key) ⇒ Number



33
34
35
# File 'lib/stretto/music_elements/key_signature.rb', line 33

def modifier_for(note_key)
  MODIFIERS[@scale][@key][note_key]
end