/* Chassis polysynth framework Copyright 2024 Gordon JC Pearce 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 #include static float blep(float p, float theta) { float t; // low (late) side of step if (p < theta) { printf("phase < theta, %f %f\n", p, theta); t = p / theta; return (2 * t) - (t * t) - 1; } // high (early) side of step if (p > (1 - theta)) { printf("phase > 1-theta %f %f \n", p, theta); t = (p - 1) / theta; return (2 * t) + (t * t) + 1; } // printf("no action\n"); return 0; } bool Voice::isFree() { return keyState == K_OFF; } void Voice::on(uint32_t key, bool reset = 0) { keyState = K_WAIT; // envState = ATTACK; note = key; if (reset) env = 0; omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f; target = 1; env = 1; } void Voice::off() { keyState = K_OFF; envState = RELEASE; target = 0; } void Voice::gate() { /* if (keyState == K_WAIT) { envState = ATTACK; keyState = K_ON; target = 1; } if (keyState == K_OFF) { envState = RELEASE; target = 0; } */ env = ((target - env) * 0.005f) + env; if (env < 0.001) env = 0; } static inline float poly3blep0(float t) { // these are just sanity checks // correct code doesn't need them if (t < 0) return 0; if (t > 1) return 1; float t2 = t * t; return t * t2 - 0.5f * t2 * t2; } // And second sample as wrapper, optimize if you want. static inline float poly3blep1(float t) { return -poly3blep0(1 - t); } void Voice::run(Synth &s, float *buffer, uint32_t samples) { float y, out, pw = 0, t; s.p.sqr = 1; s.p.saw = 1; float mix = 1; for (uint32_t i = 0; i < samples; i++) { #if 1 y = delay; delay = 0; phase += omega; float pulseWidth = 0.5; // pwm[i]; // if(pulseWidth > 1) pulseWidth = 1; // if(pulseWidth < 0) pulseWidth = 0; while (true) { if (pulseStage == 0) { if (phase < pulseWidth) break; #if 1 float t = (phase - pulseWidth) / omega; #else float t = (phase - pulseWidth) / (widthDelay - pulseWidth + freq); #endif y += mix * poly3blep0(t); delay += mix * poly3blep1(t); pulseStage = 1; } if (pulseStage == 1) { if (phase < 1) break; float t = (phase - 1) / omega; y -= poly3blep0(t); delay -= poly3blep1(t); pulseStage = 0; phase -= 1; subosc = (1-subosc); } } delay += (1 - mix) * phase + mix * (pulseStage ? 1.f : 0.f); delay += subosc; out = (2 * y) - 1; // widthDelay = pulseWidth; #else // 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; pw_rc = 0.5; // phase shifted saw offset = phase + pw_rc; if (offset > 1) offset -= 1; y -= (1 - (2 * offset)) * s.p.sqr; y -= blep(offset, omega) * s.p.sqr; // s.lastpw = pw_rc; phase += omega; if (phase > 1) { // printf("step\n"); phase -= 1; } #endif buffer[i] += (0.25 * out * env); } }