synth(pp)_mono, some optimization

This commit is contained in:
Stefano D'Angelo 2024-02-24 11:04:41 +01:00
parent e0bfcdbdd7
commit e9d80f55ac
2 changed files with 60 additions and 66 deletions

View File

@ -71,6 +71,7 @@ typedef struct plugin {
bw_ppm_state ppm_state; bw_ppm_state ppm_state;
size_t sync_count; size_t sync_count;
float noise_kv[2];
uint64_t rand_state; uint64_t rand_state;
float master_tune; float master_tune;
@ -101,7 +102,6 @@ typedef struct plugin {
char notes_pressed[128]; char notes_pressed[128];
size_t sync_left; size_t sync_left;
char vco3_waveform_cur; char vco3_waveform_cur;
char noise_color_cur;
float mod_k; float mod_k;
char vco1_waveform_cur; char vco1_waveform_cur;
char vco2_waveform_cur; char vco2_waveform_cur;
@ -179,6 +179,9 @@ static void plugin_set_sample_rate(plugin *instance, float sample_rate) {
bw_pink_filt_reset_coeffs(&instance->pink_filt_coeffs); bw_pink_filt_reset_coeffs(&instance->pink_filt_coeffs);
instance->sync_count = (size_t)bw_roundf(sample_rate * SYNC_RATE); instance->sync_count = (size_t)bw_roundf(sample_rate * SYNC_RATE);
instance->noise_kv[0] = 6.f * bw_noise_gen_get_scaling_k(&instance->noise_gen_coeffs) * bw_pink_filt_get_scaling_k(&instance->pink_filt_coeffs);
instance->noise_kv[1] = 0.1f * bw_noise_gen_get_scaling_k(&instance->noise_gen_coeffs);
} }
static size_t plugin_mem_req(plugin *instance) { static size_t plugin_mem_req(plugin *instance) {
@ -233,7 +236,6 @@ static void plugin_reset(plugin *instance) {
instance->notes_pressed[i] = 0; instance->notes_pressed[i] = 0;
instance->sync_left = instance->sync_count; instance->sync_left = instance->sync_count;
instance->vco3_waveform_cur = instance->vco3_waveform; instance->vco3_waveform_cur = instance->vco3_waveform;
instance->noise_color_cur = instance->noise_color;
instance->vco1_waveform_cur = instance->vco1_waveform; instance->vco1_waveform_cur = instance->vco1_waveform;
instance->vco2_waveform_cur = instance->vco2_waveform; instance->vco2_waveform_cur = instance->vco2_waveform;
} }
@ -401,7 +403,7 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu
// asynchronous control-rate operations // asynchronous control-rate operations
int n = instance->note - 69; int n = instance->note - 69;
int n3 = instance->vco3_kbd_ctrl ? instance->note - 69 : -69; int n3 = instance->vco3_kbd_ctrl ? n : -69;
bw_phase_gen_set_frequency(&instance->vco1_phase_gen_coeffs, bw_phase_gen_set_frequency(&instance->vco1_phase_gen_coeffs,
instance->master_tune instance->master_tune
* bw_pow2f(instance->vco1_coarse + instance->pitch_bend * bw_pow2f(instance->vco1_coarse + instance->pitch_bend
@ -427,12 +429,6 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu
instance->vco3_waveform_cur = instance->vco3_waveform; instance->vco3_waveform_cur = instance->vco3_waveform;
} }
if (instance->noise_color_cur != instance->noise_color) {
if (instance->noise_color == 2)
bw_pink_filt_reset_state(&instance->pink_filt_coeffs, &instance->pink_filt_state, 0.f);
instance->noise_color_cur = instance->noise_color;
}
if (instance->vco1_waveform_cur != instance->vco1_waveform) { if (instance->vco1_waveform_cur != instance->vco1_waveform) {
switch (instance->vco1_waveform) { switch (instance->vco1_waveform) {
case 2: case 2:
@ -457,6 +453,19 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu
instance->vco2_waveform_cur = instance->vco2_waveform; instance->vco2_waveform_cur = instance->vco2_waveform;
} }
const float cutoff_unmapped = 0.1447648273010839f * bw_logf(0.05f * instance->vcf_cutoff);
static const float cutoff_kbd_kv[4] = {
0.f, // off
0.629960524947437f * 8.333333333333333e-2f, // 1/3
0.793700525984100f * 8.333333333333333e-2f, // 2/3
8.333333333333333e-2f // full
};
const float cutoff_kbd_k = bw_pow2f(cutoff_kbd_kv[instance->vcf_kbd_ctrl - 1] * (instance->note - 60));
const float noise_k = instance->noise_kv[instance->noise_color - 1];
const char vca_open = bw_env_gen_get_phase(&instance->vca_env_gen_state) != bw_env_gen_phase_off || instance->gate;
// synchronous control-rate and audio-rate operations // synchronous control-rate and audio-rate operations
for (size_t i = 0; i < n_samples; ) { for (size_t i = 0; i < n_samples; ) {
@ -483,8 +492,9 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu
// noise generator // noise generator
bw_noise_gen_process(&instance->noise_gen_coeffs, instance->buf[0], n); bw_noise_gen_process(&instance->noise_gen_coeffs, instance->buf[0], n);
if (instance->noise_color_cur == 2) if (instance->noise_color == 2)
bw_pink_filt_process(&instance->pink_filt_coeffs, &instance->pink_filt_state, instance->buf[0], instance->buf[0], n); bw_pink_filt_process(&instance->pink_filt_coeffs, &instance->pink_filt_state, instance->buf[0], instance->buf[0], n);
// no need to ever reset pink filt, as input is noise and filter is static
bw_buf_scale(instance->buf[0], 5.f, instance->buf[0], n); bw_buf_scale(instance->buf[0], 5.f, instance->buf[0], n);
// modulation signals // modulation signals
@ -537,40 +547,27 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu
bw_osc_filt_process(&instance->osc_filt_state, out, out, n); bw_osc_filt_process(&instance->osc_filt_state, out, out, n);
const float k = instance->noise_color_cur == 2 bw_buf_scale(instance->buf[0], noise_k, instance->buf[0], n);
? 6.f * bw_noise_gen_get_scaling_k(&instance->noise_gen_coeffs) * bw_pink_filt_get_scaling_k(&instance->pink_filt_coeffs)
: 0.1f * bw_noise_gen_get_scaling_k(&instance->noise_gen_coeffs);
bw_buf_scale(instance->buf[0], k, instance->buf[0], n);
bw_buf_mix(out, instance->buf[0], out, n); bw_buf_mix(out, instance->buf[0], out, n);
// vcf // vcf
bw_env_gen_process(&instance->vcf_env_gen_coeffs, &instance->vcf_env_gen_state, instance->gate, NULL, n); bw_env_gen_process(&instance->vcf_env_gen_coeffs, &instance->vcf_env_gen_state, instance->gate, NULL, n);
if (sync) if (sync) {
instance->vcf_env_k = bw_env_gen_get_y_z1(&instance->vcf_env_gen_state); instance->vcf_env_k = bw_env_gen_get_y_z1(&instance->vcf_env_gen_state);
const float cutoff_unmapped = 0.1447648273010839f * bw_logf(0.05f * instance->vcf_cutoff); const float cutoff_vpos = cutoff_unmapped + instance->vcf_contour * instance->vcf_env_k + 0.3f * instance->vcf_modulation * instance->mod_k;
const float cutoff_vpos = cutoff_unmapped + instance->vcf_contour * instance->vcf_env_k + 0.3f * instance->vcf_modulation * instance->mod_k; const float cutoff = cutoff_kbd_k * 20.f * bw_expf(6.907755278982137 * cutoff_vpos);
float cutoff = 20.f * bw_expf(6.907755278982137 * cutoff_vpos); bw_svf_set_cutoff(&instance->vcf_coeffs, bw_clipf(cutoff, 20.f, 20e3f));
switch (instance->vcf_kbd_ctrl) {
case 2: // 1/3
cutoff *= bw_pow2f((0.629960524947437f * 8.333333333333333e-2f) * (instance->note - 60));
break;
case 3: // 2/3
cutoff *= bw_pow2f((0.793700525984100f * 8.333333333333333e-2f) * (instance->note - 60));
break;
case 4: // full
cutoff *= bw_pow2f(8.333333333333333e-2f * (instance->note - 60));
break;
default: // off, do nothing
break;
} }
bw_svf_set_cutoff(&instance->vcf_coeffs, bw_clipf(cutoff, 20.f, 20e3f));
bw_svf_process(&instance->vcf_coeffs, &instance->vcf_state, out, out, NULL, NULL, n); bw_svf_process(&instance->vcf_coeffs, &instance->vcf_state, out, out, NULL, NULL, n);
// vca // vca
bw_env_gen_process(&instance->vca_env_gen_coeffs, &instance->vca_env_gen_state, instance->gate, instance->buf[0], n); if (vca_open) {
bw_buf_mul(out, instance->buf[0], out, n); bw_env_gen_process(&instance->vca_env_gen_coeffs, &instance->vca_env_gen_state, instance->gate, instance->buf[0], n);
bw_buf_mul(out, instance->buf[0], out, n);
} else
bw_buf_fill(0.f, out, n);
// A 440 Hz osc // A 440 Hz osc

View File

@ -71,6 +71,7 @@ public:
PPM<1> ppm; PPM<1> ppm;
size_t syncCount; size_t syncCount;
float noiseKV[2];
uint64_t randState; uint64_t randState;
float masterTune; float masterTune;
@ -101,7 +102,6 @@ public:
char notesPressed[128]; char notesPressed[128];
size_t syncLeft; size_t syncLeft;
char vco3WaveformCur; char vco3WaveformCur;
char noiseColorCur;
float modK; float modK;
char vco1WaveformCur; char vco1WaveformCur;
char vco2WaveformCur; char vco2WaveformCur;
@ -165,6 +165,9 @@ void impl_set_sample_rate(impl handle, float sample_rate) {
instance->ppm.setSampleRate(sample_rate); instance->ppm.setSampleRate(sample_rate);
instance->syncCount = (size_t)bw_roundf(sample_rate * SYNC_RATE); instance->syncCount = (size_t)bw_roundf(sample_rate * SYNC_RATE);
instance->noiseKV[0] = 6.f * instance->noiseGen.getScalingK() * instance->pinkFilt.getScalingK();
instance->noiseKV[1] = 0.1f * instance->noiseGen.getScalingK();
} }
void impl_reset(impl handle) { void impl_reset(impl handle) {
@ -205,7 +208,6 @@ void impl_reset(impl handle) {
instance->notesPressed[i] = 0; instance->notesPressed[i] = 0;
instance->syncLeft = instance->syncCount; instance->syncLeft = instance->syncCount;
instance->vco3WaveformCur = instance->vco3Waveform; instance->vco3WaveformCur = instance->vco3Waveform;
instance->noiseColorCur = instance->noiseColor;
instance->vco1WaveformCur = instance->vco1Waveform; instance->vco1WaveformCur = instance->vco1Waveform;
instance->vco2WaveformCur = instance->vco2Waveform; instance->vco2WaveformCur = instance->vco2Waveform;
} }
@ -379,7 +381,7 @@ void impl_process(impl handle, const float **inputs, float **outputs, size_t n_s
// asynchronous control-rate operations // asynchronous control-rate operations
int n = instance->note - 69; int n = instance->note - 69;
int n3 = instance->vco3KbdCtrl ? instance->note - 69 : -69; int n3 = instance->vco3KbdCtrl ? n : -69;
instance->vco1PhaseGen.setFrequency( instance->vco1PhaseGen.setFrequency(
instance->masterTune instance->masterTune
* bw_pow2f(instance->vco1Coarse + instance->pitchBend * bw_pow2f(instance->vco1Coarse + instance->pitchBend
@ -405,12 +407,6 @@ void impl_process(impl handle, const float **inputs, float **outputs, size_t n_s
instance->vco3WaveformCur = instance->vco3Waveform; instance->vco3WaveformCur = instance->vco3Waveform;
} }
if (instance->noiseColorCur != instance->noiseColor) {
if (instance->noiseColor == 2)
instance->pinkFilt.reset();
instance->noiseColorCur = instance->noiseColor;
}
if (instance->vco1WaveformCur != instance->vco1Waveform) { if (instance->vco1WaveformCur != instance->vco1Waveform) {
switch (instance->vco1Waveform) { switch (instance->vco1Waveform) {
case 2: case 2:
@ -435,6 +431,19 @@ void impl_process(impl handle, const float **inputs, float **outputs, size_t n_s
instance->vco2WaveformCur = instance->vco2Waveform; instance->vco2WaveformCur = instance->vco2Waveform;
} }
const float cutoffUnmapped = 0.1447648273010839f * bw_logf(0.05f * instance->vcfCutoff);
static const float cutoffKbdKV[4] = {
0.f, // off
0.629960524947437f * 8.333333333333333e-2f, // 1/3
0.793700525984100f * 8.333333333333333e-2f, // 2/3
8.333333333333333e-2f // full
};
const float cutoffKbdK = bw_pow2f(cutoffKbdKV[instance->vcfKbdCtrl - 1] * (instance->note - 60));
const float noiseK = instance->noiseKV[instance->noiseColor - 1];
const char vcaOpen = instance->vcaEnvGen.getPhase(0) != bw_env_gen_phase_off || instance->gate;
float *b0[1] = {instance->buf[0]}; float *b0[1] = {instance->buf[0]};
float *b1[1] = {instance->buf[1]}; float *b1[1] = {instance->buf[1]};
float *b2[1] = {instance->buf[2]}; float *b2[1] = {instance->buf[2]};
@ -465,8 +474,9 @@ void impl_process(impl handle, const float **inputs, float **outputs, size_t n_s
// noise generator // noise generator
instance->noiseGen.process(b0, n); instance->noiseGen.process(b0, n);
if (instance->noiseColorCur == 2) if (instance->noiseColor == 2)
instance->pinkFilt.process(b0, b0, n); instance->pinkFilt.process(b0, b0, n);
// no need to ever reset pink filt, as input is noise and filter is static
bufScale<1>(b0, 5.f, b0, n); bufScale<1>(b0, 5.f, b0, n);
// modulation signals // modulation signals
@ -519,40 +529,27 @@ void impl_process(impl handle, const float **inputs, float **outputs, size_t n_s
instance->oscFilt.process(y, y, n); instance->oscFilt.process(y, y, n);
const float k = instance->noiseColorCur == 2 bufScale<1>(b0, noiseK, b0, n);
? 6.f * instance->noiseGen.getScalingK() * instance->pinkFilt.getScalingK()
: 0.1f * instance->noiseGen.getScalingK();
bufScale<1>(b0, k, b0, n);
bufMix<1>(y, b0, y, n); bufMix<1>(y, b0, y, n);
// vcf // vcf
instance->vcfEnvGen.process(g, nullptr, n); instance->vcfEnvGen.process(g, nullptr, n);
if (sync) if (sync) {
instance->vcfEnvK = instance->vcfEnvGen.getYZ1(0); instance->vcfEnvK = instance->vcfEnvGen.getYZ1(0);
const float cutoffUnmapped = 0.1447648273010839f * bw_logf(0.05f * instance->vcfCutoff); const float cutoffVpos = cutoffUnmapped + instance->vcfContour * instance->vcfEnvK + 0.3f * instance->vcfModulation * instance->modK;
const float cutoffVpos = cutoffUnmapped + instance->vcfContour * instance->vcfEnvK + 0.3f * instance->vcfModulation * instance->modK; const float cutoff = cutoffKbdK * 20.f * bw_expf(6.907755278982137 * cutoffVpos);
float cutoff = 20.f * bw_expf(6.907755278982137 * cutoffVpos); instance->vcf.setCutoff(bw_clipf(cutoff, 20.f, 20e3f));
switch (instance->vcfKbdCtrl) {
case 2: // 1/3
cutoff *= bw_pow2f((0.629960524947437f * 8.333333333333333e-2f) * (instance->note - 60));
break;
case 3: // 2/3
cutoff *= bw_pow2f((0.793700525984100f * 8.333333333333333e-2f) * (instance->note - 60));
break;
case 4: // full
cutoff *= bw_pow2f(8.333333333333333e-2f * (instance->note - 60));
break;
default: // off, do nothing
break;
} }
instance->vcf.setCutoff(bw_clipf(cutoff, 20.f, 20e3f));
instance->vcf.process(y, y, nullptr, nullptr, n); instance->vcf.process(y, y, nullptr, nullptr, n);
// vca // vca
instance->vcaEnvGen.process(g, b0, n); if (vcaOpen) {
bufMul<1>(y, b0, y, n); instance->vcaEnvGen.process(g, b0, n);
bufMul<1>(y, b0, y, n);
} else
bufFill<1>(0.f, y, n);
// A 440 Hz osc // A 440 Hz osc