finalized bw_noise_gen + updated synth(pp)_mono + better bw_gain debug

This commit is contained in:
Stefano D'Angelo 2023-09-21 07:59:41 +02:00
parent aefd36aac1
commit 52d33fa3c5
5 changed files with 270 additions and 41 deletions

4
TODO
View File

@ -4,7 +4,7 @@
in progress:
* add initial state (x0) to reset state of all modules
* check assumptions w.r.t. usage of math functions
* empty functions etc. to keep consistency and forward compatibility?
* empty functions etc. to keep consistency and forward compatibility
* format declarations in a more readable way
code:
general:
@ -13,8 +13,6 @@ code:
* common smoothing policy (as control rate as possible?) - smoothing control?
* IMPLEMENTATION prepocessor def???
semi-specific:
* osc post filter (and one pole init, slew rate, etc.) val from input? set state instead?
* audio rate optional pulse width/slope inputs?
* max_delay -> set sample rate? see reverb
specific:
* bw_comb: should also modulate feedback?

View File

@ -53,6 +53,7 @@ void bw_example_synth_mono_init(bw_example_synth_mono *instance) {
bw_osc_tri_set_antialiasing(&instance->vco2_tri_coeffs, 1);
bw_osc_pulse_set_antialiasing(&instance->vco3_pulse_coeffs, 1);
bw_osc_tri_set_antialiasing(&instance->vco3_tri_coeffs, 1);
bw_noise_gen_set_sample_rate_scaling(&instance->noise_gen_coeffs, 1);
bw_phase_gen_set_frequency(&instance->a440_phase_gen_coeffs, 440.f);
instance->rand_state = 0xbaddecaf600dfeed;
@ -83,6 +84,7 @@ void bw_example_synth_mono_set_sample_rate(bw_example_synth_mono *instance, floa
bw_ppm_set_sample_rate(&instance->ppm_coeffs, sample_rate);
bw_osc_saw_reset_coeffs(&instance->vco_saw_coeffs);
bw_noise_gen_reset_coeffs(&instance->noise_gen_coeffs);
}
void bw_example_synth_mono_reset(bw_example_synth_mono *instance) {

View File

@ -33,6 +33,7 @@ void bw_example_synthpp_mono_init(bw_example_synthpp_mono *instance) {
instance->vco3OscSaw.setAntialiasing(true);
instance->vco3OscPulse.setAntialiasing(true);
instance->vco3OscTri.setAntialiasing(true);
instance->noiseGen.setSampleRateScaling(true);
instance->a440PhaseGen.setFrequency(440.f);
instance->rand_state = 0xbaddecaf600dfeed;
@ -86,6 +87,7 @@ void bw_example_synthpp_mono_reset(bw_example_synthpp_mono *instance) {
instance->vco3OscTri.reset();
instance->vco3Gain.reset();
instance->oscFilt.reset();
instance->noiseGen.reset();
instance->pinkFilt.reset();
instance->noiseGain.reset();
instance->vcfEnvGen.reset();

View File

@ -461,6 +461,7 @@ static inline float bw_gain_get_gain_cur(
const bw_gain_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_gain_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_gain_coeffs_state_reset_coeffs);
return bw_one_pole_get_y_z1(&coeffs->smooth_state);
}

View File

@ -33,6 +33,9 @@
* <ul>
* <li>Version <strong>1.0.0</strong>:
* <ul>
* <li>Added <code>bw_noise_gen_reset_coeffs()</code>,
* <code>bw_noise_gen_update_coeffs_ctrl()</code>,
* and <code>bw_noise_gen_update_coeffs_audio()</code>.</li>
* <li><code>bw_noise_gen_process()</code> and
* <code>bw_noise_gen_process_multi()</code> now use
* <code>size_t</code> to count samples and channels.</li>
@ -42,6 +45,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>:
@ -87,44 +91,84 @@ typedef struct bw_noise_gen_coeffs bw_noise_gen_coeffs;
*
* #### bw_noise_gen_init()
* ```>>> */
static inline void bw_noise_gen_init(bw_noise_gen_coeffs *BW_RESTRICT coeffs, uint64_t *BW_RESTRICT state);
static inline void bw_noise_gen_init(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
uint64_t * BW_RESTRICT state);
/*! <<<```
* Initializes input parameter values and sets the `state` pointer to obtain
* pseudo-random numbers in `coeffs`.
*
* #### bw_noise_gen_set_sample_rate()
* ```>>> */
static inline void bw_noise_gen_set_sample_rate(bw_noise_gen_coeffs *BW_RESTRICT coeffs, float sample_rate);
static inline void bw_noise_gen_set_sample_rate(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value in `coeffs`.
*
* #### bw_noise_gen_reset_coeffs()
* ```>>> */
static inline void bw_noise_gen_reset_coeffs(
bw_noise_gen_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Resets coefficients in `coeffs` to assume their target values.
*
* #### bw_noise_gen_update_coeffs_ctrl()
* ```>>> */
static inline void bw_noise_gen_update_coeffs_ctrl(
bw_noise_gen_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Triggers control-rate update of coefficients in `coeffs`.
*
* #### bw_noise_gen_update_coeffs_audio()
* ```>>> */
static inline void bw_noise_gen_update_coeffs_audio(
bw_noise_gen_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* Triggers audio-rate update of coefficients in `coeffs`.
*
* #### bw_noise_gen_process1\*()
* ```>>> */
static inline float bw_noise_gen_process1(const bw_noise_gen_coeffs *BW_RESTRICT coeffs);
static inline float bw_noise_gen_process1_scaling(const bw_noise_gen_coeffs *BW_RESTRICT coeffs);
static inline float bw_noise_gen_process1(
const bw_noise_gen_coeffs * BW_RESTRICT coeffs);
static inline float bw_noise_gen_process1_scaling(
const bw_noise_gen_coeffs * BW_RESTRICT coeffs);
/*! <<<```
* These functions generate and return one sample using `coeffs`, where:
* * `bw_noise_gen_process1()` assumes that sample rate scaling is disabled;
* * `bw_noise_gen_process1_scaling()` assumes that sample rate scaling is
* enabled.
*
* Whether sample rate scaling is enabled or not is unchecked even for
* debugging purposes.
*
* #### bw_noise_gen_process()
* ```>>> */
static inline void bw_noise_gen_process(bw_noise_gen_coeffs *BW_RESTRICT coeffs, float *BW_RESTRICT y, size_t n_samples);
static inline void bw_noise_gen_process(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
float * BW_RESTRICT y,
size_t n_samples);
/*! <<<```
* Generates and fills the first `n_samples` of the output buffer `y` using
* `coeffs`.
*
* #### bw_noise_gen_process_multi()
* ```>>> */
static inline void bw_noise_gen_process_multi(bw_noise_gen_coeffs *BW_RESTRICT coeffs, float *BW_RESTRICT const *BW_RESTRICT y, size_t n_channels, size_t n_samples);
static inline void bw_noise_gen_process_multi(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
float * BW_RESTRICT const * BW_RESTRICT y,
size_t n_channels,
size_t n_samples);
/*! <<<```
* Generates and fills the first `n_samples` of the `n_channels` output
* buffers `y` using `coeffs`.
*
* #### bw_noise_gen_set_sample_rate_scaling()
* ```>>> */
static inline void bw_noise_gen_set_sample_rate_scaling(bw_noise_gen_coeffs *BW_RESTRICT coeffs, char value);
static inline void bw_noise_gen_set_sample_rate_scaling(
bw_noise_gen_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`.
@ -138,10 +182,23 @@ static inline void bw_noise_gen_set_sample_rate_scaling(bw_noise_gen_coeffs *BW_
*
* #### bw_noise_gen_get_scaling_k()
* ```>>> */
static inline float bw_noise_gen_get_scaling_k(const bw_noise_gen_coeffs *BW_RESTRICT coeffs);
static inline float bw_noise_gen_get_scaling_k(
const bw_noise_gen_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_noise_gen_coeffs_is_valid()
* ```>>> */
static inline char bw_noise_gen_coeffs_is_valid(
const bw_noise_gen_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_noise_gen_coeffs`.
* }}} */
#ifdef __cplusplus
@ -160,54 +217,208 @@ static inline float bw_noise_gen_get_scaling_k(const bw_noise_gen_coeffs *BW_RES
extern "C" {
#endif
#ifdef BW_DEBUG_DEEP
enum bw_noise_gen_coeffs_state {
bw_noise_gen_coeffs_state_invalid,
bw_noise_gen_coeffs_state_init,
bw_noise_gen_coeffs_state_set_sample_rate,
bw_noise_gen_coeffs_state_reset_coeffs
};
#endif
struct bw_noise_gen_coeffs {
#ifdef BW_DEBUG_DEEP
uint32_t hash;
enum bw_noise_gen_coeffs_state state;
#endif
// Coefficients
float scaling_k;
float scaling_k;
// Parameters
uint64_t *BW_RESTRICT state;
char sample_rate_scaling;
uint64_t * BW_RESTRICT rand_state;
char sample_rate_scaling;
};
static inline void bw_noise_gen_init(bw_noise_gen_coeffs *BW_RESTRICT coeffs, uint64_t *BW_RESTRICT state) {
coeffs->state = state;
static inline void bw_noise_gen_init(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
uint64_t * BW_RESTRICT state) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT(state != NULL);
coeffs->rand_state = state;
coeffs->sample_rate_scaling = 0;
#ifdef BW_DEBUG_DEEP
coeffs->hash = bw_hash_sdbm("bw_noise_gen_coeffs");
coeffs->state = bw_noise_gen_coeffs_state_init;
#endif
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_noise_gen_coeffs_state_init);
}
static inline void bw_noise_gen_set_sample_rate(bw_noise_gen_coeffs *BW_RESTRICT coeffs, float sample_rate) {
static inline void bw_noise_gen_set_sample_rate(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
float sample_rate) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_init);
BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f);
coeffs->scaling_k = 0.004761904761904762f * bw_sqrtf(sample_rate);
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_noise_gen_coeffs_state_set_sample_rate;
#endif
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_noise_gen_coeffs_state_set_sample_rate);
}
static inline float bw_noise_gen_process1(const bw_noise_gen_coeffs *BW_RESTRICT coeffs) {
return bw_randf(coeffs->state);
static inline void bw_noise_gen_reset_coeffs(
bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_set_sample_rate);
(void)coeffs;
#ifdef BW_DEBUG_DEEP
coeffs->state = bw_noise_gen_coeffs_state_reset_coeffs;
#endif
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state == bw_noise_gen_coeffs_state_reset_coeffs);
}
static inline float bw_noise_gen_process1_scaling(const bw_noise_gen_coeffs *BW_RESTRICT coeffs) {
return coeffs->scaling_k * bw_randf(coeffs->state);
static inline void bw_noise_gen_update_coeffs_ctrl(
bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
(void)coeffs;
}
static inline void bw_noise_gen_process(bw_noise_gen_coeffs *BW_RESTRICT coeffs, float *BW_RESTRICT y, size_t n_samples) {
static inline void bw_noise_gen_update_coeffs_audio(
bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
(void)coeffs;
}
static inline float bw_noise_gen_process1(
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
const float y = bw_randf(coeffs->rand_state);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline float bw_noise_gen_process1_scaling(
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
const float y = coeffs->scaling_k * bw_randf(coeffs->rand_state);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline void bw_noise_gen_process(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
float * BW_RESTRICT y,
size_t n_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
BW_ASSERT(y != NULL);
if (coeffs->sample_rate_scaling)
for (size_t i = 0; i < n_samples; i++)
y[i] = bw_noise_gen_process1(coeffs);
else
for (size_t i = 0; i < n_samples; i++)
y[i] = bw_noise_gen_process1_scaling(coeffs);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
BW_ASSERT_DEEP(bw_has_only_finite(y, n_samples));
}
static inline void bw_noise_gen_process_multi(bw_noise_gen_coeffs *BW_RESTRICT coeffs, float *BW_RESTRICT const *BW_RESTRICT y, size_t n_channels, size_t n_samples) {
static inline void bw_noise_gen_process_multi(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
float * BW_RESTRICT const * BW_RESTRICT y,
size_t n_channels,
size_t n_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
BW_ASSERT(y != NULL);
for (size_t i = 0; i < n_channels; i++)
bw_noise_gen_process(coeffs, y[i], n_samples);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
}
static inline void bw_noise_gen_set_sample_rate_scaling(bw_noise_gen_coeffs *BW_RESTRICT coeffs, char value) {
static inline void bw_noise_gen_set_sample_rate_scaling(
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
char value) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_init);
coeffs->sample_rate_scaling = value;
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_init);
}
static inline float bw_noise_gen_get_scaling_k(const bw_noise_gen_coeffs *BW_RESTRICT coeffs) {
static inline float bw_noise_gen_get_scaling_k(
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_set_sample_rate);
return coeffs->scaling_k;
}
static inline char bw_noise_gen_coeffs_is_valid(
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
#ifdef BW_DEBUG_DEEP
if (coeffs->hash != bw_hash_sdbm("bw_noise_gen_coeffs"))
return 0;
if (coeffs->state < bw_noise_gen_coeffs_state_init || coeffs->state > bw_noise_gen_coeffs_state_reset_coeffs)
return 0;
#endif
if (coeffs->rand_state == NULL)
return 0;
#ifdef BW_DEBUG_DEEP
if (coeffs->state >= bw_noise_gen_coeffs_state_set_sample_rate && coeffs->scaling_k <= 0.f)
return 0;
#endif
return 1;
}
#ifdef __cplusplus
}
@ -223,17 +434,24 @@ namespace Brickworks {
template<size_t N_CHANNELS>
class NoiseGen {
public:
NoiseGen(uint64_t *BW_RESTRICT state);
NoiseGen(
uint64_t * BW_RESTRICT state);
void setSampleRate(float sampleRate);
void process(
float *BW_RESTRICT const *BW_RESTRICT y,
size_t nSamples);
void process(
std::array<float *BW_RESTRICT, N_CHANNELS> y,
size_t nSamples);
void setSampleRate(
float sampleRate);
void setSampleRateScaling(bool value);
void reset();
void process(
float * BW_RESTRICT const * BW_RESTRICT y,
size_t nSamples);
void process(
std::array<float * BW_RESTRICT, N_CHANNELS> y,
size_t nSamples);
void setSampleRateScaling(
bool value);
float getScalingK();
/*! <<<...
@ -247,35 +465,43 @@ public:
* change at any time in future versions. Please, do not use it directly. */
private:
bw_noise_gen_coeffs coeffs;
bw_noise_gen_coeffs coeffs;
};
template<size_t N_CHANNELS>
inline NoiseGen<N_CHANNELS>::NoiseGen(uint64_t *BW_RESTRICT state) {
inline NoiseGen<N_CHANNELS>::NoiseGen(
uint64_t * BW_RESTRICT state) {
bw_noise_gen_init(&coeffs, state);
}
template<size_t N_CHANNELS>
inline void NoiseGen<N_CHANNELS>::setSampleRate(float sampleRate) {
inline void NoiseGen<N_CHANNELS>::setSampleRate(
float sampleRate) {
bw_noise_gen_set_sample_rate(&coeffs, sampleRate);
}
template<size_t N_CHANNELS>
inline void NoiseGen<N_CHANNELS>::reset() {
bw_noise_gen_reset_coeffs(&coeffs);
}
template<size_t N_CHANNELS>
inline void NoiseGen<N_CHANNELS>::process(
float *BW_RESTRICT const *BW_RESTRICT y,
size_t nSamples) {
size_t nSamples) {
bw_noise_gen_process_multi(&coeffs, y, N_CHANNELS, nSamples);
}
template<size_t N_CHANNELS>
inline void NoiseGen<N_CHANNELS>::process(
std::array<float *BW_RESTRICT, N_CHANNELS> y,
size_t nSamples) {
std::array<float * BW_RESTRICT, N_CHANNELS> y,
size_t nSamples) {
process(y.data(), nSamples);
}
template<size_t N_CHANNELS>
inline void NoiseGen<N_CHANNELS>::setSampleRateScaling(bool value) {
inline void NoiseGen<N_CHANNELS>::setSampleRateScaling(
bool value) {
bw_noise_gen_set_sample_rate_scaling(&coeffs, value);
}