2022-11-15 23:49:51 +00:00
|
|
|
/*
|
|
|
|
* Brickworks
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 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
|
|
|
|
*
|
|
|
|
* File author: Stefano D'Angelo
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* module_type {{{ dsp }}}
|
2022-11-19 01:25:38 +00:00
|
|
|
* version {{{ 0.2.0 }}}
|
2022-11-22 14:28:16 +00:00
|
|
|
* requires {{{ bw_config bw_common bw_one_pole bw_math }}}
|
2022-11-15 23:49:51 +00:00
|
|
|
* description {{{
|
|
|
|
* Triangle oscillator waveshaper with variable slope (increasing time over
|
|
|
|
* period) and PolyBLEP antialiasing.
|
|
|
|
* }}}
|
|
|
|
* changelog {{{
|
|
|
|
* <ul>
|
2022-11-19 01:25:38 +00:00
|
|
|
* <li>Version <strong>0.2.0</strong>:
|
|
|
|
* <ul>
|
2022-11-29 14:36:16 +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_TRI_H
|
|
|
|
#define _BW_OSC_TRI_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-11-29 14:36:16 +00:00
|
|
|
#include <bw_common.h>
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! api {{{
|
2022-11-29 14:36:16 +00:00
|
|
|
* #### bw_osc_tri_coeffs
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-11-29 14:36:16 +00:00
|
|
|
typedef struct _bw_osc_tri_coeffs bw_osc_tri_coeffs;
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-11-29 14:36:16 +00:00
|
|
|
* Coefficients.
|
|
|
|
*
|
2022-11-19 01:25:38 +00:00
|
|
|
* #### bw_osc_tri_init()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_init(bw_osc_tri_coeffs *BW_RESTRICT coeffs);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-11-29 14:36:16 +00:00
|
|
|
* Initializes the `coeffs`.
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_osc_tri_set_sample_rate()
|
|
|
|
* ```>>> */
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_set_sample_rate(bw_osc_tri_coeffs *BW_RESTRICT coeffs, float sample_rate);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-11-29 14:36:16 +00:00
|
|
|
* Sets the `sample_rate` (Hz) value for the given `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
* >>> */
|
|
|
|
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_reset_coeffs(bw_osc_tri_coeffs *BW_RESTRICT coeffs);
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_update_coeffs_ctrl(bw_osc_tri_coeffs *BW_RESTRICT coeffs);
|
|
|
|
static inline void bw_osc_tri_update_coeffs_audio(bw_osc_tri_coeffs *BW_RESTRICT coeffs);
|
|
|
|
|
|
|
|
static inline float bw_osc_tri_process1(const bw_osc_tri_coeffs *BW_RESTRICT coeffs, float x);
|
|
|
|
static inline float bw_osc_tri_process1_antialias(const bw_osc_tri_coeffs *BW_RESTRICT coeffs, float x, float x_phase_inc);
|
2022-11-15 23:49:51 +00:00
|
|
|
|
|
|
|
/*! ...
|
|
|
|
* #### bw_osc_tri_process()
|
|
|
|
* ```>>> */
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_process(bw_osc_tri_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
|
|
|
/*! <<<```
|
|
|
|
* Lets the given `instance` process `n_samples` samples from the input
|
|
|
|
* buffer `x` containing the normalized phase signal and fills the
|
|
|
|
* corresponding `n_samples` samples in the output buffer `y`.
|
|
|
|
*
|
|
|
|
* If antialiasing is on, `x_phase_inc` must contain phase increment values,
|
|
|
|
* otherwise it is ignored and can be `NULL`.
|
|
|
|
* >>> */
|
|
|
|
|
|
|
|
/*! ...
|
|
|
|
* #### bw_osc_tri_set_antialiasing()
|
|
|
|
* ```>>> */
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_set_antialiasing(bw_osc_tri_coeffs *BW_RESTRICT coeffs, char value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Sets whether the antialiasing is on (`value` non-`0`) or off (`0`) for the
|
|
|
|
* given `instance`.
|
|
|
|
*
|
|
|
|
* Default value: `0`.
|
|
|
|
* >>> */
|
|
|
|
|
|
|
|
/*! ...
|
2022-11-29 14:36:16 +00:00
|
|
|
* #### bw_osc_tri_set_tri_width()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_set_slope(bw_osc_tri_coeffs *BW_RESTRICT coeffs, float value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Sets the slope (increasing time over period) to `value` (range [`0.f`,
|
|
|
|
* `1.f`]) for the given `instance`.
|
|
|
|
*
|
|
|
|
* Default value: `0.5f`.
|
|
|
|
* }}} */
|
|
|
|
|
2022-11-29 14:36:16 +00:00
|
|
|
/*** Implementation ***/
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2022-11-29 14:36:16 +00:00
|
|
|
/* 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
|
|
|
|
2022-11-29 14:36:16 +00:00
|
|
|
#include <bw_math.h>
|
|
|
|
#include <bw_one_pole.h>
|
|
|
|
|
|
|
|
struct _bw_osc_tri_coeffs {
|
|
|
|
// Sub-components
|
|
|
|
bw_one_pole_coeffs smooth_coeffs;
|
|
|
|
bw_one_pole_state smooth_state;
|
|
|
|
|
|
|
|
// Parameters
|
|
|
|
char antialiasing;
|
|
|
|
float slope;
|
2022-11-19 01:25:38 +00:00
|
|
|
};
|
|
|
|
|
2022-11-29 14:36:16 +00:00
|
|
|
static inline void bw_osc_tri_init(bw_osc_tri_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
bw_one_pole_init(&coeffs->smooth_coeffs);
|
|
|
|
bw_one_pole_set_tau(&coeffs->smooth_coeffs, 0.005f);
|
|
|
|
coeffs->antialiasing = 0;
|
|
|
|
coeffs->slope = 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_set_sample_rate(bw_osc_tri_coeffs *BW_RESTRICT coeffs, float sample_rate) {
|
|
|
|
bw_one_pole_set_sample_rate(&coeffs->smooth_coeffs, sample_rate);
|
|
|
|
bw_one_pole_reset_coeffs(&coeffs->smooth_coeffs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_reset_coeffs(bw_osc_tri_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->slope);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_update_coeffs_ctrl(bw_osc_tri_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_update_coeffs_audio(bw_osc_tri_coeffs *BW_RESTRICT coeffs) {
|
|
|
|
bw_one_pole_process1(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->slope);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_osc_tri_process1(const bw_osc_tri_coeffs *BW_RESTRICT coeffs, float x) {
|
|
|
|
const float slope = bw_one_pole_get_y_z1(&coeffs->smooth_state);
|
|
|
|
const float phase_d = x + x;
|
|
|
|
return x < slope ? (phase_d - slope) * bw_rcpf_2(slope) : (1.f + slope - phase_d) * bw_rcpf_2(1.f - slope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PolyBLAMP residual based on Parzen window (4th-order B-spline), one-sided (x in [0, 2])
|
|
|
|
static inline float _bw_osc_tri_blamp_diff(float x) {
|
|
|
|
return x < 1.f
|
|
|
|
? x * (x * ((0.05f * x - 0.1666666666666667f) * x * x + 0.6666666666666666f) - 1.0f) + 0.4666666666666667f
|
|
|
|
: x * (x * (x * ((0.1666666666666667f - 0.01666666666666667f * x) * x - 0.6666666666666666f) + 1.333333333333333f) - 1.333333333333333f) + 0.5333333333333333f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_osc_tri_process1_antialias(const bw_osc_tri_coeffs *BW_RESTRICT coeffs, float x, float x_phase_inc) {
|
|
|
|
const float slope = bw_one_pole_get_y_z1(&coeffs->smooth_state);
|
|
|
|
const float s_1_p_pw = 1.f + slope;
|
|
|
|
const float s_1_m_pw = 1.f - slope;
|
|
|
|
const float phase_d = x + x;
|
|
|
|
float v = x < slope ? (phase_d - slope) * bw_rcpf_2(slope) : (s_1_p_pw - phase_d) * bw_rcpf_2(s_1_m_pw);
|
|
|
|
if (x_phase_inc != 0.f) {
|
|
|
|
const float phase_inc_2 = x_phase_inc + x_phase_inc;
|
|
|
|
const float phase_inc_rcp = bw_rcpf_2(x_phase_inc);
|
|
|
|
const float pw_m_phase = slope - x;
|
|
|
|
const float phase_2 = bw_copysignf(0.5f, pw_m_phase) + 0.5f - pw_m_phase;
|
|
|
|
const float s_1_m_phase = 1.f - x;
|
|
|
|
const float s_1_m_phase_2 = 1.f - phase_2;
|
|
|
|
float blamp = 0.f;
|
|
|
|
if (s_1_m_phase_2 < phase_inc_2)
|
|
|
|
blamp += _bw_osc_tri_blamp_diff(s_1_m_phase_2 * phase_inc_rcp);
|
|
|
|
if (s_1_m_phase < phase_inc_2)
|
|
|
|
blamp -= _bw_osc_tri_blamp_diff(s_1_m_phase * phase_inc_rcp);
|
|
|
|
if (x < phase_inc_2)
|
|
|
|
blamp -= _bw_osc_tri_blamp_diff(x * phase_inc_rcp);
|
|
|
|
if (phase_2 < phase_inc_2)
|
|
|
|
blamp += _bw_osc_tri_blamp_diff(phase_2 * phase_inc_rcp);
|
|
|
|
v -= bw_rcpf_2(slope * s_1_m_pw) * bw_absf(x_phase_inc) * blamp;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_process(bw_osc_tri_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++) {
|
|
|
|
bw_osc_tri_update_coeffs_audio(coeffs);
|
|
|
|
y[i] = bw_osc_tri_process1_antialias(coeffs, x[i], x_phase_inc[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (int i = 0; i < n_samples; i++) {
|
|
|
|
bw_osc_tri_update_coeffs_audio(coeffs);
|
|
|
|
y[i] = bw_osc_tri_process1(coeffs, x[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_set_antialiasing(bw_osc_tri_coeffs *BW_RESTRICT coeffs, char value) {
|
|
|
|
coeffs->antialiasing = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_osc_tri_set_slope(bw_osc_tri_coeffs *BW_RESTRICT coeffs, float value) {
|
|
|
|
coeffs->slope = value;
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|