Exemplo n.º 1
0
    def _pretty_export_import_pretty_test(self, 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)
            _tmp = f.read().decode('utf8')
            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:
            print(pstring1)
            print(pstring2)
            print(_tmp)
            show_diff(pstring1, pstring2)
        msg = 'Exported and imported score does not yield identical pretty printed representations'
        self.assertTrue(equal, msg)
Exemplo n.º 2
0
    def test_unfold_timeline(self):
        for fn, fn_target_1, fn_target_2 in MUSICXML_UNFOLD_TESTPAIRS:
            part = load_musicxml(fn, validate=False)
            part = score.unfold_part_maximal(part)
            result = save_musicxml(part).decode("UTF-8")
            with open(fn_target_1) as f:
                target = f.read()
            equal = target == result
            if not equal:
                show_diff(result, target)
            msg = "Unfolding part of MusicXML file {} does not yield expected result".format(
                fn)
            self.assertTrue(equal, msg)

            # check unfold with update_id
            part = score.unfold_part_maximal(part, update_ids=True)
            result = save_musicxml(part).decode("UTF-8")
            with open(fn_target_2) as f:
                target = f.read()
            equal = target == result
            if not equal:
                show_diff(result, target)
            msg = "Unfolding part of MusicXML file {} does not yield expected result".format(
                fn)
            self.assertTrue(equal, msg)
Exemplo n.º 3
0
def render_lilypond(part, fmt="png"):
    if fmt not in ("png", "pdf"):
        print("warning: unsupported output format")
        return None

    prvw_sfx = ".preview.{}".format(fmt)

    with TemporaryFile() as xml_fh, NamedTemporaryFile(suffix=prvw_sfx,
                                                       delete=False) as img_fh:

        # save part to musicxml in file handle xml_fh
        save_musicxml(part, xml_fh)
        # rewind read pointer of file handle before we pass it to musicxml2ly
        xml_fh.seek(0)

        img_stem = img_fh.name[:-len(prvw_sfx)]

        # convert musicxml to lilypond format (use stdout pipe)
        cmd1 = ["musicxml2ly", "-o-", "-"]
        try:
            ps1 = subprocess.run(cmd1,
                                 stdin=xml_fh,
                                 stdout=subprocess.PIPE,
                                 check=False)
            if ps1.returncode != 0:
                LOGGER.error("Command {} failed with code {}".format(
                    cmd1, ps1.returncode))
                return None
        except FileNotFoundError as f:
            LOGGER.error('Executing "{}" returned  {}.'.format(
                " ".join(cmd1), f))
            return None

        # convert lilypond format (read from pipe of ps1) to image, and save to
        # temporary filename
        cmd2 = [
            "lilypond",
            "--{}".format(fmt),
            "-dno-print-pages",
            "-dpreview",
            "-o{}".format(img_stem),
            "-",
        ]
        try:
            ps2 = subprocess.run(cmd2, input=ps1.stdout, check=False)
            if ps2.returncode != 0:
                LOGGER.error("Command {} failed with code {}".format(
                    cmd2, ps2.returncode))
                return None
        except FileNotFoundError as f:
            LOGGER.error('Executing "{}" returned {}.'.format(
                " ".join(cmd2), f))
            return

        return img_fh.name
Exemplo n.º 4
0
def render_lilypond(part, fmt='png', out_fn=None):
    if fmt not in ('png', 'pdf'):
        print('warning: unsupported output format')
        return

    prvw_sfx = '.preview.{}'.format(fmt)
    
    with TemporaryFile() as xml_fh, \
         NamedTemporaryFile(suffix=prvw_sfx, delete=False) as img_fh:

        # save part to musicxml in file handle xml_fh
        save_musicxml(part, xml_fh)
        # rewind read pointer of file handle before we pass it to musicxml2ly
        xml_fh.seek(0)

        img_stem = img_fh.name[:-len(prvw_sfx)]

        # convert musicxml to lilypond format (use stdout pipe)
        cmd1 = ['musicxml2ly', '-o-', '-']
        try:
            ps1 = subprocess.run(cmd1, stdin=xml_fh, stdout=subprocess.PIPE)
            if ps1.returncode != 0:
                LOGGER.error('Command {} failed with code {}'
                             .format(cmd1, ps1.returncode))
                return
        except FileNotFoundError as f:
            LOGGER.error('Executing "{}" returned  {}.'
                         .format(' '.join(cmd1), f))
            return
        
        # convert lilypond format (read from pipe of ps1) to image, and save to
        # temporary filename
        cmd2 = ['lilypond', '--{}'.format(fmt),
                '-dno-print-pages', '-dpreview',
                '-o{}'.format(img_stem), '-']
        try:
            ps2 = subprocess.run(cmd2, input=ps1.stdout)
            if ps2.returncode != 0:
                LOGGER.error('Command {} failed with code {}'
                             .format(cmd2, ps2.returncode))
                return
        except FileNotFoundError as f:
            LOGGER.error('Executing "{}" returned {}.'
                         .format(' '.join(cmd2), f))
            return

        return img_fh.name
Exemplo n.º 5
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)
Exemplo n.º 6
0
 def test_import_export(self):
     for fn in MUSICXML_IMPORT_EXPORT_TESTFILES:
         with open(fn) as f:
             parts = load_musicxml(f, validate=False)
             result = save_musicxml(parts).decode('UTF-8')
             f.seek(0)
             target = f.read()
             equal = target == result
             if not equal:
                 show_diff(result, target)
             msg = "Import and export of MusicXML of file {} does not yield identical result".format(fn)
             self.assertTrue(equal, msg)
Exemplo n.º 7
0
score = partitura.load_musicxml(getattr(args, "in"))

to_purge = []
added_notes = []

for note in score.timeline.iter_all(partitura.score.Note,
                                    include_subclasses=True):
    if note.step == "D" and note not in added_notes and note.duration > 1:
        containing_timepoint = next(
            iter([
                point for point in score.timeline.points
                if note in point.starting_objects[partitura.score.Note]
            ]))
        for i in range(0, note.duration):
            new_d = copy.copy(note)
            new_d._sym_dur = None
            new_d.start = score.timeline.get_or_add_point(note.start.t + i)
            new_d.end = score.timeline.get_or_add_point(new_d.start.t + 1)
            score.timeline.add(new_d, containing_timepoint.t + i,
                               containing_timepoint.t + i + 1)
            to_purge.append(note)
            added_notes.append(new_d)

for note in to_purge:
    score.timeline.remove(note)

f = open(args.out, "wb")
partitura.save_musicxml(score, f)
f.close()