ported bw_vol to new API + fixed bw_slew_lim and a bug in bw_svf

This commit is contained in:
Stefano D'Angelo 2022-11-28 10:31:07 +01:00
parent ac9a14e8e3
commit fa68b793f9
6 changed files with 102 additions and 150 deletions

View File

@ -28,6 +28,7 @@
#include <bw_math.h>
#include <bw_phase_gen.h>
#include <bw_osc_sin.h>
#include <bw_vol.h>
/*
#include <bw_osc_pulse.h>
@ -61,6 +62,7 @@ struct _bw_example_synth {
bw_phase_gen_state phase_gen_state;
bw_phase_gen_coeffs a440_phase_gen_coeffs;
bw_phase_gen_state a440_phase_gen_state;
bw_vol_coeffs vol_coeffs;
/*
bw_osc_pulse osc_pulse;
bw_osc_filt osc_filt;
@ -88,6 +90,7 @@ bw_example_synth bw_example_synth_new() {
bw_phase_gen_init(&instance->phase_gen_coeffs);
bw_phase_gen_init(&instance->a440_phase_gen_coeffs);
bw_vol_init(&instance->vol_coeffs);
bw_phase_gen_set_frequency(&instance->a440_phase_gen_coeffs, 440.f);
/*
@ -112,6 +115,7 @@ void bw_example_synth_free(bw_example_synth instance) {
void bw_example_synth_set_sample_rate(bw_example_synth instance, float sample_rate) {
bw_phase_gen_set_sample_rate(&instance->phase_gen_coeffs, sample_rate);
bw_phase_gen_set_sample_rate(&instance->a440_phase_gen_coeffs, sample_rate);
bw_vol_set_sample_rate(&instance->vol_coeffs, sample_rate);
/*
bw_osc_pulse_set_sample_rate(&instance->osc_pulse, sample_rate);
bw_svf_set_sample_rate(&instance->svf, sample_rate);
@ -126,6 +130,7 @@ void bw_example_synth_reset(bw_example_synth instance) {
bw_phase_gen_reset_state(&instance->phase_gen_coeffs, &instance->phase_gen_state);
bw_phase_gen_reset_coeffs(&instance->a440_phase_gen_coeffs);
bw_phase_gen_reset_state(&instance->a440_phase_gen_coeffs, &instance->a440_phase_gen_state);
bw_vol_reset_coeffs(&instance->vol_coeffs);
/*
bw_osc_pulse_reset(&instance->osc_pulse);
bw_osc_filt_reset(&instance->osc_filt);
@ -175,16 +180,15 @@ void bw_example_synth_process(bw_example_synth instance, const float** x, float*
a440_y = bw_osc_sin_process1(a440_y);
y[0][i] += a440_y;
}
bw_vol_process(&instance->vol_coeffs, y[0], y[0], n_samples);
}
void bw_example_synth_set_parameter(bw_example_synth instance, int index, float value) {
instance->params[index] = value;
switch (index) {
/*
case p_volume:
bw_vol_set_volume(&instance->vol, value);
bw_vol_set_volume(&instance->vol_coeffs, value);
break;
*/
case p_portamento:
bw_phase_gen_set_portamento_tau(&instance->phase_gen_coeffs, value);
break;

View File

@ -81,10 +81,10 @@ static inline void bw_slew_lim_reset_state(const bw_slew_lim_coeffs *BW_RESTRICT
* Resets the given `state` to the initial state using the given `coeffs`.
* >>> */
static inline void bw_slew_lim_reset_coeffs(bw_one_pole_coeffs *BW_RESTRICT coeffs);
static inline void bw_slew_lim_reset_coeffs(bw_slew_lim_coeffs *BW_RESTRICT coeffs);
static inline void bw_slew_lim_update_coeffs_ctrl(bw_one_pole_coeffs *BW_RESTRICT coeffs);
static inline void bw_slew_lim_update_coeffs_audio(bw_one_pole_coeffs *BW_RESTRICT coeffs);
static inline void bw_slew_lim_update_coeffs_ctrl(bw_slew_lim_coeffs *BW_RESTRICT coeffs);
static inline void bw_slew_lim_update_coeffs_audio(bw_slew_lim_coeffs *BW_RESTRICT coeffs);
static inline float bw_slew_lim_process1(const bw_slew_lim_coeffs *BW_RESTRICT coeffs, bw_slew_lim_state *BW_RESTRICT state, float x);
static inline float bw_slew_lim_process1_up(const bw_slew_lim_coeffs *BW_RESTRICT coeffs, bw_slew_lim_state *BW_RESTRICT state, float x);
@ -103,7 +103,7 @@ static inline void bw_slew_lim_process(bw_slew_lim_coeffs *BW_RESTRICT coeffs, b
/*! ...
* #### bw_slew_lim_set_max_rate()
* ```>>> */
static inline void bw_slew_lim_set_max_rate(bw_slew_lim *BW_RESTRICT coeffs, float value);
static inline void bw_slew_lim_set_max_rate(bw_slew_lim_coeffs *BW_RESTRICT coeffs, float value);
/*! <<<```
* Sets both the maximum increasing and decreasing variation rate to the
* given `value` (1/s) for the given `instance`.
@ -120,7 +120,7 @@ static inline void bw_slew_lim_set_max_rate(bw_slew_lim *BW_RESTRICT coeffs, flo
/*! ...
* #### bw_slew_lim_set_max_rate_up()
* ```>>> */
static inline void bw_slew_lim_set_max_rate_up(bw_slew_lim *BW_RESTRICT coeffs, float value);
static inline void bw_slew_lim_set_max_rate_up(bw_slew_lim_coeffs *BW_RESTRICT coeffs, float value);
/*! <<<```
* Sets the maximum increasing variation rate to the given `value` (1/s) for
* the given `instance`.
@ -134,7 +134,7 @@ static inline void bw_slew_lim_set_max_rate_up(bw_slew_lim *BW_RESTRICT coeffs,
/*! ...
* #### bw_slew_lim_set_max_inc_rate()
* ```>>> */
static inline void bw_slew_lim_set_max_rate_down(bw_slew_lim *BW_RESTRICT coeffs, float value);
static inline void bw_slew_lim_set_max_rate_down(bw_slew_lim_coeffs *BW_RESTRICT coeffs, float value);
/*! <<<```
* Sets the maximum decreasing variation rate to the given `value` (1/s) for
* the given `instance`.
@ -152,6 +152,8 @@ static inline float bw_slew_lim_get_y_z1(const bw_slew_lim_state *BW_RESTRICT st
/* 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. */
#include <bw_math.h>
struct _bw_slew_lim_coeffs {
// Coefficients
float T;
@ -170,7 +172,6 @@ struct _bw_slew_lim_state {
};
static inline void bw_slew_lim_init(bw_slew_lim_coeffs *BW_RESTRICT coeffs) {
coeffs->init_val = 0.f;
coeffs->max_inc = INFINITY;
coeffs->max_dec = INFINITY;
}
@ -179,21 +180,21 @@ static inline void bw_slew_lim_set_sample_rate(bw_slew_lim_coeffs *BW_RESTRICT c
coeffs->T = 1.f / sample_rate;
}
static inline void bw_slew_lim_reset_coeffs(bw_one_pole_coeffs *BW_RESTRICT coeffs) {
bw_one_pole_update_coeffs_ctrl(coeffs);
static inline void bw_slew_lim_reset_coeffs(bw_slew_lim_coeffs *BW_RESTRICT coeffs) {
bw_slew_lim_update_coeffs_ctrl(coeffs);
}
static inline void bw_slew_lim_reset_state(const bw_slew_lim_coeffs *BW_RESTRICT coeffs, bw_slew_lim_state *BW_RESTRICT state, float y_z1) {
state->y_z1 = y_z1;
}
static inline void bw_slew_lim_update_coeffs_ctrl(bw_one_pole_coeffs *BW_RESTRICT coeffs) {
static inline void bw_slew_lim_update_coeffs_ctrl(bw_slew_lim_coeffs *BW_RESTRICT coeffs) {
// tracking parameter changes is more trouble than it's worth
coeffs->max_inc = coeffs->T * coeffs->max_rate_up;
coeffs->max_dec = coeffs->T * coeffs->max_rate_down;
}
static inline void bw_slew_lim_update_coeffs_audio(bw_one_pole_coeffs *BW_RESTRICT coeffs) {
static inline void bw_slew_lim_update_coeffs_audio(bw_slew_lim_coeffs *BW_RESTRICT coeffs) {
}
static inline float bw_slew_lim_process1(const bw_slew_lim_coeffs *BW_RESTRICT coeffs, bw_slew_lim_state *BW_RESTRICT state, float x) {
@ -216,15 +217,15 @@ static inline float bw_slew_lim_process1_down(const bw_slew_lim_coeffs *BW_RESTR
static inline void bw_slew_lim_process(bw_slew_lim_coeffs *BW_RESTRICT coeffs, bw_slew_lim_state *BW_RESTRICT state, const float *x, float *y, int n_samples) {
bw_slew_lim_update_coeffs_ctrl(coeffs);
if (instance->max_rate_up != INFINITY) {
if (instance->max_rate_down != INFINITY)
if (coeffs->max_rate_up != INFINITY) {
if (coeffs->max_rate_down != INFINITY)
for (int i = 0; i < n_samples; i++)
y[i] = bw_slew_lim_process1(coeffs, state, x[i]);
else
for (int i = 0; i < n_samples; i++)
y[i] = bw_slew_lim_process1_up(coeffs, state, x[i]);
} else {
if (instance->max_rate_down != INFINITY)
if (coeffs->max_rate_down != INFINITY)
for (int i = 0; i < n_samples; i++)
y[i] = bw_slew_lim_process1_down(coeffs, state, x[i]);
else
@ -233,16 +234,16 @@ static inline void bw_slew_lim_process(bw_slew_lim_coeffs *BW_RESTRICT coeffs, b
}
}
static inline void bw_slew_lim_set_max_rate(bw_slew_lim *BW_RESTRICT coeffs, float value) {
static inline void bw_slew_lim_set_max_rate(bw_slew_lim_coeffs *BW_RESTRICT coeffs, float value) {
bw_slew_lim_set_max_rate_up(coeffs, value);
bw_slew_lim_set_max_rate_down(coeffs, value);
}
static inline void bw_slew_lim_set_max_rate_up(bw_slew_lim *BW_RESTRICT coeffs, float value) {
static inline void bw_slew_lim_set_max_rate_up(bw_slew_lim_coeffs *BW_RESTRICT coeffs, float value) {
coeffs->max_rate_up = value;
}
static inline void bw_slew_lim_set_max_rate_down(bw_slew_lim *BW_RESTRICT coeffs, float value) {
static inline void bw_slew_lim_set_max_rate_down(bw_slew_lim_coeffs *BW_RESTRICT coeffs, float value) {
coeffs->max_rate_down = value;
}

View File

@ -127,6 +127,7 @@ static inline void bw_svf_set_Q(bw_svf_coeffs *BW_RESTRICT coeffs, float value);
/* 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. */
#include <bw_math.h>
#include <bw_one_pole.h>
struct _bw_svf_coeffs {

View File

@ -20,7 +20,7 @@
/*!
* module_type {{{ dsp }}}
* version {{{ 0.2.0 }}}
* requires {{{ bw_config bw_common bw_inline_slew_lim bw_math }}}
* requires {{{ bw_config bw_common bw_slew_lim bw_math }}}
* description {{{
* Volume control for an arbitrary number of channels.
* }}}
@ -28,7 +28,7 @@
* <ul>
* <li>Version <strong>0.2.0</strong>:
* <ul>
* <li>Refactored API to avoid dynamic memory allocation.</li>
* <li>Refactored API.</li>
* </ul>
* </li>
* <li>Version <strong>0.1.0</strong>:
@ -47,42 +47,40 @@
extern "C" {
#endif
/*! api {{{
* #### bw_vol
* ```>>> */
typedef struct _bw_vol bw_vol;
/*! <<<```
* Instance object.
* >>> */
#include <bw_common.h>
/*! ...
/*! api {{{
* #### bw_vol_coeffs
* ```>>> */
typedef struct _bw_vol_coeffs bw_vol_coeffs;
/*! <<<```
* Coefficients.
*
* #### bw_vol_init()
* ```>>> */
void bw_vol_init(bw_vol *instance);
static inline void bw_vol_init(bw_vol_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Initializes the `instance` object.
* >>> */
/*! ...
* Initializes `coeffs`.
*
* #### bw_vol_set_sample_rate()
* ```>>> */
void bw_vol_set_sample_rate(bw_vol *instance, float sample_rate);
static inline void bw_vol_set_sample_rate(bw_vol_coeffs *BW_RESTRICT coeffs, float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value for the given `instance`.
* Sets the `sample_rate` (Hz) value for the given `coeffs`.
*
* >>> */
/*! ...
* #### bw_vol_reset()
* ```>>> */
void bw_vol_reset(bw_vol *instance);
/*! <<<```
* Resets the given `instance` to its initial state.
* >>> */
static inline void bw_vol_reset_coeffs(bw_vol_coeffs *BW_RESTRICT coeffs);
static inline void bw_vol_update_coeffs_ctrl(bw_vol_coeffs *BW_RESTRICT coeffs);
static inline void bw_vol_update_coeffs_audio(bw_vol_coeffs *BW_RESTRICT coeffs);
static inline float bw_vol_process1(const bw_vol_coeffs *BW_RESTRICT coeffs, float x);
/*! ...
* #### bw_vol_process()
* ```>>> */
void bw_vol_process(bw_vol *instance, const float **x, float **y, int n_channels, int n_samples);
static inline void bw_vol_process(bw_vol_coeffs *BW_RESTRICT coeffs, const float *x, float *y, int n_samples);
/*! <<<```
* Lets the given `instance` process `n_samples` samples from each of the
* `n_channels` input buffers and fills the corresponding `n_samples` samples
@ -95,7 +93,7 @@ void bw_vol_process(bw_vol *instance, const float **x, float **y, int n_channels
/*! ...
* #### bw_vol_set_volume()
* ```>>> */
void bw_vol_set_volume(bw_vol *instance, float value);
static inline void bw_vol_set_volume(bw_vol_coeffs *BW_RESTRICT coeffs, float value);
/*! <<<```
* Sets the volume parameter to the given `value` (range [`0.f`, `1.f`]) for
* the given `instance`.
@ -106,29 +104,65 @@ void bw_vol_set_volume(bw_vol *instance, float value);
* Default value: `1.f`.
* }}} */
/* WARNING: this definition is not part of the public API. Please, do not use
* it. */
#define _BW_VOL_BUFFER_SIZE 32
/*** 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. */
#include <bw_math.h>
#include <bw_slew_lim.h>
struct _bw_vol_coeffs {
// Sub-components
bw_slew_lim_coeffs smooth_coeffs;
bw_slew_lim_state smooth_state;
/* WARNING: the internal definition of this struct is not part of the public
* API. Its content may change at any time in future versions. Please, do not
* access its members directly. */
struct _bw_vol {
// Coefficients
float max_var;
float k;
// Parameters
float volume;
float volume_cur;
// State
char first_run;
// Buffers
float buf[_BW_VOL_BUFFER_SIZE];
};
static inline void bw_vol_init(bw_vol_coeffs *BW_RESTRICT coeffs) {
bw_slew_lim_init(&coeffs->smooth_coeffs);
bw_slew_lim_set_max_rate(&coeffs->smooth_coeffs, 1.f / 0.2f);
coeffs->volume = 1.f;
}
static inline void bw_vol_set_sample_rate(bw_vol_coeffs *BW_RESTRICT coeffs, float sample_rate) {
bw_slew_lim_set_sample_rate(&coeffs->smooth_coeffs, sample_rate);
bw_slew_lim_reset_coeffs(&coeffs->smooth_coeffs);
}
static inline void bw_vol_reset_coeffs(bw_vol_coeffs *BW_RESTRICT coeffs) {
bw_slew_lim_reset_state(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->volume);
}
static inline void bw_vol_update_coeffs_ctrl(bw_vol_coeffs *BW_RESTRICT coeffs) {
}
static inline void bw_vol_update_coeffs_audio(bw_vol_coeffs *BW_RESTRICT coeffs) {
// tracking parameter changes is more trouble than it's worth
float v = bw_slew_lim_process1(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->volume);
coeffs->k = v * v * v;
}
static inline float bw_vol_process1(const bw_vol_coeffs *BW_RESTRICT coeffs, float x) {
return coeffs->k * x;
}
static inline void bw_vol_process(bw_vol_coeffs *BW_RESTRICT coeffs, const float *x, float *y, int n_samples) {
for (int i = 0; i < n_samples; i++) {
bw_vol_update_coeffs_audio(coeffs);
y[i] = bw_vol_process1(coeffs, x[i]);
}
}
static inline void bw_vol_set_volume(bw_vol_coeffs *BW_RESTRICT coeffs, float value) {
coeffs->volume = value;
}
#ifdef __cplusplus
}
#endif

View File

@ -1,27 +0,0 @@
/*
* 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
*
* File author: Stefano D'Angelo
*/
#include <bw_osc_sin.h>
#include <bw_math.h>
void bw_osc_sin_process(const float *x, float* y, int n_samples) {
for (int i = 0; i < n_samples; i++)
y[i] = bw_sinf_3(6.283185307179586f * x[i]);
}

View File

@ -1,61 +0,0 @@
/*
* 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
*
* File author: Stefano D'Angelo
*/
#include <bw_vol.h>
#include <bw_inline_slew_lim.h>
void bw_vol_init(bw_vol *instance) {
instance->volume = 1.f;
}
void bw_vol_set_sample_rate(bw_vol *instance, float sample_rate) {
instance->max_var = bw_inline_slew_lim_get_max_var(sample_rate, 1.f / 0.05f);
}
void bw_vol_reset(bw_vol *instance) {
instance->first_run = 1;
}
void bw_vol_process(bw_vol *instance, const float **x, float **y, int n_channels, int n_samples) {
if (instance->first_run) {
instance->volume_cur = instance->volume;
instance->first_run = 0;
}
for (int i = 0; i < n_samples; i += _BW_VOL_BUFFER_SIZE) {
const uint32_t n = bw_minu32(n_samples - i, _BW_VOL_BUFFER_SIZE);
for (int j = 0; j < _BW_VOL_BUFFER_SIZE; j++) {
instance->volume_cur = bw_inline_slew_lim(instance->volume, instance->volume_cur, instance->max_var, instance->max_var);
instance->buf[j] = instance->volume_cur * instance->volume_cur * instance->volume_cur;
}
for (int j = 0; j < n_channels; j++) {
const float *in = x[j] + i;
float *out = y[j] + i;
for (int k = 0; k < _BW_VOL_BUFFER_SIZE; k++)
out[k] = instance->buf[k] * in[k];
}
}
}
void bw_vol_set_volume(bw_vol *instance, float value) {
instance->volume = value;
}