class MusicalScore::Note::Note

Attributes

actual_duration[R]
dot[R]
duration[R]
location[RW]
lyric[RW]
notation[R]
pitch[R]
rest[R]
tie[R]
time_modification[R]
type[R]

Public Class Methods

create_by_hash(doc) click to toggle source
# 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) click to toggle source
# 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
new( duration:, tie: nil, dot: 0, lyric: nil, pitch: nil, rest: true, type:, time_modification: nil, notation: nil, **rest_args ) click to toggle source
# 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

Public Instance Methods

export_xml() click to toggle source
# 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

Private Instance Methods

set_actual_duration() click to toggle source
# File lib/musical_score/note/note.rb, line 151
def set_actual_duration
    unless(@time_modification)
        @actual_duration = Rational(@duration, 1)
    else
        total_duration = @duration * @time_modification.normal_notes
        @actual_duration = Rational(total_duration, @time_modification.actual_notes)
    end
end