Process¶
- class audinterface.Process(*, process_func=None, process_func_args=None, process_func_is_mono=False, sampling_rate=None, resample=False, channels=None, mixdown=False, win_dur=None, hop_dur=None, min_signal_dur=None, max_signal_dur=None, segment=None, keep_nat=False, num_workers=1, multiprocessing=False, verbose=False)[source]¶
Processing interface.
- Parameters
process_func (
Optional
[Callable
[...
,Any
]]) – processing function, which expects the two positional argumentssignal
andsampling_rate
and any number of additional keyword arguments (seeprocess_func_args
). There are the following special arguments:'idx'
,'file'
,'root'
. If expected by the function, but not specified inprocess_func_args
, they will be replaced with: a running index, the currently processed file, the root folder. There is no restriction on the return type of the functionprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing functionprocess_func_is_mono (
bool
) – if set toTrue
and the input signal has multiple channels,process_func
will be applied to every channel individuallysampling_rate (
Optional
[int
]) – sampling rate in Hz. IfNone
it will callprocess_func
with the actual sampling rate of the signalresample (
bool
) – ifTrue
enforces given sampling rate by resamplingchannels (
Union
[int
,Sequence
[int
],None
]) – channel selection, seeaudresample.remix()
mixdown (
bool
) – apply mono mix-down on selectionwin_dur (
Union
[float
,int
,str
,Timedelta
,None
]) – window duration, if processing should be applied on a sliding window. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further optionshop_dur (
Union
[float
,int
,str
,Timedelta
,None
]) – hop duration, if processing should be applied on a sliding window. This defines the shift between two windows. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further options. Defaults towin_dur / 2
min_signal_dur (
Union
[float
,int
,str
,Timedelta
,None
]) – minimum signal length required byprocess_func
. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further options. If provided signal is shorter, it will be zero padded at the endmax_signal_dur (
Union
[float
,int
,str
,Timedelta
,None
]) – maximum signal length required byprocess_func
. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further options. If provided signal is longer, it will be cut at the endsegment (
Optional
[Segment
]) – when aaudinterface.Segment
object is provided, it will be used to find a segmentation of the input signal. Afterwards processing is applied to each segmentkeep_nat (
bool
) – if the end of segment is set toNaT
do not replace with file duration in the resultnum_workers (
Optional
[int
]) – number of parallel jobs or 1 for sequential processing. IfNone
will be set to the number of processors on the machine multiplied by 5 in case of multithreading and number of processors in case of multiprocessingmultiprocessing (
bool
) – use multiprocessing instead of multithreadingverbose (
bool
) – show debug messages
- Raises
ValueError – if
resample = True
, butsampling_rate = None
ValueError – if
hop_dur
is specified, but notwin_dur
Examples
>>> def mean(signal, sampling_rate): ... return float(signal.mean()) >>> interface = Process(process_func=mean) >>> signal = np.array([1.0, 2.0, 3.0]) >>> interface(signal, sampling_rate=3) 2.0 >>> interface.process_signal(signal, sampling_rate=3) start end 0 days 0 days 00:00:01 2.0 dtype: float64 >>> # Apply interface on an audformat conform index of a dataframe >>> import audb >>> db = audb.load( ... "emodb", ... version="1.3.0", ... media="wav/03a01Fa.wav", ... full_path=False, ... verbose=False, ... ) >>> index = db["emotion"].index >>> interface.process_index(index, root=db.root) file start end wav/03a01Fa.wav 0 days 0 days 00:00:01.898250 -0.000311 dtype: float64 >>> interface.process_index(index, root=db.root, preserve_index=True) file wav/03a01Fa.wav -0.000311 dtype: float64 >>> # Apply interface with a sliding window >>> interface = Process( ... process_func=mean, ... win_dur=1.0, ... hop_dur=0.5, ... ) >>> interface.process_index(index, root=db.root) file start end wav/03a01Fa.wav 0 days 00:00:00 0 days 00:00:01 -0.000329 0 days 00:00:00.500000 0 days 00:00:01.500000 -0.000285 dtype: float64
__call__()¶
- Process.__call__(signal, sampling_rate)[source]¶
Apply processing to signal.
This function processes the signal without transforming the output into a
pd.Series
. Instead, it will return the raw processed signal. However, if channel selection, mixdown and/or resampling is enabled, the signal will be first remixed and resampled if the input sampling rate does not fit the expected sampling rate.- Parameters
- Return type
- Returns
Processed signal
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_file()¶
- Process.process_file(file, *, start=None, end=None, root=None, process_func_args=None)[source]¶
Process the content of an audio file.
- Parameters
file (
str
) – file pathstart (
Union
[float
,int
,str
,Timedelta
,None
]) – start processing at this position. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further optionsend (
Union
[float
,int
,str
,Timedelta
,None
]) – end processing at this position. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further optionsroot (
Optional
[str
]) – root folder to expand relative file pathprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.Process.process_func_args
- Return type
- Returns
Series with processed file conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_files()¶
- Process.process_files(files, *, starts=None, ends=None, root=None, process_func_args=None)[source]¶
Process a list of files.
- Parameters
starts (
Union
[float
,int
,str
,Timedelta
,Sequence
[Union
[float
,int
,str
,Timedelta
]],None
]) – segment start positions. Time values given as float or integers are treated as seconds. Seeaudinterface.utils.to_timedelta()
for further options. If a scalar is given, it is applied to all filesends (
Union
[float
,int
,str
,Timedelta
,Sequence
[Union
[float
,int
,str
,Timedelta
]],None
]) – segment end positions. Time values given as float or integers are treated as seconds. Seeaudinterface.utils.to_timedelta()
for further options. If a scalar is given, it is applied to all filesroot (
Optional
[str
]) – root folder to expand relative file pathsprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.Process.process_func_args
- Return type
- Returns
Series with processed files conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_folder()¶
- Process.process_folder(root, *, filetype='wav', include_root=True, process_func_args=None)[source]¶
Process files in a folder.
Note
At the moment does not scan in sub-folders!
- Parameters
root (
str
) – root folderfiletype (
str
) – file extensioninclude_root (
bool
) – ifTrue
the file paths are absolute in the index of the returned resultprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.Process.process_func_args
- Return type
- Returns
Series with processed files conform to audformat
- Raises
FileNotFoundError – if folder does not exist
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_index()¶
- Process.process_index(index, *, preserve_index=False, root=None, cache_root=None, process_func_args=None)[source]¶
Process from an index conform to audformat.
If
cache_root
is notNone
, a hash value is created from the index usingaudformat.utils.hash()
and the result is stored as<cache_root>/<hash>.pkl
. When called again with the same index, results will be read from the cached file.- Parameters
index (
Index
) – index with segment informationpreserve_index (
bool
) – ifTrue
andaudinterface.Process.segment
isNone
the returned index will be of same type as the original one, otherwise always a segmented index is returnedroot (
Optional
[str
]) – root folder to expand relative file pathsprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.Process.process_func_args
- Return type
- Returns
Series with processed segments conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_signal()¶
- Process.process_signal(signal, sampling_rate, *, file=None, start=None, end=None, process_func_args=None)[source]¶
Process audio signal and return result.
Note
If a
file
is given, the index of the returned frame has levelsfile
,start
andend
. Otherwise, it consists only ofstart
andend
.- Parameters
signal (
ndarray
) – signal valuessampling_rate (
int
) – sampling rate in Hzstart (
Union
[float
,int
,str
,Timedelta
,None
]) – start processing at this position. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further optionsend (
Union
[float
,int
,str
,Timedelta
,None
]) – end processing at this position. If value is a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further optionsprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.Process.process_func_args
- Return type
- Returns
Series with processed signal conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_signal_from_index()¶
- Process.process_signal_from_index(signal, sampling_rate, index, process_func_args=None)[source]¶
Split a signal into segments and process each segment.
- Parameters
signal (
ndarray
) – signal valuessampling_rate (
int
) – sampling rate in Hzindex (
Index
) – a segmented index conform to audformat or apandas.MultiIndex
with two levels named start and end that hold start and end positions aspandas.Timedelta
objects. See alsoaudinterface.utils.signal_index()
process_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.Process.process_func_args
- Return type
- Returns
Series with processed segments conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
ValueError – if index contains duplicates