Example #1
0
    def testBasicJ(self):
        from music21 import stream, note, converter

        p1 = stream.Part()
        for m in range(3):
            m = stream.Measure()
            for i in range(4):
                m.append(note.Note('C4'))
            p1.append(m)

        p2 = stream.Part()
        for m in range(3):
            m = stream.Measure()
            for i in range(4):
                m.append(note.Note('G4'))
            p2.append(m)

        s = stream.Score()
        s.insert(0, p1)
        s.insert(0, p2)
        #s.show()

        temp = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(temp)
        self.assertEqual(len(sPost.parts), 2)
        self.assertEqual(len(sPost.parts[0].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.parts[1].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.flat.notes), 24)
Example #2
0
    def testBasicD(self):
        from music21 import stream, note, converter, spanner
        import copy

        s = stream.Stream()
        n1 = note.Note('d2', quarterLength=2.0)
        n2 = note.Note('e2', quarterLength=2.0)
        sp = spanner.Slur(n1, n2)

        s.append(n1)
        s.append(n2)
        s.append(sp)

        # the deepcopy is what creates the bug in the preservation of a weakref

        #temp = converter.freezeStr(s)

        sCopy = copy.deepcopy(s)
        temp = converter.freezeStr(sCopy)

        post = converter.thawStr(temp)
        self.assertEqual(len(post.notes), 2)
        self.assertEqual(str(post.notes[0].pitch), 'D2')
        spPost = post.spanners[0]
        self.assertEqual(spPost.getSpannedElements(), [
                         post.notes[0], post.notes[1]])
        self.assertEqual(spPost.getSpannedElementIds(), [
                         id(post.notes[0]), id(post.notes[1])])
Example #3
0
    def testBasicD(self):
        from music21 import stream, note, converter, spanner
        import copy

        s = stream.Stream()
        n1 = note.Note('d2', quarterLength=2.0)
        n2 = note.Note('e2', quarterLength=2.0)
        sp = spanner.Slur(n1, n2)

        s.append(n1)
        s.append(n2)
        s.append(sp)

        # the deepcopy is what creates the bug in the preservation of a weakref

        #temp = converter.freezeStr(s)

        sCopy = copy.deepcopy(s)
        temp = converter.freezeStr(sCopy)

        post = converter.thawStr(temp)
        self.assertEqual(len(post.notes), 2)
        self.assertEqual(str(post.notes[0].pitch), 'D2')
        spPost = post.spanners[0]
        self.assertEqual(spPost.getSpannedElements(), [post.notes[0], post.notes[1]])
        self.assertEqual(spPost.getSpannedElementIds(), [id(post.notes[0]), id(post.notes[1])])
Example #4
0
    def testBasicJ(self):
        from music21 import stream, note, converter

        p1 = stream.Part()
        for m in range(3):
            m = stream.Measure()
            for i in range(4):
                m.append(note.Note('C4'))
            p1.append(m)

        p2 = stream.Part()
        for m in range(3):
            m = stream.Measure()
            for i in range(4):
                m.append(note.Note('G4'))
            p2.append(m)

        s = stream.Score()
        s.insert(0, p1)
        s.insert(0, p2)
        # s.show()

        temp = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(temp)
        self.assertEqual(len(sPost.parts), 2)
        self.assertEqual(len(sPost.parts[0].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.parts[1].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.flat.notes), 24)
Example #5
0
    def testPickleMidi(self):
        from music21 import converter
        a = str(common.getSourceFilePath() / 'midi' / 'testPrimitive' /
                'test03.mid')

        # a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi'
        c = converter.parse(a)
        f = converter.freezeStr(c)
        d = converter.thawStr(f)
        self.assertEqual(d[1][20].volume._client.__class__.__name__, 'weakref')
Example #6
0
    def testBasicE(self):
        from music21 import corpus, converter
        s = corpus.parse('bwv66.6')

        temp = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(temp)
        # sPost.show()
        self.assertEqual(len(s.flat.notes), len(sPost.flat.notes))

        self.assertEqual(len(s.parts[0].notes), len(sPost.parts[0].notes))
Example #7
0
    def testBasicE(self):
        from music21 import corpus, converter
        s = corpus.parse('bwv66.6')

        temp = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(temp)
        #sPost.show()
        self.assertEqual(len(s.flat.notes), len(sPost.flat.notes))

        self.assertEqual(len(s.parts[0].notes), len(sPost.parts[0].notes))
Example #8
0
    def testFreezeThaw(self):
        from music21 import converter, stream

        b = Barline()
        self.assertNotIn('StyleMixin', b.classes)
        s = stream.Stream([b])
        data = converter.freezeStr(s, fmt='pickle')
        s2 = converter.thawStr(data)
        thawedBarline = s2[0]
        # Previously, raised AttributeError
        self.assertEqual(thawedBarline.hasStyleInformation, False)
Example #9
0
 def testJSONPickleSpanner(self):
     from music21 import converter, note, stream, spanner
     n1 = note.Note('C')
     n2 = note.Note('D')
     s1 = stream.Stream()
     sp = spanner.Line([n1, n2])
     s1.insert(0, sp)
     s1.append(n1)
     s1.append(n2)
     frozen = converter.freezeStr(s1, 'jsonPickle')
     # print(frozen)
     unused_thawed = converter.thawStr(frozen)
Example #10
0
    def testBasicC(self):
        from music21 import stream, note, converter

        s = stream.Stream()
        n1 = note.Note('d2', quarterLength=2.0)
        s.append(n1)
        s.append(note.Note('g~6', quarterLength=.25))

        temp = converter.freezeStr(s)
        post = converter.thawStr(temp)
        self.assertEqual(len(post.notes), 2)
        self.assertEqual(str(post.notes[0].pitch), 'D2')
Example #11
0
    def testPickleMidi(self):
        from music21 import converter
        a = str(common.getSourceFilePath()
                         / 'midi'
                         / 'testPrimitive'
                         / 'test03.mid')

        #a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi'
        c = converter.parse(a)
        f = converter.freezeStr(c)
        d = converter.thawStr(f)
        self.assertEqual(d[1][20].volume._client.__class__.__name__, 'weakref')
Example #12
0
    def testBasicC(self):
        from music21 import stream, note, converter

        s = stream.Stream()
        n1 = note.Note('d2', quarterLength=2.0)
        s.append(n1)
        s.append(note.Note('g~6', quarterLength=.25))

        temp = converter.freezeStr(s)
        post = converter.thawStr(temp)
        self.assertEqual(len(post.notes), 2)
        self.assertEqual(str(post.notes[0].pitch), 'D2')
Example #13
0
 def testJSONPickleSpanner(self):
     from music21 import converter, note, stream, spanner
     n1 = note.Note('C')
     n2 = note.Note('D')
     s1 = stream.Stream()
     sp = spanner.Line([n1, n2])
     s1.insert(0, sp)
     s1.append(n1)
     s1.append(n2)
     frozen = converter.freezeStr(s1, 'jsonPickle')
     #print frozen
     unused_thawed = converter.thawStr(frozen)
Example #14
0
 def testBasicG(self):
     from music21 import stream, note, converter, spanner
     
     s = stream.Score()
     s.repeatAppend(note.Note('G4'), 5)
     for i, syl in enumerate(['se-', 'ri-', 'al-', 'iz-', 'ing']):
         s.notes[i].addLyric(syl)
     s.append(spanner.Slur(s.notes[0], s.notes[-1]))
         
     data = converter.freezeStr(s, fmt='jsonpickle')
     #print data
     sPost = converter.unfreezeStr(data)
     self.assertEqual(len(sPost.notes), 5)
Example #15
0
    def testBasicF(self):
        from music21 import stream, note, converter, spanner

        s = stream.Score()
        s.repeatAppend(note.Note('G4'), 5)
        for i, syl in enumerate(['se-', 'ri-', 'al-', 'iz-', 'ing']):
            s.notes[i].addLyric(syl)
        s.append(spanner.Slur(s.notes[0], s.notes[-1]))

        # file writing
        #converter.freeze(s, fmt='pickle', fp='/_scratch/test.p')

        data = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(data)
        self.assertEqual(len(sPost.notes), 5)
Example #16
0
    def testBasicF(self):
        from music21 import stream, note, converter, spanner

        s = stream.Score()
        s.repeatAppend(note.Note('G4'), 5)
        for i, syl in enumerate(['se-', 'ri-', 'al-', 'iz-', 'ing']):
            s.notes[i].addLyric(syl)
        s.append(spanner.Slur(s.notes[0], s.notes[-1]))

        # file writing
        #converter.freeze(s, fmt='pickle', fp='/_scratch/test.p')

        data = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(data)
        self.assertEqual(len(sPost.notes), 5)
Example #17
0
def process_handler(body, context):
    try:
        print(body)
        input_key = body["key"]
        output_key = body["output-key"]
        part_index = body["part-index"]

        args = body.get("args", {})
    except Exception as e:
        print("error getting input params {}".format(str(e)))
        raise

    try:
        input_file = get_s3(input_key)
        input_bytes = input_file["Body"].read()
        sf = converter.thawStr(input_bytes)
        num_of_parts = len(sf.parts)
        max_index = num_of_parts - 1
    except Exception as e:
        msg = "error processing input file {}".format(str(e))
        print(msg)
        raise

    try:
        print("running part # {} (index {}) of {} (max index {})".format(
            part_index + 1, part_index, num_of_parts, max_index)
        )
        main(sf, part_index, args=args)
    except Exception as e:
        msg = "error converting file {}".format(str(e))
        print(msg)
        raise
    else:
        if part_index == max_index:
            print("finished processing last part, writing output")
            output_data = write_file_obj(sf)
            put_s3(output_data, output_key)
        else:
            print("still more parts to process, running next lambda")
            output_data = converter.freezeStr(sf, fmt="pickle")
            put_s3(output_data, input_key)
            run_process_lambda({
                "args": args,
                "key": input_key,
                "output-key": output_key,
                "part-index": part_index + 1
            })
Example #18
0
    def testSpannerSerializationOfNotesNotInPickle(self):
        '''
        test to see if spanners serialize properly if they
        contain notes not in the pickle...
        '''
        from music21 import stream, spanner, converter
        from music21 import note
        n1 = note.Note("D4")
        n2 = note.Note("E4")
        n3 = note.Note("F4")
        slur1 = spanner.Slur([n1, n2])
        s = stream.Part()
        s.insert(0, n3)
        s.insert(0, slur1)
        data = converter.freezeStr(s, fmt='pickle')

        unused_s2 = converter.thawStr(data)
Example #19
0
    def testSpannerSerializationOfNotesNotInPickle(self):
        '''
        test to see if spanners serialize properly if they
        contain notes not in the pickle...
        '''
        from music21 import stream, spanner, converter
        from music21 import note
        n1 = note.Note("D4")
        n2 = note.Note("E4")
        n3 = note.Note("F4")
        slur1 = spanner.Slur([n1, n2])
        s = stream.Part()
        s.insert(0, n3)
        s.insert(0, slur1)
        data = converter.freezeStr(s, fmt='pickle')

        unused_s2 = converter.thawStr(data)
Example #20
0
    def testBasicI(self):
        from music21 import stream, note, converter

        p1 = stream.Part()
        p1.repeatAppend(note.Note('C4'), 12)
        p1.makeMeasures(inPlace=True)
        p2 = stream.Part()
        p2.repeatAppend(note.Note('G4'), 12)
        p2.makeMeasures(inPlace=True)
        s = stream.Score()
        s.insert(0, p1)
        s.insert(0, p2)
        #s.show()

        temp = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(temp)
        self.assertEqual(len(sPost.parts), 2)
        self.assertEqual(len(sPost.parts[0].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.parts[1].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.flat.notes), 24)
Example #21
0
    def testBasicI(self):
        from music21 import stream, note, converter

        p1 = stream.Part()
        p1.repeatAppend(note.Note('C4'), 12)
        p1.makeMeasures(inPlace=True)
        p2 = stream.Part()
        p2.repeatAppend(note.Note('G4'), 12)
        p2.makeMeasures(inPlace=True)
        s = stream.Score()
        s.insert(0, p1)
        s.insert(0, p2)
        # s.show()

        temp = converter.freezeStr(s, fmt='pickle')
        sPost = converter.thawStr(temp)
        self.assertEqual(len(sPost.parts), 2)
        self.assertEqual(len(sPost.parts[0].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.parts[1].getElementsByClass('Measure')), 3)
        self.assertEqual(len(sPost.flat.notes), 24)
Example #22
0
def invoke_handler(event, context):
    try:
        query_string = event.get('queryStringParameters')
        args = query_string if query_string else {}
        print(args)
        input_key = "input/{}".format(context.aws_request_id)
        output_key = args.get("output-key", "output.xml")
    except Exception as e:
        msg = "error {}".format(str(e))
        print(msg)
        return error(msg)

    try:
        print("putting input file in s3")
        input_bytes = event.get('body', b"")
        sf = converter.parse(input_bytes)
        frozen = converter.freezeStr(sf, fmt="pickle")
        put_s3(frozen, input_key)
    except Exception as e:
        msg = "error putting input file in s3 {}".format(str(e))
        print(msg)
        return error(msg)

    try:
        run_process_lambda({
            "args": args,
            "key": input_key,
            "output-key": output_key,
            "part-index": start_index
        })
    except Exception as e:
        msg = "error invoking lambda {}".format(str(e))
        print(msg)
        return error(msg)

    return success({
        "output_key": output_key,
        "bucket": BUCKET,
        "input_key": input_key
    })
Example #23
0
    def run(self):
        """
        Process all the parts! Prepare a score!
        """

        # Things Before Parts:
        # Our mark! // Version // Paper size
        post = [u'%% LilyPond output from music21 via "outputlilypond"\n'
                u'\\version "%s"\n'
                u'\n'
                u'\\paper {\n'
                u'\t#(set-paper-size "%s")\n'
                u'\t#(define left-margin (* 1.5 cm))\n'  # TODO: this should be a setting
                u'}\n\n' % (self._setts.get_property('lilypond_version'),
                            self._setts.get_property('paper_size'))]

        # Parts:
        # Initialize the length of finished "parts" (maybe they're other things, too, like Metadata
        # or whatever... doesn't really matter).
        self._finished_parts = [None for i in xrange(len(self._score))]
        self._setts._parts_in_this_score = [None for i in xrange(len(self._score))]

        # Go through the possible parts and see what we find.
        for i in xrange(len(self._score)):
            if isinstance(self._score[i], stream.Part):
                if hasattr(self._score[i], u'lily_analysis_voice') and \
                self._score[i].lily_analysis_voice is True:
                    self._setts._analysis_notation_parts.append(i)
                self._pool.apply_async(functions.stream_to_lily,
                                       (converter.freezeStr(self._score[i]), self._setts, i),
                                       callback=self.callback)
            else:
                self._finished_parts[i] = functions.stream_to_lily(self._score[i], self._setts)

        # Wait for the multiprocessing to finish
        self._pool.close()
        self._pool.join()
        del self._pool

        # Append the parts to the score we're building. In the future, it'll be important to
        # re-arrange the parts if necessary, or maybe to filter things, so we'll keep everything
        # in this supposedly efficient loop.
        for i in xrange(len(self._finished_parts)):
            if self._finished_parts[i] != u'' and self._finished_parts[i] is not None:
                post.append(self._finished_parts[i] + u'\n')

        # Things After Parts
        # Output the \score{} block
        post.append(u'\\score {\n\t\\new StaffGroup\n\t<<\n')
        for each_part in self._setts._parts_in_this_score:
            if each_part is None:
                continue
            elif each_part in self._setts._analysis_notation_parts:
                post.extend([u'\t\t\\new VisAnnotation = "', each_part, u'" \\' + each_part + u'\n'])
            else:
                post.extend([u'\t\t\\new Staff = "', each_part, u'" \\' + each_part + u'\n'])
        post.append(u'\t>>\n')

        # Output the \layout{} block
        post.append(u'\t\\layout{\n')
        if self._setts.get_property('indent') is not None:
            post.extend([u'\t\tindent = ', self._setts.get_property('indent'), u'\n'])
        post.append("""\t\t% VisAnnotation Context
\t\t\\context
\t\t{
\t\t\t\\type "Engraver_group"
\t\t\t\\name VisAnnotation
\t\t\t\\alias Staff
\t\t\t\\consists "Output_property_engraver"
\t\t\t\\consists "Script_engraver"
\t\t\t\\consists "Text_engraver"
\t\t\t\\consists "Axis_group_engraver"
\t\t\t\\consists "Instrument_name_engraver"
\t\t}
\t\t% End VisAnnotation Context
\t\t
\t\t% Modify "StaffGroup" context to accept VisAnnotation context.
\t\t\\context
\t\t{
\t\t\t\\StaffGroup
\t\t\t\\accepts VisAnnotation
\t\t}
\t}\n}\n
""")

        self._final_result = u''.join(post)

        # Return the "finished score"
        return self._final_result
Example #24
0
 def xtestBasicK(self):
     # this fails due to finding a weakref
     from music21 import corpus, converter
     s = corpus.parse('beethoven/opus133')
     data = converter.freezeStr(s, fmt='pickle')
Example #25
0
    def run(self):
        """
        Process all the parts! Prepare a score!
        """

        # Things Before Parts:
        # Our mark! // Version // Paper size
        post = [u'%% LilyPond output from music21 via "outputlilypond"\n'
                u'\\version "%s"\n'
                u'\n'
                u'\\paper {\n'
                u'\t#(set-paper-size "%s")\n'
                u'\t#(define left-margin (* 1.5 cm))\n'  # TODO: this should be a setting
                u'}\n\n' % (self._setts.get_property('lilypond_version'),
                            self._setts.get_property('paper_size'))]

        # Parts:
        # Initialize the length of finished "parts" (maybe they're other things, too, like Metadata
        # or whatever... doesn't really matter).
        self._finished_parts = [None for i in xrange(len(self._score))]
        self._setts._parts_in_this_score = [None for i in xrange(len(self._score))]

        # Go through the possible parts and see what we find.
        for i in xrange(len(self._score)):
            if isinstance(self._score[i], stream.Part):
                if hasattr(self._score[i], u'lily_analysis_voice') and \
                self._score[i].lily_analysis_voice is True:
                    self._setts._analysis_notation_parts.append(i)
                self._pool.apply_async(functions.stream_to_lily,
                                       (converter.freezeStr(self._score[i]), self._setts, i),
                                       callback=self.callback)
            else:
                self._finished_parts[i] = functions.stream_to_lily(self._score[i], self._setts)

        # Wait for the multiprocessing to finish
        self._pool.close()
        self._pool.join()
        del self._pool

        # Append the parts to the score we're building. In the future, it'll be important to
        # re-arrange the parts if necessary, or maybe to filter things, so we'll keep everything
        # in this supposedly efficient loop.
        for i in xrange(len(self._finished_parts)):
            if self._finished_parts[i] != u'' and self._finished_parts[i] is not None:
                post.append(self._finished_parts[i] + u'\n')

        # Things After Parts
        # Output the \score{} block
        post.append(u'\\score {\n\t\\new StaffGroup\n\t<<\n')
        for each_part in self._setts._parts_in_this_score:
            if each_part is None:
                continue
            elif each_part in self._setts._analysis_notation_parts:
                post.extend([u'\t\t\\new VisAnnotation = "', each_part, u'" \\' + each_part + u'\n'])
            else:
                post.extend([u'\t\t\\new Staff = "', each_part, u'" \\' + each_part + u'\n'])
        post.append(u'\t>>\n')

        # Output the \layout{} block
        post.append(u'\t\\layout{\n')
        if self._setts.get_property('indent') is not None:
            post.extend([u'\t\tindent = ', self._setts.get_property('indent'), u'\n'])
        post.append("""\t\t% VisAnnotation Context
\t\t\\context
\t\t{
\t\t\t\\type "Engraver_group"
\t\t\t\\name VisAnnotation
\t\t\t\\alias Staff
\t\t\t\\consists "Output_property_engraver"
\t\t\t\\consists "Script_engraver"
\t\t\t\\consists "Text_engraver"
\t\t\t\\consists "Axis_group_engraver"
\t\t\t\\consists "Instrument_name_engraver"
\t\t}
\t\t% End VisAnnotation Context
\t\t
\t\t% Modify "StaffGroup" context to accept VisAnnotation context.
\t\t\\context
\t\t{
\t\t\t\\StaffGroup
\t\t\t\\accepts VisAnnotation
\t\t}
\t}\n}\n
""")

        self._final_result = u''.join(post)

        # Return the "finished score"
        return self._final_result