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])])
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)
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')
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))
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))
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)
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)
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')
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')
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)
def testBigCorpus(self): from music21 import corpus, converter #import time #print time.time() # 8.3 sec from pickle; 10.3 sec for forceSource... s = corpus.parse('beethoven/opus133') #, forceSource = True) #print time.time() # purePython: 33! sec; cPickle: 25 sec #data = converter.freezeStr(s, fmt='pickle') #print time.time() # cPickle: 5.5 sec! sf = freezeThaw.StreamFreezer(s, fastButUnsafe=True) data = sf.writeStr() #print time.time() # purePython: 9 sec; cPickle: 3.8 sec! unused_s2 = converter.thawStr(data)
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)
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 })
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)
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)
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)
def stream_to_lily(the_stream, setts, the_index=None): """ Convert a :class:`Stream` object into the LilyPond string. :param the_stream: The :class:`Stream` object to convert. Refer to "List of Supported Stream Objects" below. :type the_stream: :class:`music21.stream.Stream` :param setts: A settings object. :type setts: :class:`settings.LilyPondSettings` :param the_index: If this value is not ``None`` (the default), it is used in the return value. This is for use with multiprocessing. Refer to "Return Values" below. :returns: The LilyPond string, either by itself or in a 2- or 3-tuple. Refer to "Return Values" below. :rtype: unicode string or tuple :raises: :exc:`UnidentifiedObjectError` when ``the_stream`` is not one of the supported types. ** List of Supported Stream Objects ** * :class:`music21.stream.Part` (or :class:`music21.stream.PartStaff`) (note: If ``the_stream`` is a :class:`Part` and ``the_index`` is not ``None``, a 3-tuple will be returned.) * :class:`music21.stream.Score` (NOTE: not now!!!!!!!) * :class:`music21.metadata.Metadata` * :class:`music21.layout.StaffGroup` * :class:`music21.humdrum.spineParser.MiscTandem` * :class:`music21.humdrum.spineParser.SpineComment` * :class:`music21.humdrum.spineParser.GlobalComment` ** Special Attribute-Things ** * ``lily_analysis_voice`` on a :class:`Part`. ** Return Values ** * If ``index`` is ``None``, the return value is simply a unicode string. * If ``index`` is not ``None`` and ``the_stream`` is a :class:`Part`, a 3-tuple with the value of ``index``, the unicode string, and the 8-character unicode string used as the part name. * If ``index`` is not ``None`` but ``the_stream`` is not a :class:`Part`, a 2-tuple with the value of ``index`` and the unicode string. """ if isinstance(the_stream, str): the_stream = converter.thawStr(the_stream) obj_type = type(the_stream) post = None part_name = None #if obj_type == stream.Score: #post = ScoreMaker(the_stream, setts).get_lilypond() if obj_type == stream.Part or obj_type == stream.PartStaff: post = part_to_lily(the_stream, setts) part_name = post[:8] elif obj_type == metadata.Metadata: post = metadata_to_lily(the_stream, setts) elif obj_type == layout.StaffGroup: # TODO: Figure out how to use this by reading documentation post = u'' elif obj_type == humdrum.spineParser.MiscTandem: # http://mit.edu/music21/doc/html/moduleHumdrumSpineParser.html # Is there really nothing we can use this for? Seems like these exist only to help # the music21 developers. post = u'' elif obj_type == humdrum.spineParser.GlobalReference: # http://mit.edu/music21/doc/html/moduleHumdrumSpineParser.html # These objects have lots of metadata, so they'd be pretty useful! post = u'' elif obj_type == humdrum.spineParser.GlobalComment: # http://mit.edu/music21/doc/html/moduleHumdrumSpineParser.html # These objects have lots of metadata, so they'd be pretty useful! post = u'' elif obj_type == layout.ScoreLayout: # TODO: this (as in Lassus duos) post = u'' elif obj_type == text.TextBox: # TODO: this (as in Lassus duos) post = u'' else: # Anything else, we don't know what it is! msg = u'Unknown object in Stream; type is %s.' % type(the_stream) # DEBUG print(msg) # DEBUG post = u'' #raise UnidentifiedObjectError(msg) if the_index is not None and (obj_type == stream.Part or obj_type == stream.PartStaff): return (the_index, post, part_name) elif the_index is not None: return (the_index, post) else: return post
def stream_to_lily(the_stream, setts, the_index=None): """ Convert a :class:`Stream` object into the LilyPond string. :param the_stream: The :class:`Stream` object to convert. Refer to "List of Supported Stream Objects" below. :type the_stream: :class:`music21.stream.Stream` :param setts: A settings object. :type setts: :class:`settings.LilyPondSettings` :param the_index: If this value is not ``None`` (the default), it is used in the return value. This is for use with multiprocessing. Refer to "Return Values" below. :returns: The LilyPond string, either by itself or in a 2- or 3-tuple. Refer to "Return Values" below. :rtype: unicode string or tuple :raises: :exc:`UnidentifiedObjectError` when ``the_stream`` is not one of the supported types. ** List of Supported Stream Objects ** * :class:`music21.stream.Part` (or :class:`music21.stream.PartStaff`) (note: If ``the_stream`` is a :class:`Part` and ``the_index`` is not ``None``, a 3-tuple will be returned.) * :class:`music21.stream.Score` (NOTE: not now!!!!!!!) * :class:`music21.metadata.Metadata` * :class:`music21.layout.StaffGroup` * :class:`music21.humdrum.spineParser.MiscTandem` * :class:`music21.humdrum.spineParser.SpineComment` * :class:`music21.humdrum.spineParser.GlobalComment` ** Special Attribute-Things ** * ``lily_analysis_voice`` on a :class:`Part`. ** Return Values ** * If ``index`` is ``None``, the return value is simply a unicode string. * If ``index`` is not ``None`` and ``the_stream`` is a :class:`Part`, a 3-tuple with the value of ``index``, the unicode string, and the 8-character unicode string used as the part name. * If ``index`` is not ``None`` but ``the_stream`` is not a :class:`Part`, a 2-tuple with the value of ``index`` and the unicode string. """ if isinstance(the_stream, str): the_stream = converter.thawStr(the_stream) obj_type = type(the_stream) post = None part_name = None #if obj_type == stream.Score: #post = ScoreMaker(the_stream, setts).get_lilypond() if obj_type == stream.Part or obj_type == stream.PartStaff: post = part_to_lily(the_stream, setts) part_name = post[:8] elif obj_type == metadata.Metadata: post = metadata_to_lily(the_stream, setts) elif obj_type == layout.StaffGroup: # TODO: Figure out how to use this by reading documentation post = u'' elif obj_type == humdrum.spineParser.MiscTandem: # http://mit.edu/music21/doc/html/moduleHumdrumSpineParser.html # Is there really nothing we can use this for? Seems like these exist only to help # the music21 developers. post = u'' elif obj_type == humdrum.spineParser.GlobalReference: # http://mit.edu/music21/doc/html/moduleHumdrumSpineParser.html # These objects have lots of metadata, so they'd be pretty useful! post = u'' elif obj_type == humdrum.spineParser.GlobalComment: # http://mit.edu/music21/doc/html/moduleHumdrumSpineParser.html # These objects have lots of metadata, so they'd be pretty useful! post = u'' elif obj_type == layout.ScoreLayout: # TODO: this (as in Lassus duos) post = u'' elif obj_type == text.TextBox: # TODO: this (as in Lassus duos) post = u'' else: # Anything else, we don't know what it is! msg = u'Unknown object in Stream; type is %s.' % type( the_stream) # DEBUG print(msg) # DEBUG post = u'' #raise UnidentifiedObjectError(msg) if the_index is not None and (obj_type == stream.Part or obj_type == stream.PartStaff): return (the_index, post, part_name) elif the_index is not None: return (the_index, post) else: return post