diff --git a/note_seq/musicxml_parser.py b/note_seq/musicxml_parser.py index e39a035..4e76428 100644 --- a/note_seq/musicxml_parser.py +++ b/note_seq/musicxml_parser.py @@ -249,6 +249,22 @@ def _get_score(filename): def _parse(self): """Parse the uncompressed MusicXML document.""" + # Parse work title and creators + xml_work = self._score.find('work') + self.work_title = ( + getattr(xml_work.find('work-title'), 'text', '') + if xml_work is not None + else '' + ) + self.creators = [] + xml_identification = self._score.find('identification') + if xml_identification is not None: + self.creators = [ + creator.text + for creator in xml_identification.findall('creator') + if creator.text + ] + # Parse part-list xml_part_list = self._score.find('part-list') if xml_part_list is not None: diff --git a/note_seq/musicxml_parser_test.py b/note_seq/musicxml_parser_test.py index aa6dd80..a4937bc 100644 --- a/note_seq/musicxml_parser_test.py +++ b/note_seq/musicxml_parser_test.py @@ -1818,6 +1818,32 @@ def test_invalid_note_type(self): with self.assertRaises(musicxml_parser.InvalidNoteDurationTypeError): musicxml_parser.MusicXMLDocument(temp_file.name) + def test_work_title(self): + """Verify that the work/work-title element is parsed.""" + + xml = rb""" + + + + My Funny Valentine + + + Richard Rodgers + Lorenz Hart + + + """ + with tempfile.NamedTemporaryFile() as temp_file: + temp_file.write(xml) + temp_file.flush() + ns = musicxml_reader.musicxml_file_to_sequence_proto(temp_file.name) + + self.assertEqual(ns.sequence_metadata.title, 'My Funny Valentine') + self.assertEqual(ns.sequence_metadata.composers, + ['Richard Rodgers', 'Lorenz Hart']) + if __name__ == '__main__': absltest.main() diff --git a/note_seq/musicxml_reader.py b/note_seq/musicxml_reader.py index a60e75f..c09d57f 100644 --- a/note_seq/musicxml_reader.py +++ b/note_seq/musicxml_reader.py @@ -57,6 +57,12 @@ class MusicXMLDocument # Populate header. sequence.ticks_per_quarter = musicxml_document.midi_resolution + # Populate work title and creators. + if musicxml_document.work_title: + sequence.sequence_metadata.title = musicxml_document.work_title + for creator in musicxml_document.creators: + sequence.sequence_metadata.composers.append(creator) + # Populate time signatures. musicxml_time_signatures = musicxml_document.get_time_signatures() for musicxml_time_signature in musicxml_time_signatures: