2022-11-15 23:49:51 +00:00
|
|
|
/*
|
|
|
|
* Brickworks
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Orastron Srl unipersonale
|
|
|
|
*
|
|
|
|
* Brickworks is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* Brickworks is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2022-12-06 08:01:34 +00:00
|
|
|
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* File author: Stefano D'Angelo
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* module_type {{{ dsp }}}
|
2022-12-05 09:09:15 +00:00
|
|
|
* version {{{ 0.3.0 }}}
|
2022-11-29 17:23:30 +00:00
|
|
|
* requires {{{ bw_config bw_common bw_math bw_one_pole }}}
|
2022-11-15 23:49:51 +00:00
|
|
|
* description {{{
|
2022-12-20 07:35:28 +00:00
|
|
|
* Gain.
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}}
|
|
|
|
* changelog {{{
|
|
|
|
* <ul>
|
2022-12-05 09:09:15 +00:00
|
|
|
* <li>Version <strong>0.3.0</strong>:
|
|
|
|
* <ul>
|
2022-12-20 07:35:28 +00:00
|
|
|
* <li>Renamed as bw_gain.</li>
|
|
|
|
* <li>Changed gain parameter API to express values in linear gain
|
2022-12-05 10:17:28 +00:00
|
|
|
* and dB.</li>
|
2022-12-05 09:09:15 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* <li>Version <strong>0.2.0</strong>:
|
|
|
|
* <ul>
|
2022-11-28 09:31:07 +00:00
|
|
|
* <li>Refactored API.</li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-15 23:49:51 +00:00
|
|
|
* <li>Version <strong>0.1.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>First release.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* }}}
|
|
|
|
*/
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
#ifndef _BW_GAIN_H
|
|
|
|
#define _BW_GAIN_H
|
2022-11-15 23:49:51 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 09:31:07 +00:00
|
|
|
#include <bw_common.h>
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! api {{{
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_coeffs
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
typedef struct _bw_gain_coeffs bw_gain_coeffs;
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 17:57:49 +00:00
|
|
|
* Coefficients and related.
|
2022-11-28 09:31:07 +00:00
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_init()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_init(bw_gain_coeffs *BW_RESTRICT coeffs);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 17:57:49 +00:00
|
|
|
* Initializes input parameter values in `coeffs`.
|
2022-11-28 09:31:07 +00:00
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_set_sample_rate()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_set_sample_rate(bw_gain_coeffs *BW_RESTRICT coeffs, float sample_rate);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 17:57:49 +00:00
|
|
|
* Sets the `sample_rate` (Hz) value in `coeffs`.
|
2022-11-28 09:31:07 +00:00
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_reset_coeffs()
|
2022-12-04 17:57:49 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_reset_coeffs(bw_gain_coeffs *BW_RESTRICT coeffs);
|
2022-12-04 17:57:49 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Resets coefficients in `coeffs` to assume their target values.
|
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_update_coeffs_ctrl()
|
2022-12-04 17:57:49 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_update_coeffs_ctrl(bw_gain_coeffs *BW_RESTRICT coeffs);
|
2022-12-04 17:57:49 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Triggers control-rate update of coefficients in `coeffs`.
|
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_update_coeffs_audio()
|
2022-12-04 17:57:49 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_update_coeffs_audio(bw_gain_coeffs *BW_RESTRICT coeffs);
|
2022-12-04 17:57:49 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Triggers audio-rate update of coefficients in `coeffs`.
|
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_process1()
|
2022-12-04 17:57:49 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline float bw_gain_process1(const bw_gain_coeffs *BW_RESTRICT coeffs, float x);
|
2022-12-04 17:57:49 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Processes one input sample `x` using `coeffs` and returns the
|
|
|
|
* corresponding output sample.
|
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_process()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_process(bw_gain_coeffs *BW_RESTRICT coeffs, const float *x, float *y, int n_samples);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 17:57:49 +00:00
|
|
|
* Processes the first `n_samples` of the input buffer `x` and fills the
|
|
|
|
* first `n_samples` of the output buffer `y`, while using and updating
|
|
|
|
* `coeffs` (control and audio rate).
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_set_gain_lin()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_set_gain_lin(bw_gain_coeffs *BW_RESTRICT coeffs, float value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-20 07:35:28 +00:00
|
|
|
* Sets the gain parameter to the given `value` (linear gain) in `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* Default value: `1.f`.
|
2022-12-05 09:09:15 +00:00
|
|
|
*
|
2022-12-20 07:35:28 +00:00
|
|
|
* #### bw_gain_set_gain_dB()
|
2022-12-05 09:09:15 +00:00
|
|
|
* ```>>> */
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_set_gain_dB(bw_gain_coeffs *BW_RESTRICT coeffs, float value);
|
2022-12-05 09:09:15 +00:00
|
|
|
/*! <<<```
|
2022-12-20 07:35:28 +00:00
|
|
|
* Sets the gain parameter to the given `value` (dB) in `coeffs`.
|
2022-12-05 09:09:15 +00:00
|
|
|
*
|
|
|
|
* Default value: `0.f`.
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}} */
|
|
|
|
|
2022-11-28 09:31:07 +00:00
|
|
|
/*** Implementation ***/
|
|
|
|
|
|
|
|
/* WARNING: This part of the file is not part of the public API. Its content may
|
|
|
|
* change at any time in future versions. Please, do not use it directly. */
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2022-11-28 09:31:07 +00:00
|
|
|
#include <bw_math.h>
|
2022-11-29 17:23:30 +00:00
|
|
|
#include <bw_one_pole.h>
|
2022-11-28 09:31:07 +00:00
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
struct _bw_gain_coeffs {
|
2022-11-28 09:31:07 +00:00
|
|
|
// Sub-components
|
2022-11-29 17:23:30 +00:00
|
|
|
bw_one_pole_coeffs smooth_coeffs;
|
|
|
|
bw_one_pole_state smooth_state;
|
2022-11-19 01:25:38 +00:00
|
|
|
|
|
|
|
// Parameters
|
2022-12-20 07:35:28 +00:00
|
|
|
float gain;
|
2022-11-28 09:31:07 +00:00
|
|
|
};
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_init(bw_gain_coeffs *BW_RESTRICT coeffs) {
|
2022-11-29 17:23:30 +00:00
|
|
|
bw_one_pole_init(&coeffs->smooth_coeffs);
|
|
|
|
bw_one_pole_set_tau(&coeffs->smooth_coeffs, 0.05f);
|
2022-12-20 07:35:28 +00:00
|
|
|
coeffs->gain = 1.f;
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_set_sample_rate(bw_gain_coeffs *BW_RESTRICT coeffs, float sample_rate) {
|
2022-11-29 17:23:30 +00:00
|
|
|
bw_one_pole_set_sample_rate(&coeffs->smooth_coeffs, sample_rate);
|
|
|
|
bw_one_pole_reset_coeffs(&coeffs->smooth_coeffs);
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_reset_coeffs(bw_gain_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->gain);
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_update_coeffs_ctrl(bw_gain_coeffs *BW_RESTRICT coeffs) {
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_update_coeffs_audio(bw_gain_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
bw_one_pole_process1(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->gain);
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline float bw_gain_process1(const bw_gain_coeffs *BW_RESTRICT coeffs, float x) {
|
2022-12-05 09:09:15 +00:00
|
|
|
return bw_one_pole_get_y_z1(&coeffs->smooth_state) * x;
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_process(bw_gain_coeffs *BW_RESTRICT coeffs, const float *x, float *y, int n_samples) {
|
2022-11-28 09:31:07 +00:00
|
|
|
for (int i = 0; i < n_samples; i++) {
|
2022-12-20 07:35:28 +00:00
|
|
|
bw_gain_update_coeffs_audio(coeffs);
|
|
|
|
y[i] = bw_gain_process1(coeffs, x[i]);
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_set_gain_lin(bw_gain_coeffs *BW_RESTRICT coeffs, float value) {
|
|
|
|
coeffs->gain = value;
|
2022-12-05 09:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 07:35:28 +00:00
|
|
|
static inline void bw_gain_set_gain_dB(bw_gain_coeffs *BW_RESTRICT coeffs, float value) {
|
|
|
|
coeffs->gain = bw_dB2linf_3(value);
|
2022-11-28 09:31:07 +00:00
|
|
|
}
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|