/* 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_stairstep.h" #include "utilities/IOUtilities.h" #include "utilities/lookup3.h" #include #include #define stemobject_implementation FrequencyCurve_stairstep 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(getLength); stemobject_vtable_entry(getPropertyCount); stemobject_vtable_entry(getPropertyAtIndex); stemobject_vtable_entry(getPropertyValueStateless); stemobject_vtable_entry(setPropertyValue); stemobject_vtable_entry(sample); stemobject_vtable_end(); FrequencyCurve_stairstep * FrequencyCurve_stairstep_create(float baseValue, unsigned int stepCount, struct FrequencyCurve_stairstep_step * steps, unsigned int repeatStartIndex, unsigned int repeatCount) { stemobject_create_implementation(init, baseValue, stepCount, steps, repeatStartIndex, repeatCount) } bool FrequencyCurve_stairstep_init(FrequencyCurve_stairstep * self, float baseValue, unsigned int stepCount, struct FrequencyCurve_stairstep_step * steps, unsigned int repeatStartIndex, unsigned int repeatCount) { call_super(init, self, baseValue); self->stepCount = stepCount; self->steps = memdup(steps, stepCount * sizeof(*steps)); self->repeatStartIndex = repeatStartIndex; self->repeatCount = repeatCount; return true; } void FrequencyCurve_stairstep_dispose(FrequencyCurve_stairstep * self) { free(self->steps); call_super_virtual(dispose, self); } FrequencyCurve_stairstep * FrequencyCurve_stairstep_copy(FrequencyCurve_stairstep * self) { stemobject_copy_implementation(initCopy) } bool FrequencyCurve_stairstep_isEqual(FrequencyCurve_stairstep * self, compat_type(FrequencyCurve_stairstep *) compareUntyped) { FrequencyCurve_stairstep * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare) || compare->stepCount != self->stepCount || compare->repeatStartIndex != self->repeatStartIndex || compare->repeatCount != self->repeatCount) { return false; } return !memcmp(compare->steps, self->steps, self->stepCount * sizeof(*self->steps)); } uint32_t FrequencyCurve_stairstep_hash(FrequencyCurve_stairstep * self, uint32_t initval) { initval = hashlittle(&self->stepCount, sizeof(self->stepCount), initval); initval = hashlittle(&self->repeatStartIndex, sizeof(self->repeatStartIndex), initval); initval = hashlittle(&self->repeatCount, sizeof(self->repeatCount), initval); initval = hashlittle(self->steps, self->stepCount * sizeof(*self->steps), initval); return initval; } void FrequencyCurve_stairstep_initCopy(FrequencyCurve_stairstep * self, FrequencyCurve_stairstep * original) { call_super(initCopy, self, (FrequencyCurve *) original); self->stepCount = original->stepCount; self->steps = memdup(original->steps, original->stepCount * sizeof(*original->steps)); self->repeatStartIndex = original->repeatStartIndex; self->repeatCount = original->repeatCount; } #define PROPERTY_INDEX_BASE_VALUE 0 #define PROPERTY_INDEX_STEP_DURATION 1 #define PROPERTY_INDEX_STEP_CHANGE 2 #define PROPERTY_STEP_STRIDE 2 struct FrequencyCurve_stairstep_state { float properties[1]; }; SamplerObject_state * FrequencyCurve_stairstep_initState(FrequencyCurve_stairstep * self) { struct FrequencyCurve_stairstep_state * stateStruct = malloc(sizeof(*stateStruct) + sizeof(stateStruct->properties[0]) * self->stepCount * PROPERTY_STEP_STRIDE); stateStruct->properties[PROPERTY_INDEX_BASE_VALUE] = self->baseValue; for (unsigned int stepIndex = 0; stepIndex < self->stepCount; stepIndex++) { stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION] = self->steps[stepIndex].duration; stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_CHANGE] = self->steps[stepIndex].change; } return stateStruct; } void FrequencyCurve_stairstep_disposeState(FrequencyCurve_stairstep * self, SamplerObject_state * state) { free(state); } float FrequencyCurve_stairstep_getLength(FrequencyCurve_stairstep * self) { float timeBeforeRepeat = 0.0f; float timeAfterRepeat = 0.0f; unsigned int repeatStartIndex = self->repeatStartIndex; if (repeatStartIndex >= self->stepCount) { repeatStartIndex = self->stepCount - 1; } unsigned int stepIndex = 0; for (; stepIndex < repeatStartIndex; stepIndex++) { timeBeforeRepeat += self->steps[stepIndex].duration; } for (; stepIndex < self->stepCount; stepIndex++) { timeAfterRepeat += self->steps[stepIndex].duration; } if (self->repeatCount == STAIRSTEP_REPEAT_INFINITE) { return timeBeforeRepeat + timeAfterRepeat; } return timeBeforeRepeat + timeAfterRepeat * (self->repeatCount + 1); } unsigned int FrequencyCurve_stairstep_getPropertyCount(FrequencyCurve_stairstep * self) { return PROPERTY_INDEX_STEP_DURATION + self->stepCount * PROPERTY_STEP_STRIDE; } SynthProperty FrequencyCurve_stairstep_getPropertyAtIndex(FrequencyCurve_stairstep * self, unsigned int index) { if (index == PROPERTY_INDEX_BASE_VALUE) { return (SynthProperty) {index, "Base value", 0.0f, 9.0f, 4.0f, 1.0f / 12.0f, 0}; } index -= PROPERTY_INDEX_STEP_DURATION; unsigned int stepIndex = index / PROPERTY_STEP_STRIDE; if (stepIndex < self->stepCount) { SynthProperty property; if (index % 2 == 0) { property.propertyIndex = index + PROPERTY_INDEX_STEP_DURATION; snprintf_safe(property.description, sizeof(property.description), "Step %u duration", stepIndex + 1); property.minValue = 0.0f; property.maxValue = 1.0f; property.defaultValue = 0.25f; property.stepIncrement = 0.05f; } else { property.propertyIndex = index + PROPERTY_INDEX_STEP_DURATION; snprintf_safe(property.description, sizeof(property.description), "Step %u change", stepIndex + 1); property.minValue = -8.0f; property.maxValue = 8.0f; property.defaultValue = 1.0f; property.stepIncrement = 1.0f / 12.0f; } return property; } return SynthProperty_undefined; } float FrequencyCurve_stairstep_getPropertyValueStateless(FrequencyCurve_stairstep * self, SynthPropertyIdentifier propertyIdentifier) { if (propertyIdentifier.propertyIndex == PROPERTY_INDEX_BASE_VALUE) { return self->baseValue; } propertyIdentifier.propertyIndex -= PROPERTY_INDEX_STEP_DURATION; unsigned int stepIndex = propertyIdentifier.propertyIndex / PROPERTY_STEP_STRIDE; if (stepIndex < self->stepCount) { if (propertyIdentifier.propertyIndex % PROPERTY_STEP_STRIDE == 0) { return self->steps[stepIndex].duration; } return self->steps[stepIndex].change; } return 0.0f; } void FrequencyCurve_stairstep_setPropertyValue(FrequencyCurve_stairstep * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct FrequencyCurve_stairstep_state * stateStruct = state; stateStruct->properties[propertyIdentifier.propertyIndex] = value; } float FrequencyCurve_stairstep_sample(FrequencyCurve_stairstep * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct FrequencyCurve_stairstep_state * stateStruct = state; float sample = stateStruct->properties[PROPERTY_INDEX_BASE_VALUE]; float changeBeforeRepeat = 0.0f; float changeAfterRepeat = 0.0f; float timeBeforeRepeat = 0.0f; float timeAfterRepeat = 0.0f; unsigned int repeatStartIndex = self->repeatStartIndex; if (repeatStartIndex >= self->stepCount) { repeatStartIndex = self->stepCount - 1; } unsigned int stepIndex = 0; for (; stepIndex < repeatStartIndex; stepIndex++) { changeBeforeRepeat += stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_CHANGE]; timeBeforeRepeat += stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION]; } for (; stepIndex < self->stepCount; stepIndex++) { changeAfterRepeat += stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_CHANGE]; timeAfterRepeat += stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION]; } if (timeBeforeRepeat + timeAfterRepeat <= 0.0f) { return sample; } for (stepIndex = 0; stepIndex < repeatStartIndex && phase > stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION]; stepIndex++) { sample += stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_CHANGE]; phase -= stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION]; } if (stepIndex >= repeatStartIndex && phase > 0.0f) { unsigned int repetitionIndex = floor(phase / timeAfterRepeat); if (repetitionIndex > self->repeatCount) { repetitionIndex = self->repeatCount; } sample += changeAfterRepeat * repetitionIndex; if (phase < timeAfterRepeat * (self->repeatCount + 1)) { phase = fmodf(phase, timeAfterRepeat); } } for (; stepIndex < self->stepCount && phase > stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION]; stepIndex++) { sample += stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_CHANGE]; phase -= stateStruct->properties[stepIndex * PROPERTY_STEP_STRIDE + PROPERTY_INDEX_STEP_DURATION]; } return sample; }