ProcessWithContext

class audinterface.ProcessWithContext(*, process_func=None, process_func_args=None, sampling_rate=None, resample=False, channels=None, mixdown=False, verbose=False)[source]

Alternate processing interface that provides signal context.

In contrast to Process this interface does not look at segments in isolation, but passes the complete signal together with a list of segments to the processing function. By doing so, it becomes possible to process segments in context, e.g. by taking into account surrounding signal values or other segments.

Parameters
  • process_func (Optional[Callable[..., Sequence[Any]]]) –

    processing function, which expects four positional arguments:

    • signal

    • sampling_rate

    • starts sequence with start indices

    • ends sequence with end indices

    and any number of additional keyword arguments (see process_func_args). There are the following special arguments: 'idx', 'file', 'root'. If expected by the function, but not specified in process_func_args, they will be replaced with: a running index, the currently processed file, the root folder. The function must return a sequence of results for every segment

  • process_func_args (Optional[Dict[str, Any]]) – (keyword) arguments passed on to the processing function

  • sampling_rate (Optional[int]) – sampling rate in Hz. If None it will call process_func with the actual sampling rate of the signal

  • resample (bool) – if True enforces given sampling rate by resampling

  • channels (Union[int, Sequence[int], None]) – channel selection, see audresample.remix()

  • mixdown (bool) – apply mono mix-down on selection

  • verbose (bool) – show debug messages

Raises

ValueError – if resample = True, but sampling_rate = None

Examples

>>> def running_mean(signal, sampling_rate, starts, ends):
...     means_per_segment = [
...         signal[:, start:end].mean() for start, end in zip(starts, ends)
...     ]
...     cumsum = np.cumsum(np.pad(means_per_segment, 1))
...     return (cumsum[2:] - cumsum[:-2]) / float(2)
>>> interface = ProcessWithContext(process_func=running_mean)
>>> signal = np.array([1.0, 2.0, 3.0, 1.0, 2.0, 3.0])
>>> sampling_rate = 3
>>> starts = [0, sampling_rate]
>>> ends = [sampling_rate, 2 * sampling_rate]
>>> interface(signal, sampling_rate, starts, ends)
array([2., 1.])
>>> # 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,
... )
>>> files = list(db.files) * 3
>>> starts = [0, 0.1, 0.2]
>>> ends = [0.5, 0.6, 0.7]
>>> index = audformat.segmented_index(files, starts, ends)
>>> interface.process_index(index, root=db.root)
file             start                   end
wav/03a01Fa.wav  0 days 00:00:00         0 days 00:00:00.500000   -0.000261
                 0 days 00:00:00.100000  0 days 00:00:00.600000   -0.000199
                 0 days 00:00:00.200000  0 days 00:00:00.700000   -0.000111
dtype: float32

__call__()

ProcessWithContext.__call__(signal, sampling_rate, starts, ends)[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
  • signal (ndarray) – signal values

  • sampling_rate (int) – sampling rate in Hz

  • starts (Sequence[int]) – sequence with start values

  • ends (Sequence[int]) – sequence with end values

Return type

Any

Returns

Processed signal

Raises

channels

ProcessWithContext.channels

Channel selection.

mixdown

ProcessWithContext.mixdown

Mono mixdown.

process_func

ProcessWithContext.process_func

Process function.

process_func_args

ProcessWithContext.process_func_args

Additional keyword arguments to processing function.

process_index()

ProcessWithContext.process_index(index, *, root=None, process_func_args=None)[source]

Process from a segmented index conform to audformat.

Parameters
Return type

Series

Returns

Series with processed segments conform to audformat

Raises
  • RuntimeError – if sampling rates do not match

  • RuntimeError – if channel selection is invalid

  • RuntimeError – if sequence returned by process_func does not match length of index

process_signal_from_index()

ProcessWithContext.process_signal_from_index(signal, sampling_rate, index, process_func_args=None)[source]

Split a signal into segments and process each segment.

Parameters
Return type

Series

Returns

Series with processed segments conform to audformat

Raises

resample

ProcessWithContext.resample

Resample signal.

sampling_rate

ProcessWithContext.sampling_rate

Sampling rate in Hz.

verbose

ProcessWithContext.verbose

Show debug messages.