/*
  Copyright (c) 2023 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_thinning.h"
#include "utilities/lookup3.h"
#include <math.h>

#define stemobject_implementation WaveModifier_thinning

v_begin();
v_func(dispose);
v_func(copy);
v_func(isEqual);
v_func(hash);
v_func(getOwnPropertyCount);
v_func(getOwnProperties);
v_func(sample);
v_end();

WaveModifier_thinning * WaveModifier_thinning_create(float amount, float center) {
	stemobject_create_implementation(init, amount, center)
}

bool WaveModifier_thinning_init(WaveModifier_thinning * self, float amount, float center) {
	call_super(init, self);
	self->amount = amount;
	self->center = center;
	return true;
}

void WaveModifier_thinning_dispose(WaveModifier_thinning * self) {
	call_super_virtual(dispose, self);
}

WaveModifier_thinning * WaveModifier_thinning_copy(WaveModifier_thinning * self) {
	stemobject_copy_implementation(initCopy)
}

bool WaveModifier_thinning_isEqual(WaveModifier_thinning * self, compat_type(WaveModifier_thinning *) compareUntyped) {
	WaveModifier_thinning * compare = compareUntyped;
	if (!call_super_virtual(isEqual, self, compare)) {
		return false;
	}
	return self->amount == compare->amount &&
	       self->center == compare->center;
}

uint32_t WaveModifier_thinning_hash(WaveModifier_thinning * self, uint32_t initval) {
	initval = hashlittle(&self->amount, sizeof(self->amount), initval);
	initval = hashlittle(&self->center, sizeof(self->center), initval);
	return initval;
}

void WaveModifier_thinning_initCopy(WaveModifier_thinning * self, WaveModifier_thinning * original) {
	call_super(initCopy, self, (WaveModifier *) original);
	self->amount = original->amount;
	self->center = original->center;
}

#define PROPERTY_INDEX_BLEND 0
#define PROPERTY_INDEX_AMOUNT 1
#define PROPERTY_INDEX_CENTER 2
#define PROPERTY_COUNT 3

struct WaveModifier_thinning_state {
	SamplerObject_state * upstreamState;
	float properties[PROPERTY_COUNT];
};

unsigned int WaveModifier_thinning_getOwnPropertyCount(WaveModifier_thinning * self) {
	return PROPERTY_COUNT;
}

SynthProperty * WaveModifier_thinning_getOwnProperties(WaveModifier_thinning * self) {
	static SynthProperty properties[PROPERTY_COUNT] = {
		{.propertyIndex = 0, .description = "Blend (thinning)", .offset = offsetof(WaveModifier_thinning, blend),  .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f},
		{.propertyIndex = 1, .description = "Thinning amount",  .offset = offsetof(WaveModifier_thinning, amount), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f},
		{.propertyIndex = 2, .description = "Thinning center",  .offset = offsetof(WaveModifier_thinning, center), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 0.5f, .stepIncrement = 0.05f}
	};
	return properties;
}

float WaveModifier_thinning_sample(WaveModifier_thinning * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) {
	struct WaveModifier_thinning_state * stateStruct = state;
	float blend = stateStruct->properties[PROPERTY_INDEX_BLEND];
	float amount = stateStruct->properties[PROPERTY_INDEX_AMOUNT];
	float center = stateStruct->properties[PROPERTY_INDEX_CENTER];
	float floorPhase = floorf(phase);
	float phaseFraction = phase - floorPhase;
	float flatMin = center * (1.0f - amount);
	float flatMax = flatMin + amount;
	float modifiedPhaseDelta = phaseDelta / (1.0f - amount);
	if (phaseFraction < flatMin) {
		phaseFraction /= 1.0f - amount;
	} else if (phaseFraction > flatMax) {
		phaseFraction = (phaseFraction - flatMax + flatMin) / (1.0f - amount);
	} else {
		phaseFraction = center;
		modifiedPhaseDelta = 0.0f;
	}
	float modifiedPhase = floorPhase + phaseFraction;
	float modifiedSample = call_virtual(sample, self->upstream, stateStruct->upstreamState, modifiedPhase, modifiedPhaseDelta, time, timeDelta);
	if (blend == 1.0f) {
		return modifiedSample;
	}
	float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta);
	return modifiedSample * blend + sample * (1.0f - blend);
}
