new API for bw_osc_sin and used in example synth + use html5 range step

This commit is contained in:
Stefano D'Angelo 2022-11-28 08:36:43 +01:00
parent f3028e173d
commit 37a20be908
8 changed files with 72 additions and 109 deletions

View File

@ -131,7 +131,10 @@ window.addEventListener("load", function (e) {
range.setAttribute("name", "p" + i);
range.setAttribute("min", "0");
range.setAttribute("max", "1");
range.setAttribute("step", "any");
if (!parameters[i].step)
range.setAttribute("step", "any");
else
range.setAttribute("step", 1 / parameters[i].step);
range.value = parameters[i].defaultValue;
if (parameters[i].output)
range.setAttribute("readonly", "true");

View File

@ -131,7 +131,10 @@ window.addEventListener("load", function (e) {
range.setAttribute("name", "p" + i);
range.setAttribute("min", "0");
range.setAttribute("max", "1");
range.setAttribute("step", "any");
if (!parameters[i].step)
range.setAttribute("step", "any");
else
range.setAttribute("step", 1 / parameters[i].step);
range.value = parameters[i].defaultValue;
if (parameters[i].output)
range.setAttribute("readonly", "true");

View File

@ -19,14 +19,16 @@
#include "bw_example_synth.h"
#include <bw_math.h>
#include <bw_phase_gen.h>
#ifdef __WASM__
# include "walloc.h"
#else
# include <stdlib.h>
#endif
#include <bw_math.h>
#include <bw_phase_gen.h>
#include <bw_osc_sin.h>
/*
#include <bw_osc_pulse.h>
#include <bw_osc_filt.h>
@ -47,6 +49,7 @@ enum {
p_decay,
p_sustain,
p_release,
p_a440,
p_n
};
@ -56,6 +59,8 @@ struct _bw_example_synth {
// Sub-components
bw_phase_gen_coeffs phase_gen_coeffs;
bw_phase_gen_state phase_gen_state;
bw_phase_gen_coeffs a440_phase_gen_coeffs;
bw_phase_gen_state a440_phase_gen_state;
/*
bw_osc_pulse osc_pulse;
bw_osc_filt osc_filt;
@ -82,6 +87,9 @@ bw_example_synth bw_example_synth_new() {
return NULL;
bw_phase_gen_init(&instance->phase_gen_coeffs);
bw_phase_gen_init(&instance->a440_phase_gen_coeffs);
bw_phase_gen_set_frequency(&instance->a440_phase_gen_coeffs, 440.f);
/*
bw_osc_pulse_init(&instance->osc_pulse);
bw_osc_filt_init(&instance->osc_filt);
@ -103,6 +111,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_phase_gen_set_sample_rate(&instance->a440_phase_gen_coeffs, sample_rate);
/*
bw_osc_pulse_set_sample_rate(&instance->osc_pulse, sample_rate);
bw_svf_set_sample_rate(&instance->svf, sample_rate);
@ -115,6 +124,8 @@ 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_coeffs(&instance->a440_phase_gen_coeffs);
bw_phase_gen_reset_state(&instance->a440_phase_gen_coeffs, &instance->a440_phase_gen_state);
/*
bw_osc_pulse_reset(&instance->osc_pulse);
bw_osc_filt_reset(&instance->osc_filt);
@ -136,7 +147,9 @@ void bw_example_synth_process(bw_example_synth instance, const float** x, float*
;
//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);
//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;
/*
for (int i = 0; i < n_samples; i += BUFFER_SIZE) {
float *out = y[0] + i;
@ -154,6 +167,14 @@ void bw_example_synth_process(bw_example_synth instance, const float** x, float*
}
*/
instance->level = 0.f;
if (instance->params[p_a440] >= 0.5f)
for (int i = 0; i < n_samples; i++) {
float a440_y, a440_y_phase_inc;
bw_phase_gen_process1(&instance->a440_phase_gen_coeffs, &instance->a440_phase_gen_state, &a440_y, &a440_y_phase_inc);
a440_y = bw_osc_sin_process1(a440_y);
y[0][i] += a440_y;
}
}
void bw_example_synth_set_parameter(bw_example_synth instance, int index, float value) {

View File

@ -61,7 +61,7 @@ static struct config_io_bus config_buses_out[NUM_BUSES_OUT] = {
{ "Audio out", 1, 0, 0, IO_MONO }
};
#define NUM_PARAMETERS 11
#define NUM_PARAMETERS 12
static struct config_parameter config_parameters[NUM_PARAMETERS] = {
{ "Volume", "Volume", "", 0, 0, 0, 0.5f },
@ -74,6 +74,7 @@ static struct config_parameter config_parameters[NUM_PARAMETERS] = {
{ "Decay", "Decay", "s", 0, 0, 0, 0.f },
{ "Sustain", "Sustain", "%", 0, 0, 0, 1.f },
{ "Release", "Release", "0", 0, 0, 0, 0.f },
{ "A440", "A440", "", 0, 0, 1, 0.f },
{ "Level", "Level", "", 1, 0, 0, 0.f }
};

View File

@ -75,6 +75,12 @@ var parameters = [
output: false,
defaultValue: 0.0
},
{
name: "A440",
output: false,
defaultValue: 0.0,
step: 1
},
{
name: "Level",
output: true,

View File

@ -75,7 +75,10 @@ window.addEventListener("load", async function (e) {
range.setAttribute("name", "p" + i);
range.setAttribute("min", "0");
range.setAttribute("max", "1");
range.setAttribute("step", "any");
if (!parameters[i].step)
range.setAttribute("step", "any");
else
range.setAttribute("step", 1 / parameters[i].step);
range.value = parameters[i].defaultValue;
if (parameters[i].output)
range.setAttribute("readonly", "true");

View File

@ -19,15 +19,20 @@
/*!
* module_type {{{ dsp }}}
* version {{{ 0.1.0 }}}
* version {{{ 0.2.0 }}}
* requires {{{ bw_config bw_common bw_math }}}
* description {{{
* Sinusoidal oscillator waveshaper.
*
* This module only consists of the signal processing function.
* This module only consists of signal processing functions.
* }}}
* changelog {{{
* <ul>
* <li>Version <strong>0.2.0</strong>:
* <ul>
* <li>Refactored API.</li>
* </ul>
* </li>
* <li>Version <strong>0.1.0</strong>:
* <ul>
* <li>First release.</li>
@ -44,16 +49,36 @@
extern "C" {
#endif
#include <bw_common.h>
static inline float bw_osc_sin_process1(float x);
/*! api {{{
* #### bw_osc_sin_process()
* ```>>> */
void bw_osc_sin_process(const float *x, float* y, int n_samples);
static inline void bw_osc_sin_process(const float *x, float* y, int n_samples);
/*! <<<```
* Turns `n_samples` samples of the normalized phase signal in the `x` buffer
* into a sinusoidal signal, filling the corresponding `n_samples` in the
* output buffer `y`.
* }}} */
/*** 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>
static inline float bw_osc_sin_process1(float x) {
return bw_sinf_3(6.283185307179586f * x);
}
static inline void bw_osc_sin_process(const float *x, float* y, int n_samples) {
for (int i = 0; i < n_samples; i++)
y[i] = bw_osc_sin_process1(x[i]);
}
#ifdef __cplusplus
}
#endif

View File

@ -1,99 +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_tri.h>
#include <bw_math.h>
#include <bw_inline_one_pole.h>
void bw_osc_tri_init(bw_osc_tri *instance) {
instance->antialiasing = 0;
instance->slope = 0.5f;
}
void bw_osc_tri_set_sample_rate(bw_osc_tri *instance, float sample_rate) {
instance->smooth_mA1 = bw_inline_one_pole_get_mA1(sample_rate, 0.005f);
}
void bw_osc_tri_reset(bw_osc_tri *instance) {
instance->first_run = 1;
}
// PolyBLAMP residual based on Parzen window (4th-order B-spline), one-sided (x in [0, 2])
static inline float 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;
}
void bw_osc_tri_process(bw_osc_tri *instance, const float *x, const float *x_phase_inc, float* y, int n_samples) {
if (instance->first_run) {
instance->slope_z1 = instance->slope;
instance->first_run = 0;
}
if (instance->antialiasing) {
for (int i = 0; i < n_samples; i++) {
const float slope = bw_inline_one_pole(instance->slope, instance->slope_z1, instance->smooth_mA1);
instance->slope_z1 = slope;
const float s_1_p_pw = 1.f + slope;
const float s_1_m_pw = 1.f - slope;
const float phase_d = x[i] + x[i];
float v = x[i] < 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[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 pw_m_phase = slope - x[i];
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[i];
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 += blamp_diff(s_1_m_phase_2 * phase_inc_rcp);
if (s_1_m_phase < phase_inc_2)
blamp -= blamp_diff(s_1_m_phase * phase_inc_rcp);
if (x[i] < phase_inc_2)
blamp -= blamp_diff(x[i] * phase_inc_rcp);
if (phase_2 < phase_inc_2)
blamp += blamp_diff(phase_2 * phase_inc_rcp);
v -= bw_rcpf_2(slope * s_1_m_pw) * bw_absf(x_phase_inc[i]) * blamp;
}
y[i] = v;
}
} else {
for (int i = 0; i < n_samples; i++) {
const float slope = bw_inline_one_pole(instance->slope, instance->slope_z1, instance->smooth_mA1);
instance->slope_z1 = slope;
const float phase_d = x[i] + x[i];
y[i] = x[i] < slope ? (phase_d - slope) * bw_rcpf_2(slope) : (1.f + slope - phase_d) * bw_rcpf_2(1.f - slope);
}
}
}
void bw_osc_tri_set_antialiasing(bw_osc_tri *instance, char value) {
instance->antialiasing = value;
}
void bw_osc_tri_set_slope(bw_osc_tri *instance, float value) {
instance->slope = value;
}