/* 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/AmplitudeEnvelope_linearADSR.h" #include "utilities/lookup3.h" #define stemobject_implementation AmplitudeEnvelope_linearADSR 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(); AmplitudeEnvelope_linearADSR * AmplitudeEnvelope_linearADSR_create(float peakAmplitude, float sustainAmplitude, float punchAmplitude, float attackTime, float decayTime, float sustainTime, float releaseTime) { stemobject_create_implementation(init, peakAmplitude, sustainAmplitude, punchAmplitude, attackTime, decayTime, sustainTime, releaseTime) } bool AmplitudeEnvelope_linearADSR_init(AmplitudeEnvelope_linearADSR * self, float peakAmplitude, float sustainAmplitude, float punchAmplitude, float attackTime, float decayTime, float sustainTime, float releaseTime) { call_super(init, self, peakAmplitude, 1.0f); self->sustainAmplitude = sustainAmplitude; self->punchAmplitude = punchAmplitude; self->attackTime = attackTime; self->decayTime = decayTime; self->sustainTime = sustainTime; self->releaseTime = releaseTime; return true; } void AmplitudeEnvelope_linearADSR_dispose(AmplitudeEnvelope_linearADSR * self) { call_super_virtual(dispose, self); } AmplitudeEnvelope_linearADSR * AmplitudeEnvelope_linearADSR_copy(AmplitudeEnvelope_linearADSR * self) { stemobject_copy_implementation(initCopy) } bool AmplitudeEnvelope_linearADSR_isEqual(AmplitudeEnvelope_linearADSR * self, compat_type(AmplitudeEnvelope_linearADSR *) compareUntyped) { AmplitudeEnvelope_linearADSR * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return compare->baseAmplitude == self->baseAmplitude && compare->sustainAmplitude == self->sustainAmplitude && compare->punchAmplitude == self->punchAmplitude && compare->attackTime == self->attackTime && compare->decayTime == self->decayTime && compare->sustainTime == self->sustainTime && compare->releaseTime == self->releaseTime; } uint32_t AmplitudeEnvelope_linearADSR_hash(AmplitudeEnvelope_linearADSR * self, uint32_t initval) { initval = hashlittle(&self->baseAmplitude, sizeof(self->baseAmplitude), initval); initval = hashlittle(&self->sustainAmplitude, sizeof(self->sustainAmplitude), initval); initval = hashlittle(&self->punchAmplitude, sizeof(self->punchAmplitude), initval); initval = hashlittle(&self->attackTime, sizeof(self->attackTime), initval); initval = hashlittle(&self->decayTime, sizeof(self->decayTime), initval); initval = hashlittle(&self->sustainTime, sizeof(self->sustainTime), initval); initval = hashlittle(&self->releaseTime, sizeof(self->releaseTime), initval); return initval; } void AmplitudeEnvelope_linearADSR_initCopy(AmplitudeEnvelope_linearADSR * self, AmplitudeEnvelope_linearADSR * original) { call_super(initCopy, self, (AmplitudeEnvelope *) original); self->baseAmplitude = original->baseAmplitude; self->sustainAmplitude = original->sustainAmplitude; self->punchAmplitude = original->punchAmplitude; self->attackTime = original->attackTime; self->decayTime = original->decayTime; self->sustainTime = original->sustainTime; self->releaseTime = original->releaseTime; } #define PROPERTY_INDEX_PEAK_AMPLITUDE 0 #define PROPERTY_INDEX_SUSTAIN_AMPLITUDE 1 #define PROPERTY_INDEX_PUNCH_AMPLITUDE 2 struct AmplitudeEnvelope_linearADSR_state { float properties[3]; }; SamplerObject_state * AmplitudeEnvelope_linearADSR_initState(AmplitudeEnvelope_linearADSR * self) { struct AmplitudeEnvelope_linearADSR_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->properties[PROPERTY_INDEX_PEAK_AMPLITUDE] = self->baseAmplitude; stateStruct->properties[PROPERTY_INDEX_SUSTAIN_AMPLITUDE] = self->sustainAmplitude; stateStruct->properties[PROPERTY_INDEX_PUNCH_AMPLITUDE] = self->punchAmplitude; return stateStruct; } void AmplitudeEnvelope_linearADSR_disposeState(AmplitudeEnvelope_linearADSR * self, SamplerObject_state * state) { free(state); } float AmplitudeEnvelope_linearADSR_getLength(AmplitudeEnvelope_linearADSR * self) { return self->attackTime + self->decayTime + self->sustainTime + self->releaseTime; } static SynthProperty synthProperties[5]; static void initSynthProperties(void) { SYNTH_PROPERTY(PROPERTY_INDEX_PEAK_AMPLITUDE, baseAmplitude, "Peak amp", 0.0f, 1.0f, 0.625f, 0.05f) SYNTH_PROPERTY(PROPERTY_INDEX_SUSTAIN_AMPLITUDE, sustainAmplitude, "Sustain amp", 0.0f, 1.0f, 0.25f, 0.05f) SYNTH_PROPERTY(PROPERTY_INDEX_PUNCH_AMPLITUDE, punchAmplitude, "Punch", 0.0f, 1.0f, 0.0f, 0.05f) } SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() float AmplitudeEnvelope_linearADSR_getPropertyValueStateless(AmplitudeEnvelope_linearADSR * self, SynthPropertyIdentifier propertyIdentifier) { switch (propertyIdentifier.propertyIndex) { case PROPERTY_INDEX_PEAK_AMPLITUDE: return self->baseAmplitude; case PROPERTY_INDEX_SUSTAIN_AMPLITUDE: return self->sustainAmplitude; case PROPERTY_INDEX_PUNCH_AMPLITUDE: return self->punchAmplitude; } return 0.0f; } void AmplitudeEnvelope_linearADSR_setPropertyValue(AmplitudeEnvelope_linearADSR * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct AmplitudeEnvelope_linearADSR_state * stateStruct = state; stateStruct->properties[propertyIdentifier.propertyIndex] = value; } float AmplitudeEnvelope_linearADSR_sample(AmplitudeEnvelope_linearADSR * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct AmplitudeEnvelope_linearADSR_state * stateStruct = state; float peakAmplitude = stateStruct->properties[PROPERTY_INDEX_PEAK_AMPLITUDE]; float sustainAmplitude = stateStruct->properties[PROPERTY_INDEX_SUSTAIN_AMPLITUDE]; float punchAmplitude = sustainAmplitude * stateStruct->properties[PROPERTY_INDEX_PUNCH_AMPLITUDE] * 2.0f; float attackTime = self->attackTime; float decayTime = self->decayTime; float sustainTime = self->sustainTime; float releaseTime = self->releaseTime; return peakAmplitude * phase / (attackTime + (attackTime == 0.0f)) * (phase < attackTime) + (punchAmplitude * (1.0f - (phase - attackTime) / (decayTime + sustainTime + (decayTime + sustainTime == 0.0f)))) * (phase >= attackTime && phase < attackTime + decayTime + sustainTime) + (peakAmplitude - (peakAmplitude - sustainAmplitude) * (phase - attackTime) / (decayTime + (decayTime == 0.0f))) * (phase >= attackTime && phase < attackTime + decayTime) + sustainAmplitude * (phase >= attackTime + decayTime && phase < attackTime + decayTime + sustainTime) + sustainAmplitude * (1.0f - (phase - attackTime - decayTime - sustainTime) / (releaseTime + (releaseTime == 0.0f))) * (phase >= attackTime + decayTime + sustainTime && phase <= attackTime + decayTime + sustainTime + releaseTime); }