new bw_pan and bw_balance + using BW_RESTRICT in process1 in bw_phase_gen and bw_svf

This commit is contained in:
Stefano D'Angelo 2023-01-16 19:50:27 +01:00
parent a5cb9b9c06
commit 46383fde17
5 changed files with 439 additions and 8 deletions

2
TODO
View File

@ -22,6 +22,8 @@ code:
* Q to slope and viceversa functions in 2nd order shelf filters? keep updated values (seamless switch, syncrhonicity)?
* treat unused variable/function warnings
* csch for bw_peak bandwidth -> Q
* sample rate-constant coeffs? (pan case)
* pan process with no out: should just reset coeffs?
build system:
* make makefiles handle paths with spaces etc

211
include/bw_balance.h Normal file
View File

@ -0,0 +1,211 @@
/*
* Brickworks
*
* Copyright (C) 2023 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
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
*
* File author: Stefano D'Angelo
*/
/*!
* module_type {{{ dsp }}}
* version {{{ 0.3.0 }}}
* requires {{{ bw_config bw_common bw_gain bw_math bw_one_pole }}}
* description {{{
* Stereo balance.
* }}}
* changelog {{{
* <ul>
* <li>Version <strong>0.3.0</strong>:
* <ul>
* <li>First release.</li>
* </ul>
* </li>
* </ul>
* }}}
*/
#ifndef _BW_BALANCE_H
#define _BW_BALANCE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <bw_common.h>
/*! api {{{
* #### bw_balance_coeffs
* ```>>> */
typedef struct _bw_balance_coeffs bw_balance_coeffs;
/*! <<<```
* Coefficients and related.
*
* #### bw_balance_init()
* ```>>> */
static inline void bw_balance_init(bw_balance_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Initializes input parameter values in `coeffs`.
*
* #### bw_balance_set_sample_rate()
* ```>>> */
static inline void bw_balance_set_sample_rate(bw_balance_coeffs *BW_RESTRICT coeffs, float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value in `coeffs`.
*
* #### bw_balance_reset_coeffs()
* ```>>> */
static inline void bw_balance_reset_coeffs(bw_balance_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Resets coefficients in `coeffs` to assume their target values.
*
* #### bw_balance_update_coeffs_ctrl()
* ```>>> */
static inline void bw_balance_update_coeffs_ctrl(bw_balance_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Triggers control-rate update of coefficients in `coeffs`.
*
* #### bw_balance_update_coeffs_audio()
* ```>>> */
static inline void bw_balance_update_coeffs_audio(bw_balance_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Triggers audio-rate update of coefficients in `coeffs`.
*
* #### bw_balance_process1()
* ```>>> */
static inline float bw_balance_process1(const bw_balance_coeffs *BW_RESTRICT coeffs, float x_l, float x_r, float *BW_RESTRICT y_l, float *BW_RESTRICT y_r);
/*! <<<```
* Processes one set of input samples `x_l` (left) and `x_r` (right) using
* `coeffs`, while using and updating `state`. The left and right output
* samples are put into `y_l` (left) and `y_r` (right) respectively.
*
* #### bw_balance_process()
* ```>>> */
static inline void bw_balance_process(bw_balance_coeffs *BW_RESTRICT coeffs, const float *x_l, const float *x_r, float *y_l, float *y_r, int n_samples);
/*! <<<```
* Processes the first `n_samples` of the input buffers `x_l` (left) and
* `x_r` (right) and fills the first `n_samples` of the output buffers `y_l`
* (left) and `y_r` (right), if they are not `NULL`, while using and updating
* `coeffs` (control and audio rate).
*
* #### bw_balance_set_balance()
* ```>>> */
static inline void bw_balance_set_balance(bw_balance_coeffs *BW_RESTRICT coeffs, float value);
/*! <<<```
* Sets the balance `value`, where `-1.f` corresponds to hard left balance,
* `0.f` to center balance, and `1.f` to hard right balance.
*
* Default value: `0.f`.
* }}} */
/*** 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_gain.h>
struct _bw_balance_coeffs {
// Sub-components
bw_gain_coeffs l_coeffs;
bw_gain_coeffs r_coeffs;
// Parameters
float balance;
float balance_prev;
};
static inline void bw_balance_init(bw_balance_coeffs *BW_RESTRICT coeffs) {
bw_gain_init(&coeffs->l_coeffs);
bw_gain_init(&coeffs->r_coeffs);
coeffs->balance = 0.f;
}
static inline void bw_balance_set_sample_rate(bw_balance_coeffs *BW_RESTRICT coeffs, float sample_rate) {
bw_gain_set_sample_rate(&coeffs->l_coeffs, sample_rate);
bw_gain_set_sample_rate(&coeffs->r_coeffs, sample_rate);
}
static inline void _bw_balance_do_update_coeffs(bw_gain_coeffs *BW_RESTRICT coeffs, char force) {
if (force || coeffs->balance != coeffs->balance_prev) {
bw_gain_set_gain_lin(&coeffs->l_coeffs, bw_minf(1.f - coeffs->balance, 1.f));
bw_gain_set_gain_lin(&coeffs->r_coeffs, bw_minf(1.f + coeffs->balance, 1.f));
coeffs->balance_prev = coeffs->balance;
}
}
static inline void bw_balance_reset_coeffs(bw_balance_coeffs *BW_RESTRICT coeffs) {
_bw_balance_do_update_coeffs(coeffs, 1);
bw_gain_reset_coeffs(&coeffs->l_coeffs);
bw_gain_reset_coeffs(&coeffs->r_coeffs);
}
static inline void bw_balance_update_coeffs_ctrl(bw_balance_coeffs *BW_RESTRICT coeffs) {
_bw_balance_do_update_coeffs(coeffs, 0);
bw_gain_update_coeffs_ctrl(&coeffs->l_coeffs);
bw_gain_update_coeffs_ctrl(&coeffs->r_coeffs);
}
static inline void bw_balance_update_coeffs_audio(bw_balance_coeffs *BW_RESTRICT coeffs) {
bw_gain_update_coeffs_audio(&coeffs->l_coeffs);
bw_gain_update_coeffs_audio(&coeffs->r_coeffs);
}
static inline float bw_balance_process1(const bw_balance_coeffs *BW_RESTRICT coeffs, float x_l, float x_r, float *BW_RESTRICT y_l, float *BW_RESTRICT y_r) {
*y_l = bw_gain_process1(&coeffs->l_coeffs, x_l);
*y_r = bw_gain_process1(&coeffs->r_coeffs, x_r);
}
static inline void bw_balance_process(bw_balance_coeffs *BW_RESTRICT coeffs, const float *x_l, const float *x_r, float *y_l, float *y_r, int n_samples){
bw_balance_update_coeffs_ctrl(coeffs);
if (y_l != NULL) {
if (y_r != NULL) {
for (int i = 0; i < n_samples; i++) {
bw_balance_update_coeffs_audio(coeffs);
bw_balance_process1(coeffs, state, x_l[i], x_r[i], y_l + i, y_r + i);
}
} else {
for (int i = 0; i < n_samples; i++) {
bw_balance_update_coeffs_audio(coeffs);
float r;
bw_balance_process1(coeffs, state, x_l[i], x_r[i], y_l + i, &r);
}
}
} else {
if (y_r != NULL) {
for (int i = 0; i < n_samples; i++) {
bw_balance_update_coeffs_audio(coeffs);
float l;
bw_balance_process1(coeffs, state, x_l[i], x_r[i], &l, y_r + i);
}
} else {
for (int i = 0; i < n_samples; i++) {
bw_balance_update_coeffs_audio(coeffs);
float l, r;
bw_balance_process1(coeffs, state, x_l[i], x_r[i], &l, &r);
}
}
}
}
static inline void bw_balance_set_balance(bw_balance_coeffs *BW_RESTRICT coeffs, float value) {
coeffs->balance = value;
}
#ifdef __cplusplus
}
#endif
#endif

212
include/bw_pan.h Normal file
View File

@ -0,0 +1,212 @@
/*
* Brickworks
*
* Copyright (C) 2023 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
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
*
* File author: Stefano D'Angelo
*/
/*!
* module_type {{{ dsp }}}
* version {{{ 0.3.0 }}}
* requires {{{ bw_config bw_common bw_gain bw_math bw_one_pole }}}
* description {{{
* Stereo panner.
* }}}
* changelog {{{
* <ul>
* <li>Version <strong>0.3.0</strong>:
* <ul>
* <li>First release.</li>
* </ul>
* </li>
* </ul>
* }}}
*/
#ifndef _BW_PAN_H
#define _BW_PAN_H
#ifdef __cplusplus
extern "C" {
#endif
#include <bw_common.h>
/*! api {{{
* #### bw_pan_coeffs
* ```>>> */
typedef struct _bw_pan_coeffs bw_pan_coeffs;
/*! <<<```
* Coefficients and related.
*
* #### bw_pan_init()
* ```>>> */
static inline void bw_pan_init(bw_pan_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Initializes input parameter values in `coeffs`.
*
* #### bw_pan_set_sample_rate()
* ```>>> */
static inline void bw_pan_set_sample_rate(bw_pan_coeffs *BW_RESTRICT coeffs, float sample_rate);
/*! <<<```
* Sets the `sample_rate` (Hz) value in `coeffs`.
*
* #### bw_pan_reset_coeffs()
* ```>>> */
static inline void bw_pan_reset_coeffs(bw_pan_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Resets coefficients in `coeffs` to assume their target values.
*
* #### bw_pan_update_coeffs_ctrl()
* ```>>> */
static inline void bw_pan_update_coeffs_ctrl(bw_pan_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Triggers control-rate update of coefficients in `coeffs`.
*
* #### bw_pan_update_coeffs_audio()
* ```>>> */
static inline void bw_pan_update_coeffs_audio(bw_pan_coeffs *BW_RESTRICT coeffs);
/*! <<<```
* Triggers audio-rate update of coefficients in `coeffs`.
*
* #### bw_pan_process1()
* ```>>> */
static inline float bw_pan_process1(const bw_pan_coeffs *BW_RESTRICT coeffs, float x, float *BW_RESTRICT y_l, float *BW_RESTRICT y_r);
/*! <<<```
* Processes one input sample `x` using `coeffs`, while using and updating
* `state`. The left and right output samples are put into `y_l` (left) and
* `y_r` (right) respectively.
*
* #### bw_pan_process()
* ```>>> */
static inline void bw_pan_process(bw_pan_coeffs *BW_RESTRICT coeffs, const float *x, float *y_l, float *y_r, int n_samples);
/*! <<<```
* Processes the first `n_samples` of the input buffer `x` and fills the
* first `n_samples` of the output buffers `y_l` (left) and `y_r` (right), if
* they are not `NULL`, while using and updating `coeffs` (control and audio
* rate).
*
* #### bw_pan_set_pan()
* ```>>> */
static inline void bw_pan_set_pan(bw_pan_coeffs *BW_RESTRICT coeffs, float value);
/*! <<<```
* Sets the panning `value`, where `-1.f` corresponds to hard left pan, `0.f`
* to center pan, and `1.f` to hard right pan.
*
* Default value: `0.f`.
* }}} */
/*** 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_gain.h>
struct _bw_pan_coeffs {
// Sub-components
bw_gain_coeffs l_coeffs;
bw_gain_coeffs r_coeffs;
// Parameters
float pan;
float pan_prev;
};
static inline void bw_pan_init(bw_pan_coeffs *BW_RESTRICT coeffs) {
bw_gain_init(&coeffs->l_coeffs);
bw_gain_init(&coeffs->r_coeffs);
coeffs->pan = 0.f;
}
static inline void bw_pan_set_sample_rate(bw_pan_coeffs *BW_RESTRICT coeffs, float sample_rate) {
bw_gain_set_sample_rate(&coeffs->l_coeffs, sample_rate);
bw_gain_set_sample_rate(&coeffs->r_coeffs, sample_rate);
}
static inline void _bw_pan_do_update_coeffs(bw_gain_coeffs *BW_RESTRICT coeffs, char force) {
if (force || coeffs->pan != coeffs->pan_prev) {
const float k = 0.125f * coeffs->pan + 0.125f;
bw_gain_set_gain_lin(&coeffs->l_coeffs, bw_cos2pif_3(k));
bw_gain_set_gain_lin(&coeffs->r_coeffs, bw_sin2pif_3(k));
coeffs->pan_prev = coeffs->pan;
}
}
static inline void bw_pan_reset_coeffs(bw_pan_coeffs *BW_RESTRICT coeffs) {
_bw_pan_do_update_coeffs(coeffs, 1);
bw_gain_reset_coeffs(&coeffs->l_coeffs);
bw_gain_reset_coeffs(&coeffs->r_coeffs);
}
static inline void bw_pan_update_coeffs_ctrl(bw_pan_coeffs *BW_RESTRICT coeffs) {
_bw_pan_do_update_coeffs(coeffs, 0);
bw_gain_update_coeffs_ctrl(&coeffs->l_coeffs);
bw_gain_update_coeffs_ctrl(&coeffs->r_coeffs);
}
static inline void bw_pan_update_coeffs_audio(bw_pan_coeffs *BW_RESTRICT coeffs) {
bw_gain_update_coeffs_audio(&coeffs->l_coeffs);
bw_gain_update_coeffs_audio(&coeffs->r_coeffs);
}
static inline float bw_pan_process1(const bw_pan_coeffs *BW_RESTRICT coeffs, float x, float *BW_RESTRICT y_l, float *BW_RESTRICT y_r) {
*y_l = bw_gain_process1(&coeffs->l_coeffs, x);
*y_r = bw_gain_process1(&coeffs->r_coeffs, x);
}
static inline void bw_pan_process(bw_pan_coeffs *BW_RESTRICT coeffs, const float *x, float *y_l, float *y_r, int n_samples) {
bw_pan_update_coeffs_ctrl(coeffs);
if (y_l != NULL) {
if (y_r != NULL) {
for (int i = 0; i < n_samples; i++) {
bw_pan_update_coeffs_audio(coeffs);
bw_pan_process1(coeffs, state, x[i], y_l + i, y_r + i);
}
} else {
for (int i = 0; i < n_samples; i++) {
bw_pan_update_coeffs_audio(coeffs);
float r;
bw_pan_process1(coeffs, state, x[i], y_l + i, &r);
}
}
} else {
if (y_r != NULL) {
for (int i = 0; i < n_samples; i++) {
bw_pan_update_coeffs_audio(coeffs);
float l;
bw_pan_process1(coeffs, state, x[i], &l, y_r + i);
}
} else {
for (int i = 0; i < n_samples; i++) {
bw_pan_update_coeffs_audio(coeffs);
float l, r;
bw_pan_process1(coeffs, state, x[i], &l, &r);
}
}
}
}
static inline void bw_pan_set_pan(bw_pan_coeffs *BW_RESTRICT coeffs, float value) {
coeffs->pan = value;
}
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,7 +1,7 @@
/*
* Brickworks
*
* Copyright (C) 2022 Orastron Srl unipersonale
* Copyright (C) 2022, 2023 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
@ -20,7 +20,7 @@
/*!
* module_type {{{ dsp }}}
* version {{{ 0.2.0 }}}
* version {{{ 0.3.0 }}}
* requires {{{ bw_config bw_common bw_math bw_one_pole }}}
* description {{{
* Phase generator with portamento and exponential frequency modulation.
@ -29,6 +29,11 @@
* }}}
* changelog {{{
* <ul>
* <li>Version <strong>0.3.0</strong>:
* <ul>
* <li>Added `BW_RESTRICT` to `bw_phase_gen_process1\*()`.</li>
* </ul>
* </li>
* <li>Version <strong>0.2.0</strong>:
* <ul>
* <li>Refactored API.</li>
@ -103,8 +108,8 @@ static inline void bw_phase_gen_update_coeffs_audio(bw_phase_gen_coeffs *BW_REST
*
* #### bw_phase_gen_process1\*()
* ```>>> */
static inline void bw_phase_gen_process1(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float *y, float *y_phase_inc);
static inline void bw_phase_gen_process1_mod(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float x_mod, float *y, float *y_phase_inc);
static inline void bw_phase_gen_process1(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float *BW_RESTRICT y, float *BW_RESTRICT y_phase_inc);
static inline void bw_phase_gen_process1_mod(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float x_mod, float *BW_RESTRICT y, float *BW_RESTRICT y_phase_inc);
/*! <<<```
* These functions generate and return one sample using `coeffs`, while using
* and updating `state`, and put the corresponding phase increment value in
@ -214,12 +219,12 @@ static inline float _bw_phase_gen_update_phase(bw_phase_gen_state *BW_RESTRICT s
return state->phase;
}
static inline void bw_phase_gen_process1(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float *y, float *y_phase_inc) {
static inline void bw_phase_gen_process1(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float *BW_RESTRICT y, float *BW_RESTRICT y_phase_inc) {
*y_phase_inc = bw_one_pole_get_y_z1(&coeffs->portamento_state);
*y = _bw_phase_gen_update_phase(state, *y_phase_inc);
}
static inline void bw_phase_gen_process1_mod(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float x_mod, float *y, float *y_phase_inc) {
static inline void bw_phase_gen_process1_mod(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float x_mod, float *BW_RESTRICT y, float *BW_RESTRICT y_phase_inc) {
*y_phase_inc = bw_one_pole_get_y_z1(&coeffs->portamento_state) * bw_pow2f_3(x_mod);
*y = _bw_phase_gen_update_phase(state, *y_phase_inc);
}

View File

@ -31,6 +31,7 @@
* <li>Version <strong>0.3.0</strong>:
* <ul>
* <li>Added prewarping control.</li>
* <li>Added `BW_RESTRICT` to `bw_svf_process1()`.</li>
* </ul>
* </li>
* <li>Version <strong>0.2.0</strong>:
@ -107,7 +108,7 @@ static inline void bw_svf_update_coeffs_audio(bw_svf_coeffs *BW_RESTRICT coeffs)
*
* #### bw_svf_process1()
* ```>>> */
static inline void bw_svf_process1(const bw_svf_coeffs *BW_RESTRICT coeffs, bw_svf_state *BW_RESTRICT state, float x, float *y_lp, float *y_bp, float *y_hp);
static inline void bw_svf_process1(const bw_svf_coeffs *BW_RESTRICT coeffs, bw_svf_state *BW_RESTRICT state, float x, float *BW_RESTRICT y_lp, float *BW_RESTRICT y_bp, float *BW_RESTRICT y_hp);
/*! <<<```
* Processes one input sample `x` using `coeffs`, while using and updating
* `state`. The lowpass, bandpass, and highpass output samples are put into
@ -260,7 +261,7 @@ static inline void bw_svf_update_coeffs_audio(bw_svf_coeffs *BW_RESTRICT coeffs)
_bw_svf_do_update_coeffs(coeffs, 0);
}
static inline void bw_svf_process1(const bw_svf_coeffs *BW_RESTRICT coeffs, bw_svf_state *BW_RESTRICT state, float x, float *y_lp, float *y_bp, float *y_hp) {
static inline void bw_svf_process1(const bw_svf_coeffs *BW_RESTRICT coeffs, bw_svf_state *BW_RESTRICT state, float x, float *BW_RESTRICT y_lp, float *BW_RESTRICT y_bp, float *BW_RESTRICT y_hp) {
const float kk = coeffs->kf * state->cutoff_z1;
const float lp_xz1 = state->lp_z1 - kk * state->bp_z1;
const float bp_xz1 = state->bp_z1 - kk * state->hp_z1;