/* * Brickworks * * Copyright (C) 2023, 2024 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 */ /*! * module_type {{{ dsp }}} * version {{{ 1.2.0 }}} * requires {{{ bw_common bw_gain bw_math bw_one_pole }}} * description {{{ * Dry/wet mixer. * }}} * changelog {{{ * * }}} */ #ifndef BW_DRY_WET_H #define BW_DRY_WET_H #include #ifdef __cplusplus extern "C" { #endif /*! api {{{ * #### bw_dry_wet_coeffs * ```>>> */ typedef struct bw_dry_wet_coeffs bw_dry_wet_coeffs; /*! <<<``` * Coefficients and related. * * #### bw_dry_wet_init() * ```>>> */ static inline void bw_dry_wet_init( bw_dry_wet_coeffs * BW_RESTRICT coeffs); /*! <<<``` * Initializes input parameter values in `coeffs`. * * #### bw_dry_wet_set_sample_rate() * ```>>> */ static inline void bw_dry_wet_set_sample_rate( bw_dry_wet_coeffs * BW_RESTRICT coeffs, float sample_rate); /*! <<<``` * Sets the `sample_rate` (Hz) value in `coeffs`. * * #### bw_dry_wet_reset_coeffs() * ```>>> */ static inline void bw_dry_wet_reset_coeffs( bw_dry_wet_coeffs * BW_RESTRICT coeffs); /*! <<<``` * Resets coefficients in `coeffs` to assume their target values. * * #### bw_dry_wet_update_coeffs_ctrl() * ```>>> */ static inline void bw_dry_wet_update_coeffs_ctrl( bw_dry_wet_coeffs * BW_RESTRICT coeffs); /*! <<<``` * Triggers control-rate update of coefficients in `coeffs`. * * #### bw_dry_wet_update_coeffs_audio() * ```>>> */ static inline void bw_dry_wet_update_coeffs_audio( bw_dry_wet_coeffs * BW_RESTRICT coeffs); /*! <<<``` * Triggers audio-rate update of coefficients in `coeffs`. * * #### bw_dry_wet_process1() * ```>>> */ static inline float bw_dry_wet_process1( const bw_dry_wet_coeffs * BW_RESTRICT coeffs, float x_dry, float x_wet); /*! <<<``` * Processes one dry input sample `x_dry` and one wet input sample `x_wet` * using `coeffs` and returns the corresponding output sample. * * #### bw_dry_wet_process() * ```>>> */ static inline void bw_dry_wet_process( bw_dry_wet_coeffs * BW_RESTRICT coeffs, const float * x_dry, const float * x_wet, float * y, size_t n_samples); /*! <<<``` * Processes the first `n_samples` of the dry input buffer `x_dry` and of the * wet input buffer `x_wet` and fills the first `n_samples` of the output * buffer `y`, while using and updating `coeffs` (control and audio rate). * * #### bw_dry_wet_process_multi() * ```>>> */ static inline void bw_dry_wet_process_multi( bw_dry_wet_coeffs * BW_RESTRICT coeffs, const float * const * x_dry, const float * const * x_wet, float * const * y, size_t n_channels, size_t n_samples); /*! <<<``` * Processes the first `n_samples` of the `n_channels` dry input buffers * `x_dry` and of the `n_channels` wet input buffers `x_wet`, and fills the * first `n_samples` of the `n_channels` output buffers `y`, while using and * updating the common `coeffs` (control and audio rate). * * #### bw_dry_wet_set_wet() * ```>>> */ static inline void bw_dry_wet_set_wet( bw_dry_wet_coeffs * BW_RESTRICT coeffs, float value); /*! <<<``` * Sets the wet gain parameter to the given `value` (linear gain) in `coeffs`. * * Valid range: [`0.f`, `1.f`]. * * Default value: `1.f`. * * #### bw_dry_wet_set_smooth_tau() * ```>>> */ static inline void bw_dry_wet_set_smooth_tau( bw_dry_wet_coeffs * BW_RESTRICT coeffs, float value); /*! <<<``` * Sets the smoothing time constant `value` (s) in `coeffs`. * * `value` must be non-negative. * * Default value: `0.05f`. * * #### bw_dry_wet_get_wet() * ```>>> */ static inline float bw_dry_wet_get_wet( const bw_dry_wet_coeffs * BW_RESTRICT coeffs); /*! <<<``` * Returns the current wet parameter value (linear gain) in `coeffs`. * * #### bw_dry_wet_get_wet_cur() * ```>>> */ static inline float bw_dry_wet_get_wet_cur( const bw_dry_wet_coeffs * BW_RESTRICT coeffs); /*! <<<``` * Returns the actual current wet coefficient (linear gain) in `coeffs`. * * `coeffs` must be at least in the "reset" state. * * #### bw_dry_wet_coeffs_is_valid() * ```>>> */ static inline char bw_dry_wet_coeffs_is_valid( const bw_dry_wet_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_dry_wet_coeffs`. * }}} */ #ifdef __cplusplus } #endif /*** 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 #ifdef __cplusplus extern "C" { #endif #ifdef BW_DEBUG_DEEP enum bw_dry_wet_coeffs_state { bw_dry_wet_coeffs_state_invalid, bw_dry_wet_coeffs_state_init, bw_dry_wet_coeffs_state_set_sample_rate, bw_dry_wet_coeffs_state_reset_coeffs }; #endif struct bw_dry_wet_coeffs { #ifdef BW_DEBUG_DEEP uint32_t hash; enum bw_dry_wet_coeffs_state state; #endif // Sub-components bw_gain_coeffs gain_coeffs; }; static inline void bw_dry_wet_init( bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); bw_gain_init(&coeffs->gain_coeffs); #ifdef BW_DEBUG_DEEP coeffs->hash = bw_hash_sdbm("bw_dry_wet_coeffs"); coeffs->state = bw_dry_wet_coeffs_state_init; #endif BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state == bw_dry_wet_coeffs_state_init); } static inline void bw_dry_wet_set_sample_rate( bw_dry_wet_coeffs * BW_RESTRICT coeffs, float sample_rate) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_init); BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f); bw_gain_set_sample_rate(&coeffs->gain_coeffs, sample_rate); #ifdef BW_DEBUG_DEEP coeffs->state = bw_dry_wet_coeffs_state_set_sample_rate; #endif BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state == bw_dry_wet_coeffs_state_set_sample_rate); } static inline void bw_dry_wet_reset_coeffs( bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_set_sample_rate); bw_gain_reset_coeffs(&coeffs->gain_coeffs); #ifdef BW_DEBUG_DEEP coeffs->state = bw_dry_wet_coeffs_state_reset_coeffs; #endif BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state == bw_dry_wet_coeffs_state_reset_coeffs); } static inline void bw_dry_wet_update_coeffs_ctrl( bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); bw_gain_update_coeffs_ctrl(&coeffs->gain_coeffs); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); } static inline void bw_dry_wet_update_coeffs_audio( bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); bw_gain_update_coeffs_audio(&coeffs->gain_coeffs); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); } static inline float bw_dry_wet_process1( const bw_dry_wet_coeffs * BW_RESTRICT coeffs, float x_dry, float x_wet) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); BW_ASSERT(bw_is_finite(x_dry)); BW_ASSERT(bw_is_finite(x_wet)); const float y = bw_gain_process1(&coeffs->gain_coeffs, x_wet - x_dry) + x_dry; BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); BW_ASSERT(bw_is_finite(y)); return y; } static inline void bw_dry_wet_process( bw_dry_wet_coeffs * BW_RESTRICT coeffs, const float * x_dry, const float * x_wet, float * y, size_t n_samples) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); BW_ASSERT(x_dry != BW_NULL); BW_ASSERT_DEEP(bw_has_only_finite(x_dry, n_samples)); BW_ASSERT(x_wet != BW_NULL); BW_ASSERT_DEEP(bw_has_only_finite(x_wet, n_samples)); BW_ASSERT(y != BW_NULL); bw_dry_wet_update_coeffs_ctrl(coeffs); for (size_t i = 0; i < n_samples; i++) { bw_dry_wet_update_coeffs_audio(coeffs); y[i] = bw_dry_wet_process1(coeffs, x_dry[i], x_wet[i]); } BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); BW_ASSERT_DEEP(bw_has_only_finite(y, n_samples)); } static inline void bw_dry_wet_process_multi( bw_dry_wet_coeffs * BW_RESTRICT coeffs, const float * const * x_dry, const float * const * x_wet, float * const * y, size_t n_channels, size_t n_samples) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); BW_ASSERT(x_dry != BW_NULL); BW_ASSERT(x_wet != BW_NULL); BW_ASSERT(y != BW_NULL); #ifndef BW_NO_DEBUG for (size_t i = 0; i < n_channels; i++) for (size_t j = i + 1; j < n_channels; j++) BW_ASSERT(y[i] != y[j]); for (size_t i = 0; i < n_channels; i++) for (size_t j = 0; j < n_channels; j++) { BW_ASSERT(i == j || x_dry[i] != y[j]); BW_ASSERT(i == j || x_wet[i] != y[j]); } #endif bw_dry_wet_update_coeffs_ctrl(coeffs); for (size_t i = 0; i < n_samples; i++) { bw_dry_wet_update_coeffs_audio(coeffs); for (size_t j = 0; j < n_channels; j++) y[j][i] = bw_dry_wet_process1(coeffs, x_dry[j][i], x_wet[j][i]); } BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); } static inline void bw_dry_wet_set_wet( bw_dry_wet_coeffs * BW_RESTRICT coeffs, float value) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_init); BW_ASSERT(bw_is_finite(value)); BW_ASSERT(value >= 0.f && value <= 1.f); bw_gain_set_gain_lin(&coeffs->gain_coeffs, value); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_init); } static inline void bw_dry_wet_set_smooth_tau( bw_dry_wet_coeffs * BW_RESTRICT coeffs, float value) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_init); BW_ASSERT(bw_is_finite(value)); BW_ASSERT(value >= 0.f); bw_gain_set_smooth_tau(&coeffs->gain_coeffs, value); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_init); } static inline float bw_dry_wet_get_wet( const bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); return bw_gain_get_gain_lin(&coeffs->gain_coeffs); } static inline float bw_dry_wet_get_wet_cur( const bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); BW_ASSERT_DEEP(bw_dry_wet_coeffs_is_valid(coeffs)); BW_ASSERT_DEEP(coeffs->state >= bw_dry_wet_coeffs_state_reset_coeffs); return bw_gain_get_gain_cur(&coeffs->gain_coeffs); } static inline char bw_dry_wet_coeffs_is_valid( const bw_dry_wet_coeffs * BW_RESTRICT coeffs) { BW_ASSERT(coeffs != BW_NULL); #ifdef BW_DEBUG_DEEP if (coeffs->hash != bw_hash_sdbm("bw_dry_wet_coeffs")) return 0; if (coeffs->state < bw_dry_wet_coeffs_state_init || coeffs->state > bw_dry_wet_coeffs_state_reset_coeffs) return 0; #endif return bw_gain_coeffs_is_valid(&coeffs->gain_coeffs); } #ifdef __cplusplus } #ifndef BW_CXX_NO_ARRAY # include #endif namespace Brickworks { /*** Public C++ API ***/ /*! api_cpp {{{ * ##### Brickworks::DryWet * ```>>> */ template class DryWet { public: DryWet(); void setSampleRate( float sampleRate); void reset(); void process( const float * const * xDry, const float * const * xWet, float * const * y, size_t nSamples); #ifndef BW_CXX_NO_ARRAY void process( std::array xDry, std::array xWet, std::array y, size_t nSamples); #endif void setWet( float value); void setSmoothTau( float value); float getWet(); float getWetCur(); /*! <<<... * } * ``` * }}} */ /*** 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_dry_wet_coeffs coeffs; }; template inline DryWet::DryWet() { bw_dry_wet_init(&coeffs); } template inline void DryWet::setSampleRate( float sampleRate) { bw_dry_wet_set_sample_rate(&coeffs, sampleRate); } template inline void DryWet::reset() { bw_dry_wet_reset_coeffs(&coeffs); } template inline void DryWet::process( const float * const * xDry, const float * const * xWet, float * const * y, size_t nSamples) { bw_dry_wet_process_multi(&coeffs, xDry, xWet, y, N_CHANNELS, nSamples); } #ifndef BW_CXX_NO_ARRAY template inline void DryWet::process( std::array xDry, std::array xWet, std::array y, size_t nSamples) { process(xDry.data(), xWet.data(), y.data(), nSamples); } #endif template inline void DryWet::setWet( float value) { bw_dry_wet_set_wet(&coeffs, value); } template inline void DryWet::setSmoothTau( float value) { bw_dry_wet_set_smooth_tau(&coeffs, value); } template inline float DryWet::getWet() { return bw_dry_wet_get_wet(&coeffs); } template inline float DryWet::getWetCur() { return bw_dry_wet_get_wet_cur(&coeffs); } } #endif #endif