71 lines
2.3 KiB
C++
71 lines
2.3 KiB
C++
#include "generator.hpp"
|
|
/*
|
|
sonnenlicht poly ensemble
|
|
|
|
Copyright 2024 Gordon JC Pearce <gordonjcp@gjcp.net>
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include <math.h>
|
|
#include <string.h>
|
|
|
|
#include <cstdio>
|
|
|
|
#include "DistrhoPluginInfo.h"
|
|
|
|
Generator::Generator() {
|
|
}
|
|
|
|
void Generator::setupGenerator(double sampleRate) {
|
|
// create the phase increments for each semitone
|
|
for (uint8_t i = 0; i < 12; i++) {
|
|
phase[i] = 0;
|
|
uint32_t f;
|
|
f = (1 << 31) * (65.406 * powf(2, 0.083334 * (i + 12)) / sampleRate);
|
|
omega[i] = f;
|
|
}
|
|
}
|
|
|
|
void Generator::runBlock(float *output, uint8_t *noteTable, uint32_t frames) {
|
|
memset(output, 0, frames * sizeof(float));
|
|
|
|
for (uint32_t i = 0; i < frames; i++) {
|
|
for (uint8_t p = 0; p < 12; p++) {
|
|
phase[p] += omega[p];
|
|
}
|
|
|
|
for (uint8_t k = 0; k < NUM_VOICES; k++) {
|
|
uint8_t n1 = noteTable[k] % 12, n2 = (noteTable[k] / 12 - 3);
|
|
float n = ((phase[n1] & (0x80000000 >> n2)) ? 0.25 : -0.25);
|
|
|
|
voices[k].vc34 = ((n - voices[k].vc34) * voices[k].c34) + voices[k].vc34;
|
|
|
|
n -= voices[k].vc34;
|
|
n *= (phase[n1] & (0x80000000 >> n2)) ? 1 : 0;
|
|
|
|
output[i] += n * (noteTable[k] & 0x80 ? 0 : 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Voice::startNote(uint8_t key, double sampleRate) {
|
|
// start a new note
|
|
// float fc = 88.4 * powf(2, 0.083334 * (key - 36));
|
|
// c34 = powf((1 - 2 * fc / sampleRate), 2);
|
|
float fc = 88.4 * powf(2, 0.083334 * (key - 36));
|
|
float coef = 1 - exp(-6.283 * fc / 48000.0);
|
|
c34 = coef;
|
|
printf("key = %d, shaper cutoff = %f, coefficient = %f %f\n", key, fc, coef, c34);
|
|
}
|