def testJSONSerializationMetadata(self): from music21.musicxml import xmlHandler from music21.musicxml import fromMxObjects from music21.musicxml import testFiles from music21 import metadata md = metadata.Metadata( title='Concerto in F', date='2010', composer='Frank', ) #environLocal.printDebug([str(md.json)]) self.assertEqual(md.composer, 'Frank') #md.jsonPrint() mdNew = metadata.Metadata() jsonString = freezeThaw.JSONFreezer(md).json freezeThaw.JSONThawer(mdNew).json = jsonString self.assertEqual(mdNew.date, '2010/--/--') self.assertEqual(mdNew.composer, 'Frank') self.assertEqual(mdNew.title, 'Concerto in F') # test getting meta data from an imported source d = xmlHandler.Document() d.read(testFiles.mozartTrioK581Excerpt) # @UndefinedVariable mxScore = d.score # get the mx score directly md = fromMxObjects.mxScoreToMetadata(mxScore) self.assertEqual(md.movementNumber, '3') self.assertEqual(md.movementName, 'Menuetto (Excerpt from Second Trio)') self.assertEqual(md.title, 'Quintet for Clarinet and Strings') self.assertEqual(md.number, 'K. 581') self.assertEqual(md.composer, 'Wolfgang Amadeus Mozart') # convert to json and see if data is still there #md.jsonPrint() mdNew = metadata.Metadata() jsonString = freezeThaw.JSONFreezer(md).json freezeThaw.JSONThawer(mdNew).json = jsonString self.assertEqual(mdNew.movementNumber, '3') self.assertEqual(mdNew.movementName, 'Menuetto (Excerpt from Second Trio)') self.assertEqual(mdNew.title, 'Quintet for Clarinet and Strings') self.assertEqual(mdNew.number, 'K. 581') self.assertEqual(mdNew.composer, 'Wolfgang Amadeus Mozart')
def parseData(self, xmlString, number=None): '''Open MusicXML data from a string.''' from music21.musicxml import xmlHandler as musicxmlHandler c = musicxmlHandler.Document() c.read(xmlString) self._mxScore = c.score # the mxScore object from the musicxml Document if len(self._mxScore) == 0: #print xmlString raise SubConverterException( 'score from xmlString (%s...) either has no parts defined or was incompletely parsed' % xmlString[:30]) self.load()
def parseFile(self, fp, number=None): ''' Open from a file path; check to see if there is a pickled version available and up to date; if so, open that, otherwise open source. ''' # return fp to load, if pickle needs to be written, fp pickle # this should be able to work on a .mxl file, as all we are doing # here is seeing which is more recent from music21 import converter from music21.musicxml import xmlHandler as musicxmlHandler musxmlDocument = musicxmlHandler.Document() environLocal.printDebug(['opening musicxml file:', fp]) # here, we can see if this is a mxl or similar archive arch = converter.ArchiveManager(fp) if arch.isArchive(): archData = arch.getData() musxmlDocument.read(archData) else: # its a file path or a raw musicxml string musxmlDocument.open(fp) # get mxScore object from .score attribute self._mxScore = musxmlDocument.score #print self._mxScore # check that we have parts if self._mxScore is None or len(self._mxScore) == 0: raise SubConverterException( 'score from file path (%s) no parts defined' % fp) # movement titles can be stored in more than one place in musicxml # manually insert file name as a title if no titles are defined if self._mxScore.get('movementTitle') == None: mxWork = self._mxScore.get('workObj') if mxWork == None or mxWork.get('workTitle') == None: junk, fn = os.path.split(fp) # set as movement title self._mxScore.set('movementTitle', fn) self.load()
def testMetadataLoadCorpus(self): from music21.musicxml import xmlHandler from music21.musicxml import testFiles as mTF from music21.musicxml import fromMxObjects document = xmlHandler.Document() document.read(mTF.mozartTrioK581Excerpt) # @UndefinedVariable mxScore = document.score # get the mx score directly md = fromMxObjects.mxScoreToMetadata(mxScore) self.assertEqual(md.movementNumber, '3') self.assertEqual(md.movementName, 'Menuetto (Excerpt from Second Trio)') self.assertEqual(md.title, 'Quintet for Clarinet and Strings') self.assertEqual(md.number, 'K. 581') # get contributors directly from Metadata interface self.assertEqual(md.composer, 'Wolfgang Amadeus Mozart') document.read(mTF.binchoisMagnificat) # @UndefinedVariable mxScore = document.score # get the mx score directly md = fromMxObjects.mxScoreToMetadata(mxScore) self.assertEqual(md.composer, 'Gilles Binchois')