Class: MusicalScore::Note::Pitch

Inherits:
ElementBase show all
Includes:
Comparable, Contracts
Defined in:
lib/musical_score/note/pitch.rb

Constant Summary collapse

@@key =

pitch names

{
    :C => 0,
    :D => 2,
    :E => 4,
    :F => 5,
    :G => 7,
    :A => 9,
    :B => 11
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step, alter = 0, octave = 0) ⇒ Pitch

Returns a new instance of Pitch.



29
30
31
32
33
# File 'lib/musical_score/note/pitch.rb', line 29

def initialize(step, alter = 0, octave = 0)
    @step   = step.to_sym
    @alter  = alter
    @octave = octave
end

Instance Attribute Details

#alterObject

Returns the value of attribute alter.



17
18
19
# File 'lib/musical_score/note/pitch.rb', line 17

def alter
  @alter
end

#octaveObject

Returns the value of attribute octave.



17
18
19
# File 'lib/musical_score/note/pitch.rb', line 17

def octave
  @octave
end

#stepObject

Returns the value of attribute step.



17
18
19
# File 'lib/musical_score/note/pitch.rb', line 17

def step
  @step
end

Class Method Details

.create_by_hash(doc) ⇒ Object



104
105
106
107
108
109
# File 'lib/musical_score/note/pitch.rb', line 104

def self.create_by_hash(doc)
    step   = doc["step"][0].to_sym
    octave = doc["octave"][0].to_i
    alter  = doc.has_key?("alter") ? doc["alter"][0].to_i : 0
    return MusicalScore::Note::Pitch.new(step, alter, octave)
end

.create_by_xml(xml_doc) ⇒ Object



96
97
98
99
100
101
# File 'lib/musical_score/note/pitch.rb', line 96

def self.create_by_xml(xml_doc)
    step   = xml_doc.elements["step"].text.to_sym
    octave = xml_doc.elements["octave"].text.to_i
    alter  = xml_doc.elements["alter"] ? xml_doc.elements["alter"].text.to_i : 0
    return MusicalScore::Note::Pitch.new(step, alter, octave)
end

.new_note_flat(note_number) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/musical_score/note/pitch.rb', line 68

def self.new_note_flat(note_number)
    step_key_num   = note_number % NUMBER_OF_NOTES
    octave         = note_number / NUMBER_OF_NOTES
    # calculate step and alter
    candidate_keys = @@key.keys.select{ |item| step_key_num <= @@key[item] }
    key            = candidate_keys.min_by{ |item| @@key[item] }

    return MusicalScore::Note::Pitch.new(key, step_key_num-@@key[key], octave)
end

.new_note_sharp(note_number) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/musical_score/note/pitch.rb', line 51

def self.new_note_sharp(note_number)
    step_key_num   = note_number % NUMBER_OF_NOTES
    octave         = note_number / NUMBER_OF_NOTES
    # calculate step and alter
    candidate_keys = @@key.keys.select{ |item| step_key_num >= @@key[item] }
    key            = candidate_keys.max_by{ |item| @@key[item] }

    return MusicalScore::Note::Pitch.new(key, step_key_num-@@key[key], octave)
end

Instance Method Details

#<=>(other) ⇒ Object

Pitch is comparable

Examples:

a = MusicalScore::Note::Pitch.new(:C, 0, 3)
b = MusicalScore::Note::Pitch.new(:D, 0 ,3)
a < b # => true


41
42
43
# File 'lib/musical_score/note/pitch.rb', line 41

def <=> (other)
    self.note_number <=> other.note_number
end

#export_xmlObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/musical_score/note/pitch.rb', line 111

def export_xml
    pitch_element  = REXML::Element.new('pitch')
    step_element   = REXML::Element.new('step').add_text(@step.to_s)
    octave_element = REXML::Element.new('octave').add_text(@octave.to_s)

    pitch_element.add_element(step_element)
    if (@alter != 0)
        alter_element = REXML::Element.new('alter').add_text(@alter.to_s)
        pitch_element.add_element(alter_element)
    end
    pitch_element.add_element(octave_element)

    return pitch_element
end

#note_numberObject



78
79
80
81
# File 'lib/musical_score/note/pitch.rb', line 78

def note_number
    result = (NUMBER_OF_NOTES * octave) + @@key[step] + alter
    return result
end

#to_s(is_note_str = false) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/musical_score/note/pitch.rb', line 86

def to_s(is_note_str = false)
    if is_note_str
        result = "%s%s%d" % [@step.to_s, alter_to_s, @octave]
        return result
    else
        return self.to_s
    end
end