/* Copyright (c) 2022 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_timeValueTransform.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_timeValueTransform 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_timeValueTransform * WaveModifier_timeValueTransform_create(float timeOffset, float timeScale, float valueOffset, float valueScale) { stemobject_create_implementation(init, timeOffset, timeScale, valueOffset, valueScale) } bool WaveModifier_timeValueTransform_init(WaveModifier_timeValueTransform * self, float timeOffset, float timeScale, float valueOffset, float valueScale) { call_super(init, self); self->timeOffset = timeOffset; self->timeScale = timeScale; self->valueOffset = valueOffset; self->valueScale = valueScale; return true; } void WaveModifier_timeValueTransform_dispose(WaveModifier_timeValueTransform * self) { call_super_virtual(dispose, self); } WaveModifier_timeValueTransform * WaveModifier_timeValueTransform_copy(WaveModifier_timeValueTransform * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_timeValueTransform_isEqual(WaveModifier_timeValueTransform * self, compat_type(WaveModifier_timeValueTransform *) compareUntyped) { WaveModifier_timeValueTransform * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return self->timeOffset == compare->timeOffset && self->timeScale == compare->timeScale && self->valueOffset == compare->valueOffset && self->valueScale == compare->valueScale; } uint32_t WaveModifier_timeValueTransform_hash(WaveModifier_timeValueTransform * self, uint32_t initval) { initval = hashlittle(&self->timeOffset, sizeof(self->timeOffset), initval); initval = hashlittle(&self->timeScale, sizeof(self->timeScale), initval); initval = hashlittle(&self->valueOffset, sizeof(self->valueOffset), initval); initval = hashlittle(&self->valueScale, sizeof(self->valueScale), initval); return initval; } void WaveModifier_timeValueTransform_initCopy(WaveModifier_timeValueTransform * self, WaveModifier_timeValueTransform * original) { call_super(initCopy, self, (WaveModifier *) original); self->timeOffset = original->timeOffset; self->timeScale = original->timeScale; self->valueOffset = original->valueOffset; self->valueScale = original->valueScale; } #define PROPERTY_INDEX_BLEND 0 #define PROPERTY_INDEX_TIME_OFFSET 1 #define PROPERTY_INDEX_TIME_SCALE 2 #define PROPERTY_INDEX_VALUE_OFFSET 3 #define PROPERTY_INDEX_VALUE_SCALE 4 #define PROPERTY_COUNT 5 struct WaveModifier_timeValueTransform_state { SamplerObject_state * upstreamState; float properties[PROPERTY_COUNT]; }; unsigned int WaveModifier_timeValueTransform_getOwnPropertyCount(WaveModifier_timeValueTransform * self) { return PROPERTY_COUNT; } SynthProperty * WaveModifier_timeValueTransform_getOwnProperties(WaveModifier_timeValueTransform * self) { static SynthProperty properties[PROPERTY_COUNT] = { {.propertyIndex = 0, .description = "Blend (time/value)", .offset = offsetof(WaveModifier_timeValueTransform, blend), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}, {.propertyIndex = 1, .description = "Time offset", .offset = offsetof(WaveModifier_timeValueTransform, timeOffset), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f}, {.propertyIndex = 2, .description = "Time scale", .offset = offsetof(WaveModifier_timeValueTransform, timeScale), .minValue = 0.1f, .maxValue = 5.0f, .defaultValue = 1.0f, .stepIncrement = 0.1f}, {.propertyIndex = 3, .description = "Value offset", .offset = offsetof(WaveModifier_timeValueTransform, valueOffset), .minValue = -1.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f}, {.propertyIndex = 4, .description = "Value scale", .offset = offsetof(WaveModifier_timeValueTransform, valueScale), .minValue = 0.1f, .maxValue = 5.0f, .defaultValue = 1.0f, .stepIncrement = 0.1f} }; return properties; } float WaveModifier_timeValueTransform_sample(WaveModifier_timeValueTransform * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_timeValueTransform_state * stateStruct = state; float blend = stateStruct->properties[PROPERTY_INDEX_BLEND]; float timeOffset = stateStruct->properties[PROPERTY_INDEX_TIME_OFFSET]; float timeScale = stateStruct->properties[PROPERTY_INDEX_TIME_SCALE]; float valueOffset = stateStruct->properties[PROPERTY_INDEX_VALUE_OFFSET]; float valueScale = stateStruct->properties[PROPERTY_INDEX_VALUE_SCALE]; phase += timeOffset; phase *= timeScale; phaseDelta *= timeScale; float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); float modifiedSample = (sample + valueOffset) * valueScale; return modifiedSample * blend + sample * (1.0f - blend); }