From 7a2630951fe020e5422b2a117a391560e2d9d385 Mon Sep 17 00:00:00 2001 From: Stefano D'Angelo Date: Sat, 12 Aug 2023 17:20:34 +0200 Subject: [PATCH] polished bw_{balance,bd_reduce,one_pole} + removed bwpp_{balance,bd_reduce} + fixed fxpp_bitcrush --- .../src/bw_example_fxpp_bitcrush.h | 2 +- include/bw_balance.h | 125 +++++++++++++++--- include/bw_bd_reduce.h | 105 +++++++++++++-- include/bw_one_pole.h | 6 +- include/bwpp_balance.h | 93 ------------- include/bwpp_bd_reduce.h | 83 ------------ 6 files changed, 205 insertions(+), 209 deletions(-) delete mode 100644 include/bwpp_balance.h delete mode 100644 include/bwpp_bd_reduce.h diff --git a/examples/fxpp_bitcrush/src/bw_example_fxpp_bitcrush.h b/examples/fxpp_bitcrush/src/bw_example_fxpp_bitcrush.h index 1e752c0..366a88e 100644 --- a/examples/fxpp_bitcrush/src/bw_example_fxpp_bitcrush.h +++ b/examples/fxpp_bitcrush/src/bw_example_fxpp_bitcrush.h @@ -24,7 +24,7 @@ #include "platform.h" #include -#include +#include using namespace Brickworks; diff --git a/include/bw_balance.h b/include/bw_balance.h index cc98d3c..c12503d 100644 --- a/include/bw_balance.h +++ b/include/bw_balance.h @@ -29,8 +29,15 @@ *
    *
  • Version 1.0.0: *
      - *
    • Now using size_t instead of - * BW_SIZE_T.
    • + *
    • bw_balance_process() and + * bw_balance_process_multi() now use + * size_t to count samples and channels.
    • + *
    • Added more const specifiers to input + * arguments.
    • + *
    • Moved C++ code to C header.
    • + *
    • Added overladed C++ process() function taking + * C-style arrays as arguments.
    • + *
    • Removed usage of reserved identifiers.
    • *
    *
  • *
  • Version 0.6.0: @@ -56,8 +63,8 @@ * }}} */ -#ifndef _BW_BALANCE_H -#define _BW_BALANCE_H +#ifndef BW_BALANCE_H +#define BW_BALANCE_H #include @@ -68,7 +75,7 @@ extern "C" { /*! api {{{ * #### bw_balance_coeffs * ```>>> */ -typedef struct _bw_balance_coeffs bw_balance_coeffs; +typedef struct bw_balance_coeffs bw_balance_coeffs; /*! <<<``` * Coefficients and related. * @@ -112,7 +119,7 @@ static inline void bw_balance_process1(const bw_balance_coeffs *BW_RESTRICT coef * * #### 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); +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, size_t 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` @@ -121,7 +128,7 @@ static inline void bw_balance_process(bw_balance_coeffs *BW_RESTRICT coeffs, con * * #### bw_balance_process_multi() * ```>>> */ -static inline void bw_balance_process_multi(bw_balance_coeffs *BW_RESTRICT coeffs, const float **x_l, const float **x_r, float **y_l, float **y_r, int n_channels, int n_samples); +static inline void bw_balance_process_multi(bw_balance_coeffs *BW_RESTRICT coeffs, const float * const *x_l, const float * const *x_r, float **y_l, float **y_r, size_t n_channels, size_t n_samples); /*! <<<``` * Processes the first `n_samples` of the `n_channels` input buffers `x_l` * (left) and `x_r` (right) and fills the first `n_samples` of the @@ -154,7 +161,7 @@ static inline void bw_balance_set_balance(bw_balance_coeffs *BW_RESTRICT coeffs, extern "C" { #endif -struct _bw_balance_coeffs { +struct bw_balance_coeffs { // Sub-components bw_gain_coeffs l_coeffs; bw_gain_coeffs r_coeffs; @@ -175,7 +182,7 @@ static inline void bw_balance_set_sample_rate(bw_balance_coeffs *BW_RESTRICT coe bw_gain_set_sample_rate(&coeffs->r_coeffs, sample_rate); } -static inline void _bw_balance_do_update_coeffs(bw_balance_coeffs *BW_RESTRICT coeffs, char force) { +static inline void bw_balance_do_update_coeffs(bw_balance_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)); @@ -184,13 +191,13 @@ static inline void _bw_balance_do_update_coeffs(bw_balance_coeffs *BW_RESTRICT c } static inline void bw_balance_reset_coeffs(bw_balance_coeffs *BW_RESTRICT coeffs) { - _bw_balance_do_update_coeffs(coeffs, 1); + 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_balance_do_update_coeffs(coeffs, 0); bw_gain_update_coeffs_ctrl(&coeffs->l_coeffs); bw_gain_update_coeffs_ctrl(&coeffs->r_coeffs); } @@ -205,19 +212,19 @@ static inline void bw_balance_process1(const bw_balance_coeffs *BW_RESTRICT coef *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){ +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, size_t n_samples){ bw_balance_update_coeffs_ctrl(coeffs); - for (int i = 0; i < n_samples; i++) { + for (size_t i = 0; i < n_samples; i++) { bw_balance_update_coeffs_audio(coeffs); bw_balance_process1(coeffs, x_l[i], x_r[i], y_l + i, y_r + i); } } -static inline void bw_balance_process_multi(bw_balance_coeffs *BW_RESTRICT coeffs, const float **x_l, const float **x_r, float **y_l, float **y_r, int n_channels, int n_samples) { +static inline void bw_balance_process_multi(bw_balance_coeffs *BW_RESTRICT coeffs, const float * const *x_l, const float * const *x_r, float **y_l, float **y_r, size_t n_channels, size_t n_samples) { bw_balance_update_coeffs_ctrl(coeffs); - for (int i = 0; i < n_samples; i++) { + for (size_t i = 0; i < n_samples; i++) { bw_balance_update_coeffs_audio(coeffs); - for (int j = 0; j < n_channels; j++) + for (size_t j = 0; j < n_channels; j++) bw_balance_process1(coeffs, x_l[j][i], x_r[j][i], y_l[j] + i, y_r[j] + i); } } @@ -227,6 +234,92 @@ static inline void bw_balance_set_balance(bw_balance_coeffs *BW_RESTRICT coeffs, } #ifdef __cplusplus +} + +#include + +namespace Brickworks { + +/*** Public C++ API ***/ + +/*! api {{{ + * ##### Brickworks::Balance + * ```>>> */ +template +class Balance { +public: + Balance(); + + void setSampleRate(float sampleRate); + void reset(); + void process( + const float * const *x_l, + const float * const *x_r, + float **y_l, + float **y_r, + size_t nSamples); + void process( + std::array x_l, + std::array x_r, + std::array y_l, + std::array y_r, + size_t nSamples); + + void setBalance(float value); +/*! <<<... + * } + * ``` + * }}} */ + +/*** 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. */ + +private: + bw_balance_coeffs coeffs; +}; + +template +inline Balance::Balance() { + bw_balance_init(&coeffs); +} + +template +inline void Balance::setSampleRate(float sampleRate) { + bw_balance_set_sample_rate(&coeffs, sampleRate); +} + +template +inline void Balance::reset() { + bw_balance_reset_coeffs(&coeffs); +} + +template +inline void Balance::process( + const float * const *x_l, + const float * const *x_r, + float **y_l, + float **y_r, + size_t nSamples) { + bw_balance_process_multi(&coeffs, x_l, x_r, y_l, y_r, N_CHANNELS, nSamples); +} + +template +inline void Balance::process( + std::array x_l, + std::array x_r, + std::array y_l, + std::array y_r, + size_t nSamples) { + process(x_l.data(), x_r.data(), y_l.data(), y_r.data(), nSamples); +} + +template +inline void Balance::setBalance(float value) { + bw_balance_set_balance(&coeffs, value); +} + } #endif diff --git a/include/bw_bd_reduce.h b/include/bw_bd_reduce.h index 8d38676..d86d47d 100644 --- a/include/bw_bd_reduce.h +++ b/include/bw_bd_reduce.h @@ -33,8 +33,15 @@ *
      *
    • Version 1.0.0: *
        - *
      • Now using size_t instead of - * BW_SIZE_T.
      • + *
      • bw_bd_reduce_process() and + * bw_bd_reduce_process_multi() now use + * size_t to count samples and channels.
      • + *
      • Added more const specifiers to input + * arguments.
      • + *
      • Moved C++ code to C header.
      • + *
      • Added overladed C++ process() function taking + * C-style arrays as arguments.
      • + *
      • Removed usage of reserved identifiers.
      • *
      *
    • *
    • Version 0.6.0: @@ -62,8 +69,8 @@ * }}} */ -#ifndef _BW_BD_REDUCE_H -#define _BW_BD_REDUCE_H +#ifndef BW_BD_REDUCE_H +#define BW_BD_REDUCE_H #include @@ -74,7 +81,7 @@ extern "C" { /*! api {{{ * #### bw_bd_reduce_coeffs * ```>>> */ -typedef struct _bw_bd_reduce_coeffs bw_bd_reduce_coeffs; +typedef struct bw_bd_reduce_coeffs bw_bd_reduce_coeffs; /*! <<<``` * Coefficients and related. * @@ -111,7 +118,7 @@ static inline float bw_bd_reduce_process1(const bw_bd_reduce_coeffs *BW_RESTRICT * * #### bw_bd_reduce_process() * ```>>> */ -static inline void bw_bd_reduce_process(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float *x, float *y, int n_samples); +static inline void bw_bd_reduce_process(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, 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 @@ -119,7 +126,7 @@ static inline void bw_bd_reduce_process(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, * * #### bw_bd_reduce_process_multi() * ```>>> */ -static inline void bw_bd_reduce_process_multi(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float **x, float **y, int n_channels, int n_samples); +static inline void bw_bd_reduce_process_multi(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float * const *x, float **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 @@ -149,7 +156,7 @@ static inline void bw_bd_reduce_set_bit_depth(bw_bd_reduce_coeffs *BW_RESTRICT c extern "C" { #endif -struct _bw_bd_reduce_coeffs { +struct bw_bd_reduce_coeffs { // Coefficients float ki; float k; @@ -186,16 +193,16 @@ static inline float bw_bd_reduce_process1(const bw_bd_reduce_coeffs *BW_RESTRICT return coeffs->ki * (bw_floorf(coeffs->k * bw_clipf(x, -coeffs->max, coeffs->max)) + 0.5f); } -static inline void bw_bd_reduce_process(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float *x, float *y, int n_samples) { +static inline void bw_bd_reduce_process(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float *x, float *y, size_t n_samples) { bw_bd_reduce_update_coeffs_ctrl(coeffs); - for (int i = 0; i < n_samples; i++) + for (size_t i = 0; i < n_samples; i++) y[i] = bw_bd_reduce_process1(coeffs, x[i]); } -static inline void bw_bd_reduce_process_multi(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float **x, float **y, int n_channels, int n_samples) { +static inline void bw_bd_reduce_process_multi(bw_bd_reduce_coeffs *BW_RESTRICT coeffs, const float * const *x, float **y, size_t n_channels, size_t n_samples) { bw_bd_reduce_update_coeffs_ctrl(coeffs); - for (int i = 0; i < n_samples; i++) - for (int j = 0; j < n_channels; j++) + for (size_t i = 0; i < n_samples; i++) + for (size_t j = 0; j < n_channels; j++) y[j][i] = bw_bd_reduce_process1(coeffs, x[j][i]); } @@ -204,6 +211,78 @@ static inline void bw_bd_reduce_set_bit_depth(bw_bd_reduce_coeffs *BW_RESTRICT c } #ifdef __cplusplus +} + +#include + +namespace Brickworks { + +/*** Public C++ API ***/ + +/*! api {{{ + * ##### Brickworks::BDReduce + * ```>>> */ +template +class BDReduce { +public: + BDReduce(); + + void reset(); + void process( + const float * const *x, + float **y, + size_t nSamples); + void process( + std::array x, + std::array y, + size_t nSamples); + + void setBitDepth(char value); +/*! <<<... + * } + * ``` + * }}} */ + +/*** 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. */ + +private: + bw_bd_reduce_coeffs coeffs; +}; + +template +inline BDReduce::BDReduce() { + bw_bd_reduce_init(&coeffs); +} + +template +inline void BDReduce::reset() { + bw_bd_reduce_reset_coeffs(&coeffs); +} + +template +inline void BDReduce::process( + const float * const *x, + float **y, + size_t nSamples) { + bw_bd_reduce_process_multi(&coeffs, x, y, N_CHANNELS, nSamples); +} + +template +inline void BDReduce::process( + std::array x, + std::array y, + size_t nSamples) { + process(x.data(), y.data(), nSamples); +} + +template +inline void BDReduce::setBitDepth(char value) { + bw_bd_reduce_set_bit_depth(&coeffs, value); +} + } #endif diff --git a/include/bw_one_pole.h b/include/bw_one_pole.h index 0ef9b57..3e4c8e5 100644 --- a/include/bw_one_pole.h +++ b/include/bw_one_pole.h @@ -421,7 +421,7 @@ static inline void bw_one_pole_set_sample_rate(bw_one_pole_coeffs *BW_RESTRICT c BW_ASSERT_DEEP(coeffs->state == bw_one_pole_coeffs_state_set_sample_rate); } -static inline void _bw_one_pole_do_update_coeffs_ctrl(bw_one_pole_coeffs *BW_RESTRICT coeffs) { +static inline void bw_one_pole_do_update_coeffs_ctrl(bw_one_pole_coeffs *BW_RESTRICT coeffs) { if (coeffs->param_changed) { if (coeffs->param_changed & BW_ONE_POLE_PARAM_CUTOFF_UP) coeffs->mA1u = coeffs->cutoff_up > 1.591549430918953e8f ? 0.f : bw_expf(coeffs->Ttm2pi * coeffs->cutoff_up); @@ -441,7 +441,7 @@ static inline void bw_one_pole_reset_coeffs(bw_one_pole_coeffs *BW_RESTRICT coef BW_ASSERT_DEEP(coeffs->state >= bw_one_pole_coeffs_state_set_sample_rate); coeffs->param_changed = ~0; - _bw_one_pole_do_update_coeffs_ctrl(coeffs); + bw_one_pole_do_update_coeffs_ctrl(coeffs); #ifdef BW_DEBUG_DEEP coeffs->state = _bw_one_pole_coeffs_state_reset_coeffs; @@ -476,7 +476,7 @@ static inline void bw_one_pole_update_coeffs_ctrl(bw_one_pole_coeffs *BW_RESTRIC BW_ASSERT_DEEP(bw_one_pole_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_one_pole_coeffs_state_reset_coeffs); - _bw_one_pole_do_update_coeffs_ctrl(coeffs); + bw_one_pole_do_update_coeffs_ctrl(coeffs); BW_ASSERT_DEEP(bw_one_pole_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_one_pole_coeffs_state_reset_coeffs); diff --git a/include/bwpp_balance.h b/include/bwpp_balance.h deleted file mode 100644 index d11eb02..0000000 --- a/include/bwpp_balance.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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 . - * - * File author: Stefano D'Angelo - */ - -#ifndef BWPP_BALANCE_H -#define BWPP_BALANCE_H - -#include -#include - -namespace Brickworks { - -/*! api {{{ - * ##### Brickworks::Balance - * ```>>> */ -template -class Balance { -public: - Balance(); - - void setSampleRate(float sampleRate); - void reset(); - void process( - std::array x_l, - std::array x_r, - std::array y_l, - std::array y_r, - int nSamples); - - void setBalance(float value); -/*! <<<... - * } - * ``` - * }}} */ - -/*** 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. */ - -private: - bw_balance_coeffs coeffs; -}; - -template -inline Balance::Balance() { - bw_balance_init(&coeffs); -} - -template -inline void Balance::setSampleRate(float sampleRate) { - bw_balance_set_sample_rate(&coeffs, sampleRate); -} - -template -inline void Balance::reset() { - bw_balance_reset_coeffs(&coeffs); -} - -template -inline void Balance::process( - std::array x_l, - std::array x_r, - std::array y_l, - std::array y_r, - int nSamples) { - bw_balance_process_multi(&coeffs, x_l.data(), x_r.data(), y_l.data(), y_r.data(), N_CHANNELS, nSamples); -} - -template -inline void Balance::setBalance(float value) { - bw_balance_set_balance(&coeffs, value); -} - -} - -#endif diff --git a/include/bwpp_bd_reduce.h b/include/bwpp_bd_reduce.h deleted file mode 100644 index d8c3514..0000000 --- a/include/bwpp_bd_reduce.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 . - * - * File author: Stefano D'Angelo - */ - -#ifndef BWPP_BD_REDUCE_H -#define BWPP_BD_REDUCE_H - -#include -#include - -namespace Brickworks { - -/*! api {{{ - * ##### Brickworks::BDReduce - * ```>>> */ -template -class BDReduce { -public: - BDReduce(); - - void reset(); - void process( - std::array x, - std::array y, - int nSamples); - - void setBitDepth(char value); -/*! <<<... - * } - * ``` - * }}} */ - -/*** 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. */ - -private: - bw_bd_reduce_coeffs coeffs; -}; - -template -inline BDReduce::BDReduce() { - bw_bd_reduce_init(&coeffs); -} - -template -inline void BDReduce::reset() { - bw_bd_reduce_reset_coeffs(&coeffs); -} - -template -inline void BDReduce::process( - std::array x, - std::array y, - int nSamples) { - bw_bd_reduce_process_multi(&coeffs, x.data(), y.data(), N_CHANNELS, nSamples); -} - -template -inline void BDReduce::setBitDepth(char value) { - bw_bd_reduce_set_bit_depth(&coeffs, value); -} - -} - -#endif