espnet2.fileio.sound_scp.SoundScpWriter
espnet2.fileio.sound_scp.SoundScpWriter
class espnet2.fileio.sound_scp.SoundScpWriter(outdir: Path | str, scpfile: Path | str, format='wav', multi_columns: bool = False, output_name_format: str = '{key}.{audio_format}', output_name_format_multi_columns: str = '{key}-CH{channel}.{audio_format}', subtype: str | None = None)
Bases: object
Writer class for ‘wav.scp’.
This class allows for writing audio file paths and their corresponding sampling rates to a specified ‘wav.scp’ file. It supports writing single-channel or multi-channel audio data, as well as specifying output formats and naming conventions for the generated audio files.
dir
Directory where audio files will be saved.
- Type: Path
fscp
File handle for writing to the .scp file.
- Type: TextIOWrapper
format
The output audio format (default is ‘wav’).
- Type: str
subtype
Subtype for the audio files (e.g., ‘PCM_16’).
- Type: Optional[str]
output_name_format
Naming format for generated audio files.
- Type: str
multi_columns
If True, saves multi-channel data as multiple monaural audio files.
- Type: bool
output_name_format
Naming format for generated audio files when multi_columns is enabled.
- Type: str
data
Dictionary storing the mapping of keys to file paths.
Type: dict
Parameters:
- outdir (Union *[*Path , str ]) – Directory where audio files will be saved.
- scpfile (Union *[*Path , str ]) – Path to the output ‘wav.scp’ file.
- format (str) – The output audio format (default is ‘wav’).
- multi_columns (bool) – Save multi-channel data as multiple monaural audio files (default is False).
- output_name_format (str) – The naming format of generated audio files (default is ‘{key}.{audio_format}’).
- output_name_format_multi_columns (str) – The naming format of generated audio files when multi_columns is given (default is ‘{key}-CH{channel}.{audio_format}’).
- dtype (Optional) – Data type of the audio signal (default is None).
- subtype (Optional *[*str ]) – Subtype for the audio files (default is None).
####### Examples
>>> writer = SoundScpWriter('./data/', './data/wav.scp')
>>> writer['aa'] = 16000, numpy_array
>>> writer['bb'] = 16000, numpy_array
aa ./data/aa.wav bb ./data/bb.wav
>>> writer = SoundScpWriter(
'./data/', './data/feat.scp', multi_columns=True,
)
>>> numpy_array.shape
(100, 2)
>>> writer['aa'] = 16000, numpy_array
aa ./data/aa-CH0.wav ./data/aa-CH1.wav
NOTE
The audio files will be written in the specified output directory. Ensure that the directory exists or is created before writing.
close()
Writer class for ‘wav.scp’.
This class allows for writing audio file paths to a ‘wav.scp’ file, where each line corresponds to a key and its associated audio file. It can handle both single-channel and multi-channel audio data, with the ability to specify the output format and naming conventions for generated audio files.
Args: : outdir: The output directory where audio files will be saved. scpfile: The path to the ‘wav.scp’ file that will be created. format: The output audio format (default: “wav”). multi_columns: If True, saves multi-channel data as multiple monaural <br/>
audio files (default: False). <br/> output_name_format: The naming format of generated audio files (default: : “{key}.{audio_format}”). <br/> output_name_format_multi_columns: The naming format of generated audio : files when multi_columns is True (default: “{key}-CH{channel}.{audio_format}”). <br/> dtype: Data type of the audio file (optional). subtype: Subtype of the audio file (optional).
Examples: : ```python
writer = SoundScpWriter('./data/', './data/wav.scp') writer['aa'] = 16000, numpy_array writer['bb'] = 16000, numpy_array aa ./data/aa.wav bb ./data/bb.wav
<br/> ```python >>> writer = SoundScpWriter( './data/', './data/feat.scp', multi_columns=True, ) >>> numpy_array.shape (100, 2) >>> writer['aa'] = 16000, numpy_array aa ./data/aa-CH0.wav ./data/aa-CH1.wav
Attributes: : dir: The output directory as a Path object. fscp: The file handle for the ‘wav.scp’ file. format: The audio format for the output files. subtype: The audio subtype, if specified. output_name_format: The naming format for output files. multi_columns: Boolean indicating if multi-channel files should be saved. output_name_format_multi_columns: The naming format for multi-channel files. data: A dictionary storing the paths of written audio files.
Raises: : ValueError: If the value tuple does not contain exactly two elements. TypeError: If the value is not a tuple of int and numpy.ndarray. RuntimeError: If the input signal is not 1 or 2 dimensional.
get_path(key)
Retrieve the file path associated with the given key.
- Parameters:key (str) – The key for which to retrieve the associated file path.
- Returns: The file path or list of file paths corresponding to the specified key in the wav.scp format.
- Return type: Union[str, List[str]]
####### Examples
>>> writer = SoundScpWriter('./data/', './data/wav.scp')
>>> writer['sample_key'] = 16000, numpy_array
>>> writer.get_path('sample_key')
'./data/sample_key.wav'
>>> writer = SoundScpWriter('./data/', './data/wav.scp',
... multi_columns=True)
>>> writer['multi_key'] = 16000, numpy_array
>>> writer.get_path('multi_key')
['./data/multi_key-CH0.wav', './data/multi_key-CH1.wav']