/* 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/WaveSampler_pulse.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveSampler_pulse 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(); WaveSampler_pulse * WaveSampler_pulse_create(float duty, float amplitude) { stemobject_create_implementation(init, duty, amplitude) } bool WaveSampler_pulse_init(WaveSampler_pulse * self, float duty, float amplitude) { call_super(init, self); self->duty = duty; self->amplitude = amplitude; return true; } void WaveSampler_pulse_dispose(WaveSampler_pulse * self) { call_super_virtual(dispose, self); } WaveSampler_pulse * WaveSampler_pulse_copy(WaveSampler_pulse * self) { stemobject_copy_implementation(initCopy) } bool WaveSampler_pulse_isEqual(WaveSampler_pulse * self, compat_type(WaveSampler_pulse *) compareUntyped) { WaveSampler_pulse * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return compare->duty == self->duty && compare->amplitude == self->amplitude; } uint32_t WaveSampler_pulse_hash(WaveSampler_pulse * self, uint32_t initval) { initval = hashlittle(&self->duty, sizeof(self->duty), initval); initval = hashlittle(&self->amplitude, sizeof(self->amplitude), initval); return initval; } void WaveSampler_pulse_initCopy(WaveSampler_pulse * self, WaveSampler_pulse * original) { call_super(initCopy, self, (WaveSampler *) original); self->duty = original->duty; self->amplitude = original->amplitude; } #define PROPERTY_INDEX_DUTY 0 #define PROPERTY_INDEX_AMPLITUDE 1 struct WaveSampler_pulse_state { float properties[2]; }; SamplerObject_state * WaveSampler_pulse_initState(WaveSampler_pulse * self) { struct WaveSampler_pulse_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->properties[PROPERTY_INDEX_DUTY] = self->duty; stateStruct->properties[PROPERTY_INDEX_AMPLITUDE] = self->amplitude; return stateStruct; } void WaveSampler_pulse_disposeState(WaveSampler_pulse * self, SamplerObject_state * state) { free(state); } static SynthProperty synthProperties[2]; static void initSynthProperties(void) { SYNTH_PROPERTY(0, duty, "Duty", 0.0f, 1.0f, 0.5f, 0.05f) SYNTH_PROPERTY(1, amplitude, "Amplitude", 0.0f, 1.0f, 0.5f, 0.05f) } SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() float WaveSampler_pulse_getPropertyValueStateless(WaveSampler_pulse * self, SynthPropertyIdentifier propertyIdentifier) { if (propertyIdentifier.propertyIndex == 0) { return self->duty; } if (propertyIdentifier.propertyIndex == 1) { return self->amplitude; } return 0.0f; } void WaveSampler_pulse_setPropertyValue(WaveSampler_pulse * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct WaveSampler_pulse_state * stateStruct = state; stateStruct->properties[propertyIdentifier.propertyIndex] = value; } float WaveSampler_pulse_sample(WaveSampler_pulse * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveSampler_pulse_state * stateStruct = state; float duty = stateStruct->properties[PROPERTY_INDEX_DUTY]; float amplitude = stateStruct->properties[PROPERTY_INDEX_AMPLITUDE]; float modPhase = fmodf(phase + 1.0f, 1.0f); float sample = (modPhase < duty) * 2.0f - 1.0f; float lastPhase = modPhase - phaseDelta; if (lastPhase < duty && modPhase >= duty) { sample = (duty - lastPhase) / (modPhase - lastPhase) * 2.0f - 1.0f; } else if (lastPhase < 0.0f && modPhase < duty) { sample = modPhase / (modPhase - lastPhase) * 2.0f - 1.0f; } return sample * amplitude; }