90 lines
2.3 KiB
C++
90 lines
2.3 KiB
C++
/*
|
|
Chassis polysynth framework
|
|
|
|
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 "voice.hpp"
|
|
|
|
#include <math.h>
|
|
|
|
static float blep(float phase, float theta) {
|
|
float t;
|
|
|
|
if (phase < theta) {
|
|
t = phase / theta;
|
|
return (2 * t) - (t * t) - 1;
|
|
}
|
|
if (phase > (1 - theta)) {
|
|
t = (phase - 1) / theta;
|
|
return (2 * t) + (t * t) + 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool Voice::isFree() {
|
|
return keyState == K_OFF;
|
|
}
|
|
|
|
void Voice::on(uint32_t key, bool reset = 0) {
|
|
keyState = K_ON;
|
|
envState = ATTACK;
|
|
note = key;
|
|
if (reset) env = 0;
|
|
omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f;
|
|
target = 1;
|
|
}
|
|
|
|
void Voice::off() {
|
|
keyState = K_OFF;
|
|
envState = RELEASE;
|
|
target = 0;
|
|
}
|
|
|
|
void Voice::run(Synth &s, float *buffer, uint32_t samples) {
|
|
float y, offset, pw = 0;
|
|
env = ((target - env) * 0.01) + env;
|
|
pw = 0.5 - s.lfo * 0.4;
|
|
|
|
// s.p.sqr=1;
|
|
|
|
for (uint32_t i = 0; i < samples; i++) {
|
|
|
|
// sawtooth
|
|
y = 1 - (2 * phase);
|
|
y += blep(phase, omega);
|
|
|
|
// need this if either saw or square is on;
|
|
y *= (s.p.saw + s.p.sqr);
|
|
|
|
// bodged for 16ms 48000Hz
|
|
pw_rc = ((pw - pw_rc) * 0.000625) + pw_rc;
|
|
|
|
// phase shifted saw
|
|
offset = phase + pw_rc;
|
|
if (offset > 1) offset -= 1;
|
|
|
|
y -= (1 - (2 * offset)) * s.p.sqr;
|
|
y -= blep(offset, omega + (pw_rc - s.lastpw)) * s.p.sqr;
|
|
//s.lastpw = pw_rc;
|
|
|
|
buffer[i] += (0.125 * y * env);
|
|
phase += omega;
|
|
if (phase > 1) phase -= 1;
|
|
|
|
}
|
|
}
|