# drumscript/notation_generator/xml_exporter.py
"""
Module for rendering the drum score to MusicXML format.
MusicXML is the industry standard for sharing sheet music between
applications like Sibelius, Guitar Pro, Logic, Cubase, or MuseScore.
"""
from pathlib import Path
[docs]
def export_xml(score, output_path=None):
"""
Exports a generated DrumScript score object to a MusicXML file.
:param score: The music21.stream.Score object generated by build_score().
:type score: object
:param output_path: The exact file path to save the XML. Defaults to 'drumscript.xml' in CWD.
:type output_path: str, optional
:return: The path to the generated XML file.
:rtype: str
"""
# 1. Handle output routing
if output_path is None:
# xml_path = Path.cwd() / "drum_score.xml"
xml_path = Path.cwd() / "drumscript.xml"
else:
xml_path = Path(output_path)
# Ensure the parent directory of the requested file path exists
xml_path.parent.mkdir(parents=True, exist_ok=True)
output_path = str(xml_path)
print(f"Generating MusicXML: {output_path}")
# 2. Use music21's built-in MusicXML writer
try:
# music21's score.write() natively handles the XML translation
score.write("musicxml", fp=output_path)
print(f"MusicXML successfully saved to: {output_path}")
return output_path
except Exception as e:
print(f"Failed to generate MusicXML: {e}")
return None
# --------------------------------------------------------------------------uncomment during testing
# from datetime import datetime
# print("\n# ------------------------------------------------------------------------------------")
# datetimestamp = datetime.now()
# print(f'\ndate/time: {datetimestamp}')
# --------------------------------------------------------------------------------------------------