#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This module contains methods for importing and exporting symbolic music formats.
"""
from typing import Union
from .importmusicxml import load_musicxml
from .importmidi import load_score_midi, load_performance_midi
from .musescore import load_via_musescore
from .importmatch import load_match
from .importmei import load_mei
from .importkern import load_kern
from .importparangonada import load_parangonada_csv
from .exportparangonada import save_parangonada_csv
from .importmusic21 import load_music21
from partitura.utils.misc import (
deprecated_alias,
deprecated_parameter,
PathLike,
)
from partitura.score import Score, Part, merge_parts
from partitura.performance import Performance
class NotSupportedFormatError(Exception):
pass
[docs]@deprecated_alias(score_fn="filename")
@deprecated_parameter("ensure_list")
def load_score(filename: PathLike, force_note_ids="keep") -> Score:
"""
Load a score format supported by partitura. Currently the accepted formats
are MusicXML, MIDI, Kern and MEI, plus all formats for which
MuseScore has support import-support (requires MuseScore 3).
Parameters
----------
filename : str or file-like object
Filename of the score to parse, or a file-like object
force_note_ids : (None, bool or "keep")
When True each Note in the returned Part(s) will have a newly
assigned unique id attribute. Existing note id attributes in
the input file will be discarded. If 'keep', only notes without
a note id will be assigned one. If None or False, the notes in the
resulting Part(s) will have an id only if the input file has ids for
the notes.
Returns
-------
scr: :class:`partitura.score.Score`
A score instance.
"""
part = None
# Catch exceptions
exception_dictionary = dict()
# Load MusicXML
try:
return load_musicxml(
filename=filename,
force_note_ids=force_note_ids,
)
except Exception as e:
exception_dictionary["MusicXML"] = e
# Load MIDI
try:
if (force_note_ids is None) or (not force_note_ids):
assign_note_ids = False
else:
assign_note_ids = True
return load_score_midi(
filename=filename,
assign_note_ids=assign_note_ids,
)
except Exception as e:
exception_dictionary["MIDI"] = e
# Load MEI
try:
return load_mei(filename=filename)
except Exception as e:
exception_dictionary["MEI"] = e
# Load Kern
try:
return load_kern(
filename=filename,
force_note_ids=force_note_ids,
)
except Exception as e:
exception_dictionary["Kern"] = e
# Load MuseScore
try:
return load_via_musescore(
filename=filename,
force_note_ids=force_note_ids,
)
except Exception as e:
exception_dictionary["MuseScore"] = e
try:
# Load the score information from a Matchfile
_, _, part = load_match(
filename=filename,
create_score=True,
)
except Exception as e:
exception_dictionary["matchfile"] = e
if part is None:
for score_format, exception in exception_dictionary.items():
print(f"Error loading score as {score_format}:")
print(exception)
raise NotSupportedFormatError
[docs]def load_score_as_part(filename: PathLike) -> Part:
"""
load part helper function:
Load a score format supported by partitura and
merge the result in a single part
Parameters
----------
filename : str or file-like object
Filename of the score to parse, or a file-like object
Returns
-------
part: :class:`partitura.score.Part`
A part instance.
"""
scr = load_score(filename)
part = merge_parts(scr.parts)
return part
# alias
lp = load_score_as_part