/* 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_sawtooth.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveSampler_sawtooth 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_sawtooth * WaveSampler_sawtooth_create(float incline, float amplitude) { stemobject_create_implementation(init, incline, amplitude) } bool WaveSampler_sawtooth_init(WaveSampler_sawtooth * self, float incline, float amplitude) { call_super(init, self); self->incline = incline; self->amplitude = amplitude; return true; } void WaveSampler_sawtooth_dispose(WaveSampler_sawtooth * self) { call_super_virtual(dispose, self); } WaveSampler_sawtooth * WaveSampler_sawtooth_copy(WaveSampler_sawtooth * self) { stemobject_copy_implementation(initCopy) } bool WaveSampler_sawtooth_isEqual(WaveSampler_sawtooth * self, compat_type(WaveSampler_sawtooth *) compareUntyped) { WaveSampler_sawtooth * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return compare->incline == self->incline && compare->amplitude == self->amplitude; } uint32_t WaveSampler_sawtooth_hash(WaveSampler_sawtooth * self, uint32_t initval) { initval = hashlittle(&self->incline, sizeof(self->incline), initval); initval = hashlittle(&self->amplitude, sizeof(self->amplitude), initval); return initval; } void WaveSampler_sawtooth_initCopy(WaveSampler_sawtooth * self, WaveSampler_sawtooth * original) { call_super(initCopy, self, (WaveSampler *) original); self->incline = original->incline; self->amplitude = original->amplitude; } #define PROPERTY_INDEX_INCLINE 0 #define PROPERTY_INDEX_AMPLITUDE 1 struct WaveSampler_sawtooth_state { float properties[2]; }; SamplerObject_state * WaveSampler_sawtooth_initState(WaveSampler_sawtooth * self) { struct WaveSampler_sawtooth_state * stateStruct = malloc(sizeof(*stateStruct)); stateStruct->properties[PROPERTY_INDEX_INCLINE] = self->incline; stateStruct->properties[PROPERTY_INDEX_AMPLITUDE] = self->amplitude; return stateStruct; } void WaveSampler_sawtooth_disposeState(WaveSampler_sawtooth * self, SamplerObject_state * state) { free(state); } static SynthProperty synthProperties[2]; static void initSynthProperties(void) { SYNTH_PROPERTY(0, incline, "Incline", 0.0f, 1.0f, 1.0f, 0.05f) SYNTH_PROPERTY(1, amplitude, "Amplitude", 0.0f, 1.0f, 1.0f, 0.05f); } SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() float WaveSampler_sawtooth_getPropertyValueStateless(WaveSampler_sawtooth * self, SynthPropertyIdentifier propertyIdentifier) { if (propertyIdentifier.propertyIndex == 0) { return self->incline; } if (propertyIdentifier.propertyIndex == 1) { return self->amplitude; } return 0.0f; } void WaveSampler_sawtooth_setPropertyValue(WaveSampler_sawtooth * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct WaveSampler_sawtooth_state * stateStruct = state; stateStruct->properties[propertyIdentifier.propertyIndex] = value; } float WaveSampler_sawtooth_sample(WaveSampler_sawtooth * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveSampler_sawtooth_state * stateStruct = state; float incline = stateStruct->properties[PROPERTY_INDEX_INCLINE]; float amplitude = stateStruct->properties[PROPERTY_INDEX_AMPLITUDE]; float modPhase = fmodf(phase + 1.0f, 1.0f); float lastPhase = phase - phaseDelta; float modLastPhase = fmodf(lastPhase + 1.0f, 1.0f); float sample = (modPhase / (incline + (incline == 0.0f)) * (modPhase < incline) + ((1.0f - modPhase) / (1.0f - incline + (incline == 1.0f))) * (modPhase >= incline)) * 2.0f - 1.0f; unsigned int slopeIndex = floorf(phase) * 2 + (modPhase > incline); unsigned int lastSlopeIndex = floorf(lastPhase) * 2 + (modLastPhase > incline); if (slopeIndex != lastSlopeIndex) { float lastSample = (modLastPhase / (incline + (incline == 0.0f)) * (modLastPhase < incline) + ((1.0f - modLastPhase) / (1.0f - incline + (incline == 1.0f))) * (modLastPhase >= incline)) * 2.0f - 1.0f; float transitionX = incline * (slopeIndex % 2); if (modPhase < modLastPhase) { modPhase += 1.0f; transitionX += 1.0f; } float ratio = (modPhase - transitionX) / (modPhase - modLastPhase); sample = lastSample + (sample - lastSample) * ratio; //sample = (sample + lastSample) * 0.5f; //sample = 2.0f; /* float modLastPhase = fmodf(lastPhase + 1.0f, 1.0f); float lastSample = (modLastPhase / (incline + (incline == 0.0f)) * (modLastPhase < incline) + ((1.0f - modLastPhase) / (1.0f - incline + (incline == 1.0f))) * (modLastPhase >= incline)) * 2.0f - 1.0f; float ratio = fminf(1.0f, (modPhase - incline * 0.5f) / phaseDelta); sample = sample * (1.0f - ratio) + lastSample * ratio; } else if (lastPhase + 1.0f < incline) { //float modLastPhase = fmodf(lastPhase + 1.0f, 1.0f); //float lastSample = (modLastPhase / (incline + (incline == 0.0f)) * (modLastPhase < incline) + // ((1.0f - modLastPhase) / (1.0f - incline + (incline == 1.0f))) * (modLastPhase >= incline)) * 2.0f - 1.0f; //float ratio = fminf(1.0f, (modPhase - (1.0f - incline) * 0.5f) / phaseDelta); //sample = sample * (1.0f - ratio) + lastSample * ratio; float ratio = modPhase / phaseDelta; sample = sample * ratio + (1.0f - ratio);*/ } /* phase *= self->audioData->sampleRate / 220.0f; int frameIndex1 = floorf(phase); float sample1 = samplePCM(self, frameIndex1); int frameIndex2 = ceilf(phase); float sample2 = samplePCM(self, frameIndex2); return sample1 + (sample2 - sample1) * (phase - frameIndex1); */ /*if (lastPhase < incline && lastPhase < 0.0f) { float ratio = modPhase / (modPhase - lastPhase); sample = sample * ratio + (1.0f - ratio); }*/ return sample * amplitude; }