finalized bw_env_follow, untested + wip bw_comp

This commit is contained in:
Stefano D'Angelo 2023-09-15 08:30:01 +02:00
parent 0ef455722f
commit 69b74c4ccb
2 changed files with 601 additions and 88 deletions

View File

@ -31,8 +31,10 @@
* <ul>
* <li>Version <strong>1.0.0</strong>:
* <ul>
* <li>Added initial value arguments in
* <code>bw_comp_reset_state()</code>.</li>
* <li>Added <code>bw_comp_reset_state_multi()</code> and updated C++
* API in this regard.</li>
* <li>Now <code>bw_comp_reset_state()</code> returns the initial
* output value.</li>
* <li>Added overloaded C++ <code>reset()</code> functions taking
* arrays as arguments.</li>
* <li><code>bw_comp_process()</code> and
@ -44,6 +46,8 @@
* <li>Added overloaded C++ <code>process()</code> function taking
* C-style arrays as arguments.</li>
* <li>Removed usage of reserved identifiers.</li>
* <li>Clearly specified parameter validity ranges.</li>
* <li>Added debugging code.</li>
* </ul>
* </li>
* <li>Version <strong>0.6.0</strong>:
@ -92,45 +96,77 @@ typedef struct bw_comp_state bw_comp_state;
*
* #### bw_comp_init()
* ```>>> */
static inline void bw_comp_init(bw_comp_coeffs *BW_RESTRICT coeffs);
static inline void bw_comp_init(
bw_comp_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Initializes input parameter values in `coeffs`.
*
* #### bw_comp_set_sample_rate()
* ```>>> */
static inline void bw_comp_set_sample_rate(bw_comp_coeffs *BW_RESTRICT coeffs, float sample_rate);
static inline void bw_comp_set_sample_rate(
bw_comp_coeffs * BW_RESTRICT coeffs,
float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value in `coeffs`.
*
* #### bw_comp_reset_coeffs()
* ```>>> */
static inline void bw_comp_reset_coeffs(bw_comp_coeffs *BW_RESTRICT coeffs);
static inline void bw_comp_reset_coeffs(
bw_comp_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Resets coefficients in `coeffs` to assume their target values.
*
* #### bw_comp_reset_state()
* ```>>> */
static inline void bw_comp_reset_state(const bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT state, float x_0, float x_sc_0);
static inline float bw_comp_reset_state(
const bw_comp_coeffs * BW_RESTRICT coeffs,
bw_comp_state * BW_RESTRICT state,
float x_0,
float x_sc_0);
/*! <<<```
* Resets the given `state` to its initial values using the given `coeffs`
* and the quiescent/initial input value `x_0` and sidechain input value
* `x_sc_0`.
* and the initial input value `x_0` and sidechain input value `x_sc_0`.
*
* Returns the corresponding initial output value.
*
* #### bw_comp_reset_state_multi()
* ```>>> */
static inline void bw_comp_reset_state_multi(
const bw_comp_coeffs * BW_RESTRICT coeffs,
bw_comp_state * BW_RESTRICT const * BW_RESTRICT state,
const float * x_0,
const float * x_sc_0,
float * y_0,
size_t n_channels);
/*! <<<```
* Resets each of the `n_channels` `state`s to its initial values using the
* given `coeffs` and the corresponding initial input value in the `x_0`
* array and sidechain input value in the `x_sc_0` array.
*
* The corresponding initial output values are written into the `y_0` array,
* if not `NULL`.
*
* #### bw_comp_update_coeffs_ctrl()
* ```>>> */
static inline void bw_comp_update_coeffs_ctrl(bw_comp_coeffs *BW_RESTRICT coeffs);
static inline void bw_comp_update_coeffs_ctrl(
bw_comp_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Triggers control-rate update of coefficients in `coeffs`.
*
* #### bw_comp_update_coeffs_audio()
* ```>>> */
static inline void bw_comp_update_coeffs_audio(bw_comp_coeffs *BW_RESTRICT coeffs);
static inline void bw_comp_update_coeffs_audio(
bw_comp_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Triggers audio-rate update of coefficients in `coeffs`.
*
* #### bw_comp_process1()
* ```>>> */
static inline float bw_comp_process1(const bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT state, float x, float x_sc);
static inline float bw_comp_process1(
const bw_comp_coeffs * BW_RESTRICT coeffs,
bw_comp_state * BW_RESTRICT state,
float x,
float x_sc);
/*! <<<```
* Processes one input sample `x` and the corresponding sidechain input
* sample `x_sc` using `coeffs`, while using and updating `state`. Returns
@ -138,7 +174,13 @@ static inline float bw_comp_process1(const bw_comp_coeffs *BW_RESTRICT coeffs, b
*
* #### bw_comp_process()
* ```>>> */
static inline void bw_comp_process(bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT state, const float *x, const float *x_sc, float *y, size_t n_samples);
static inline void bw_comp_process(
bw_comp_coeffs * BW_RESTRICT coeffs,
bw_comp_state * BW_RESTRICT state,
const float * x,
const float * x_sc,
float * y,
size_t n_samples);
/*! <<<```
* Processes the first `n_samples` of the input buffer `x` and the first
* `n_samples` of the sidechain input buffer `x_sc`, and fills the first
@ -147,7 +189,14 @@ static inline void bw_comp_process(bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_s
*
* #### bw_comp_process_multi()
* ```>>> */
static inline void bw_comp_process_multi(bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT const *BW_RESTRICT state, const float * const *x, const float * const *x_sc, float * const *y, size_t n_channels, size_t n_samples);
static inline void bw_comp_process_multi(
bw_comp_coeffs * BW_RESTRICT coeffs,
bw_comp_state * BW_RESTRICT const * BW_RESTRICT state,
const float * const * x,
const float * const * x_sc,
float * const * y,
size_t n_channels,
size_t n_samples);
/*! <<<```
* Processes the first `n_samples` of the `n_channels` input buffers `x` and
* the first `n_samples` of the `n_channels` sidechain input buffers `x_sc`,
@ -157,23 +206,33 @@ static inline void bw_comp_process_multi(bw_comp_coeffs *BW_RESTRICT coeffs, bw_
*
* #### bw_comp_set_thresh_lin()
* ```>>> */
static inline void bw_comp_set_thresh_lin(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_thresh_lin(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the threshold `value` (linear) in `coeffs`.
*
* Valid range: [`1e-20f`, `1e20f`].
*
* Default value: `1.f`.
*
* #### bw_comp_set_thresh_dBFS()
* ```>>> */
static inline void bw_comp_set_thresh_dBFS(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_thresh_dBFS(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the threshold `value` (dBFS) in `coeffs`.
*
* Valid range: [`-400.f`, `400.f`].
*
* Default value: `0.f`.
*
* #### bw_comp_set_ratio()
* ```>>> */
static inline void bw_comp_set_ratio(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_ratio(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the compression ratio `value` in `coeffs`.
*
@ -186,34 +245,50 @@ static inline void bw_comp_set_ratio(bw_comp_coeffs *BW_RESTRICT coeffs, float v
*
* #### bw_comp_set_attack_tau()
* ```>>> */
static inline void bw_comp_set_attack_tau(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_attack_tau(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the attack time constant `value` (s) in `coeffs`.
*
* `value` must be non-negative.
*
* Default value: `0.f`.
*
* #### bw_comp_set_release_tau()
* ```>>> */
static inline void bw_comp_set_release_tau(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_release_tau(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the release time constant `value` (s) in `coeffs`.
*
* `value` must be non-negative.
*
* Default value: `0.f`.
*
* #### bw_comp_set_gain_lin()
* ```>>> */
static inline void bw_comp_set_gain_lin(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_gain_lin(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the output makeup gain `value` (linear ratio) in `coeffs`.
*
* `value` must be finite.
*
* Default value: `1.f`.
*
* #### bw_comp_set_gain_dB()
* ```>>> */
static inline void bw_comp_set_gain_dB(bw_comp_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_comp_set_gain_dB(
bw_comp_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the output makeup gain `value` (dB) in `coeffs`.
*
* `value` must be less than or equal to `770.630f`.
*
* Default value: `0.f`.
* }}} */
@ -235,7 +310,22 @@ static inline void bw_comp_set_gain_dB(bw_comp_coeffs *BW_RESTRICT coeffs, float
extern "C" {
#endif
#ifdef BW_DEBUG_DEEP
enum bw_comp_coeffs_state {
bw_comp_coeffs_state_invalid,
bw_comp_coeffs_state_init,
bw_comp_coeffs_state_set_sample_rate,
bw_comp_coeffs_state_reset_coeffs
};
#endif
struct bw_comp_coeffs {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
enum bw_comp_coeffs_state state;
uint32_t reset_id;
#endif
// Sub-components
bw_env_follow_coeffs env_follow_coeffs;
bw_gain_coeffs gain_coeffs;
@ -252,32 +342,78 @@ struct bw_comp_coeffs {
};
struct bw_comp_state {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
uint32_t coeffs_reset_id;
#endif
// Sub-components
bw_env_follow_state env_follow_state;
};
static inline void bw_comp_init(bw_comp_coeffs *BW_RESTRICT coeffs) {
static inline void bw_comp_init(
bw_comp_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
bw_env_follow_init(&coeffs->env_follow_coeffs);
bw_gain_init(&coeffs->gain_coeffs);
bw_one_pole_set_tau(&coeffs->smooth_coeffs, 0.05f);
coeffs->thresh = 1.f;
coeffs->ratio = 1.f;
#ifdef BW_DEBUG_DEEP
coeffs->hash = bw_hash_sdbm("bw_comp_coeffs");
coeffs->state = bw_comp_coeffs_state_init;
coeffs->reset_id = coeffs->hash + 1;
#endif
BW_ASSERT_DEEP(bw_comp_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_comp_coeffs_state_init);
}
static inline void bw_comp_set_sample_rate(bw_comp_coeffs *BW_RESTRICT coeffs, float sample_rate) {
static inline void bw_comp_set_sample_rate(
bw_comp_coeffs * BW_RESTRICT coeffs,
float sample_rate) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_comp_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_comp_coeffs_state_init);
BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f);
bw_env_follow_set_sample_rate(&coeffs->env_follow_coeffs, sample_rate);
bw_gain_set_sample_rate(&coeffs->gain_coeffs, sample_rate);
bw_one_pole_set_sample_rate(&coeffs->smooth_coeffs, sample_rate);
bw_one_pole_reset_coeffs(&coeffs->smooth_coeffs);
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_comp_coeffs_state_set_sample_rate;
#endif
BW_ASSERT_DEEP(bw_comp_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_comp_coeffs_state_set_sample_rate);
}
static inline void bw_comp_reset_coeffs(bw_comp_coeffs *BW_RESTRICT coeffs) {
static inline void bw_comp_reset_coeffs(
bw_comp_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_comp_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_comp_coeffs_state_set_sample_rate);
bw_env_follow_reset_coeffs(&coeffs->env_follow_coeffs);
bw_gain_reset_coeffs(&coeffs->gain_coeffs);
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &coeffs->smooth_thresh_state, coeffs->thresh);
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &coeffs->smooth_ratio_state, coeffs->ratio);
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_comp_coeffs_state_reset_coeffs;
coeffs->reset_id++;
#endif
BW_ASSERT_DEEP(bw_comp_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_comp_coeffs_state_reset_coeffs);
}
static inline void bw_comp_reset_state(const bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT state, float x_0, float x_sc_0) {
static inline float bw_comp_reset_state(
const bw_comp_coeffs * BW_RESTRICT coeffs,
bw_comp_state * BW_RESTRICT state,
float x_0,
float x_sc_0) {
(void)x_0;
bw_env_follow_reset_state(&coeffs->env_follow_coeffs, &state->env_follow_state, x_sc_0);
}
@ -297,8 +433,18 @@ static inline void bw_comp_update_coeffs_audio(bw_comp_coeffs *BW_RESTRICT coeff
static inline float bw_comp_process1(const bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT state, float x, float x_sc) {
const float env = bw_env_follow_process1(&coeffs->env_follow_coeffs, &state->env_follow_state, x_sc);
const float thresh = bw_one_pole_get_y_z1(&coeffs->smooth_thresh_state);
const float y = env > thresh ? bw_pow2f(coeffs->kc * bw_log2f(thresh * bw_rcpf(env))) * x : x;
return bw_gain_process1(&coeffs->gain_coeffs, y);
float y;
if (env > thresh) {
float v = thresh * bw_rcpf(env);
if (v >= 1.175494350822287e-38f) {
v = coeffs->kc * bw_log2f(v);
y = v > -126.f ? bw_pow2f(v) * x: thresh;
} else
y = thresh;
} else
y = x;
y = bw_gain_process1(&coeffs->gain_coeffs, y);
return y;
}
static inline void bw_comp_process(bw_comp_coeffs *BW_RESTRICT coeffs, bw_comp_state *BW_RESTRICT state, const float *x, const float *x_sc, float *y, size_t n_samples) {

View File

@ -30,6 +30,12 @@
* <ul>
* <li>Version <strong>1.0.0</strong>:
* <ul>
* <li>Added <code>bw_env_follow_reset_state_multi()</code> and updated
* C++ API in this regard.</li>
* <li>Now <code>bw_env_follow_reset_state()</code> returns the initial
* output value.</li>
* <li>Added overloaded C++ <code>reset()</code> functions taking
* arrays as arguments.</li>
* <li><code>bw_env_follow_process()</code> and
* <code>bw_env_follow_process_multi()</code> now use
* <code>size_t</code> to count samples and channels.</li>
@ -39,6 +45,8 @@
* <li>Added overloaded C++ <code>process()</code> function taking
* C-style arrays as arguments.</li>
* <li>Removed usage of reserved identifiers.</li>
* <li>Clearly specified parameter validity ranges.</li>
* <li>Added debugging code.</li>
* </ul>
* </li>
* <li>Version <strong>0.6.0</strong>:
@ -95,50 +103,86 @@ typedef struct bw_env_follow_state bw_env_follow_state;
*
* #### bw_env_follow_init()
* ```>>> */
static inline void bw_env_follow_init(bw_env_follow_coeffs *BW_RESTRICT coeffs);
static inline void bw_env_follow_init(
bw_env_follow_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Initializes input parameter values in `coeffs`.
*
* #### bw_env_follow_set_sample_rate()
* ```>>> */
static inline void bw_env_follow_set_sample_rate(bw_env_follow_coeffs *BW_RESTRICT coeffs, float sample_rate);
static inline void bw_env_follow_set_sample_rate(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value in `coeffs`.
*
* #### bw_env_follow_reset_coeffs()
* ```>>> */
static inline void bw_env_follow_reset_coeffs(bw_env_follow_coeffs *BW_RESTRICT coeffs);
static inline void bw_env_follow_reset_coeffs(
bw_env_follow_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Resets coefficients in `coeffs` to assume their target values.
*
* #### bw_env_follow_reset_state()
* ```>>> */
static inline void bw_env_follow_reset_state(const bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT state);
static inline float bw_env_follow_reset_state(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT state,
float x_0);
/*! <<<```
* Resets the given `state` to its initial values using the given `coeffs`.
* Resets the given `state` to its initial values using the given `coeffs`
* and the initial input value `x_0`.
*
* Returns the corresponding initial output value.
*
* #### bw_env_follow_reset_state_multi()
* ```>>> */
static inline void bw_env_follow_reset_state_multi(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT const * BW_RESTRICT state,
const float * x_0,
float * y_0,
size_t n_channels);
/*! <<<```
* Resets each of the `n_channels` `state`s to its initial values using the
* given `coeffs` and the corresponding initial input value in the `x_0`
* array.
*
* The corresponding initial output values are written into the `y_0` array,
* if not `NULL`.
*
* #### bw_env_follow_update_coeffs_ctrl()
* ```>>> */
static inline void bw_env_follow_update_coeffs_ctrl(bw_env_follow_coeffs *BW_RESTRICT coeffs);
static inline void bw_env_follow_update_coeffs_ctrl(
bw_env_follow_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Triggers control-rate update of coefficients in `coeffs`.
*
* #### bw_env_follow_update_coeffs_audio()
* ```>>> */
static inline void bw_env_follow_update_coeffs_audio(bw_env_follow_coeffs *BW_RESTRICT coeffs);
static inline void bw_env_follow_update_coeffs_audio(
bw_env_follow_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Triggers audio-rate update of coefficients in `coeffs`.
*
* #### bw_env_follow_process1()
* ```>>> */
static inline float bw_env_follow_process1(const bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT state, float x);
static inline float bw_env_follow_process1(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT state,
float x);
/*! <<<```
* Processes one input sample `x` using `coeffs`, while using and updating
* `state`. Returns the corresponding output sample.
*
* #### bw_env_follow_process()
* ```>>> */
static inline void bw_env_follow_process(bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT state, const float *x, float *y, size_t n_samples);
static inline void bw_env_follow_process(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT state,
const float * x,
float * y,
size_t n_samples);
/*! <<<```
* 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 both
@ -148,7 +192,13 @@ static inline void bw_env_follow_process(bw_env_follow_coeffs *BW_RESTRICT coeff
*
* #### bw_env_follow_process_multi()
* ```>>> */
static inline void bw_env_follow_process_multi(bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT const *BW_RESTRICT state, const float * const *x, float * const *y, size_t n_channels, size_t n_samples);
static inline void bw_env_follow_process_multi(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT const * BW_RESTRICT state,
const float * const * x,
float * const * y,
size_t n_channels,
size_t n_samples);
/*! <<<```
* Processes the first `n_samples` of the `n_channels` input buffers `x` and
* fills the first `n_samples` of the `n_channels` output buffers `y`, while
@ -159,27 +209,64 @@ static inline void bw_env_follow_process_multi(bw_env_follow_coeffs *BW_RESTRICT
*
* #### bw_env_follow_set_attack_tau()
* ```>>> */
static inline void bw_env_follow_set_attack_tau(bw_env_follow_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_env_follow_set_attack_tau(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the upgoing (attack) time constant in `coeffs` of the one-pole filter
* to `value` (s).
*
* `value` must be non-negative.
*
* Default value: `0.f`.
*
* #### bw_env_follow_set_release_tau()
* ```>>> */
static inline void bw_env_follow_set_release_tau(bw_env_follow_coeffs *BW_RESTRICT coeffs, float value);
static inline void bw_env_follow_set_release_tau(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
float value);
/*! <<<```
* Sets the downgoing (release) time constant in `coeffs` of the one-pole
* filter to `value` (s).
*
* `value` must be non-negative.
*
* Default value: `0.f`.
*
* #### bw_env_follow_get_y_z1()
* ```>>> */
static inline float bw_env_follow_get_y_z1(const bw_env_follow_state *BW_RESTRICT state);
static inline float bw_env_follow_get_y_z1(
const bw_env_follow_state * BW_RESTRICT state);
/*! <<<```
* Returns the last output sample as stored in `state`.
*
* #### bw_env_follow_coeffs_is_valid()
* ```>>> */
static inline char bw_env_follow_coeffs_is_valid(
const bw_env_follow_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Tries to determine whether `coeffs` is valid and returns non-`0` if it
* seems to be the case and `0` if it is certainly not. False positives are
* possible, false negatives are not.
*
* `coeffs` must at least point to a readable memory block of size greater
* than or equal to that of `bw_env_follow_coeffs`.
*
* #### bw_env_follow_state_is_valid()
* ```>>> */
static inline char bw_env_follow_state_is_valid(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
const bw_env_follow_state * BW_RESTRICT state);
/*! <<<```
* Tries to determine whether `state` is valid and returns non-`0` if it
* seems to be the case and `0` if it is certainly not. False positives are
* possible, false negatives are not.
*
* If `coeffs` is not `NULL` extra cross-checks might be performed (`state`
* is supposed to be associated to `coeffs`).
*
* `state` must at least point to a readable memory block of size greater
* than or equal to that of `bw_env_follow_state`.
* }}} */
#ifdef __cplusplus
@ -198,45 +285,193 @@ static inline float bw_env_follow_get_y_z1(const bw_env_follow_state *BW_RESTRIC
extern "C" {
#endif
#ifdef BW_DEBUG_DEEP
enum bw_env_follow_coeffs_state {
bw_env_follow_coeffs_state_invalid,
bw_env_follow_coeffs_state_init,
bw_env_follow_coeffs_state_set_sample_rate,
bw_env_follow_coeffs_state_reset_coeffs
};
#endif
struct bw_env_follow_coeffs {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
enum bw_env_follow_coeffs_state state;
uint32_t reset_id;
#endif
// Sub-components
bw_one_pole_coeffs one_pole_coeffs;
};
struct bw_env_follow_state {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
uint32_t coeffs_reset_id;
#endif
// Sub-components
bw_one_pole_state one_pole_state;
};
static inline void bw_env_follow_init(bw_env_follow_coeffs *BW_RESTRICT coeffs) {
static inline void bw_env_follow_init(
bw_env_follow_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
bw_one_pole_init(&coeffs->one_pole_coeffs);
#ifdef BW_DEBUG_DEEP
coeffs->hash = bw_hash_sdbm("bw_env_follow_coeffs");
coeffs->state = bw_env_follow_coeffs_state_init;
coeffs->reset_id = coeffs->hash + 1;
#endif
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_env_follow_coeffs_state_init);
}
static inline void bw_env_follow_set_sample_rate(bw_env_follow_coeffs *BW_RESTRICT coeffs, float sample_rate) {
static inline void bw_env_follow_set_sample_rate(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
float sample_rate) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_init);
BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f);
bw_one_pole_set_sample_rate(&coeffs->one_pole_coeffs, sample_rate);
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_env_follow_coeffs_state_set_sample_rate;
#endif
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_env_follow_coeffs_state_set_sample_rate);
}
static inline void bw_env_follow_reset_coeffs(bw_env_follow_coeffs *BW_RESTRICT coeffs) {
static inline void bw_env_follow_reset_coeffs(
bw_env_follow_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_set_sample_rate);
bw_one_pole_reset_coeffs(&coeffs->one_pole_coeffs);
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_env_follow_coeffs_state_reset_coeffs;
coeffs->reset_id++;
#endif
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_env_follow_coeffs_state_reset_coeffs);
}
static inline void bw_env_follow_reset_state(const bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT state) {
bw_one_pole_reset_state(&coeffs->one_pole_coeffs, &state->one_pole_state, 0.f);
static inline float bw_env_follow_reset_state(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT state,
float x_0) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT(bw_is_finite(x_0));
const float x = bw_absf(x_0);
const float y = bw_one_pole_reset_state(&coeffs->one_pole_coeffs, &state->one_pole_state, x);
#ifdef BW_DEBUG_DEEP
state->hash = bw_hash_sdbm("bw_env_follow_state");
state->coeffs_reset_id = coeffs->reset_id;
#endif
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_env_follow_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline void bw_env_follow_update_coeffs_ctrl(bw_env_follow_coeffs *BW_RESTRICT coeffs) {
static inline void bw_env_follow_reset_state_multi(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT const * BW_RESTRICT state,
const float * x_0,
float * y_0,
size_t n_channels) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT(x_0 != NULL);
if (y_0 != NULL)
for (size_t i = 0; i < n_channels; i++)
y_0[i] = bw_env_follow_reset_state(coeffs, state[i], x_0[i]);
else
for (size_t i = 0; i < n_channels; i++)
bw_env_follow_reset_state(coeffs, state[i], x_0[i]);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(y_0 != NULL ? bw_has_only_finite(y_0, n_channels) : 1);
}
static inline void bw_env_follow_update_coeffs_ctrl(
bw_env_follow_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
bw_one_pole_update_coeffs_ctrl(&coeffs->one_pole_coeffs);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
}
static inline void bw_env_follow_update_coeffs_audio(bw_env_follow_coeffs *BW_RESTRICT coeffs) {
static inline void bw_env_follow_update_coeffs_audio(
bw_env_follow_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
bw_one_pole_update_coeffs_audio(&coeffs->one_pole_coeffs);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
}
static inline float bw_env_follow_process1(const bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT state, float x) {
static inline float bw_env_follow_process1(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT state,
float x) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_env_follow_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(x));
x = bw_absf(x);
return bw_one_pole_process1_asym(&coeffs->one_pole_coeffs, &state->one_pole_state, x);
const float y = bw_one_pole_process1_asym(&coeffs->one_pole_coeffs, &state->one_pole_state, x);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_env_follow_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline void bw_env_follow_process(bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT state, const float *x, float *y, size_t n_samples) {
static inline void bw_env_follow_process(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT state,
const float * x,
float * y,
size_t n_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_env_follow_state_is_valid(coeffs, state));
BW_ASSERT(x != NULL);
BW_ASSERT_DEEP(bw_has_only_finite(x, n_samples));
bw_env_follow_update_coeffs_ctrl(coeffs);
if (y != NULL)
for (size_t i = 0; i < n_samples; i++) {
@ -248,9 +483,26 @@ static inline void bw_env_follow_process(bw_env_follow_coeffs *BW_RESTRICT coeff
bw_env_follow_update_coeffs_audio(coeffs);
bw_env_follow_process1(coeffs, state, x[i]);
}
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_env_follow_state_is_valid(coeffs, state));
BW_ASSERT_DEEP(y != NULL ? bw_has_only_finite(y, n_samples) : 1);
}
static inline void bw_env_follow_process_multi(bw_env_follow_coeffs *BW_RESTRICT coeffs, bw_env_follow_state *BW_RESTRICT const *BW_RESTRICT state, const float * const *x, float * const *y, size_t n_channels, size_t n_samples) {
static inline void bw_env_follow_process_multi(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
bw_env_follow_state * BW_RESTRICT const * BW_RESTRICT state,
const float * const * x,
float * const * y,
size_t n_channels,
size_t n_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT(x != NULL);
bw_env_follow_update_coeffs_ctrl(coeffs);
if (y != NULL)
for (size_t i = 0; i < n_samples; i++) {
@ -267,20 +519,81 @@ static inline void bw_env_follow_process_multi(bw_env_follow_coeffs *BW_RESTRICT
for (size_t j = 0; j < n_channels; j++)
bw_env_follow_process1(coeffs, state[j], x[j][i]);
}
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_reset_coeffs);
}
static inline void bw_env_follow_set_attack_tau(bw_env_follow_coeffs *BW_RESTRICT coeffs, float value) {
static inline void bw_env_follow_set_attack_tau(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
float value) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_init);
BW_ASSERT(!bw_is_nan(value));
BW_ASSERT(value >= 0.f);
bw_one_pole_set_tau_up(&coeffs->one_pole_coeffs, value);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_init);
}
static inline void bw_env_follow_set_release_tau(bw_env_follow_coeffs *BW_RESTRICT coeffs, float value) {
static inline void bw_env_follow_set_release_tau(
bw_env_follow_coeffs * BW_RESTRICT coeffs,
float value) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_init);
BW_ASSERT(!bw_is_nan(value));
BW_ASSERT(value >= 0.f);
bw_one_pole_set_tau_down(&coeffs->one_pole_coeffs, value);
BW_ASSERT_DEEP(bw_env_follow_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_env_follow_coeffs_state_init);
}
static inline float bw_env_follow_get_y_z1(const bw_env_follow_state *BW_RESTRICT state) {
static inline float bw_env_follow_get_y_z1(
const bw_env_follow_state * BW_RESTRICT state) {
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_env_follow_state_is_valid(NULL, state));
return bw_one_pole_get_y_z1(&state->one_pole_state);
}
static inline char bw_env_follow_coeffs_is_valid(
const bw_env_follow_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
#ifdef BW_DEBUG_DEEP
if (coeffs->hash != bw_hash_sdbm("bw_env_follow_coeffs"))
return 0;
if (coeffs->state < bw_env_follow_coeffs_state_init || coeffs->state > bw_env_follow_coeffs_state_reset_coeffs)
return 0;
#endif
return bw_one_pole_coeffs_is_valid(&coeffs->one_pole_coeffs);
}
static inline char bw_env_follow_state_is_valid(
const bw_env_follow_coeffs * BW_RESTRICT coeffs,
const bw_env_follow_state * BW_RESTRICT state) {
BW_ASSERT(state != NULL);
#ifdef BW_DEBUG_DEEP
if (state->hash != bw_hash_sdbm("bw_env_follow_state"))
return 0;
if (coeffs != NULL && coeffs->reset_id != state->coeffs_reset_id)
return 0;
#endif
(void)coeffs;
return bw_one_pole_state_is_valid(&coeffs->one_pole_coeffs, &state->one_pole_state);
}
#ifdef __cplusplus
}
@ -298,21 +611,43 @@ class EnvFollow {
public:
EnvFollow();
void setSampleRate(float sampleRate);
void reset();
void setSampleRate(
float sampleRate);
void reset(
float x0 = 0.f,
float * BW_RESTRICT y0 = nullptr);
void reset(
float x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0);
void reset(
const float * x0,
float * y0 = nullptr);
void reset(
std::array<float, N_CHANNELS> x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0 = nullptr);
void process(
const float * const *x,
float * const *y,
const float * const * x,
float * const * y,
size_t nSamples);
void process(
std::array<const float *, N_CHANNELS> x,
std::array<float *, N_CHANNELS> y,
size_t nSamples);
void setAttackTau(float value);
void setReleaseTau(float value);
void setAttackTau(
float value);
float getYZ1(size_t channel);
void setReleaseTau(
float value);
float getYZ1(
size_t channel);
/*! <<<...
* }
* ```
@ -326,7 +661,7 @@ public:
private:
bw_env_follow_coeffs coeffs;
bw_env_follow_state states[N_CHANNELS];
bw_env_follow_state *BW_RESTRICT statesP[N_CHANNELS];
bw_env_follow_state * BW_RESTRICT statesP[N_CHANNELS];
};
template<size_t N_CHANNELS>
@ -337,21 +672,50 @@ inline EnvFollow<N_CHANNELS>::EnvFollow() {
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::setSampleRate(float sampleRate) {
inline void EnvFollow<N_CHANNELS>::setSampleRate(
float sampleRate) {
bw_env_follow_set_sample_rate(&coeffs, sampleRate);
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::reset() {
inline void EnvFollow<N_CHANNELS>::reset(
float x0,
float * BW_RESTRICT y0) {
bw_env_follow_reset_coeffs(&coeffs);
if (y0 != nullptr)
for (size_t i = 0; i < N_CHANNELS; i++)
bw_env_follow_reset_state(&coeffs, states + i);
y0[i] = bw_env_follow_reset_state(&coeffs, states + i, x0);
else
for (size_t i = 0; i < N_CHANNELS; i++)
bw_env_follow_reset_state(&coeffs, states + i, x0);
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::reset(
float x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
reset(x0, y0 != nullptr ? y0->data() : nullptr);
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::reset(
const float * x0,
float * y0) {
bw_env_follow_reset_coeffs(&coeffs);
bw_env_follow_reset_state_multi(&coeffs, statesP, x0, y0, N_CHANNELS);
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::reset(
std::array<float, N_CHANNELS> x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
reset(x0.data(), y0 != nullptr ? y0->data() : nullptr);
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::process(
const float * const *x,
float * const *y,
const float * const * x,
float * const * y,
size_t nSamples) {
bw_env_follow_process_multi(&coeffs, statesP, x, y, N_CHANNELS, nSamples);
}
@ -365,17 +729,20 @@ inline void EnvFollow<N_CHANNELS>::process(
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::setAttackTau(float value) {
inline void EnvFollow<N_CHANNELS>::setAttackTau(
float value) {
bw_env_follow_set_attack_tau(&coeffs, value);
}
template<size_t N_CHANNELS>
inline void EnvFollow<N_CHANNELS>::setReleaseTau(float value) {
inline void EnvFollow<N_CHANNELS>::setReleaseTau(
float value) {
bw_env_follow_set_release_tau(&coeffs, value);
}
template<size_t N_CHANNELS>
inline float EnvFollow<N_CHANNELS>::getYZ1(size_t channel) {
inline float EnvFollow<N_CHANNELS>::getYZ1(
size_t channel) {
return bw_env_follow_get_y_z1(states + channel);
}