/* 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_highLowpass.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_highLowpass 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(getOwnPropertyCount); stemobject_vtable_entry(getOwnProperties); stemobject_vtable_entry(sample); stemobject_vtable_end(); WaveModifier_highLowpass * WaveModifier_highLowpass_create(float lpStrength, float hpStrength) { stemobject_create_implementation(init, lpStrength, hpStrength) } bool WaveModifier_highLowpass_init(WaveModifier_highLowpass * self, float lpStrength, float hpStrength) { call_super(init, self); self->lpStrength = lpStrength; self->hpStrength = hpStrength; return true; } void WaveModifier_highLowpass_dispose(WaveModifier_highLowpass * self) { call_super_virtual(dispose, self); } WaveModifier_highLowpass * WaveModifier_highLowpass_copy(WaveModifier_highLowpass * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_highLowpass_isEqual(WaveModifier_highLowpass * self, compat_type(WaveModifier_highLowpass *) compareUntyped) { WaveModifier_highLowpass * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return self->lpStrength == compare->lpStrength && self->hpStrength == compare->hpStrength; } uint32_t WaveModifier_highLowpass_hash(WaveModifier_highLowpass * self, uint32_t initval) { initval = hashlittle(&self->lpStrength, sizeof(self->lpStrength), initval); initval = hashlittle(&self->hpStrength, sizeof(self->hpStrength), initval); return initval; } void WaveModifier_highLowpass_initCopy(WaveModifier_highLowpass * self, WaveModifier_highLowpass * original) { call_super(initCopy, self, (WaveModifier *) original); self->lpStrength = original->lpStrength; self->hpStrength = original->hpStrength; } #define PROPERTY_INDEX_BLEND 0 #define PROPERTY_INDEX_LP_STRENGTH 1 #define PROPERTY_INDEX_HP_STRENGTH 2 #define PROPERTY_COUNT 3 struct WaveModifier_highLowpass_state { SamplerObject_state * upstreamState; float properties[PROPERTY_COUNT]; float lpFilteredSample; float hpFilteredSample; }; SamplerObject_state * WaveModifier_highLowpass_initState(WaveModifier_highLowpass * self) { struct WaveModifier_highLowpass_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->properties[PROPERTY_INDEX_BLEND] = self->blend; stateStruct->properties[PROPERTY_INDEX_LP_STRENGTH] = self->lpStrength; stateStruct->properties[PROPERTY_INDEX_HP_STRENGTH] = self->hpStrength; stateStruct->lpFilteredSample = 0.0f; stateStruct->hpFilteredSample = 0.0f; stateStruct->upstreamState = call_virtual(initState, self->upstream); return stateStruct; } unsigned int WaveModifier_highLowpass_getOwnPropertyCount(WaveModifier_highLowpass * self) { return PROPERTY_COUNT; } SynthProperty * WaveModifier_highLowpass_getOwnProperties(WaveModifier_highLowpass * self) { static SynthProperty properties[PROPERTY_COUNT] = { {.propertyIndex = 0, .description = "Blend (distortion)", .offset = offsetof(WaveModifier_highLowpass, blend), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}, {.propertyIndex = 1, .description = "LP strength", .offset = offsetof(WaveModifier_highLowpass, hpStrength), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f}, {.propertyIndex = 2, .description = "HP strength", .offset = offsetof(WaveModifier_highLowpass, lpStrength), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f} }; return properties; } #define NOMINAL_SAMPLE_RATE 44100 float WaveModifier_highLowpass_sample(WaveModifier_highLowpass * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_highLowpass_state * stateStruct = state; float lpStrength = stateStruct->properties[PROPERTY_INDEX_LP_STRENGTH]; float hpStrength = stateStruct->properties[PROPERTY_INDEX_HP_STRENGTH]; float lastLPFilteredSample = stateStruct->lpFilteredSample; float hpFilteredSample = stateStruct->hpFilteredSample; lpStrength = powf(cbrtf(lpStrength), NOMINAL_SAMPLE_RATE * timeDelta); hpStrength = hpStrength * hpStrength * NOMINAL_SAMPLE_RATE * timeDelta; float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); float lpFilteredSample = lastLPFilteredSample + (sample - lastLPFilteredSample) * (1.0f - lpStrength); stateStruct->lpFilteredSample = lpFilteredSample; hpFilteredSample += lpFilteredSample - lastLPFilteredSample; hpFilteredSample -= hpFilteredSample * hpStrength; stateStruct->hpFilteredSample = hpFilteredSample; float blend = stateStruct->properties[PROPERTY_INDEX_BLEND]; return hpFilteredSample * blend + sample * (1.0f - blend); }