Class: MusicalScore::Note::Note

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration:, tie: nil, dot: 0, lyric: nil, pitch:, rest: false, type:, time_modification: nil, notation: nil, **rest_args) ⇒ Note

Returns a new instance of Note.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/musical_score/note/note.rb', line 27

def initialize(
    duration:,
    tie: nil,
    dot: 0,
    lyric: nil,
    pitch: nil,
    rest: true,
    type:,
    time_modification: nil,
    notation: nil,
    **rest_args
    )
    @duration = duration
    @tie      = tie
    @dot      = dot
    @lyric    = lyric
    @pitch    = pitch
    @rest     = rest
    @type     = type
    @time_modification = time_modification
    @notation = notation

    set_actual_duration
end

Instance Attribute Details

#actual_durationObject (readonly)

Returns the value of attribute actual_duration.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def actual_duration
  @actual_duration
end

#dotObject (readonly)

Returns the value of attribute dot.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def dot
  @dot
end

#durationObject (readonly)

Returns the value of attribute duration.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def duration
  @duration
end

#locationObject

Returns the value of attribute location.



11
12
13
# File 'lib/musical_score/note/note.rb', line 11

def location
  @location
end

#lyricObject

Returns the value of attribute lyric.



11
12
13
# File 'lib/musical_score/note/note.rb', line 11

def lyric
  @lyric
end

#notationObject (readonly)

Returns the value of attribute notation.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def notation
  @notation
end

#pitchObject (readonly)

Returns the value of attribute pitch.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def pitch
  @pitch
end

#restObject (readonly)

Returns the value of attribute rest.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def rest
  @rest
end

#tieObject (readonly)

Returns the value of attribute tie.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def tie
  @tie
end

#time_modificationObject (readonly)

Returns the value of attribute time_modification.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def time_modification
  @time_modification
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/musical_score/note/note.rb', line 12

def type
  @type
end

Class Method Details

.create_by_hash(doc) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/musical_score/note/note.rb', line 117

def self.create_by_hash(doc)
    dots     = doc.has_key?("dot") ? doc["dot"].size : 0
    duration = doc["duration"][0].to_i
    type     = MusicalScore::Note::Type.new(doc["type"][0])
    tie      = doc.has_key?("tie") ? doc.dig("tie", 0, "type").to_sym : nil
    notation = doc.has_key?("notations") ? MusicalScore::Note::Notation::Notation.create_by_hash(doc["notations"][0]) : nil
    time_modification = doc.has_key?("time-modification") ? MusicalScore::Note::TimeModification.create_by_hash(doc["time-modification"][0]) : nil
    rest = doc.has_key?("rest")

    if (rest)
        return MusicalScore::Note::Note.new(duration: duration, tie: tie, dot: dots, rest: rest, type: type, time_modification: time_modification, notation: notation)
    else
        pitch = MusicalScore::Note::Pitch.create_by_hash(doc["pitch"][0])
        lyric = doc.has_key?("lyric") ? MusicalScore::Note::Lyric.create_by_hash(doc["lyric"][0]) : nil
        return MusicalScore::Note::Note.new(duration: duration, tie: tie, dot: dots, type: type, lyric: lyric, pitch: pitch, time_modification: time_modification, notation: notation)
    end

end

.create_by_xml(xml_doc) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/musical_score/note/note.rb', line 90

def self.create_by_xml(xml_doc)
    dots = 0
    xml_doc.elements.each("dot") do |elemet|
        dots += 1
    end
    duration = xml_doc.elements["duration"].text.to_i
    type     = MusicalScore::Note::Type.new(xml_doc.elements["type"].text)

    tie = xml_doc.elements["tie"] ? xml_doc.elements["tie"].attributes["type"].to_sym : nil

    notation_doc = xml_doc.elements["notations"]
    notation     = notation_doc ? MusicalScore::Note::Notation::Notation.create_by_xml(notation_doc) : nil

    time_modification_doc = xml_doc.elements["time-modification"]
    time_modification = time_modification_doc ? MusicalScore::Note::TimeModification.create_by_xml(time_modification_doc) : nil
    rest = xml_doc.elements["rest"] ? true : false
    if (rest)
        return MusicalScore::Note::Note.new(duration: duration, tie: tie, dot: dots, rest: rest, type: type, time_modification: time_modification, notation: notation)
    else
        pitch = MusicalScore::Note::Pitch.create_by_xml(xml_doc.elements["pitch"])
        lyric_doc = xml_doc.elements["lyric"]
        lyric = lyric_doc ? MusicalScore::Note::Lyric.create_by_xml(lyric_doc) : nil
        return MusicalScore::Note::Note.new(duration: duration, tie: tie, dot: dots, type: type, lyric: lyric, pitch: pitch, time_modification: time_modification, notation: notation)
    end
end

Instance Method Details

#export_xmlObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/musical_score/note/note.rb', line 136

def export_xml
    note_element = REXML::Element.new('note')
    note_element.add_element('rest') if @rest
    note_element.add_element(@pitch.export_xml) if @pitch
    note_element.add_element('duration').add_text(@duration.to_s)
    note_element.add_element('tie').add_attribute('type', @tie.to_s) if @tie
    note_element.add_element(@type.export_xml)
    note_element.add_element(@time_modification.export_xml) if @time_modification
    note_element.add_element(@lyric.export_xml(1)) if @lyric
    note_element.add_element(@notation.export_xml) if @notation

    return note_element
end