2022-11-15 23:49:51 +00:00
|
|
|
/*
|
|
|
|
* Brickworks
|
|
|
|
*
|
2023-04-02 19:54:17 +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 bw_math }}}
|
2022-11-15 23:49:51 +00:00
|
|
|
* description {{{
|
|
|
|
* Sawtooth oscillator waveshaper with PolyBLEP antialiasing.
|
2022-12-04 08:30:16 +00:00
|
|
|
*
|
|
|
|
* It turns a normalized phase signal, such as that geneated by
|
|
|
|
* [bw\_phase\_gen](bw_phase_gen), into a sawtooth wave.
|
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_saw_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>
|
2023-04-02 19:54:17 +00:00
|
|
|
* <li>Version <strong>0.4.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Fixed unused parameter warnings.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* <li>Version <strong>0.2.0</strong>:
|
|
|
|
* <ul>
|
2022-11-29 14:24:06 +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_SAW_H
|
|
|
|
#define _BW_OSC_SAW_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
|
|
|
|
|
|
|
|
/*! api {{{
|
2022-11-29 14:24:06 +00:00
|
|
|
* #### bw_osc_saw_coeffs
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-11-29 14:24:06 +00:00
|
|
|
typedef struct _bw_osc_saw_coeffs bw_osc_saw_coeffs;
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* Coefficients and related.
|
2022-11-29 14:24:06 +00:00
|
|
|
*
|
2022-11-19 01:25:38 +00:00
|
|
|
* #### bw_osc_saw_init()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-11-29 14:24:06 +00:00
|
|
|
static inline void bw_osc_saw_init(bw_osc_saw_coeffs *BW_RESTRICT coeffs);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* Initializes input parameter values in `coeffs`.
|
|
|
|
*
|
|
|
|
* #### bw_osc_saw_process1\*()
|
|
|
|
* ```>>> */
|
2022-11-29 14:24:06 +00:00
|
|
|
static inline float bw_osc_saw_process1(const bw_osc_saw_coeffs *BW_RESTRICT coeffs, float x);
|
|
|
|
static inline float bw_osc_saw_process1_antialias(const bw_osc_saw_coeffs *BW_RESTRICT coeffs, float x, float x_phase_inc);
|
2022-12-04 08:30:16 +00:00
|
|
|
/*! <<<```
|
|
|
|
* These function process one input sample `x`, indicating the normalized
|
|
|
|
* phase, using `coeffs`. They return the corresponding output sample.
|
|
|
|
*
|
|
|
|
* In particular:
|
|
|
|
* * `bw_osc_saw_process1()` assumes that antialiasing is disabled;
|
|
|
|
* * `bw_osc_saw_process1_antialias()` assumes that antialiasing is enabled
|
|
|
|
* and requires the corresponding phase increment value to be passed via
|
|
|
|
* `x_phase_inc`.
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_osc_saw_process()
|
|
|
|
* ```>>> */
|
2022-11-29 14:24:06 +00:00
|
|
|
static inline void bw_osc_saw_process(bw_osc_saw_coeffs *BW_RESTRICT coeffs, const float *x, const float *x_phase_inc, 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`, containing the
|
|
|
|
* normalized phase signal, and fills the first `n_samples` of the output
|
2023-07-04 07:34:37 +00:00
|
|
|
* buffer `y`, while using `coeffs`.
|
2022-12-04 08:30:16 +00:00
|
|
|
*
|
|
|
|
* If antialiasing is enabled, `x_phase_inc` must contain phase increment
|
|
|
|
* values, otherwise it is ignored and can be `NULL`.
|
|
|
|
*
|
2023-07-04 07:34:37 +00:00
|
|
|
* #### bw_osc_saw_process_multi()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_osc_saw_process_multi(bw_osc_saw_coeffs *BW_RESTRICT coeffs, const float **x, const float **x_phase_inc, float **y, int n_channels, int n_samples);
|
|
|
|
/*! <<<```
|
|
|
|
* Processes the first `n_samples` of the `n_channels` input buffers `x`,
|
|
|
|
* containing the normalized phase signals, and fills the first `n_samples`
|
|
|
|
* of the `n_samples` output buffers `y`, while using `coeffs`.
|
|
|
|
*
|
|
|
|
* If antialiasing is enabled, each of the `n_channels` buffers pointed by
|
|
|
|
* `x_phase_inc` must contain phase increment values, otherwise `x_phase_inc`
|
|
|
|
* is ignored and can be `NULL`.
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_osc_saw_set_antialiasing()
|
|
|
|
* ```>>> */
|
2022-11-29 14:24:06 +00:00
|
|
|
static inline void bw_osc_saw_set_antialiasing(bw_osc_saw_coeffs *BW_RESTRICT coeffs, char value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* Sets whether the antialiasing is on (`value` non-`0`) or off (`0`) in
|
|
|
|
* `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2022-12-04 08:30:16 +00:00
|
|
|
* Default value: `0` (off).
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}} */
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-29 14:24:06 +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. */
|
|
|
|
|
|
|
|
#include <bw_math.h>
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-11-29 14:24:06 +00:00
|
|
|
struct _bw_osc_saw_coeffs {
|
2022-11-19 01:25:38 +00:00
|
|
|
// Parameters
|
|
|
|
char antialiasing;
|
|
|
|
};
|
|
|
|
|
2022-11-29 14:24:06 +00:00
|
|
|
static inline void bw_osc_saw_init(bw_osc_saw_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
coeffs->antialiasing = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_osc_saw_process1(const bw_osc_saw_coeffs *BW_RESTRICT coeffs, float x) {
|
2023-04-02 19:54:17 +00:00
|
|
|
(void)coeffs;
|
2022-11-29 14:24:06 +00:00
|
|
|
return x + x - 1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PolyBLEP residual based on Parzen window (4th-order B-spline), one-sided (x in [0, 2])
|
|
|
|
static inline float _bw_osc_saw_blep_diff(float x) {
|
|
|
|
return x < 1.f
|
|
|
|
? x * ((0.25f * x - 0.6666666666666666f) * x * x + 1.333333333333333f) - 1.f
|
|
|
|
: x * (x * ((0.6666666666666666f - 0.08333333333333333f * x) * x - 2.f) + 2.666666666666667f) - 1.333333333333333f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_osc_saw_process1_antialias(const bw_osc_saw_coeffs *BW_RESTRICT coeffs, float x, float x_phase_inc) {
|
2023-04-02 19:54:17 +00:00
|
|
|
(void)coeffs;
|
2022-11-29 14:24:06 +00:00
|
|
|
const float s_1_m_phase = 1.f - x;
|
|
|
|
float v = x - s_1_m_phase;
|
|
|
|
if (x_phase_inc != 0.f) {
|
|
|
|
const float phase_inc_2 = x_phase_inc + x_phase_inc;
|
2023-08-10 06:00:44 +00:00
|
|
|
const float phase_inc_rcp = bw_rcpf(x_phase_inc);
|
2022-11-29 14:24:06 +00:00
|
|
|
if (s_1_m_phase < phase_inc_2)
|
|
|
|
v += _bw_osc_saw_blep_diff(s_1_m_phase * phase_inc_rcp);
|
|
|
|
if (x < phase_inc_2)
|
|
|
|
v -= _bw_osc_saw_blep_diff(x * phase_inc_rcp);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_saw_process(bw_osc_saw_coeffs *BW_RESTRICT coeffs, const float *x, const float *x_phase_inc, float *y, int n_samples) {
|
|
|
|
if (coeffs->antialiasing)
|
|
|
|
for (int i = 0; i < n_samples; i++)
|
|
|
|
y[i] = bw_osc_saw_process1_antialias(coeffs, x[i], x_phase_inc[i]);
|
|
|
|
else
|
|
|
|
for (int i = 0; i < n_samples; i++)
|
|
|
|
y[i] = bw_osc_saw_process1(coeffs, x[i]);
|
|
|
|
}
|
|
|
|
|
2023-07-04 07:34:37 +00:00
|
|
|
static inline void bw_osc_saw_process_multi(bw_osc_saw_coeffs *BW_RESTRICT coeffs, const float **x, const float **x_phase_inc, float **y, int n_channels, int n_samples) {
|
|
|
|
if (x_phase_inc != NULL)
|
|
|
|
for (int i = 0; i < n_channels; i++)
|
|
|
|
bw_osc_saw_process(coeffs, x[i], x_phase_inc[i], y[i], n_samples);
|
|
|
|
else
|
|
|
|
for (int i = 0; i < n_channels; i++)
|
|
|
|
bw_osc_saw_process(coeffs, x[i], NULL, y[i], n_samples);
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:24:06 +00:00
|
|
|
static inline void bw_osc_saw_set_antialiasing(bw_osc_saw_coeffs *BW_RESTRICT coeffs, char value) {
|
|
|
|
coeffs->antialiasing = value;
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|