#include #include #include #include #include "env.h" #include "osc.h" #include "filter.h" #include "voice.h" #include "engine.h" #include "psyn.h" static double _freqs[128]; void engine_init() { uint32_t i; for (i = 0; i < 128; i++) { _freqs[i] = 440.0 * pow(2.0, (i - 69.0) / 12.0); } osc_init(); env_init(&_env, 0.0125, 0.025, 5.0, 0.0, 0.25); env_init(&_env2, 0.0125, 0.025, 0.5, 0.0, 0.25); } void engine_run(struct engine_t *engine, uint32_t samples, float *left, float *right) { struct voice_t *v; uint32_t pos; uint32_t i; for (pos = 0; pos < samples; pos++) { left[pos] = 0.0; right[pos] = 0.0; for (i = 0; i < NUM_LFO; i++) { osc_tick(&engine->osc[i]); } v = engine->voice; for (i = 0; i < NUM_POLYPHONY; i++, v++) { if (!v->playing) continue; voice_run(v, 1, left + pos, right + pos); } } } void engine_startvoice(struct engine_t *engine, uint8_t note, uint8_t velocity) { struct voice_t *v = engine->voice; uint32_t i; for (i = 0; i < NUM_POLYPHONY; i++, v++) { if (v->playing) continue; /* Constant voice parameters */ v->playing = true; v->sample = 0; v->released = 0; v->note = note; v->velocity = velocity / 127.0; /* Voice parameters which determine sound */ voice_init(v, _freqs[note]); return; } } void engine_endvoice(struct engine_t *engine, uint8_t note, uint8_t velocity) { struct voice_t *v = engine->voice; uint32_t i; for (i = 0; i < NUM_POLYPHONY; i++, v++) { if (v->released > 0 || v->note != note) continue; v->released = v->sample; } }