2024-09-03 11:42:16 +00:00
|
|
|
/*
|
2024-09-03 15:17:32 +00:00
|
|
|
Chassis polysynth framework
|
2024-09-03 11:42:16 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2024-09-03 14:22:56 +00:00
|
|
|
|
|
|
|
#include "voice.hpp"
|
|
|
|
|
2024-09-03 15:17:32 +00:00
|
|
|
#include <math.h>
|
2024-09-03 14:31:54 +00:00
|
|
|
|
|
|
|
static float blep(float phase, float theta) {
|
|
|
|
float t;
|
|
|
|
|
2024-09-03 15:17:32 +00:00
|
|
|
if (phase < theta) {
|
2024-09-03 14:31:54 +00:00
|
|
|
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() {
|
2024-09-03 14:22:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-03 22:41:01 +00:00
|
|
|
void Voice::run(Synth &s, float *buffer, uint32_t samples) {
|
2024-09-03 23:40:40 +00:00
|
|
|
float y;
|
|
|
|
env = ((target - env) * 0.01) + env;
|
2024-09-03 14:22:56 +00:00
|
|
|
for (uint32_t i = 0; i < samples; i++) {
|
|
|
|
phase += omega;
|
2024-09-03 23:40:40 +00:00
|
|
|
if (phase > 1) phase -= 1;
|
|
|
|
y = (2 * phase) - 1;
|
|
|
|
y -= blep(phase, omega);
|
2024-09-03 14:31:54 +00:00
|
|
|
|
2024-09-03 23:40:40 +00:00
|
|
|
buffer[i] += (0.25 * y * env) * s.p.saw;
|
2024-09-03 14:22:56 +00:00
|
|
|
}
|
|
|
|
}
|