espnet2.fileio.score_scp.MIDReader
espnet2.fileio.score_scp.MIDReader
class espnet2.fileio.score_scp.MIDReader(fname: ~pathlib.Path | str, add_rest: bool = True, dtype: type = <class 'numpy.int16'>)
Bases: Mapping
Reader class for ‘mid.scp’.
This class reads MIDI files and extracts the tempo and note sequences from them. The data is organized in a key-value format, where each key corresponds to a path of a MIDI file.
fname
The filename or path of the ‘mid.scp’ file.
- Type: Union[Path, str]
dtype
The data type to be used for the MIDI values (default: np.int16).
- Type: type
add_rest
A flag to indicate whether to add rest notes to the note sequence (default: True).
- Type: bool
data
A dictionary containing the key-value pairs from the ‘mid.scp’ file.
Type: dict
Parameters:
- fname (Union *[*Path , str ]) – The path to the ‘mid.scp’ file.
- add_rest (bool , optional) – Whether to include rest notes in the output. Defaults to True.
- dtype (type , optional) – The data type for MIDI values. Defaults to np.int16.
Returns: A tuple containing the tempo (int) and a list of NOTE objects : representing the notes in the MIDI file.
Return type: tuple
######### Examples
key1 /some/path/a.mid key2 /some/path/b.mid key3 /some/path/c.mid key4 /some/path/d.mid …
>>> reader = MIDReader('mid.scp')
>>> tempo, note_list = reader['key1']
- Raises:AssertionError – If the miditoolkit package is not available or if the tempo changes are not correctly identified in the MIDI file.
####### NOTE The MIDI files do not contain explicit rest notes; however, the add_rest option allows the insertion of implicit rest notes based on the timing of the notes.
get_path(key)
Retrieve the file path associated with the given key.
This method returns the file path for the specified key in the mapping. The key must exist in the internal data structure.
- Parameters:key (str) – The key for which to retrieve the file path.
- Returns: The file path associated with the provided key.
- Return type: str
- Raises:KeyError – If the key is not found in the data.
######### Examples
>>> reader = MIDReader('mid.scp')
>>> path = reader.get_path('key1')
>>> print(path) # Output: /some/path/a.mid
####### NOTE Ensure that the key exists in the mapping before calling this method to avoid a KeyError.
keys()
Reader class for ‘mid.scp’.
This class provides functionality to read MIDI files and extract relevant musical information, including tempo and note sequences. It can read a mapping of keys to MIDI file paths from a specified file, allowing easy access to the MIDI data.
fname
The path to the ‘mid.scp’ file.
- Type: Union[Path, str]
dtype
The data type for note representation (default: np.int16).
- Type: type
add_rest
Indicates whether to add rest notes into the note sequence (default: True).
- Type: bool
data
A dictionary mapping keys to MIDI file paths.
Type: dict
Parameters:
- fname (Union *[*Path , str ]) – The path to the ‘mid.scp’ file.
- add_rest (bool , optional) – Whether to include rest notes in the output. Defaults to True.
- dtype (type , optional) – The data type for note representation. Defaults to np.int16.
Returns: The tempo of the MIDI file. notes_list (list): A list of NOTE objects representing the notes.
Return type: tempo (int)
######### Examples
>>> reader = MIDReader('mid.scp')
>>> tempo, note_list = reader['key1']
>>> print(tempo)
120
>>> print(note_list)
[<__main__.NOTE object at 0x...>, <__main__.NOTE object at 0x...>, ...]
- Raises:AssertionError – If the miditoolkit package is not installed or if the MIDI file cannot be parsed.
####### NOTE The MIDReader class uses the miditoolkit package to parse MIDI files. Ensure that the package is installed for proper functionality.