clarity.enhancer.multiband_compressor package¶
Submodules¶
clarity.enhancer.multiband_compressor.compressor_qmul module¶
“Module for the Compressor class.
- class clarity.enhancer.multiband_compressor.compressor_qmul.Compressor(threshold: float = 0.0, ratio: float = 1.0, attack: float = 15.0, release: float = 100.0, makeup_gain: float = 0.0, knee_width: float = 0.0, sample_rate: float = 44100.0)[source]¶
Bases:
object
Based in the compressor from [1]. Code adapted from JUCE C++ source code. Optimization using IIR filters.
References: [1] Giannoulis, D., Massberg, M., & Reiss, J. D. (2012). Digital dynamic range compressor design - A tutorial and analysis. Journal of the Audio Engineering Society, 60(6), 399-408.
Example: >>> import librosa >>> import matplotlib.pyplot as plt
>>> signal, sr = librosa.load( ... librosa.ex("brahms"), ... sr=None, ... duration=10, ... mono=False ... ) >>> if signal.ndim == 1: >>> signal = signal[np.newaxis, :]
>>> compressor = Compressor( ... threshold=-30.0, ... ratio=4.0, ... attack=10.0, ... release=100.0, ... makeup_gain=1.25, ... sample_rate=sr, ... knee_width=10.0, ...)
>>> compressed_signal = compressor(signal) >>> fig, axes = plt.subplots(2, 1) >>> axes[0].specgram(signal[0], Fs=sr, NFFT=512, noverlap=256) >>> axes[0].set_title("original signal") >>> axes[1].specgram(compressed_signal[0], Fs=sr, NFFT=512, noverlap=256) >>> axes[1].set_title("Compressed signal") >>> plt.yticks([x for x in range(0, int(sr / 2), 1000)]) >>> plt.tight_layout() >>> plt.show() >>> plt.close()
>>> plt.figure() >>> plt.plot(signal[0]) >>> plt.plot(compressed_signal[0]) >>> plt.show()
clarity.enhancer.multiband_compressor.crossover module¶
Class compute crossover filter for one crossover frequency.
- class clarity.enhancer.multiband_compressor.crossover.Crossover(freq_crossover: list | ndarray | int, sample_rate: float = 44100)[source]¶
Bases:
object
A class to compute crossover filter for two or more crossover frequencies. This is based on [1]
References: [1] D’Appolito, J. A. (1984, October). Active realization of multiway all-pass crossover systems. In Audio Engineering Society Convention 76. Audio Engineering Society.
Example: >>> xover_freqs = np.array( … [250, 500, 1000, 1500, 2000, 3000, 4000, 5000, 6000, 8000] …) * np.sqrt(2) >>> xover = Crossover(xover_freqs) >>> signal = np.random.randn(1000) >>> filtered_signal = xover(signal) >>> xover.plot_filter()
- FILTER_ORDER = 4¶
- plot_filter()[source]¶
Method to plot the frequency response of the filter. This can help to validate the Class is generating the expected filters
- xover_component(signal: ndarray, filter_idx: int, len_xover_freqs: int, axis: int = -1) ndarray [source]¶
Apply the crossover filter to the signal.
- Parameters:
signal (np.ndarray) – The input signal.
filter_idx (int) – The index of the filter to apply.
len_xover_freqs (int) – The number of crossover frequencies.
axis (int) – The axis along which to apply the filter. Default is -1.
`More information in scipy.signal.lfilter`
documentation.
- Returns:
The filtered signal.
- Return type:
np.ndarray
- clarity.enhancer.multiband_compressor.crossover.compute_coefficients(xover_freqs: ndarray, sample_rate: float = 44100, order: int = 4) tuple[ndarray, ndarray, ndarray, ndarray] [source]¶
Compute the filter coefficients. These are independent of the signal.
- Parameters:
xover_freqs (ndarray) – The crossover frequencies.
sample_rate (float) – The sample rate of the signal.
order (int) – The order of the filter.
- Returns:
The numerator of the filter. astore (ndarray): The denominator of the filter. bstore_phi (ndarray): The phase correction for the numerator of the
all pass filter.
- astore_phi (ndarray): The phase correction for the denominator of the
all pass filter.
- Return type:
bsotre (ndarray)
- clarity.enhancer.multiband_compressor.crossover.linkwitz_riley(xover_freqs: float, btype: str, order: int = 4) tuple[ndarray, ndarray] [source]¶
Compute the Linkwitz-Riley filter. Makes a filter that is equivalent to passing through butterworth twice to get linkwitz-riley
- Parameters:
xover_freqs – The crossover frequency.
btype – The type of filter.
order – The order of the filter.
- Returns:
The numerator and denominator of the filter.
- Return type:
np.ndarray
- clarity.enhancer.multiband_compressor.crossover.make_all_pass(b1: ndarray, a1: ndarray, b2: ndarray, a2: ndarray) tuple[ndarray, ndarray] [source]¶
Take two filters [b1,a1] and [b2,a2] and calculate the coefficient of a filter that is equivalent to passing the input through each filter in parallel and summing the result
- Parameters:
b1 – Numerator of the first filter
a1 – Denominator of the first filter
b2 – Numerator of the second filter
a2 – Denominator of the second filter
- Returns:
The numerator and denominator of the all pass filter.
- Return type:
np.ndarray
clarity.enhancer.multiband_compressor.multiband_compressor module¶
An implementation for Multiband Dynamic Range Compressor.
- class clarity.enhancer.multiband_compressor.multiband_compressor.MultibandCompressor(crossover_frequencies: int | float | list | np.ndarray, sample_rate: float = 44100, compressors_params: dict | None = None)[source]¶
Bases:
object
Multiband Compressor.
- set_compressors(attack: list | float = 15.0, release: list | float = 100.0, threshold: list | float = 0.0, ratio: list | float = 1.0, makeup_gain: list | float = 0.0, knee_width: list | float = 0.0) None [source]¶
Set the compressors parameters.
Parameters can be a float or a list with the same length as center_frequencies.
- Parameters:
attack (list | float) – The attack time in milliseconds.
release (list | float) – The release time in milliseconds.
threshold (list | float) – The threshold in dB.
ratio (list | float) – The ratio.
gain (list | float) – The make-up gain in dB.
knee_width (list | float) – The knee width in dB.
- Returns:
The compressors.
- Return type:
list[Compressor]
- Raises:
ValueError – If the parameters are not the same length as
crossover frequencies + 1. –
Module contents¶
- class clarity.enhancer.multiband_compressor.Compressor(threshold: float = 0.0, ratio: float = 1.0, attack: float = 15.0, release: float = 100.0, makeup_gain: float = 0.0, knee_width: float = 0.0, sample_rate: float = 44100.0)[source]¶
Bases:
object
Based in the compressor from [1]. Code adapted from JUCE C++ source code. Optimization using IIR filters.
References: [1] Giannoulis, D., Massberg, M., & Reiss, J. D. (2012). Digital dynamic range compressor design - A tutorial and analysis. Journal of the Audio Engineering Society, 60(6), 399-408.
Example: >>> import librosa >>> import matplotlib.pyplot as plt
>>> signal, sr = librosa.load( ... librosa.ex("brahms"), ... sr=None, ... duration=10, ... mono=False ... ) >>> if signal.ndim == 1: >>> signal = signal[np.newaxis, :]
>>> compressor = Compressor( ... threshold=-30.0, ... ratio=4.0, ... attack=10.0, ... release=100.0, ... makeup_gain=1.25, ... sample_rate=sr, ... knee_width=10.0, ...)
>>> compressed_signal = compressor(signal) >>> fig, axes = plt.subplots(2, 1) >>> axes[0].specgram(signal[0], Fs=sr, NFFT=512, noverlap=256) >>> axes[0].set_title("original signal") >>> axes[1].specgram(compressed_signal[0], Fs=sr, NFFT=512, noverlap=256) >>> axes[1].set_title("Compressed signal") >>> plt.yticks([x for x in range(0, int(sr / 2), 1000)]) >>> plt.tight_layout() >>> plt.show() >>> plt.close()
>>> plt.figure() >>> plt.plot(signal[0]) >>> plt.plot(compressed_signal[0]) >>> plt.show()
- class clarity.enhancer.multiband_compressor.Crossover(freq_crossover: list | ndarray | int, sample_rate: float = 44100)[source]¶
Bases:
object
A class to compute crossover filter for two or more crossover frequencies. This is based on [1]
References: [1] D’Appolito, J. A. (1984, October). Active realization of multiway all-pass crossover systems. In Audio Engineering Society Convention 76. Audio Engineering Society.
Example: >>> xover_freqs = np.array( … [250, 500, 1000, 1500, 2000, 3000, 4000, 5000, 6000, 8000] …) * np.sqrt(2) >>> xover = Crossover(xover_freqs) >>> signal = np.random.randn(1000) >>> filtered_signal = xover(signal) >>> xover.plot_filter()
- FILTER_ORDER = 4¶
- plot_filter()[source]¶
Method to plot the frequency response of the filter. This can help to validate the Class is generating the expected filters
- xover_component(signal: ndarray, filter_idx: int, len_xover_freqs: int, axis: int = -1) ndarray [source]¶
Apply the crossover filter to the signal.
- Parameters:
signal (np.ndarray) – The input signal.
filter_idx (int) – The index of the filter to apply.
len_xover_freqs (int) – The number of crossover frequencies.
axis (int) – The axis along which to apply the filter. Default is -1.
`More information in scipy.signal.lfilter`
documentation.
- Returns:
The filtered signal.
- Return type:
np.ndarray
- class clarity.enhancer.multiband_compressor.MultibandCompressor(crossover_frequencies: int | float | list | np.ndarray, sample_rate: float = 44100, compressors_params: dict | None = None)[source]¶
Bases:
object
Multiband Compressor.
- set_compressors(attack: list | float = 15.0, release: list | float = 100.0, threshold: list | float = 0.0, ratio: list | float = 1.0, makeup_gain: list | float = 0.0, knee_width: list | float = 0.0) None [source]¶
Set the compressors parameters.
Parameters can be a float or a list with the same length as center_frequencies.
- Parameters:
attack (list | float) – The attack time in milliseconds.
release (list | float) – The release time in milliseconds.
threshold (list | float) – The threshold in dB.
ratio (list | float) – The ratio.
gain (list | float) – The make-up gain in dB.
knee_width (list | float) – The knee width in dB.
- Returns:
The compressors.
- Return type:
list[Compressor]
- Raises:
ValueError – If the parameters are not the same length as
crossover frequencies + 1. –