Added input processing to state
This commit is contained in:
parent
787bf50957
commit
a8c231df50
23
state.c
23
state.c
@ -1,10 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "const.h"
|
||||
#include "input.h"
|
||||
|
||||
#include "state.h"
|
||||
|
||||
void update_state(state_t* state, input_t* input) {
|
||||
puts("AHHHHHH");
|
||||
state->clock_bpm = map_linear(input->pots[0], BPM_MIN, BPM_MAX);
|
||||
state->vco_freq = map_exponential(input->pots[1], VCO_FREQ_MIN, VCO_FREQ_MAX);
|
||||
state->vco_volume = map_exponential(input->pots[2], VCO_VOLUME_MIN, VCO_VOLUME_MAX);
|
||||
state->filter_freq = map_exponential(input->pots[3], FILTER_FREQ_MIN, FILTER_FREQ_MAX);
|
||||
state->filter_resonance = map_linear(input->pots[4], FILTER_RES_MIN, FILTER_RES_MAX);
|
||||
state->env1_attack = map_linear(input->pots[5], ENV_ATTACK_MIN, ENV_ATTACK_MAX);
|
||||
state->env1_release = map_linear(input->pots[6], ENV_RELEASE_MIN, ENV_RELEASE_MAX);
|
||||
state->env2_attack = map_linear(input->pots[7], ENV_ATTACK_MIN, ENV_ATTACK_MAX);
|
||||
state->env2_release = map_linear(input->pots[8], ENV_RELEASE_MIN, ENV_RELEASE_MAX);
|
||||
state->reverb_amount = map_linear(input->pots[9], REVERB_AMOUNT_MIN, REVERB_AMOUNT_MAX);
|
||||
|
||||
static bool pressed = false;
|
||||
if (!pressed && input->buttons[0]) {
|
||||
state->vco_mode = (vco_mode_t)((state->vco_mode + 1) % 4);
|
||||
pressed = true;
|
||||
} else pressed = false;
|
||||
|
||||
state->quant_enabled = input->buttons[1];
|
||||
state->amen_enabled = input->buttons[2];
|
||||
}
|
||||
|
||||
|
||||
16
state.h
16
state.h
@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "input.h"
|
||||
#include "vco.h"
|
||||
|
||||
@ -25,5 +27,17 @@ typedef struct {
|
||||
bool amen_enabled;
|
||||
} state_t;
|
||||
|
||||
static inline float map_linear(float v, float min, float max) {
|
||||
return min + v * (max - min);
|
||||
}
|
||||
|
||||
static inline float map_exponential(float v, float min, float max) {
|
||||
return min + powf(max / min, v);
|
||||
}
|
||||
|
||||
static inline float map_squared(float v, float min, float max) {
|
||||
return min + (v * v) * (max - min);
|
||||
}
|
||||
|
||||
void update_state(state_t* state, input_t* input);
|
||||
|
||||
|
||||
42
synth.cc
42
synth.cc
@ -101,34 +101,16 @@ float amenbreak(float beat_samples, float playback_rate) {
|
||||
|
||||
void synth_init(state_t* state) {
|
||||
osc.Init(SAMPLE_RATE);
|
||||
osc.SetWaveform(vco_mode_to_daisy(VCO_SAW));
|
||||
osc.SetFreq(440.0f);
|
||||
osc.SetAmp(1.0f);
|
||||
|
||||
filter.Init(SAMPLE_RATE);
|
||||
filter.SetFreq(2000.0f);
|
||||
filter.SetRes(0.0f);
|
||||
|
||||
vco_env.Init(SAMPLE_RATE);
|
||||
vco_env.SetTime(ADENV_SEG_ATTACK, 0.01f);
|
||||
vco_env.SetTime(ADENV_SEG_DECAY, 0.8f);
|
||||
vco_env.SetMin(0.0f);
|
||||
vco_env.SetMax(1.0f);
|
||||
|
||||
vco_env.SetMin(0);
|
||||
vco_env.SetMax(FILTER_FREQ_MAX);
|
||||
filter_env.Init(SAMPLE_RATE);
|
||||
filter_env.SetTime(ADENV_SEG_ATTACK, 0.01f);
|
||||
filter_env.SetTime(ADENV_SEG_DECAY, 0.5f);
|
||||
filter_env.SetMin(0.0f);
|
||||
filter_env.SetMax(2000.0f);
|
||||
|
||||
state->clock_bpm = 0.1f;
|
||||
|
||||
filter_env.SetMin(0);
|
||||
filter_env.SetMax(FILTER_FREQ_MAX);
|
||||
clock_phase = 0.0f;
|
||||
clock_trig = false;
|
||||
|
||||
amen_phase = 0.0f;
|
||||
state->amen_enabled = true;
|
||||
state->reverb_amount = 1.0;
|
||||
}
|
||||
|
||||
float get_sample(void) {
|
||||
@ -160,25 +142,21 @@ float get_sample(void) {
|
||||
float vco_freq = pot_to_freq(state->vco_freq, VCO_FREQ_MIN, VCO_FREQ_MAX);
|
||||
if (state->quant_enabled) vco_freq = quantize(vco_freq, 12.0f);
|
||||
|
||||
//osc.SetFreq(vco_freq);
|
||||
osc.SetFreq(vco_freq);
|
||||
static int n = VCO_SINE;
|
||||
osc.SetWaveform(vco_mode_to_daisy((vco_mode_t)(n++ % 4)));
|
||||
//osc.SetAmp(1.0f);
|
||||
|
||||
osc.SetAmp(1.0f);
|
||||
float vco_out = osc.Process();
|
||||
|
||||
float base_cutoff = pot_to_freq(state->filter_freq, FILTER_FREQ_MIN, FILTER_FREQ_MAX);
|
||||
float mod_cutoff = base_cutoff + filter_env_out * (FILTER_FREQ_MAX - FILTER_FREQ_MIN);
|
||||
mod_cutoff = fclamp(mod_cutoff, FILTER_FREQ_MIN, FILTER_FREQ_MAX);
|
||||
|
||||
//filter.SetFreq(mod_cutoff);
|
||||
//filter.SetRes(state.filter_resonance);
|
||||
filter.SetFreq(base_cutoff + filter_env_out);
|
||||
filter.SetRes(state->filter_resonance);
|
||||
filter.Process(vco_out);
|
||||
float filtered = filter.Low();
|
||||
|
||||
float vca_out = filtered * vco_env_out;// * state.vco_volume;
|
||||
float vca_out = filtered * vco_env_out * state->vco_volume;
|
||||
|
||||
float reverb_out = vca_out; //reverb(vca_out, state.reverb_amount);
|
||||
float reverb_out = reverb(vca_out, state->reverb_amount);
|
||||
|
||||
float amen_out = 0.0f;
|
||||
if (state->amen_enabled) amen_out = amenbreak(beat_samples, playback_rate);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user