/* Copyright (c) 2023 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_timeCurve.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_timeCurve stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(copy); stemobject_vtable_entry(isEqual); stemobject_vtable_entry(hash); stemobject_vtable_entry(getOwnPropertyCount); stemobject_vtable_entry(getOwnProperties); stemobject_vtable_entry(sample); stemobject_vtable_end(); WaveModifier_timeCurve * WaveModifier_timeCurve_create(float strength, float center) { stemobject_create_implementation(init, strength, center) } bool WaveModifier_timeCurve_init(WaveModifier_timeCurve * self, float strength, float center) { call_super(init, self); self->strength = strength; self->center = center; return true; } void WaveModifier_timeCurve_dispose(WaveModifier_timeCurve * self) { call_super_virtual(dispose, self); } WaveModifier_timeCurve * WaveModifier_timeCurve_copy(WaveModifier_timeCurve * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_timeCurve_isEqual(WaveModifier_timeCurve * self, compat_type(WaveModifier_timeCurve *) compareUntyped) { WaveModifier_timeCurve * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return self->strength == compare->strength && self->center == compare->center; } uint32_t WaveModifier_timeCurve_hash(WaveModifier_timeCurve * self, uint32_t initval) { initval = hashlittle(&self->strength, sizeof(self->strength), initval); initval = hashlittle(&self->center, sizeof(self->center), initval); return initval; } void WaveModifier_timeCurve_initCopy(WaveModifier_timeCurve * self, WaveModifier_timeCurve * original) { call_super(initCopy, self, (WaveModifier *) original); self->strength = original->strength; self->center = original->center; } #define PROPERTY_INDEX_BLEND 0 #define PROPERTY_INDEX_STRENGTH 1 #define PROPERTY_INDEX_CENTER 2 #define PROPERTY_COUNT 3 struct WaveModifier_timeCurve_state { SamplerObject_state * upstreamState; float properties[PROPERTY_COUNT]; }; unsigned int WaveModifier_timeCurve_getOwnPropertyCount(WaveModifier_timeCurve * self) { return PROPERTY_COUNT; } SynthProperty * WaveModifier_timeCurve_getOwnProperties(WaveModifier_timeCurve * self) { static SynthProperty properties[PROPERTY_COUNT] = { {.propertyIndex = 0, .description = "Blend (time curve)", .offset = offsetof(WaveModifier_timeCurve, blend), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}, {.propertyIndex = 1, .description = "Strength", .offset = offsetof(WaveModifier_timeCurve, strength), .minValue = 0.0f, .maxValue = 20.0f, .defaultValue = 0.0f, .stepIncrement = 0.25f}, {.propertyIndex = 2, .description = "Center", .offset = offsetof(WaveModifier_timeCurve, center), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 0.5f, .stepIncrement = 0.05f} }; return properties; } float WaveModifier_timeCurve_sample(WaveModifier_timeCurve * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_timeCurve_state * stateStruct = state; float blend = stateStruct->properties[PROPERTY_INDEX_BLEND]; float strength = stateStruct->properties[PROPERTY_INDEX_STRENGTH]; float center = stateStruct->properties[PROPERTY_INDEX_CENTER]; float localPhase = fmodf(phase + 1.0f + center, 1.0f); if (localPhase < 0.5f) { localPhase = powf(localPhase * 2.0f, 1.0f + strength) * 0.5f; } else { localPhase = 1.0f - powf((1.0f - localPhase) * 2.0f, 1.0f + strength) * 0.5f; } localPhase = fmodf(localPhase + 1.0f - center, 1.0f); float modifiedPhase = floorf(phase) + localPhase; float modifiedSample = call_virtual(sample, self->upstream, stateStruct->upstreamState, modifiedPhase, phaseDelta, time, timeDelta); if (blend == 1.0f) { return modifiedSample; } float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); return modifiedSample * blend + sample * (1.0f - blend); }