espnet2.fileio.score_scp.XMLWriter
espnet2.fileio.score_scp.XMLWriter
class espnet2.fileio.score_scp.XMLWriter(outdir: Path | str, scpfile: Path | str)
Bases: object
Writer class for ‘midi.scp’.
This class is responsible for writing XML representations of musical scores to specified files and maintaining a corresponding SCP (score control protocol) file that maps keys to file paths.
dir
The output directory where XML files will be stored.
- Type: Path
fscp
The file object for writing the SCP file.
- Type: TextIOWrapper
data
A dictionary that maps keys to their corresponding XML file paths.
Type: dict
Parameters:
- outdir (Union *[*Path , str ]) – The directory where XML files will be saved.
- scpfile (Union *[*Path , str ]) – The path of the SCP file to be written.
####### Examples
key1 /some/path/a.musicxml key2 /some/path/b.musicxml key3 /some/path/c.musicxml key4 /some/path/d.musicxml …
>>> writer = XMLWriter('./data/', './data/xml.scp')
>>> writer['aa'] = xml_obj
>>> writer['bb'] = xml_obj
- Raises:AssertionError – If the value provided does not contain exactly four elements (lyrics, notes, segmentations, and tempo) when using the __setitem__ method.
NOTE
Ensure that the music21 library is installed, as it is required for creating and writing XML files.
close()
Writer class for ‘midi.scp’.
This class allows for writing MusicXML files and generating a corresponding SCP (script) file that maps keys to their respective MusicXML file paths. The data is organized in a way that facilitates easy access and storage.
Attributes: : dir (Path): The directory where MusicXML files will be stored. fscp (TextIOWrapper): The file handle for the SCP file. data (dict): A dictionary mapping keys to their corresponding MusicXML paths.
Args: : outdir (Union[Path, str]): The output directory for MusicXML files. scpfile (Union[Path, str]): The path for the SCP file to be created.
Examples: : key1 /some/path/a.musicxml key2 /some/path/b.musicxml key3 /some/path/c.musicxml key4 /some/path/d.musicxml … <br/>
>>> writer = XMLWriter('./data/', './data/xml.scp') >>> writer['aa'] = xml_obj >>> writer['bb'] = xml_obj
Raises: : AssertionError: If the provided value tuple does not contain exactly four elements when using __setitem__.
Note: : The XML values provided to __setitem__ should include lyrics, notes, segmentations, and tempo, in that order.
get_path(key)
Retrieve the file path associated with a given key.
This method returns the path to the XML file that corresponds to the specified key in the internal data dictionary.
- Parameters:key (str) – The key whose associated file path is to be retrieved.
- Returns: The file path associated with the given key.
- Return type: str
- Raises:KeyError – If the key does not exist in the data dictionary.
####### Examples
>>> writer = XMLWriter('./data/', './data/xml.scp')
>>> writer['example'] = xml_obj
>>> path = writer.get_path('example')
>>> print(path) # Output: ./data/example.musicxml
NOTE
Ensure that the key exists in the data dictionary before calling this method to avoid a KeyError.