Signal processing

Module for basic audio signal processing and array operations.

to_array(data, sample_width, channels) Extract individual channels of audio data and return a list of arrays of numeric samples.
extract_single_channel(data, fmt, channels, …)
compute_average_channel(data, fmt, channels) Compute and return average channel of multi-channel audio data.
compute_average_channel_stereo(data, …) Compute and return average channel of stereo audio data.
separate_channels(data, fmt, channels) Create a list of arrays of audio samples (array.array objects), one for each channel.
calculate_energy_single_channel(data, …) Calculate the energy of mono audio data.
calculate_energy_multichannel(x, sample_width) Calculate the energy of multi-channel audio data.
auditok.signal.calculate_energy_multichannel(x, sample_width, aggregation_fn=<built-in function max>)[source]

Calculate the energy of multi-channel audio data. Energy is calculated channel-wise. An aggregation function is applied to the resulting energies (default: max). Also see calculate_energy_single_channel().

Parameters:
  • data (bytes) – single-channel audio data.
  • sample_width (int) – size in bytes of one audio sample (one channel considered).
  • aggregation_fn (callable, default: max) – aggregation function to apply to the resulting per-channel energies.
Returns:

energy – aggregated energy of multi-channel audio signal.

Return type:

float

auditok.signal.calculate_energy_single_channel(data, sample_width)[source]

Calculate the energy of mono audio data. Energy is computed as:

\[energy = 20 \log(\sqrt({1}/{N}\sum_{i}^{N}{a_i}^2)) % # noqa: W605\]

where a_i is the i-th audio sample and N is the number of audio samples in data.

Parameters:
  • data (bytes) – single-channel audio data.
  • sample_width (int) – size in bytes of one audio sample.
Returns:

energy – energy of audio signal.

Return type:

float

auditok.signal.compute_average_channel(data, fmt, channels)[source]

Compute and return average channel of multi-channel audio data. If the number of channels is 2, use compute_average_channel_stereo() (much faster). This function uses satandard array module to convert bytes data into an array of numeric values.

Parameters:
  • data (bytes) – multi-channel audio data to mix down.
  • fmt (str) – format (single character) to pass to array.array to convert data into an array of samples. This should be “b” if audio data’s sample width is 1, “h” if it’s 2 and “i” if it’s 4.
  • channels (int) – number of channels of audio data.
Returns:

mono_audio – mixed down audio data.

Return type:

bytes

auditok.signal.compute_average_channel_stereo(data, sample_width)[source]

Compute and return average channel of stereo audio data. This function should be used when the number of channels is exactly 2 because in that case we can use standard audioop module which much faster then calling compute_average_channel().

Parameters:
  • data (bytes) – 2-channel audio data to mix down.
  • sample_width (int) – size in bytes of one audio sample (one channel considered).
Returns:

mono_audio – mixed down audio data.

Return type:

bytes

auditok.signal.separate_channels(data, fmt, channels)[source]

Create a list of arrays of audio samples (array.array objects), one for each channel.

Parameters:
  • data (bytes) – multi-channel audio data to mix down.
  • fmt (str) – format (single character) to pass to array.array to convert data into an array of samples. This should be “b” if audio data’s sample width is 1, “h” if it’s 2 and “i” if it’s 4.
  • channels (int) – number of channels of audio data.
Returns:

channels_arr – list of audio channels, each as a standard array.array.

Return type:

list

auditok.signal.to_array(data, sample_width, channels)[source]

Extract individual channels of audio data and return a list of arrays of numeric samples. This will always return a list of array.array objects (one per channel) even if audio data is mono.

Parameters:
  • data (bytes) – raw audio data.
  • sample_width (int) – size in bytes of one audio sample (one channel considered).
Returns:

samples_arrays – list of arrays of audio samples.

Return type:

list