/* 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/SignalFilter_distortion.h" #include "utilities/lookup3.h" #include #define stemobject_implementation SignalFilter_distortion 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(getPropertyCount); stemobject_vtable_entry(getPropertyAtIndex); stemobject_vtable_entry(getPropertyValueStateless); stemobject_vtable_entry(setPropertyValue); stemobject_vtable_entry(transformSample); stemobject_vtable_end(); SignalFilter_distortion * SignalFilter_distortion_create(float amplification, float clipRange) { stemobject_create_implementation(init, amplification, clipRange) } bool SignalFilter_distortion_init(SignalFilter_distortion * self, float amplification, float clipRange) { call_super(init, self); self->amplification = amplification; self->clipRange = clipRange; return true; } void SignalFilter_distortion_dispose(SignalFilter_distortion * self) { call_super_virtual(dispose, self); } SignalFilter_distortion * SignalFilter_distortion_copy(SignalFilter_distortion * self) { stemobject_copy_implementation(initCopy) } bool SignalFilter_distortion_isEqual(SignalFilter_distortion * self, compat_type(SignalFilter_distortion *) compareUntyped) { SignalFilter_distortion * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return compare->amplification == self->amplification && compare->clipRange == self->clipRange; } uint32_t SignalFilter_distortion_hash(SignalFilter_distortion * self, uint32_t initval) { initval = hashlittle(&self->amplification, sizeof(self->amplification), initval); initval = hashlittle(&self->clipRange, sizeof(self->clipRange), initval); return initval; } void SignalFilter_distortion_initCopy(SignalFilter_distortion * self, SignalFilter_distortion * original) { call_super(initCopy, self, (SignalFilter *) original); self->amplification = original->amplification; self->clipRange = original->clipRange; } #define PROPERTY_INDEX_AMPLIFICATION 0 #define PROPERTY_INDEX_CLIP_RANGE 1 struct SignalFilter_distortion_state { float properties[2]; }; SamplerObject_state * SignalFilter_distortion_initState(SignalFilter_distortion * self) { struct SignalFilter_distortion_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->properties[PROPERTY_INDEX_AMPLIFICATION] = self->amplification; stateStruct->properties[PROPERTY_INDEX_CLIP_RANGE] = self->clipRange; return stateStruct; } void SignalFilter_distortion_disposeState(SignalFilter_distortion * self, SamplerObject_state * state) { free(state); } static SynthProperty synthProperties[2]; static void initSynthProperties(void) { SYNTH_PROPERTY(0, amplification, "Amplification", 0.0f, 4.0f, 0.0f, 0.05f) SYNTH_PROPERTY(1, clipRange, "Clip range", 0.0f, 1.0f, 1.0f, 0.05f) } SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() float SignalFilter_distortion_getPropertyValueStateless(SignalFilter_distortion * self, SynthPropertyIdentifier propertyIdentifier) { switch (propertyIdentifier.propertyIndex) { case 0: return self->amplification; case 1: return self->clipRange; } return 0.0f; } void SignalFilter_distortion_setPropertyValue(SignalFilter_distortion * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct SignalFilter_distortion_state * stateStruct = state; stateStruct->properties[propertyIdentifier.propertyIndex] = value; } float SignalFilter_distortion_transformSample(SignalFilter_distortion * self, SamplerObject_state * state, float sample, float time, float timeDelta) { struct SignalFilter_distortion_state * stateStruct = state; float amplification = stateStruct->properties[PROPERTY_INDEX_AMPLIFICATION]; float clipRange = stateStruct->properties[PROPERTY_INDEX_CLIP_RANGE]; return fminf(clipRange, fmaxf(-self->clipRange, sample * (amplification + 1.0f))); }