/* 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/WaveModifier_distortion.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_distortion 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_distortion * WaveModifier_distortion_create(float amplification, float clipRange) { stemobject_create_implementation(init, amplification, clipRange) } bool WaveModifier_distortion_init(WaveModifier_distortion * self, float amplification, float clipRange) { call_super(init, self); self->amplification = amplification; self->clipRange = clipRange; return true; } void WaveModifier_distortion_dispose(WaveModifier_distortion * self) { call_super_virtual(dispose, self); } WaveModifier_distortion * WaveModifier_distortion_copy(WaveModifier_distortion * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_distortion_isEqual(WaveModifier_distortion * self, compat_type(WaveModifier_distortion *) compareUntyped) { WaveModifier_distortion * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return self->amplification == compare->amplification && self->clipRange == compare->clipRange; } uint32_t WaveModifier_distortion_hash(WaveModifier_distortion * self, uint32_t initval) { initval = hashlittle(&self->amplification, sizeof(self->amplification), initval); initval = hashlittle(&self->clipRange, sizeof(self->clipRange), initval); return initval; } void WaveModifier_distortion_initCopy(WaveModifier_distortion * self, WaveModifier_distortion * original) { call_super(initCopy, self, (WaveModifier *) original); self->amplification = original->amplification; self->clipRange = original->clipRange; } #define PROPERTY_INDEX_BLEND 0 #define PROPERTY_INDEX_AMPLIFICATION 1 #define PROPERTY_INDEX_CLIP_RANGE 2 #define PROPERTY_COUNT 3 struct WaveModifier_distortion_state { SamplerObject_state * upstreamState; float properties[PROPERTY_COUNT]; }; unsigned int WaveModifier_distortion_getOwnPropertyCount(WaveModifier_distortion * self) { return PROPERTY_COUNT; } SynthProperty * WaveModifier_distortion_getOwnProperties(WaveModifier_distortion * self) { static SynthProperty properties[PROPERTY_COUNT] = { {.propertyIndex = 0, .description = "Blend (distortion)", .offset = offsetof(WaveModifier_distortion, blend), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}, {.propertyIndex = 1, .description = "Amplification", .offset = offsetof(WaveModifier_distortion, amplification), .minValue = 0.0f, .maxValue = 4.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f}, {.propertyIndex = 2, .description = "Clip range", .offset = offsetof(WaveModifier_distortion, clipRange), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f} }; return properties; } float WaveModifier_distortion_sample(WaveModifier_distortion * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_distortion_state * stateStruct = state; float amplification = stateStruct->properties[PROPERTY_INDEX_AMPLIFICATION]; float clipRange = stateStruct->properties[PROPERTY_INDEX_CLIP_RANGE]; float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); float modifiedSample = fminf(clipRange, fmaxf(-clipRange, sample * (amplification + 1.0f))); float blend = stateStruct->properties[PROPERTY_INDEX_BLEND]; return modifiedSample * blend + sample * (1.0f - blend); }