From 3535315aa4f428ed7f30ef8cd560940707180739 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 21 Jan 2010 10:46:09 +0000 Subject: [PATCH] Read parameters from file --- engine.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++------ engine.h | 1 + psyn.c | 8 +++--- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/engine.c b/engine.c index aebe1c4..4cb8457 100644 --- a/engine.c +++ b/engine.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "env.h" #include "osc.h" @@ -23,15 +24,73 @@ void engine_init() } osc_init(); - env_init(&_engine.params[0].env, 0.0125, 0.025, 5.0, 0.0, 0.25); - env_init(&_engine.params[1].env, 0.0, 0.0, 0.5, 0.0, 0.25); + engine_load_program(); +} - osc_setfreq(&_engine.lfo[0], 10.0); - osc_setfreq(&_engine.lfo[1], 2.0); - osc_setfreq(&_engine.lfo[2], 1.0); - _engine.lfo[0].level = 1.0; - _engine.lfo[1].level = 1.0; - _engine.lfo[2].level = 1.0; +void engine_load_program() +{ + FILE *f = fopen("program.txt", "r"); + + while (!feof(f)) { + char buf[1024]; + size_t l = fgets(buf, sizeof buf, f); + + if (!strncmp(buf, "voice", 5)) { + // Ignore voice + } else if (!strncmp(buf, "lfo", 3)) { + int lfo; + float freq; + int r = sscanf(buf, "lfo %d %f", &lfo, &freq); + if (r >= 2 && lfo >= 0 && lfo < NUM_LFO) { + osc_setfreq(&_engine.lfo[lfo], freq); + _engine.lfo[lfo].level = 1.0; + } + } else if (!strncmp(buf, "osc", 3)) { + int osc; + char sshape[1024]; + float level; + float freq; + float phase; + int r = sscanf(buf, "osc %d %s %f %f %f", &osc, sshape, &level, &freq, &phase); + if (r >= 5 && osc >= 0 && osc < VOICE_OSCILLATORS) { + int shape = 0; + if (!strcmp(sshape, "SINE")) shape = OSC_SINE; + else if (!strcmp(sshape, "SAW")) shape = OSC_SAW; + else if (!strcmp(sshape, "TRIANGLE")) shape = OSC_TRIANGLE; + else if (!strcmp(sshape, "SQUARE")) shape = OSC_SQUARE; + else if (!strcmp(sshape, "MOOGSAW")) shape = OSC_MOOGSAW; + else if (!strcmp(sshape, "EXP")) shape = OSC_EXP; + + _engine.params[osc].shape.value = shape; + _engine.params[osc].level.value = level; + _engine.params[osc].freq_mult.value = freq; + _engine.params[osc].phase.value = phase; + } + } else if (!strncmp(buf, "env", 3)) { + int env; + float attack; + float hold; + float decay; + float sustain; + float release; + int r = sscanf(buf, "env %d %f %f %f %f %f", &env, &attack, &hold, &decay, &sustain, &release); + if (r >= 6 && env >= 0 && env < VOICE_OSCILLATORS) { + env_init(&_engine.params[env].env, attack, hold, decay, sustain, release); + } + } else if (!strncmp(buf, "cutoff_env", 10)) { + int env; + float attack; + float hold; + float decay; + float sustain; + float release; + int r = sscanf(buf, "cutoff_env %d %f %f %f %f %f", &env, &attack, &hold, &decay, &sustain, &release); + if (r >= 6 && env >= 0 && env < 1) { + env_init(&_engine.cutoff_env, attack, hold, decay, sustain, release); + } + } + } + fclose(f); } void engine_run(struct engine_t *engine, uint32_t samples, float *left, float *right) diff --git a/engine.h b/engine.h index a2027e7..94c78f9 100644 --- a/engine.h +++ b/engine.h @@ -21,6 +21,7 @@ struct engine_t extern struct engine_t _engine; void engine_init(); +void engine_load_program(); void engine_run(struct engine_t *engine, uint32_t samples, float *left, float *right); void engine_startvoice(struct engine_t *engine, uint8_t note, uint8_t velocity); void engine_endvoice(struct engine_t *engine, uint8_t note, uint8_t velocity); diff --git a/psyn.c b/psyn.c index 5899b93..96cdcdb 100644 --- a/psyn.c +++ b/psyn.c @@ -117,10 +117,10 @@ static void run(LV2_Handle lv2instance, uint32_t sample_count) control_setstep(&_engine.lowpass, *psyn->ctrlLP, sample_count); _engine.monosynth = (int)*psyn->ctrlMono; - for (i = 0; i < 4; i++) { - _engine.params[i].shape.value = *psyn->ctrlOscShape[i]; - _engine.params[i].level.value = *psyn->ctrlOscLevel[i]; - } +// for (i = 0; i < 4; i++) { +// _engine.params[i].shape.value = *psyn->ctrlOscShape[i]; +// _engine.params[i].level.value = *psyn->ctrlOscLevel[i]; +// } while (frame < sample_count) { uint32_t to;