duration_in_seconds()

audmath.duration_in_seconds(duration, sampling_rate=None)[source]

Duration in seconds.

Converts the given duration value to seconds. A unit can be provided when duration is given as a string. As units the following values are possible.

Unit

Meaning

W

week

D, days, day

day

h, hours, hour, hr

hour

m, minutes, minute, min, T

minute

s, seconds, second, sec, S

second

ms, milliseconds, millisecond, millis, milli, L

millisecond

us, μs, microseconds, microsecond, micros, micro, U

microsecond

ns, nanoseconds, nanoseconds, nanos, nano, N

nanosecond

Parameters
  • duration (Union[float, int, str, timedelta64, None]) – if duration is a float, integer or string without unit it is treated as seconds or if sampling_rate is provided as samples. If duration is provided as a string with unit, e.g. '2ms' or '2 ms', or as a numpy.timedelta64 or pandas.Timedelta object it will be converted to seconds and sampling_rate is always ignored. If duration is None, numpy.nan, pandas.NA, pandas.NaT, '', 'None', 'NaN', 'NaT', or any other lower/mixed case version of those strings numpy.nan is returned. If duration is numpy.inf, 'Inf' or any other lower/mixed case version of that string numpy.inf is returned, and -numpy.inf for the negative case

  • sampling_rate (Union[float, int, None]) – sampling rate in Hz. Is ignored if duration is provided with a unit

Return type

floating

Returns

duration in seconds

Raises
  • ValueError – if the provided unit is not supported

  • ValueError – if duration is a string that does not match a valid ‘<value><unit>’ pattern

Examples

>>> duration_in_seconds(2)
2.0
>>> duration_in_seconds(2.0)
2.0
>>> duration_in_seconds('2')
2.0
>>> duration_in_seconds('2ms')
0.002
>>> duration_in_seconds('2 ms')
0.002
>>> duration_in_seconds('ms')
0.001
>>> duration_in_seconds(2000, sampling_rate=1000)
2.0
>>> duration_in_seconds(np.timedelta64(2, 's'))
2.0
>>> duration_in_seconds(pd.to_timedelta(2, 's'))
2.0
>>> duration_in_seconds('Inf')
inf
>>> duration_in_seconds(None)
nan