finalized bw_pink_filt + updated synth_mono

This commit is contained in:
Stefano D'Angelo 2023-09-21 10:22:02 +02:00
parent 52d33fa3c5
commit e3cd1d4e21
3 changed files with 447 additions and 58 deletions

View File

@ -85,6 +85,7 @@ void bw_example_synth_mono_set_sample_rate(bw_example_synth_mono *instance, floa
bw_osc_saw_reset_coeffs(&instance->vco_saw_coeffs);
bw_noise_gen_reset_coeffs(&instance->noise_gen_coeffs);
bw_pink_filt_reset_coeffs(&instance->pink_filt_coeffs);
}
void bw_example_synth_mono_reset(bw_example_synth_mono *instance) {
@ -109,7 +110,7 @@ void bw_example_synth_mono_reset(bw_example_synth_mono *instance) {
bw_osc_tri_reset_coeffs(&instance->vco3_tri_coeffs);
bw_gain_reset_coeffs(&instance->vco3_gain_coeffs);
bw_osc_filt_reset_state(&instance->osc_filt_state, 0.f);
bw_pink_filt_reset_state(&instance->pink_filt_coeffs, &instance->pink_filt_state);
bw_pink_filt_reset_state(&instance->pink_filt_coeffs, &instance->pink_filt_state, 0.f);
bw_gain_reset_coeffs(&instance->noise_gain_coeffs);
bw_env_gen_reset_coeffs(&instance->vcf_env_gen_coeffs);
bw_env_gen_reset_state(&instance->vcf_env_gen_coeffs, &instance->vcf_env_gen_state, 0);
@ -173,7 +174,7 @@ void bw_example_synth_mono_process(bw_example_synth_mono *instance, const float*
if (instance->params[p_noise_color] >= 0.5f)
bw_pink_filt_process(&instance->pink_filt_coeffs, &instance->pink_filt_state, instance->buf[0], instance->buf[0], n);
else
bw_pink_filt_reset_state(&instance->pink_filt_coeffs, &instance->pink_filt_state); // FIXME: calling this here is sloppy coding
bw_pink_filt_reset_state(&instance->pink_filt_coeffs, &instance->pink_filt_state, 0.f); // FIXME: calling this here is sloppy coding
bw_buf_scale(instance->buf[0], 5.f, instance->buf[0], n);
for (int j = 0; j < n; j++)

View File

@ -42,6 +42,17 @@
* <ul>
* <li>Version <strong>1.0.0</strong>:
* <ul>
* <li>Added <code>bw_pink_filt_reset_coeffs()</code>,
* <code>bw_pink_filt_update_coeffs_ctrl()</code>, and
* <code>bw_pink_filt_update_coeffs_audio()</code>.</li>
* <li>Added initial input value to
* <code>bw_pink_filt_reset_state()</code>.</li>
* <li>Added <code>bw_pink_filt_reset_state_multi()</code> and updated
* C++ API in this regard.</li>
* <li>Now <code>bw_pink_filt_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_pink_filt_process()</code> and
* <code>bw_pink_filt_process_multi()</code> now use
* <code>size_t</code> to count samples and channels.</li>
@ -51,6 +62,7 @@
* <li>Added overloaded C++ <code>process()</code> function taking
* C-style arrays as arguments.</li>
* <li>Removed usage of reserved identifiers.</li>
* <li>Added debugging code.</li>
* </ul>
* </li>
* <li>Version <strong>0.6.0</strong>:
@ -115,26 +127,65 @@ typedef struct bw_pink_filt_state bw_pink_filt_state;
*
* #### bw_pink_filt_init()
* ```>>> */
static inline void bw_pink_filt_init(bw_pink_filt_coeffs *BW_RESTRICT coeffs);
static inline void bw_pink_filt_init(
bw_pink_filt_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Initializes input parameter values in `coeffs`.
*
* #### bw_pink_filt_set_sample_rate()
* ```>>> */
static inline void bw_pink_filt_set_sample_rate(bw_pink_filt_coeffs *BW_RESTRICT coeffs, float sample_rate);
static inline void bw_pink_filt_set_sample_rate(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value in `coeffs`.
*
* #### bw_pink_filt_reset_coeffs()
* ```>>> */
static inline void bw_pink_filt_reset_coeffs(
bw_pink_filt_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Resets coefficients in `coeffs` to assume their target values.
*
* #### bw_pink_filt_reset_state()
* ```>>> */
static inline void bw_pink_filt_reset_state(const bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state);
static inline float bw_pink_filt_reset_state(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_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_pink_filt_reset_state_multi()
* ```>>> */
static inline void bw_pink_filt_reset_state_multi(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_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_pink_filt_process1()
* ```>>> */
static inline float bw_pink_filt_process1(const bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state, float x);
static inline float bw_pink_filt_process1_scaling(const bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state, float x);
static inline float bw_pink_filt_process1(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_state * BW_RESTRICT state,
float x);
static inline float bw_pink_filt_process1_scaling(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_state * BW_RESTRICT state,
float x);
/*! <<<```
* These function process one input sample `x` using `coeffs`, while using
* and updating `state`. They return the corresponding output sample.
@ -144,9 +195,17 @@ static inline float bw_pink_filt_process1_scaling(const bw_pink_filt_coeffs *BW_
* * `bw_pink_filt_process1_scaling()` assumes that sample rate scaling is
* enabled.
*
* Whether sample rate scaling is enabled or not is unchecked even for
* debugging purposes.
*
* #### bw_pink_filt_process()
* ```>>> */
static inline void bw_pink_filt_process(bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state, const float *x, float *y, size_t n_samples);
static inline void bw_pink_filt_process(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_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 `coeffs` and
@ -154,7 +213,13 @@ static inline void bw_pink_filt_process(bw_pink_filt_coeffs *BW_RESTRICT coeffs,
*
* #### bw_pink_filt_process_multi()
* ```>>> */
static inline void bw_pink_filt_process_multi(bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_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_pink_filt_process_multi(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_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
@ -163,7 +228,9 @@ static inline void bw_pink_filt_process_multi(bw_pink_filt_coeffs *BW_RESTRICT c
*
* #### bw_pink_filt_set_sample_rate_scaling()
* ```>>> */
static inline void bw_pink_filt_set_sample_rate_scaling(bw_pink_filt_coeffs *BW_RESTRICT coeffs, char value);
static inline void bw_pink_filt_set_sample_rate_scaling(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
char value);
/*! <<<```
* Sets whether the output should be scaled (`value` non-`0`) or not (`0`)
* according to the sample rate in `coeffs`.
@ -177,10 +244,39 @@ static inline void bw_pink_filt_set_sample_rate_scaling(bw_pink_filt_coeffs *BW_
*
* #### bw_pink_filt_get_scaling_k()
* ```>>> */
static inline float bw_pink_filt_get_scaling_k(const bw_pink_filt_coeffs *BW_RESTRICT coeffs);
static inline float bw_pink_filt_get_scaling_k(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Returns the sample rate scaling factor that is applied or would be applied
* if sample rate scaling were enabled, as stored in `coeffs`.
*
* #### bw_pink_filt_coeffs_is_valid()
* ```>>> */
static inline char bw_pink_filt_coeffs_is_valid(
const bw_pink_filt_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_pink_filt_coeffs`.
*
* #### bw_pink_filt_state_is_valid()
* ```>>> */
static inline char bw_pink_filt_state_is_valid(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
const bw_pink_filt_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_pink_filt_state`.
* }}} */
#ifdef __cplusplus
@ -196,38 +292,172 @@ static inline float bw_pink_filt_get_scaling_k(const bw_pink_filt_coeffs *BW_RES
extern "C" {
#endif
#ifdef BW_DEBUG_DEEP
enum bw_pink_filt_coeffs_state {
bw_pink_filt_coeffs_state_invalid,
bw_pink_filt_coeffs_state_init,
bw_pink_filt_coeffs_state_set_sample_rate,
bw_pink_filt_coeffs_state_reset_coeffs
};
#endif
struct bw_pink_filt_coeffs {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
enum bw_pink_filt_coeffs_state state;
uint32_t reset_id;
#endif
// Coefficients
float scaling_k;
float scaling_k;
// Parameters
float sample_rate_scaling;
char sample_rate_scaling;
};
struct bw_pink_filt_state {
float s1_z1;
float s2_z1;
float s3_z1;
float s4_z1;
#ifdef BW_DEBUG_DEEP
uint32_t hash;
uint32_t coeffs_reset_id;
#endif
// States
float s1_z1;
float s2_z1;
float s3_z1;
float s4_z1;
};
static inline void bw_pink_filt_init(bw_pink_filt_coeffs *BW_RESTRICT coeffs) {
static inline void bw_pink_filt_init(
bw_pink_filt_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
coeffs->sample_rate_scaling = 0;
#ifdef BW_DEBUG_DEEP
coeffs->hash = bw_hash_sdbm("bw_pink_filt_coeffs");
coeffs->state = bw_pink_filt_coeffs_state_init;
coeffs->reset_id = coeffs->hash + 1;
#endif
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_pink_filt_coeffs_state_init);
}
static inline void bw_pink_filt_set_sample_rate(bw_pink_filt_coeffs *BW_RESTRICT coeffs, float sample_rate) {
static inline void bw_pink_filt_set_sample_rate(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
float sample_rate) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_init);
BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f);
coeffs->scaling_k = 210.f / bw_sqrtf(sample_rate);
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_pink_filt_coeffs_state_set_sample_rate;
#endif
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_pink_filt_coeffs_state_set_sample_rate);
}
static inline void bw_pink_filt_reset_state(const bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state) {
static inline void bw_pink_filt_reset_coeffs(
bw_pink_filt_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_set_sample_rate);
(void)coeffs;
state->s1_z1 = 0.f;
state->s2_z1 = 0.f;
state->s3_z1 = 0.f;
state->s4_z1 = 0.f;
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_pink_filt_coeffs_state_reset_coeffs;
coeffs->reset_id++;
#endif
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_pink_filt_coeffs_state_reset_coeffs);
}
static inline float bw_pink_filt_process1(const bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state, float x) {
static inline float bw_pink_filt_reset_state(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_state * BW_RESTRICT state,
float x_0) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT(bw_is_finite(x_0));
(void)coeffs;
state->s1_z1 = x_0;
state->s2_z1 = x_0;
state->s3_z1 = x_0;
state->s4_z1 = x_0;
const float y = coeffs->sample_rate_scaling ? coeffs->scaling_k * x_0 : x_0;
#ifdef BW_DEBUG_DEEP
state->hash = bw_hash_sdbm("bw_pink_filt_state");
state->coeffs_reset_id = coeffs->reset_id;
#endif
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline void bw_pink_filt_reset_state_multi(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_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_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_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_pink_filt_reset_state(coeffs, state[i], x_0[i]);
else
for (size_t i = 0; i < n_channels; i++)
bw_pink_filt_reset_state(coeffs, state[i], x_0[i]);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(y_0 != NULL ? bw_has_only_finite(y_0, n_channels) : 1);
}
static inline void bw_pink_filt_update_coeffs_ctrl(
bw_pink_filt_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
(void)coeffs;
}
static inline void bw_pink_filt_update_coeffs_audio(
bw_pink_filt_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
(void)coeffs;
}
static inline float bw_pink_filt_process1(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_state * BW_RESTRICT state,
float x) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(x));
(void)coeffs;
const float s1 = 0.320696754235142f * x + state->s1_z1;
state->s1_z1 = 0.999760145116749f * s1 - 0.3204568993518913f * x;
@ -236,37 +466,145 @@ static inline float bw_pink_filt_process1(const bw_pink_filt_coeffs *BW_RESTRICT
const float s3 = 0.2962862885898576f * s2 + state->s3_z1;
state->s3_z1 = 0.9687905029568185f * s3 - 0.265076791546676f * s2;
const float s4 = 0.3882183163519794f * s3 + state->s4_z1;
state->s4_z1 = 0.6573784623288251f * s4 - 0.04559677868080467 * s3;
state->s4_z1 = 0.6573784623288251f * s4 - 0.04559677868080467f * s3;
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(s4));
return s4;
}
static inline float bw_pink_filt_process1_scaling(const bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state, float x) {
(void)coeffs;
return coeffs->scaling_k * bw_pink_filt_process1(coeffs, state, x);
static inline float bw_pink_filt_process1_scaling(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_state * BW_RESTRICT state,
float x) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(x));
const float y = coeffs->scaling_k * bw_pink_filt_process1(coeffs, state, x);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline void bw_pink_filt_process(bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_state *BW_RESTRICT state, const float *x, float* y, size_t n_samples) {
static inline void bw_pink_filt_process(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_state * BW_RESTRICT state,
const float * x,
float * y,
size_t n_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT(x != NULL);
BW_ASSERT_DEEP(bw_has_only_finite(x, n_samples));
BW_ASSERT(y != NULL);
if (coeffs->sample_rate_scaling)
for (size_t i = 0; i < n_samples; i++)
y[i] = bw_pink_filt_process1_scaling(coeffs, state, x[i]);
else
for (size_t i = 0; i < n_samples; i++)
y[i] = bw_pink_filt_process1(coeffs, state, x[i]);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_pink_filt_state_is_valid(coeffs, state));
BW_ASSERT_DEEP(bw_has_only_finite(y, n_samples));
}
static inline void bw_pink_filt_process_multi(bw_pink_filt_coeffs *BW_RESTRICT coeffs, bw_pink_filt_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_pink_filt_process_multi(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
bw_pink_filt_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_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
BW_ASSERT(state != NULL);
BW_ASSERT(x != NULL);
BW_ASSERT(y != NULL);
for (size_t i = 0; i < n_channels; i++)
bw_pink_filt_process(coeffs, state[i], x[i], y[i], n_samples);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_reset_coeffs);
}
static inline void bw_pink_filt_set_sample_rate_scaling(bw_pink_filt_coeffs *BW_RESTRICT coeffs, char value) {
static inline void bw_pink_filt_set_sample_rate_scaling(
bw_pink_filt_coeffs * BW_RESTRICT coeffs,
char value) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_init);
coeffs->sample_rate_scaling = value;
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_init);
}
static inline float bw_pink_filt_get_scaling_k(const bw_pink_filt_coeffs *BW_RESTRICT coeffs) {
static inline float bw_pink_filt_get_scaling_k(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_pink_filt_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_pink_filt_coeffs_state_set_sample_rate);
return coeffs->scaling_k;
}
static inline char bw_pink_filt_coeffs_is_valid(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
#ifdef BW_DEBUG_DEEP
if (coeffs->hash != bw_hash_sdbm("bw_pink_filt_coeffs"))
return 0;
if (coeffs->state < bw_pink_filt_coeffs_state_init || coeffs->state > bw_pink_filt_coeffs_state_reset_coeffs)
return 0;
if (coeffs->state >= bw_pink_filt_coeffs_state_set_sample_rate && coeffs->scaling_k <= 0.f)
return 0;
#endif
(void)coeffs;
return 1;
}
static inline char bw_pink_filt_state_is_valid(
const bw_pink_filt_coeffs * BW_RESTRICT coeffs,
const bw_pink_filt_state * BW_RESTRICT state) {
BW_ASSERT(state != NULL);
#ifdef BW_DEBUG_DEEP
if (state->hash != bw_hash_sdbm("bw_pink_filt_state"))
return 0;
if (coeffs != NULL && coeffs->reset_id != state->coeffs_reset_id)
return 0;
#endif
(void)coeffs;
return bw_is_finite(state->s1_z1) && bw_is_finite(state->s2_z1) && bw_is_finite(state->s3_z1) && bw_is_finite(state->s4_z1);
}
#ifdef __cplusplus
}
@ -284,18 +622,37 @@ class PinkFilt {
public:
PinkFilt();
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,
size_t nSamples);
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);
std::array<float *, N_CHANNELS> y,
size_t nSamples);
void setSampleRateScaling(bool value);
void setSampleRateScaling(
bool value);
float getScalingK();
/*! <<<...
@ -309,9 +666,9 @@ public:
* change at any time in future versions. Please, do not use it directly. */
private:
bw_pink_filt_coeffs coeffs;
bw_pink_filt_state states[N_CHANNELS];
bw_pink_filt_state *BW_RESTRICT statesP[N_CHANNELS];
bw_pink_filt_coeffs coeffs;
bw_pink_filt_state states[N_CHANNELS];
bw_pink_filt_state * BW_RESTRICT statesP[N_CHANNELS];
};
template<size_t N_CHANNELS>
@ -322,34 +679,65 @@ inline PinkFilt<N_CHANNELS>::PinkFilt() {
}
template<size_t N_CHANNELS>
inline void PinkFilt<N_CHANNELS>::setSampleRate(float sampleRate) {
inline void PinkFilt<N_CHANNELS>::setSampleRate(
float sampleRate) {
bw_pink_filt_set_sample_rate(&coeffs, sampleRate);
}
template<size_t N_CHANNELS>
inline void PinkFilt<N_CHANNELS>::reset() {
for (size_t i = 0; i < N_CHANNELS; i++)
bw_pink_filt_reset_state(&coeffs, states + i);
inline void PinkFilt<N_CHANNELS>::reset(
float x0,
float * BW_RESTRICT y0) {
bw_pink_filt_reset_coeffs(&coeffs);
if (y0 != nullptr)
for (size_t i = 0; i < N_CHANNELS; i++)
y0[i] = bw_pink_filt_reset_state(&coeffs, states + i, x0);
else
for (size_t i = 0; i < N_CHANNELS; i++)
bw_pink_filt_reset_state(&coeffs, states + i, x0);
}
template<size_t N_CHANNELS>
inline void PinkFilt<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 PinkFilt<N_CHANNELS>::reset(
const float * x0,
float * y0) {
bw_pink_filt_reset_coeffs(&coeffs);
bw_pink_filt_reset_state_multi(&coeffs, statesP, x0, y0, N_CHANNELS);
}
template<size_t N_CHANNELS>
inline void PinkFilt<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 PinkFilt<N_CHANNELS>::process(
const float * const *x,
float * const *y,
size_t nSamples) {
const float * const * x,
float * const * y,
size_t nSamples) {
bw_pink_filt_process_multi(&coeffs, statesP, x, y, N_CHANNELS, nSamples);
}
template<size_t N_CHANNELS>
inline void PinkFilt<N_CHANNELS>::process(
std::array<const float *, N_CHANNELS> x,
std::array<float *, N_CHANNELS> y,
size_t nSamples) {
std::array<float *, N_CHANNELS> y,
size_t nSamples) {
process(x.data(), y.data(), nSamples);
}
template<size_t N_CHANNELS>
inline void PinkFilt<N_CHANNELS>::setSampleRateScaling(bool value) {
inline void PinkFilt<N_CHANNELS>::setSampleRateScaling(
bool value) {
bw_pink_filt_set_sample_rate_scaling(&coeffs, value);
}

View File

@ -291,8 +291,8 @@ struct bw_trem_coeffs {
struct bw_trem_state {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
uint32_t coeffs_reset_id;
uint32_t hash;
uint32_t coeffs_reset_id;
#endif
// Sub-components