2022-11-15 23:49:51 +00:00
|
|
|
/*
|
|
|
|
* Brickworks
|
|
|
|
*
|
2023-07-04 07:34:37 +00:00
|
|
|
* Copyright (C) 2022, 2023 Orastron Srl unipersonale
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* 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
|
2022-12-06 08:01:34 +00:00
|
|
|
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* File author: Stefano D'Angelo
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* module_type {{{ dsp }}}
|
2023-08-08 08:46:05 +00:00
|
|
|
* version {{{ 1.0.0 }}}
|
2023-07-21 06:56:27 +00:00
|
|
|
* requires {{{ bw_common }}}
|
2022-11-15 23:49:51 +00:00
|
|
|
* description {{{
|
|
|
|
* Post-filter to decolorate oscillator waveshapers when antialiasing is on.
|
|
|
|
*
|
2022-12-04 08:30:16 +00:00
|
|
|
* This <a href="https://en.wikipedia.org/wiki/Linear_time-invariant_system"
|
|
|
|
* target="_blank">linear time-invariant filter</a> can be added in series of
|
|
|
|
* oscillator waveshapers that use PolyBLEP antialiasing
|
|
|
|
* (i.e., [bw_osc_saw](bw_osc_saw), [bw_osc_pulse](bw_osc_pulse),
|
|
|
|
* [bw_osc_tri](bw_osc_tri)) to compensate for high-frequency attenuation.
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}}
|
|
|
|
* changelog {{{
|
|
|
|
* <ul>
|
2023-08-08 08:46:05 +00:00
|
|
|
* <li>Version <strong>1.0.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Now using <code>size_t</code> instead of
|
|
|
|
* <code>BW_SIZE_T</code>.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-07-21 06:56:27 +00:00
|
|
|
* <li>Version <strong>0.6.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Removed dependency on bw_config.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-07-04 07:34:37 +00:00
|
|
|
* <li>Version <strong>0.5.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Added <code>bw_osc_filt_process_multi()</code>.</li>
|
2023-07-13 10:35:37 +00:00
|
|
|
* <li>Added C++ wrapper.</li>
|
2023-07-04 07:34:37 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* <li>Version <strong>0.2.0</strong>:
|
|
|
|
* <ul>
|
2022-11-28 17:31:52 +00:00
|
|
|
* <li>Refactored API.</li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-15 23:49:51 +00:00
|
|
|
* <li>Version <strong>0.1.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>First release.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* }}}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BW_OSC_FILT_H
|
|
|
|
#define _BW_OSC_FILT_H
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#include <bw_common.h>
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 17:31:52 +00:00
|
|
|
/*! api {{{
|
|
|
|
* #### bw_osc_filt_state
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-11-28 17:31:52 +00:00
|
|
|
typedef struct _bw_osc_filt_state bw_osc_filt_state;
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* Internal state and related.
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_osc_filt_reset()
|
|
|
|
* ```>>> */
|
2022-11-28 17:31:52 +00:00
|
|
|
static inline void bw_osc_filt_reset_state(bw_osc_filt_state *BW_RESTRICT state);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* Resets the given `state` to its initial values.
|
|
|
|
*
|
|
|
|
* #### bw_osc_filt_process1()
|
|
|
|
* ```>>> */
|
2022-11-28 17:31:52 +00:00
|
|
|
static inline float bw_osc_filt_process1(bw_osc_filt_state *BW_RESTRICT state, float x);
|
2022-12-04 08:30:16 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Processes one input sample `x` usign and updating `state`. Returns the
|
|
|
|
* corresponding output sample.
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_osc_filt_process()
|
|
|
|
* ```>>> */
|
2023-06-07 13:41:20 +00:00
|
|
|
static inline void bw_osc_filt_process(bw_osc_filt_state *BW_RESTRICT state, const float *x, float *y, int n_samples);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* 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
|
|
|
|
* `state`.
|
2023-07-04 07:34:37 +00:00
|
|
|
*
|
|
|
|
* #### bw_osc_filt_process_multi()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_osc_filt_process_multi(bw_osc_filt_state **BW_RESTRICT state, const float **x, float **y, int n_channels, int 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
|
|
|
|
* using and updating each of the `n_channels` `state`s.
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}} */
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 17:31:52 +00:00
|
|
|
/*** 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. */
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 17:31:52 +00:00
|
|
|
struct _bw_osc_filt_state {
|
2022-11-19 01:25:38 +00:00
|
|
|
float x_z1;
|
|
|
|
float y_z1;
|
|
|
|
};
|
|
|
|
|
2022-11-28 17:31:52 +00:00
|
|
|
static inline void bw_osc_filt_reset_state(bw_osc_filt_state *BW_RESTRICT state) {
|
|
|
|
state->x_z1 = 0.f;
|
|
|
|
state->y_z1 = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_osc_filt_process1(bw_osc_filt_state *BW_RESTRICT state, float x) {
|
|
|
|
const float y = 1.371308261611209f * x + 0.08785458027104826f * state->x_z1 - 4.591628418822578e-1f * state->y_z1;
|
|
|
|
state->x_z1 = x;
|
|
|
|
state->y_z1 = y;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2023-06-07 13:41:20 +00:00
|
|
|
static inline void bw_osc_filt_process(bw_osc_filt_state *BW_RESTRICT state, const float *x, float *y, int n_samples) {
|
2022-11-28 17:31:52 +00:00
|
|
|
for (int i = 0; i < n_samples; i++)
|
|
|
|
y[i] = bw_osc_filt_process1(state, x[i]);
|
|
|
|
}
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2023-07-04 07:34:37 +00:00
|
|
|
static inline void bw_osc_filt_process_multi(bw_osc_filt_state **BW_RESTRICT state, const float **x, float **y, int n_channels, int n_samples) {
|
|
|
|
for (int i = 0; i < n_channels; i++)
|
|
|
|
bw_osc_filt_process(state[i], x[i], y[i], n_samples);
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|