/* Copyright (c) 2021 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "audiosynth/WaveModifier_sfxrHighLowpass.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_sfxrHighLowpass stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(copy); stemobject_vtable_entry(isEqual); stemobject_vtable_entry(hash); stemobject_vtable_entry(initState); stemobject_vtable_entry(disposeState); stemobject_vtable_entry(sample); stemobject_vtable_end(); WaveModifier_sfxrHighLowpass * WaveModifier_sfxrHighLowpass_create(float lpCutoff, float lpRamp, float lpResonance, float hpCutoff, float hpRamp) { stemobject_create_implementation(init, lpCutoff, lpRamp, lpResonance, hpCutoff, hpRamp) } bool WaveModifier_sfxrHighLowpass_init(WaveModifier_sfxrHighLowpass * self, float lpCutoff, float lpRamp, float lpResonance, float hpCutoff, float hpRamp) { call_super(init, self); self->lpCutoff = lpCutoff; self->lpRamp = lpRamp; self->lpResonance = lpResonance; self->hpCutoff = hpCutoff; self->hpRamp = hpRamp; return true; } void WaveModifier_sfxrHighLowpass_dispose(WaveModifier_sfxrHighLowpass * self) { call_super_virtual(dispose, self); } WaveModifier_sfxrHighLowpass * WaveModifier_sfxrHighLowpass_copy(WaveModifier_sfxrHighLowpass * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_sfxrHighLowpass_isEqual(WaveModifier_sfxrHighLowpass * self, compat_type(WaveModifier_sfxrHighLowpass *) compareUntyped) { WaveModifier_sfxrHighLowpass * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return self->lpCutoff == compare->lpCutoff && self->lpRamp == compare->lpRamp && self->lpResonance == compare->lpResonance && self->hpCutoff == compare->hpCutoff && self->hpRamp == compare->hpRamp; } uint32_t WaveModifier_sfxrHighLowpass_hash(WaveModifier_sfxrHighLowpass * self, uint32_t initval) { initval = hashlittle(&self->lpCutoff, sizeof(self->lpCutoff), initval); initval = hashlittle(&self->lpRamp, sizeof(self->lpRamp), initval); initval = hashlittle(&self->lpResonance, sizeof(self->lpResonance), initval); initval = hashlittle(&self->hpCutoff, sizeof(self->hpCutoff), initval); initval = hashlittle(&self->hpRamp, sizeof(self->hpRamp), initval); return initval; } void WaveModifier_sfxrHighLowpass_initCopy(WaveModifier_sfxrHighLowpass * self, WaveModifier_sfxrHighLowpass * original) { call_super(initCopy, self, (WaveModifier *) original); self->lpCutoff = original->lpCutoff; self->lpRamp = original->lpRamp; self->lpResonance = original->lpResonance; self->hpCutoff = original->hpCutoff; self->hpRamp = original->hpRamp; } struct WaveModifier_sfxrHighLowpass_state { SamplerObject_state * upstreamState; float blend; float fltp; float fltdp; float fltw; float fltw_d; float fltdmp; float fltphp; float flthp; float flthp_d; }; SamplerObject_state * WaveModifier_sfxrHighLowpass_initState(WaveModifier_sfxrHighLowpass * self) { struct WaveModifier_sfxrHighLowpass_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->blend = self->blend; stateStruct->upstreamState = call_virtual(initState, self->upstream); stateStruct->fltp = 0.0f; stateStruct->fltdp = 0.0f; stateStruct->fltw = powf(self->lpCutoff, 3.0f) * 0.1f; stateStruct->fltw_d = 1.0f + self->lpRamp * 0.0001f; stateStruct->fltdmp = 5.0f / (1.0f + powf(self->lpResonance, 2.0f) * 20.0f) * (0.01f + stateStruct->fltw); if (stateStruct->fltdmp > 0.8f) { stateStruct->fltdmp = 0.8f; } stateStruct->fltphp = 0.0f; stateStruct->flthp = powf(self->hpCutoff, 2.0f) * 0.1f; stateStruct->flthp_d = 1.0f + self->hpRamp * 0.0003f; return stateStruct; } void WaveModifier_sfxrHighLowpass_disposeState(WaveModifier_sfxrHighLowpass * self, SamplerObject_state * state) { struct WaveModifier_sfxrHighLowpass_state * stateStruct = (struct WaveModifier_sfxrHighLowpass_state *) state; call_virtual(disposeState, self->upstream, stateStruct->upstreamState); free(stateStruct); } float WaveModifier_sfxrHighLowpass_sample(WaveModifier_sfxrHighLowpass * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_sfxrHighLowpass_state * stateStruct = (struct WaveModifier_sfxrHighLowpass_state *) state; float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); float modifiedSample = sample; stateStruct->flthp *= stateStruct->flthp_d; if (stateStruct->flthp < 0.00001f) { stateStruct->flthp = 0.00001f; } if (stateStruct->flthp > 0.1f) { stateStruct->flthp = 0.1f; } for (unsigned int supersample = 0; supersample < 8; supersample++) { float pp = stateStruct->fltp; stateStruct->fltw *= stateStruct->fltw_d; if (stateStruct->fltw < 0.0f) { stateStruct->fltw = 0.0f; } else if (stateStruct->fltw > 0.1f) { stateStruct->fltw = 0.1f; } if (self->lpCutoff != 1.0f) { stateStruct->fltdp += (modifiedSample - stateStruct->fltp) * stateStruct->fltw; stateStruct->fltdp -= stateStruct->fltdp * stateStruct->fltdmp; } else { stateStruct->fltp = modifiedSample; stateStruct->fltdp = 0.0f; } stateStruct->fltp += stateStruct->fltdp; stateStruct->fltphp += stateStruct->fltp - pp; stateStruct->fltphp -= stateStruct->fltphp * stateStruct->flthp; } modifiedSample = stateStruct->fltphp; float blend = stateStruct->blend; return modifiedSample * blend + sample * (1.0f - blend); }