/* 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/FrequencyCurve_ramp.h" #include "utilities/lookup3.h" #include #define stemobject_implementation FrequencyCurve_ramp 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(sample); stemobject_vtable_end(); FrequencyCurve_ramp * FrequencyCurve_ramp_create(float baseValue, float linearRamp, float curvedRamp, float onset, float endpoint, float resetPoint, FrequencyCurve_ramp_boundType boundType) { stemobject_create_implementation(init, baseValue, linearRamp, curvedRamp, onset, endpoint, resetPoint, boundType) } bool FrequencyCurve_ramp_init(FrequencyCurve_ramp * self, float baseValue, float linearRamp, float curvedRamp, float onset, float endpoint, float resetPoint, FrequencyCurve_ramp_boundType boundType) { call_super(init, self, baseValue); self->linearRamp = linearRamp; self->curvedRamp = curvedRamp; self->onset = onset; self->endpoint = endpoint; self->resetPoint = resetPoint; self->boundType = boundType; return true; } void FrequencyCurve_ramp_dispose(FrequencyCurve_ramp * self) { call_super_virtual(dispose, self); } FrequencyCurve_ramp * FrequencyCurve_ramp_copy(FrequencyCurve_ramp * self) { stemobject_copy_implementation(initCopy) } bool FrequencyCurve_ramp_isEqual(FrequencyCurve_ramp * self, compat_type(FrequencyCurve_ramp *) compareUntyped) { FrequencyCurve_ramp * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return compare->linearRamp == self->linearRamp && compare->curvedRamp == self->curvedRamp && compare->onset == self->onset && compare->endpoint == self->endpoint && compare->resetPoint == self->resetPoint && compare->boundType == self->boundType; } uint32_t FrequencyCurve_ramp_hash(FrequencyCurve_ramp * self, uint32_t initval) { initval = hashlittle(&self->linearRamp, sizeof(self->linearRamp), initval); initval = hashlittle(&self->curvedRamp, sizeof(self->curvedRamp), initval); initval = hashlittle(&self->onset, sizeof(self->onset), initval); initval = hashlittle(&self->endpoint, sizeof(self->endpoint), initval); initval = hashlittle(&self->resetPoint, sizeof(self->resetPoint), initval); initval = hashlittle(&self->boundType, sizeof(self->boundType), initval); return initval; } void FrequencyCurve_ramp_initCopy(FrequencyCurve_ramp * self, FrequencyCurve_ramp * original) { call_super(initCopy, self, (FrequencyCurve *) original); self->linearRamp = original->linearRamp; self->curvedRamp = original->curvedRamp; self->onset = original->onset; self->endpoint = original->endpoint; self->resetPoint = original->resetPoint; self->boundType = original->boundType; } #define PROPERTY_INDEX_BASE_VALUE 0 #define PROPERTY_INDEX_LINEAR_RAMP 1 #define PROPERTY_INDEX_CURVED_RAMP 2 #define PROPERTY_INDEX_ONSET 3 #define PROPERTY_INDEX_ENDPOINT 4 #define PROPERTY_INDEX_RESET_POINT 5 struct FrequencyCurve_ramp_state { float properties[6]; }; SamplerObject_state * FrequencyCurve_ramp_initState(FrequencyCurve_ramp * self) { struct FrequencyCurve_ramp_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->properties[PROPERTY_INDEX_BASE_VALUE] = self->baseValue; stateStruct->properties[PROPERTY_INDEX_LINEAR_RAMP] = self->linearRamp; stateStruct->properties[PROPERTY_INDEX_CURVED_RAMP] = self->curvedRamp; stateStruct->properties[PROPERTY_INDEX_ONSET] = self->onset; stateStruct->properties[PROPERTY_INDEX_ENDPOINT] = self->endpoint; stateStruct->properties[PROPERTY_INDEX_RESET_POINT] = self->resetPoint; return stateStruct; } void FrequencyCurve_ramp_disposeState(FrequencyCurve_ramp * self, SamplerObject_state * state) { free(state); } static SynthProperty synthProperties[6]; static void initSynthProperties(void) { SYNTH_PROPERTY(PROPERTY_INDEX_BASE_VALUE, baseValue, "Base value", 0.0f, 9.0f, 4.0f, 1.0f / 12.0f) SYNTH_PROPERTY(PROPERTY_INDEX_LINEAR_RAMP, linearRamp, "Linear change", -16.0f, 16.0f, 1.0f, 0.05f) SYNTH_PROPERTY(PROPERTY_INDEX_CURVED_RAMP, curvedRamp, "Curved change", -16.0f, 16.0f, 0.0f, 0.05f) SYNTH_PROPERTY(PROPERTY_INDEX_ONSET, onset, "Onset", 0.0f, 1.0f, 0.0f, 0.05f) SYNTH_PROPERTY(PROPERTY_INDEX_ENDPOINT, endpoint, "Endpoint", 0.0f, 1.0f, 1.0f, 0.05f) SYNTH_PROPERTY(PROPERTY_INDEX_RESET_POINT, resetPoint, "Reset point", 0.0f, 1.0f, 1.0f, 0.05f) } SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() float FrequencyCurve_ramp_getPropertyValueStateless(FrequencyCurve_ramp * self, SynthPropertyIdentifier propertyIdentifier) { switch (propertyIdentifier.propertyIndex) { case PROPERTY_INDEX_BASE_VALUE: return self->baseValue; case PROPERTY_INDEX_LINEAR_RAMP: return self->linearRamp; case PROPERTY_INDEX_CURVED_RAMP: return self->curvedRamp; case PROPERTY_INDEX_ONSET: return self->onset; case PROPERTY_INDEX_ENDPOINT: return self->endpoint; case PROPERTY_INDEX_RESET_POINT: return self->resetPoint; } return 0.0f; } void FrequencyCurve_ramp_setPropertyValue(FrequencyCurve_ramp * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct FrequencyCurve_ramp_state * stateStruct = state; stateStruct->properties[propertyIdentifier.propertyIndex] = value; } float FrequencyCurve_ramp_sample(FrequencyCurve_ramp * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct FrequencyCurve_ramp_state * stateStruct = state; float baseValue = stateStruct->properties[PROPERTY_INDEX_BASE_VALUE]; float linearRamp = stateStruct->properties[PROPERTY_INDEX_LINEAR_RAMP]; float curvedRamp = stateStruct->properties[PROPERTY_INDEX_CURVED_RAMP]; float onset = stateStruct->properties[PROPERTY_INDEX_ONSET]; float endpoint = stateStruct->properties[PROPERTY_INDEX_ENDPOINT]; float resetPoint = stateStruct->properties[PROPERTY_INDEX_RESET_POINT]; if (self->boundType == RAMP_RESET) { phase = fmaxf(fmodf(phase, resetPoint), onset); } else if (self->boundType == RAMP_CLAMP) { phase = fmaxf(fminf(phase, endpoint), onset); } float rampPhase = (phase - onset) / fmaxf(endpoint - onset, 0.0f); return baseValue + linearRamp * rampPhase + curvedRamp * rampPhase * rampPhase; }