envelope.c: point-based linear interpolating envelope generator

This commit is contained in:
veclav talica
2024-06-13 17:47:09 +05:00
parent 9923a7270e
commit b35498e230
4 changed files with 88 additions and 5 deletions

View File

@ -8,12 +8,15 @@
#include "sokol_log.h"
#include "../config.h"
#include "../envelope.c"
#include "../oscillators.c"
#include "../phaser.c"
static struct stfu_sinewave sine0;
static struct stfu_phaser phaser0;
static struct stfu_phaser phaser1;
static struct stfu_phaser phaser2;
static struct stfu_envelope envelope;
static float key_frequency = STFU_C4_FREQUENCY;
@ -22,8 +25,12 @@ static void stream(float *buffer, int num_frames, int num_channels) {
// sine0 = stfu_pump_sinewave(sine0); // Carrier
phaser0 = stfu_pump_phaser(phaser0);
phaser1 = stfu_pump_phaser(phaser1);
buffer[2 * i + 0] = sinf(phaser0.v + sinf(phaser1.v));
buffer[2 * i + 1] = sinf(phaser0.v + sinf(phaser1.v));
phaser2 = stfu_pump_phaser(phaser2);
envelope = stfu_pump_envelope(envelope);
buffer[2 * i + 0] =
envelope.v * sinf(phaser2.v + sinf(phaser0.v + sinf(phaser1.v)));
buffer[2 * i + 1] =
envelope.v * sinf(phaser2.v + sinf(phaser0.v + sinf(phaser1.v)));
}
}
@ -37,6 +44,11 @@ static void init(void) {
// sine0 = stfu_init_sinewave(STFU_A4_FREQUENCY / 2.0f, 0.0f, 0.8f);
phaser0 = stfu_init_phaser(key_frequency);
phaser1 = stfu_init_phaser(key_frequency * 0.25);
phaser2 = stfu_init_phaser(key_frequency * 2.0);
envelope = stfu_init_envelope(
(struct stfu_envelope_point[]){
[0] = {.dur = 0.5, .vol = 0.33}, [1] = {.dur = 1.0, .vol = 0}},
2);
}
static void cleanup(void) { saudio_shutdown(); }
@ -90,8 +102,10 @@ static void event(const sapp_event *e) {
break;
}
phaser0 = stfu_set_phaser_frequency(phaser0, key_frequency);
phaser1 = stfu_set_phaser_frequency(phaser1, key_frequency * 0.25);
phaser0 = stfu_set_phaser_frequency(phaser0, key_frequency * 0.25);
phaser1 = stfu_set_phaser_frequency(phaser1, key_frequency * 0.125);
phaser2 = stfu_set_phaser_frequency(phaser2, key_frequency);
envelope = stfu_trigger_envelope(envelope);
break;
}