ported bw_osc_pulse to new API
This commit is contained in:
parent
2b32ec75d6
commit
f98f07009d
@ -26,8 +26,9 @@
|
||||
#endif
|
||||
|
||||
#include <bw_math.h>
|
||||
#include <bw_noise_gen.h>
|
||||
#include <bw_phase_gen.h>
|
||||
#include <bw_osc_pulse.h>
|
||||
#include <bw_noise_gen.h>
|
||||
#include <bw_svf.h>
|
||||
#include <bw_osc_sin.h>
|
||||
#include <bw_vol.h>
|
||||
@ -57,12 +58,14 @@ enum {
|
||||
p_n
|
||||
};
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
//#define BUFFER_SIZE 128
|
||||
|
||||
struct _bw_example_synth {
|
||||
// Sub-components
|
||||
bw_phase_gen_coeffs phase_gen_coeffs;
|
||||
bw_phase_gen_state phase_gen_state;
|
||||
bw_osc_pulse_coeffs osc_pulse_coeffs;
|
||||
bw_osc_pulse_state osc_pulse_state;
|
||||
bw_noise_gen_coeffs noise_gen_coeffs;
|
||||
bw_svf_coeffs svf_coeffs;
|
||||
bw_svf_state svf_state;
|
||||
@ -88,7 +91,7 @@ struct _bw_example_synth {
|
||||
float level;
|
||||
|
||||
// Buffers
|
||||
float buf[BUFFER_SIZE];
|
||||
//float buf[BUFFER_SIZE];
|
||||
};
|
||||
|
||||
bw_example_synth bw_example_synth_new() {
|
||||
@ -97,6 +100,7 @@ bw_example_synth bw_example_synth_new() {
|
||||
return NULL;
|
||||
|
||||
bw_phase_gen_init(&instance->phase_gen_coeffs);
|
||||
bw_osc_pulse_init(&instance->osc_pulse_coeffs);
|
||||
bw_noise_gen_init(&instance->noise_gen_coeffs, &instance->rand_state);
|
||||
bw_phase_gen_init(&instance->a440_phase_gen_coeffs);
|
||||
bw_svf_init(&instance->svf_coeffs);
|
||||
@ -126,6 +130,7 @@ void bw_example_synth_free(bw_example_synth instance) {
|
||||
|
||||
void bw_example_synth_set_sample_rate(bw_example_synth instance, float sample_rate) {
|
||||
bw_phase_gen_set_sample_rate(&instance->phase_gen_coeffs, sample_rate);
|
||||
bw_osc_pulse_set_sample_rate(&instance->osc_pulse_coeffs, sample_rate);
|
||||
bw_noise_gen_set_sample_rate(&instance->noise_gen_coeffs, sample_rate);
|
||||
bw_phase_gen_set_sample_rate(&instance->a440_phase_gen_coeffs, sample_rate);
|
||||
bw_svf_set_sample_rate(&instance->svf_coeffs, sample_rate);
|
||||
@ -140,21 +145,19 @@ void bw_example_synth_set_sample_rate(bw_example_synth instance, float sample_ra
|
||||
|
||||
void bw_example_synth_reset(bw_example_synth instance) {
|
||||
bw_phase_gen_reset_coeffs(&instance->phase_gen_coeffs);
|
||||
bw_phase_gen_reset_state(&instance->phase_gen_coeffs, &instance->phase_gen_state);
|
||||
bw_phase_gen_reset_state(&instance->phase_gen_coeffs, &instance->phase_gen_state, 0.f);
|
||||
bw_osc_pulse_reset_coeffs(&instance->osc_pulse_coeffs);
|
||||
bw_osc_pulse_reset_state(&instance->osc_pulse_coeffs, &instance->osc_pulse_state);
|
||||
bw_phase_gen_reset_coeffs(&instance->a440_phase_gen_coeffs);
|
||||
bw_phase_gen_reset_state(&instance->a440_phase_gen_coeffs, &instance->a440_phase_gen_state);
|
||||
bw_phase_gen_reset_state(&instance->a440_phase_gen_coeffs, &instance->a440_phase_gen_state, 0.f);
|
||||
bw_svf_reset_coeffs(&instance->svf_coeffs);
|
||||
bw_svf_reset_state(&instance->svf_coeffs, &instance->svf_state);
|
||||
bw_vol_reset_coeffs(&instance->vol_coeffs);
|
||||
bw_env_follow_reset_coeffs(&instance->env_follow_coeffs);
|
||||
bw_env_follow_reset_state(&instance->env_follow_coeffs, &instance->env_follow_state);
|
||||
/*
|
||||
bw_osc_pulse_reset(&instance->osc_pulse);
|
||||
bw_osc_filt_reset(&instance->osc_filt);
|
||||
bw_svf_reset(&instance->svf);
|
||||
bw_env_gen_reset(&instance->env_gen);
|
||||
bw_vol_reset(&instance->vol);
|
||||
bw_env_follow_reset(&instance->env_follow);
|
||||
*/
|
||||
instance->note = -1;
|
||||
}
|
||||
@ -168,13 +171,25 @@ void bw_example_synth_process(bw_example_synth instance, const float** x, float*
|
||||
} else
|
||||
;
|
||||
//bw_env_gen_set_gate(&instance->env_gen, 0);
|
||||
|
||||
//bw_phase_gen_process(&instance->phase_gen_coeffs, &instance->phase_gen_state, NULL, y[0], instance->buf, n_samples);
|
||||
for (int i = 0; i < n_samples; i++)
|
||||
y[0][i] = 0.f;
|
||||
|
||||
if (instance->note != -1)
|
||||
bw_noise_gen_process(&instance->noise_gen_coeffs, y[0], n_samples);
|
||||
if (instance->note != -1) {
|
||||
bw_phase_gen_update_coeffs_ctrl(&instance->phase_gen_coeffs);
|
||||
bw_osc_pulse_update_coeffs_ctrl(&instance->osc_pulse_coeffs);
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
float phase, phase_inc;
|
||||
bw_phase_gen_update_coeffs_audio(&instance->phase_gen_coeffs);
|
||||
bw_phase_gen_process1(&instance->phase_gen_coeffs, &instance->phase_gen_state, &phase, &phase_inc);
|
||||
bw_osc_pulse_update_coeffs_audio(&instance->osc_pulse_coeffs);
|
||||
y[0][i] = bw_osc_pulse_process1_antialias(&instance->osc_pulse_coeffs, &instance->osc_pulse_state, phase, phase_inc);
|
||||
}
|
||||
} else {
|
||||
bw_osc_pulse_reset_state(&instance->osc_pulse_coeffs, &instance->osc_pulse_state); // FIXME
|
||||
bw_phase_gen_process(&instance->phase_gen_coeffs, &instance->phase_gen_state, NULL, NULL, NULL, n_samples);
|
||||
for (int i = 0; i < n_samples; i++)
|
||||
y[0][i] = 0.f;
|
||||
}
|
||||
|
||||
//bw_noise_gen_process(&instance->noise_gen_coeffs, y[0], n_samples);
|
||||
|
||||
bw_svf_process(&instance->svf_coeffs, &instance->svf_state, y[0], y[0], NULL, NULL, n_samples);
|
||||
|
||||
@ -217,6 +232,9 @@ void bw_example_synth_set_parameter(bw_example_synth instance, int index, float
|
||||
case p_portamento:
|
||||
bw_phase_gen_set_portamento_tau(&instance->phase_gen_coeffs, value);
|
||||
break;
|
||||
case p_pulse_width:
|
||||
bw_osc_pulse_set_pulse_width(&instance->osc_pulse_coeffs, value);
|
||||
break;
|
||||
case p_cutoff:
|
||||
bw_svf_set_cutoff(&instance->svf_coeffs, 20.f + (20e3f - 20.f) * value * value * value);
|
||||
break;
|
||||
@ -224,9 +242,7 @@ void bw_example_synth_set_parameter(bw_example_synth instance, int index, float
|
||||
bw_svf_set_Q(&instance->svf_coeffs, 0.5f + 9.5f * value);
|
||||
break;
|
||||
/*
|
||||
case p_pulse_width:
|
||||
bw_osc_pulse_set_pulse_width(&instance->osc_pulse, value);
|
||||
break;
|
||||
|
||||
case p_attack:
|
||||
bw_env_gen_set_attack(&instance->env_gen, value);
|
||||
break;
|
||||
|
@ -29,7 +29,7 @@
|
||||
* <ul>
|
||||
* <li>Version <strong>0.2.0</strong>:
|
||||
* <ul>
|
||||
* <li>Refactored API to avoid dynamic memory allocation.</li>
|
||||
* <li>Refactored API.</li>
|
||||
* </ul>
|
||||
* </li>
|
||||
* <li>Version <strong>0.1.0</strong>:
|
||||
@ -49,41 +49,49 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/*! api {{{
|
||||
* #### bw_osc_pulse
|
||||
* #### bw_osc_pulse_coeffs
|
||||
* ```>>> */
|
||||
typedef struct _bw_osc_pulse bw_osc_pulse;
|
||||
typedef struct _bw_osc_pulse_coeffs bw_osc_pulse_coeffs;
|
||||
/*! <<<```
|
||||
* Instance object.
|
||||
* Coefficients.
|
||||
*
|
||||
* ### bw_one_pole_state
|
||||
* >>> */
|
||||
|
||||
/*! ...
|
||||
typedef struct _bw_osc_pulse_state bw_osc_pulse_state;
|
||||
/*! <<<```
|
||||
* State.
|
||||
*
|
||||
* #### bw_osc_pulse_init()
|
||||
* ```>>> */
|
||||
void bw_osc_pulse_init(bw_osc_pulse *instance);
|
||||
static inline void bw_osc_pulse_init(bw_osc_pulse_coeffs *BW_RESTRICT coeffs);
|
||||
/*! <<<```
|
||||
* Initializes the `instance` object.
|
||||
* >>> */
|
||||
|
||||
/*! ...
|
||||
* Initializes the `coeffs`.
|
||||
*
|
||||
* #### bw_osc_pulse_set_sample_rate()
|
||||
* ```>>> */
|
||||
void bw_osc_pulse_set_sample_rate(bw_osc_pulse *instance, float sample_rate);
|
||||
static inline void bw_osc_pulse_set_sample_rate(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, float sample_rate);
|
||||
/*! <<<```
|
||||
* Sets the `sample_rate` (Hz) value for the given `instance`.
|
||||
* >>> */
|
||||
|
||||
/*! ...
|
||||
* #### bw_osc_pulse_reset()
|
||||
* Sets the `sample_rate` (Hz) value for the given `coeffs`.
|
||||
*
|
||||
* #### bw_osc_pulse_reset_state()
|
||||
* ```>>> */
|
||||
void bw_osc_pulse_reset(bw_osc_pulse *instance);
|
||||
static inline void bw_osc_pulse_reset_state(const bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state);
|
||||
/*! <<<```
|
||||
* Resets the given `instance` to its initial state.
|
||||
* >>> */
|
||||
|
||||
static inline void bw_osc_pulse_reset_coeffs(bw_osc_pulse_coeffs *BW_RESTRICT coeffs);
|
||||
|
||||
static inline void bw_osc_pulse_update_coeffs_ctrl(bw_osc_pulse_coeffs *BW_RESTRICT coeffs);
|
||||
static inline void bw_osc_pulse_update_coeffs_audio(bw_osc_pulse_coeffs *BW_RESTRICT coeffs);
|
||||
|
||||
static inline float bw_osc_pulse_process1(const bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state, float x);
|
||||
static inline float bw_osc_pulse_process1_antialias(const bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state, float x, float x_phase_inc);
|
||||
|
||||
/*! ...
|
||||
* #### bw_osc_pulse_process()
|
||||
* ```>>> */
|
||||
void bw_osc_pulse_process(bw_osc_pulse *instance, const float *x, const float *x_phase_inc, float* y, int n_samples);
|
||||
static inline void bw_osc_pulse_process(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state, const float *x, const float *x_phase_inc, float *y, int n_samples);
|
||||
/*! <<<```
|
||||
* Lets the given `instance` process `n_samples` samples from the input
|
||||
* buffer `x` containing the normalized phase signal and fills the
|
||||
@ -96,7 +104,7 @@ void bw_osc_pulse_process(bw_osc_pulse *instance, const float *x, const float *x
|
||||
/*! ...
|
||||
* #### bw_osc_pulse_set_antialiasing()
|
||||
* ```>>> */
|
||||
void bw_osc_pulse_set_antialiasing(bw_osc_pulse *instance, char value);
|
||||
static inline void bw_osc_pulse_set_antialiasing(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, char value);
|
||||
/*! <<<```
|
||||
* Sets whether the antialiasing is on (`value` non-`0`) or off (`0`) for the
|
||||
* given `instance`.
|
||||
@ -107,7 +115,7 @@ void bw_osc_pulse_set_antialiasing(bw_osc_pulse *instance, char value);
|
||||
/*! ...
|
||||
* #### bw_osc_pulse_set_pulse_width()
|
||||
* ```>>> */
|
||||
void bw_osc_pulse_set_pulse_width(bw_osc_pulse *instance, float value);
|
||||
static inline void bw_osc_pulse_set_pulse_width(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, float value);
|
||||
/*! <<<```
|
||||
* Sets the pulse width (actually, the duty cycle) to `value` (range
|
||||
* [`0.f`, `1.f`]) for the given `instance`.
|
||||
@ -115,22 +123,110 @@ void bw_osc_pulse_set_pulse_width(bw_osc_pulse *instance, float value);
|
||||
* Default value: `0.5f`.
|
||||
* }}} */
|
||||
|
||||
/* WARNING: the internal definition of this struct is not part of the public
|
||||
* API. Its content may change at any time in future versions. Please, do not
|
||||
* access its members directly. */
|
||||
struct _bw_osc_pulse {
|
||||
// Coefficients
|
||||
float smooth_mA1;
|
||||
/*** 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>
|
||||
#include <bw_one_pole.h>
|
||||
|
||||
struct _bw_osc_pulse_coeffs {
|
||||
// Sub-components
|
||||
bw_one_pole_coeffs smooth_coeffs;
|
||||
bw_one_pole_state smooth_state;
|
||||
|
||||
// Parameters
|
||||
char first_run;
|
||||
char antialiasing;
|
||||
float pulse_width;
|
||||
|
||||
// State
|
||||
float pw_z1;
|
||||
char antialiasing;
|
||||
float pulse_width;
|
||||
};
|
||||
|
||||
struct _bw_osc_pulse_state {
|
||||
// empty, but we keep for potential forward API compatibility
|
||||
char unused;
|
||||
};
|
||||
|
||||
static inline void bw_osc_pulse_init(bw_osc_pulse_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->pulse_width = 0.5f;
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_set_sample_rate(bw_osc_pulse_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_pulse_reset_coeffs(bw_osc_pulse_coeffs *BW_RESTRICT coeffs) {
|
||||
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->pulse_width);
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_reset_state(const bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state) {
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_update_coeffs_ctrl(bw_osc_pulse_coeffs *BW_RESTRICT coeffs) {
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_update_coeffs_audio(bw_osc_pulse_coeffs *BW_RESTRICT coeffs) {
|
||||
bw_one_pole_process1(&coeffs->smooth_coeffs, &coeffs->smooth_state, coeffs->pulse_width);
|
||||
}
|
||||
|
||||
static inline float bw_osc_pulse_process1(const bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state, float x) {
|
||||
const float pw = bw_one_pole_get_y_z1(&coeffs->smooth_state);
|
||||
return bw_signf(pw - x);
|
||||
}
|
||||
|
||||
// PolyBLEP residual based on Parzen window (4th-order B-spline), one-sided (x in [0, 2])
|
||||
static inline float _bw_osc_pulse_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_pulse_process1_antialias(const bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state, float x, float x_phase_inc) {
|
||||
const float pw = bw_one_pole_get_y_z1(&coeffs->smooth_state);
|
||||
const float pw_m_phase = pw - x;
|
||||
float v = bw_copysignf(1.f, pw_m_phase); // pw = phase case should be properly compensated by the AA
|
||||
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 phase_2 = 0.5f * v + 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;
|
||||
if (s_1_m_phase < phase_inc_2)
|
||||
v -= _bw_osc_pulse_blep_diff(s_1_m_phase * phase_inc_rcp);
|
||||
if (s_1_m_phase_2 < phase_inc_2)
|
||||
v += _bw_osc_pulse_blep_diff(s_1_m_phase_2 * phase_inc_rcp);
|
||||
if (x < phase_inc_2)
|
||||
v += _bw_osc_pulse_blep_diff(x * phase_inc_rcp);
|
||||
if (phase_2 < phase_inc_2)
|
||||
v -= _bw_osc_pulse_blep_diff(phase_2 * phase_inc_rcp);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_process(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, bw_osc_pulse_state *BW_RESTRICT state, 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_pulse_update_coeffs_audio(coeffs);
|
||||
y[i] = bw_osc_pulse_process1_antialias(coeffs, state, x[i], x_phase_inc[i]);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_osc_pulse_update_coeffs_audio(coeffs);
|
||||
y[i] = bw_osc_pulse_process1(coeffs, state, x[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_set_antialiasing(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, char value) {
|
||||
coeffs->antialiasing = value;
|
||||
}
|
||||
|
||||
static inline void bw_osc_pulse_set_pulse_width(bw_osc_pulse_coeffs *BW_RESTRICT coeffs, float value) {
|
||||
coeffs->pulse_width = value;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -78,7 +78,7 @@ static inline void bw_phase_gen_set_sample_rate(bw_phase_gen_coeffs *BW_RESTRICT
|
||||
*
|
||||
* #### bw_phase_gen_reset()
|
||||
* ```>>> */
|
||||
static inline void bw_phase_gen_reset_state(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state);
|
||||
static inline void bw_phase_gen_reset_state(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float phase_0);
|
||||
/*! <<<```
|
||||
* Resets the given `state` to the initial state using the given `coeffs`.
|
||||
* >>> */
|
||||
@ -177,8 +177,8 @@ static inline void bw_phase_gen_reset_coeffs(bw_phase_gen_coeffs *BW_RESTRICT co
|
||||
bw_one_pole_reset_state(&coeffs->portamento_coeffs, &coeffs->portamento_state, coeffs->portamento_target);
|
||||
}
|
||||
|
||||
static inline void bw_phase_gen_reset_state(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state) {
|
||||
state->phase = 0.f;
|
||||
static inline void bw_phase_gen_reset_state(const bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, float phase_0) {
|
||||
state->phase = phase_0;
|
||||
}
|
||||
|
||||
static inline void bw_phase_gen_update_coeffs_ctrl(bw_phase_gen_coeffs *BW_RESTRICT coeffs) {
|
||||
@ -207,30 +207,60 @@ static inline void bw_phase_gen_process1_mod(const bw_phase_gen_coeffs *BW_RESTR
|
||||
|
||||
static inline void bw_phase_gen_process(bw_phase_gen_coeffs *BW_RESTRICT coeffs, bw_phase_gen_state *BW_RESTRICT state, const float *x_mod, float* y, float *y_phase_inc, int n_samples) {
|
||||
bw_phase_gen_update_coeffs_ctrl(coeffs);
|
||||
if (x_mod != NULL) {
|
||||
if (y_phase_inc != NULL)
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
bw_phase_gen_process1_mod(coeffs, state, x_mod[i], y + i, y_phase_inc + i);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v_phase_inc;
|
||||
bw_phase_gen_process1_mod(coeffs, state, x_mod[i], y + i, &v_phase_inc);
|
||||
}
|
||||
if (y != NULL) {
|
||||
if (x_mod != NULL) {
|
||||
if (y_phase_inc != NULL)
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
bw_phase_gen_process1_mod(coeffs, state, x_mod[i], y + i, y_phase_inc + i);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v_phase_inc;
|
||||
bw_phase_gen_process1_mod(coeffs, state, x_mod[i], y + i, &v_phase_inc);
|
||||
}
|
||||
} else {
|
||||
if (y_phase_inc != NULL)
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
bw_phase_gen_process1(coeffs, state, y + i, y_phase_inc + i);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v_phase_inc;
|
||||
bw_phase_gen_process1(coeffs, state, y + i, &v_phase_inc);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (y_phase_inc != NULL)
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
bw_phase_gen_process1(coeffs, state, y + i, y_phase_inc + i);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v_phase_inc;
|
||||
bw_phase_gen_process1(coeffs, state, y + i, &v_phase_inc);
|
||||
}
|
||||
if (x_mod != NULL) {
|
||||
if (y_phase_inc != NULL)
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v;
|
||||
bw_phase_gen_process1_mod(coeffs, state, x_mod[i], &v, y_phase_inc + i);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v, v_phase_inc;
|
||||
bw_phase_gen_process1_mod(coeffs, state, x_mod[i], &v, &v_phase_inc);
|
||||
}
|
||||
} else {
|
||||
if (y_phase_inc != NULL)
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v;
|
||||
bw_phase_gen_process1(coeffs, state, &v, y_phase_inc + i);
|
||||
}
|
||||
else
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
bw_phase_gen_update_coeffs_audio(coeffs);
|
||||
float v, v_phase_inc;
|
||||
bw_phase_gen_process1(coeffs, state, &v, &v_phase_inc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,93 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <bw_osc_pulse.h>
|
||||
|
||||
#include <bw_math.h>
|
||||
#include <bw_inline_one_pole.h>
|
||||
|
||||
void bw_osc_pulse_init(bw_osc_pulse *instance) {
|
||||
instance->antialiasing = 0;
|
||||
instance->pulse_width = 0.5f;
|
||||
}
|
||||
|
||||
void bw_osc_pulse_set_sample_rate(bw_osc_pulse *instance, float sample_rate) {
|
||||
instance->smooth_mA1 = bw_inline_one_pole_get_mA1(sample_rate, 0.005f);
|
||||
}
|
||||
|
||||
void bw_osc_pulse_reset(bw_osc_pulse *instance) {
|
||||
instance->first_run = 1;
|
||||
}
|
||||
|
||||
// PolyBLEP residual based on Parzen window (4th-order B-spline), one-sided (x in [0, 2])
|
||||
static inline float 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.0f) + 2.666666666666667f) - 1.333333333333333f;
|
||||
}
|
||||
|
||||
void bw_osc_pulse_process(bw_osc_pulse *instance, const float *x, const float *x_phase_inc, float* y, int n_samples) {
|
||||
if (instance->first_run) {
|
||||
instance->pw_z1 = instance->pulse_width;
|
||||
instance->first_run = 0;
|
||||
}
|
||||
|
||||
if (instance->antialiasing) {
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
const float pw = bw_inline_one_pole(instance->pulse_width, instance->pw_z1, instance->smooth_mA1);
|
||||
instance->pw_z1 = pw;
|
||||
|
||||
const float pw_m_phase = pw - x[i];
|
||||
float v = bw_copysignf(1.f, pw_m_phase); // pw = phase case should be properly compensated by the AA
|
||||
|
||||
if (x_phase_inc[i] != 0.f) {
|
||||
const float phase_inc_2 = x_phase_inc[i] + x_phase_inc[i];
|
||||
const float phase_inc_rcp = bw_rcpf_2(x_phase_inc[i]);
|
||||
const float phase_2 = 0.5f * v + 0.5f - pw_m_phase;
|
||||
const float s_1_m_phase = 1.f - x[i];
|
||||
const float s_1_m_phase_2 = 1.f - phase_2;
|
||||
if (s_1_m_phase < phase_inc_2)
|
||||
v -= blep_diff(s_1_m_phase * phase_inc_rcp);
|
||||
if (s_1_m_phase_2 < phase_inc_2)
|
||||
v += blep_diff(s_1_m_phase_2 * phase_inc_rcp);
|
||||
if (x[i] < phase_inc_2)
|
||||
v += blep_diff(x[i] * phase_inc_rcp);
|
||||
if (phase_2 < phase_inc_2)
|
||||
v -= blep_diff(phase_2 * phase_inc_rcp);
|
||||
}
|
||||
|
||||
y[i] = v;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n_samples; i++) {
|
||||
const float pw = bw_inline_one_pole(instance->pulse_width, instance->pw_z1, instance->smooth_mA1);
|
||||
instance->pw_z1 = pw;
|
||||
|
||||
y[i] = bw_signf(pw - x[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bw_osc_pulse_set_antialiasing(bw_osc_pulse *instance, char value) {
|
||||
instance->antialiasing = value;
|
||||
}
|
||||
|
||||
void bw_osc_pulse_set_pulse_width(bw_osc_pulse *instance, float value) {
|
||||
instance->pulse_width = value;
|
||||
}
|
Loading…
Reference in New Issue
Block a user