Class: MusicalScore::Attribute::Key
- Inherits:
-
ElementBase
- Object
- ElementBase
- MusicalScore::Attribute::Key
- Includes:
- Contracts
- Defined in:
- lib/musical_score/attribute/key.rb
Constant Summary collapse
- @@mode =
i(major minor)
- @@circle_of_fifths =
[0, 7, 2, 9, 4, 11, 6, 1, 8, 3, 10, 5]
Instance Attribute Summary collapse
-
#fifths ⇒ Object
readonly
Returns the value of attribute fifths.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
- #export_xml ⇒ Object
-
#initialize(fifths, mode) ⇒ Key
constructor
A new instance of Key.
- #tonic_key_and_altered_pitches ⇒ Object
Constructor Details
#initialize(fifths, mode) ⇒ Key
Returns a new instance of Key.
15 16 17 18 |
# File 'lib/musical_score/attribute/key.rb', line 15 def initialize(fifths, mode) @fifths = fifths @mode = mode.to_sym end |
Instance Attribute Details
#fifths ⇒ Object (readonly)
Returns the value of attribute fifths.
9 10 11 |
# File 'lib/musical_score/attribute/key.rb', line 9 def fifths @fifths end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
9 10 11 |
# File 'lib/musical_score/attribute/key.rb', line 9 def mode @mode end |
Class Method Details
.create_by_hash(doc) ⇒ Object
63 64 65 66 67 |
# File 'lib/musical_score/attribute/key.rb', line 63 def self.create_by_hash(doc) fifths = doc["fifths"][0].to_i mode = doc["mode"][0].to_sym return MusicalScore::Attribute::Key.new(fifths, mode) end |
.create_by_xml(xml_doc) ⇒ Object
56 57 58 59 60 |
# File 'lib/musical_score/attribute/key.rb', line 56 def self.create_by_xml(xml_doc) fifths = xml_doc.elements["fifths"].text.to_i mode = xml_doc.elements["mode"].text.to_sym return MusicalScore::Attribute::Key.new(fifths, mode) end |
Instance Method Details
#export_xml ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/musical_score/attribute/key.rb', line 69 def export_xml key_element = REXML::Element.new('key') fifths_element = REXML::Element.new('fifths').add_text(@fifths.to_s) mode_element = REXML::Element.new('mode').add_text(@mode.to_s) key_element.add_element(fifths_element) key_element.add_element(mode_element) return key_element end |
#tonic_key_and_altered_pitches ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/musical_score/attribute/key.rb', line 23 def tonic_key_and_altered_pitches if @fifths >= 0 pitch_number = @@circle_of_fifths[@fifths] major_pitch = MusicalScore::Note::Pitch.new_note_sharp(pitch_number) minor_number = (pitch_number + RELATED_KEY_SLIDE_NUMBER) % NUMBER_OF_NOTES minor_pitch = MusicalScore::Note::Pitch.new_note_sharp(minor_number) altered_pitches = Array.new @fifths.times do |i| count = (i + SHARP_START_INDEX) % NUMBER_OF_NOTES altered_pitches.push(MusicalScore::Note::Pitch.new_note_sharp(@@circle_of_fifths[count])) end return { :major_pitch => major_pitch, :minor_pitch => minor_pitch, :altered_pitches => altered_pitches } else reversed = @@circle_of_fifths.reverse fif = @fifths.abs pitch_number = reversed[fif-1] major_pitch = MusicalScore::Note::Pitch.new_note_flat(pitch_number) minor_number = (pitch_number + RELATED_KEY_SLIDE_NUMBER) % NUMBER_OF_NOTES minor_pitch = MusicalScore::Note::Pitch.new_note_flat(minor_number) altered_pitches = Array.new fif.times do |i| count = (i + FLAT_START_INDEX) % NUMBER_OF_NOTES altered_pitches.push(MusicalScore::Note::Pitch.new_note_flat(reversed[count])) end return { :major_pitch => major_pitch, :minor_pitch => minor_pitch, :altered_pitches => altered_pitches } end end |