/* 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_compression.h" #include "utilities/lookup3.h" #include #define stemobject_implementation SignalFilter_compression 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_compression * SignalFilter_compression_create(float strength) { stemobject_create_implementation(init, strength) } bool SignalFilter_compression_init(SignalFilter_compression * self, float strength) { call_super(init, self); self->strength = strength; return true; } void SignalFilter_compression_dispose(SignalFilter_compression * self) { call_super_virtual(dispose, self); } SignalFilter_compression * SignalFilter_compression_copy(SignalFilter_compression * self) { stemobject_copy_implementation(initCopy) } bool SignalFilter_compression_isEqual(SignalFilter_compression * self, compat_type(SignalFilter_compression *) compareUntyped) { SignalFilter_compression * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return compare->strength == self->strength; } uint32_t SignalFilter_compression_hash(SignalFilter_compression * self, uint32_t initval) { initval = hashlittle(&self->strength, sizeof(self->strength), initval); return initval; } void SignalFilter_compression_initCopy(SignalFilter_compression * self, SignalFilter_compression * original) { call_super(initCopy, self, (SignalFilter *) original); self->strength = original->strength; } struct SignalFilter_compression_state { float strength; }; SamplerObject_state * SignalFilter_compression_initState(SignalFilter_compression * self) { struct SignalFilter_compression_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->strength = self->strength; return stateStruct; } void SignalFilter_compression_disposeState(SignalFilter_compression * self, SamplerObject_state * state) { free(state); } static SynthProperty synthProperties[1]; static void initSynthProperties(void) { SYNTH_PROPERTY(0, strength, "Strength", 0.0f, 1.0f, 0.0f, 0.05f) } SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() float SignalFilter_compression_getPropertyValueStateless(SignalFilter_compression * self, SynthPropertyIdentifier propertyIdentifier) { return self->strength; } void SignalFilter_compression_setPropertyValue(SignalFilter_compression * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct SignalFilter_compression_state * stateStruct = state; stateStruct->strength = value; } float SignalFilter_compression_transformSample(SignalFilter_compression * self, SamplerObject_state * state, float sample, float time, float timeDelta) { struct SignalFilter_compression_state * stateStruct = state; return powf(fabsf(sample), 1.0f / (1.0f + 4.0f * stateStruct->strength)) * ((sample > 0.0f) * 2 - 1); }