Read parameters from file

master
Peter Nelson 2010-01-21 10:46:09 +00:00
parent a0d931c082
commit 3535315aa4
3 changed files with 72 additions and 12 deletions

View File

@ -1,6 +1,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#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)

View File

@ -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);

8
psyn.c
View File

@ -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;