예제 #1
0
def make_part_tuplet():
    # create a part
    part = score.Part('My Part')

    # create contents
    divs = 12
    ts = score.TimeSignature(3, 4)
    page1 = score.Page(1)
    system1 = score.System(1)

    note1 = score.Note(id='n0', step='A', octave=4, voice=1, staff=1)
    rest1 = score.Rest(voice=1, staff=1)
    note2 = score.Note(id='n2', step='C', octave=4, voice=1, staff=1)
    rest2 = score.Rest(id='r0', voice=1, staff=1)
    
    # and add the contents to the part:
    part.set_quarter_duration(0, divs)
    part.add(ts, 0)
    part.add(page1, 0)
    part.add(system1, 0)
    part.add(note1, 0, 8)
    part.add(rest1, 8, 16)
    part.add(note2, 16, 24)
    part.add(rest2, 24, 36)

    score.add_measures(part)
    score.find_tuplets(part)
    score.set_end_times(part)

    return part
예제 #2
0
    def test_export_import_pprint(self):
        # create a part
        part1 = score.Part('My Part')

        # create contents
        divs = 10
        ts = score.TimeSignature(3, 4)
        page1 = score.Page(1)
        system1 = score.System(1)
        measure1 = score.Measure(number=1)
        note1 = score.Note(step='A', octave=4, voice=1, staff=1)
        rest1 = score.Rest(voice=1, staff=1)
        note2 = score.Note(step='C', octave=5, alter=-1, voice=2, staff=1)
        
        # and add the contents to the part:
        part1.set_quarter_duration(0, divs)
        part1.add(ts, 0)
        part1.add(measure1, 0, 30)
        part1.add(page1, 0)
        part1.add(system1, 0)
        part1.add(note1, 0, 15)
        part1.add(rest1, 15, 30)
        part1.add(note2, 0, 30)
        
        score.set_end_times(part1)
        
        # pretty print the part
        pstring1 = part1.pretty()

        with TemporaryFile() as f:
            # save part to musicxml
            save_musicxml(part1, f)
            f.flush()
            f.seek(0)
            # load part from musicxml
            part2 = load_musicxml(f)

        # pretty print saved/loaded part:
        pstring2 = part2.pretty()

        # test pretty printed strings for equality
        equal = pstring1 == pstring2

        if not equal:
            show_diff(pstring1, pstring2)
        msg = 'Exported and imported score does not yield identical pretty printed representations'
        self.assertTrue(equal, msg)
예제 #3
0
def make_part_slur():
    # create a part
    part = score.Part('My Part')
    # create contents
    divs = 12
    ts = score.TimeSignature(3, 4)
    page1 = score.Page(1)
    system1 = score.System(1)

    note0 = score.Note(id='n0', step='A', octave=4, voice=1, staff=1)
    note1 = score.Note(id='n1', step='A', octave=4, voice=1, staff=1)
    note2 = score.Note(id='n2', step='A', octave=4, voice=1, staff=1)
    note3 = score.Note(id='n3', step='A', octave=4, voice=1, staff=1)

    note4 = score.Note(id='n4', step='A', octave=3, voice=2, staff=1)
    note5 = score.Note(id='n5', step='A', octave=3, voice=2, staff=1)

    slur1 = score.Slur(start_note=note0, end_note=note5)
    slur2 = score.Slur(start_note=note4, end_note=note3)
    
    # and add the contents to the part:
    part.set_quarter_duration(0, divs)
    part.add(ts, 0)
    part.add(page1, 0)
    part.add(system1, 0)
    part.add(note0, 0, 12)
    part.add(note1, 12, 24)
    part.add(note2, 24, 36)
    part.add(note3, 36, 48)
    part.add(note4, 0, 6)
    part.add(note5, 6, 33)
    part.add(slur1, 
                      slur1.start_note.start.t,
                      slur1.end_note.end.t)
    part.add(slur2, 
                      slur2.start_note.start.t,
                      slur2.end_note.end.t)

    score.add_measures(part)
    score.tie_notes(part)
    score.set_end_times(part)
    return part
예제 #4
0
def _parse_parts(document, part_dict):
    """
    Populate the Part instances that are the values of `part_dict` with the
    musical content in document.

    Parameters
    ----------
    document : lxml.etree.ElementTree
        The ElementTree representation of the MusicXML document
    part_dict : dict
        A dictionary with key--value pairs (part_id, Part instance), as returned
        by the _parse_partlist() function.
    """

    for part_el in document.findall('part'):

        part_id = part_el.get('id', 'P1')
        part = part_dict.get(part_id, score.Part(part_id))

        position = 0
        ongoing = {}

        # add new page and system at start of part
        _handle_new_page(position, part, ongoing)
        _handle_new_system(position, part, ongoing)

        for measure_el in part_el.xpath('measure'):
            position = _handle_measure(measure_el, position, part, ongoing)

        # remove unfinished elements from the timeline
        for k, o in ongoing.items():
            if k not in ('page', 'system'):
                if isinstance(o, list):
                    for o_i in o:
                        part.remove(o_i)
                else:
                    part.remove(o)

        # set end times for various musical elements that only have a start time
        # when constructed from MusicXML
        score.set_end_times(part)
예제 #5
0
def _parse_parts(document, part_dict):
    """
    Populate the Part instances that are the values of `part_dict` with the
    musical content in document.

    Parameters
    ----------
    document : lxml.etree.ElementTree
        The ElementTree representation of the MusicXML document
    part_dict : dict
        A dictionary with key--value pairs (part_id, Part instance), as returned
        by the _parse_partlist() function.
    """

    for part_el in document.findall("part"):

        part_id = part_el.get("id", "P1")
        part = part_dict.get(part_id, score.Part(part_id))

        position = 0
        ongoing = {}
        doc_order = 0
        # add new page and system at start of part
        _handle_new_page(position, part, ongoing)
        _handle_new_system(position, part, ongoing)

        for measure_el in part_el.xpath("measure"):
            position, doc_order = _handle_measure(measure_el, position, part,
                                                  ongoing, doc_order)

        # remove unfinished elements from the timeline
        for k, o in ongoing.items():
            if k not in ("page", "system"):
                if isinstance(o, list):
                    for o_i in o:
                        part.remove(o_i)
                else:
                    part.remove(o)

        # check whether all grace notes have a main note
        for gn in part.iter_all(score.GraceNote):
            if gn.main_note is None:

                for no in part.iter_all(
                        score.Note,
                        include_subclasses=False,
                        start=gn.start.t,
                        end=gn.start.t + 1,
                ):
                    if no.voice == gn.voice:
                        gn.last_grace_note_in_seq.grace_next = no

            if gn.main_note is None:
                print(
                    "grace note without recoverable same voice main note: {}".
                    format(gn))
                print("might be cadenza notation")

        # set end times for various musical elements that only have a start time
        # when constructed from MusicXML
        score.set_end_times(part)