/* 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_phaser.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_phaser 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(getOwnPropertyCount); stemobject_vtable_entry(getOwnProperties); stemobject_vtable_entry(sample); stemobject_vtable_end(); #define SAMPLES_PER_TIME_UNIT_44100 180 WaveModifier_phaser * WaveModifier_phaser_create(float timeOffset, bool smooth, unsigned int bufferSize) { stemobject_create_implementation(init, timeOffset, smooth, bufferSize) } bool WaveModifier_phaser_init(WaveModifier_phaser * self, float timeOffset, bool smooth, unsigned int bufferSize) { call_super(init, self); self->timeOffset = timeOffset; self->smooth = smooth; self->bufferSize = bufferSize; return true; } void WaveModifier_phaser_dispose(WaveModifier_phaser * self) { call_super_virtual(dispose, self); } WaveModifier_phaser * WaveModifier_phaser_copy(WaveModifier_phaser * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_phaser_isEqual(WaveModifier_phaser * self, compat_type(WaveModifier_phaser *) compareUntyped) { WaveModifier_phaser * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return self->timeOffset == compare->timeOffset && self->smooth == compare->smooth && self->bufferSize == compare->bufferSize; } uint32_t WaveModifier_phaser_hash(WaveModifier_phaser * self, uint32_t initval) { initval = hashlittle(&self->timeOffset, sizeof(self->timeOffset), initval); initval = hashlittle(&self->smooth, sizeof(self->smooth), initval); initval = hashlittle(&self->bufferSize, sizeof(self->bufferSize), initval); return initval; } void WaveModifier_phaser_initCopy(WaveModifier_phaser * self, WaveModifier_phaser * original) { call_super(initCopy, self, (WaveModifier *) original); self->timeOffset = original->timeOffset; self->smooth = original->smooth; self->bufferSize = original->bufferSize; } #define PROPERTY_INDEX_BLEND 0 #define PROPERTY_INDEX_TIME_OFFSET 1 #define PROPERTY_COUNT 2 struct WaveModifier_phaser_state { SamplerObject_state * upstreamState; float properties[PROPERTY_COUNT]; float * sampleBuffer; unsigned int sampleBufferIndex; }; SamplerObject_state * WaveModifier_phaser_initState(WaveModifier_phaser * self) { struct WaveModifier_phaser_state * stateStruct = malloc(sizeof(*stateStruct) + sizeof(stateStruct->sampleBuffer[0]) * self->bufferSize); stateStruct->properties[PROPERTY_INDEX_BLEND] = self->blend; stateStruct->properties[PROPERTY_INDEX_TIME_OFFSET] = self->timeOffset; stateStruct->upstreamState = call_virtual(initState, self->upstream); stateStruct->sampleBuffer = (void *) stateStruct + sizeof(*stateStruct); memset(stateStruct->sampleBuffer, 0, sizeof(stateStruct->sampleBuffer[0]) * self->bufferSize); stateStruct->sampleBufferIndex = 0; return stateStruct; } unsigned int WaveModifier_phaser_getOwnPropertyCount(WaveModifier_phaser * self) { return PROPERTY_COUNT; } SynthProperty * WaveModifier_phaser_getOwnProperties(WaveModifier_phaser * self) { static SynthProperty properties[PROPERTY_COUNT] = { {.propertyIndex = 0, .description = "Blend (phaser)", .offset = offsetof(WaveModifier_phaser, blend), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}, {.propertyIndex = 1, .description = "Time offset", .offset = offsetof(WaveModifier_phaser, timeOffset), .minValue = -1.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f} }; return properties; } float WaveModifier_phaser_sample(WaveModifier_phaser * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_phaser_state * stateStruct = state; float blend = stateStruct->properties[PROPERTY_INDEX_BLEND]; float timeOffset = stateStruct->properties[PROPERTY_INDEX_TIME_OFFSET]; unsigned int bufferSize = self->bufferSize; float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); float modifiedSample = sample; modifiedSample *= 0.5f; stateStruct->sampleBuffer[stateStruct->sampleBufferIndex % bufferSize] = modifiedSample; float phaserTime = fabsf(timeOffset) * SAMPLES_PER_TIME_UNIT_44100 / timeDelta / 44100; float phasedSample = stateStruct->sampleBuffer[(stateStruct->sampleBufferIndex - ((int) phaserTime) % bufferSize + bufferSize) % bufferSize]; if (self->smooth) { float phasedSample2 = stateStruct->sampleBuffer[(stateStruct->sampleBufferIndex - ((int) phaserTime - 1) % bufferSize + bufferSize) % bufferSize]; float weight = fmodf(phaserTime, 1.0f); modifiedSample += phasedSample2 * weight + phasedSample * (1.0f - weight); } else { modifiedSample += phasedSample; } stateStruct->sampleBufferIndex++; return modifiedSample * blend + sample * (1.0f - blend); }