/* 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_spline.h" #include "gamemath/BezierCurve.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveSampler_spline stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(copy); stemobject_vtable_entry(isEqual); stemobject_vtable_entry(hash); stemobject_vtable_entry(sample); stemobject_vtable_end(); WaveSampler_spline * WaveSampler_spline_create(SplineInfo spline) { stemobject_create_implementation(init, spline) } bool WaveSampler_spline_init(WaveSampler_spline * self, SplineInfo spline) { call_super(init, self); self->spline = spline; return true; } void WaveSampler_spline_dispose(WaveSampler_spline * self) { disposeSplineInfo(&self->spline); call_super_virtual(dispose, self); } WaveSampler_spline * WaveSampler_spline_copy(WaveSampler_spline * self) { stemobject_copy_implementation(initCopy) } bool WaveSampler_spline_isEqual(WaveSampler_spline * self, compat_type(WaveSampler_spline *) compareUntyped) { WaveSampler_spline * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return isSplineInfoEqual(&compare->spline, &self->spline); } uint32_t WaveSampler_spline_hash(WaveSampler_spline * self, uint32_t initval) { initval = hashSplineInfo(&self->spline, initval); return initval; } void WaveSampler_spline_initCopy(WaveSampler_spline * self, WaveSampler_spline * original) { call_super(initCopy, self, (WaveSampler *) original); self->spline = copySplineInfo(&original->spline); } float sampleSplineAtIndex(SplineInfo * spline, unsigned int index, float x, float * outXBefore) { if (x < spline->points[0].position.x) { if (outXBefore != NULL) { *outXBefore = 0.0f; } return spline->points[0].position.y; } if (index + 1 >= spline->pointCount) { if (outXBefore != NULL) { *outXBefore = spline->points[spline->pointCount - 1].position.x; } return spline->points[spline->pointCount - 1].position.y; } if (outXBefore != NULL) { *outXBefore = spline->points[index].position.x; } return BezierCurve_sampleYAtX(spline->points[index].position, spline->points[index].trailingControlPoint, spline->points[index + 1].leadingControlPoint, spline->points[index + 1].position, x, spline->sampleIterations); } float WaveSampler_spline_sample(WaveSampler_spline * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { float modPhase = fmodf(phase + 1.0f, 1.0f); float lastModPhase = fmodf(modPhase - phaseDelta + 1.0f, 1.0f); unsigned int pointIndex, lastPointIndex; for (pointIndex = 0; pointIndex < self->spline.pointCount; pointIndex++) { if (self->spline.points[pointIndex].position.x > modPhase) { pointIndex--; break; } } for (lastPointIndex = 0; lastPointIndex < self->spline.pointCount; lastPointIndex++) { if (self->spline.points[lastPointIndex].position.x > lastModPhase) { lastPointIndex--; break; } } float xBeforeSample; float sample = sampleSplineAtIndex(&self->spline, pointIndex, modPhase, &xBeforeSample); if (pointIndex == lastPointIndex) { return sample; } float lastSample = sampleSplineAtIndex(&self->spline, lastPointIndex, lastModPhase, NULL); if (modPhase < lastModPhase) { modPhase += 1.0f; xBeforeSample += 1.0f; } float ratio = (modPhase - xBeforeSample) / (modPhase - lastModPhase); return lastSample + (sample - lastSample) * ratio; }