/* 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_layered.h" #include "utilities/IOUtilities.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveSampler_layered 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_layered * WaveSampler_layered_create(WaveSampler_layered_blendMode blendMode, unsigned int layerCount, struct WaveSampler_layered_layer * layers) { stemobject_create_implementation(init, blendMode, layerCount, layers) } bool WaveSampler_layered_init(WaveSampler_layered * self, WaveSampler_layered_blendMode blendMode, unsigned int layerCount, struct WaveSampler_layered_layer * layers) { call_super(init, self); self->blendMode = blendMode; self->layerCount = layerCount; self->layers = memdup(layers, layerCount * sizeof(*layers)); return true; } void WaveSampler_layered_dispose(WaveSampler_layered * self) { for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { call_virtual(dispose, self->layers[layerIndex].sampler); } free(self->layers); call_super_virtual(dispose, self); } WaveSampler_layered * WaveSampler_layered_copy(WaveSampler_layered * self) { stemobject_copy_implementation(initCopy) } bool WaveSampler_layered_isEqual(WaveSampler_layered * self, compat_type(WaveSampler_layered *) compareUntyped) { WaveSampler_layered * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare) || self->layerCount != compare->layerCount || self->blendMode != compare->blendMode) { return false; } for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { if (self->layers[layerIndex].blend != compare->layers[layerIndex].blend || self->layers[layerIndex].mute != compare->layers[layerIndex].mute || self->layers[layerIndex].solo != compare->layers[layerIndex].solo || self->layers[layerIndex].frequencyOffset != compare->layers[layerIndex].frequencyOffset || self->layers[layerIndex].phaseOffset != compare->layers[layerIndex].phaseOffset || !call_virtual(isEqual, self->layers[layerIndex].sampler, compare->layers[layerIndex].sampler)) { return false; } } return true; } uint32_t WaveSampler_layered_hash(WaveSampler_layered * self, uint32_t initval) { for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { initval = hashlittle(&self->layers[layerIndex].blend, sizeof(self->layers[layerIndex].blend), initval); uint32_t muteSoloSettings = self->layers[layerIndex].mute | (self->layers[layerIndex].solo << 1); initval = hashword(&muteSoloSettings, 1, initval); initval = hashlittle(&self->layers[layerIndex].frequencyOffset, sizeof(self->layers[layerIndex].frequencyOffset), initval); initval = hashlittle(&self->layers[layerIndex].phaseOffset, sizeof(self->layers[layerIndex].phaseOffset), initval); initval = call_virtual(hash, self->layers[layerIndex].sampler, initval); } return initval; } void WaveSampler_layered_initCopy(WaveSampler_layered * self, WaveSampler_layered * original) { call_super(initCopy, self, (WaveSampler *) original); self->blendMode = original->blendMode; self->layerCount = original->layerCount; self->layers = memdup(original->layers, original->layerCount * sizeof(*original->layers)); for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { self->layers[layerIndex].sampler = call_virtual(copy, original->layers[layerIndex].sampler); } } struct WaveSampler_layered_state { float blend; float frequencyOffset; float phaseOffset; SamplerObject_state * state; }; SamplerObject_state * WaveSampler_layered_initState(WaveSampler_layered * self) { if (self->layerCount == 0) { return NULL; } struct WaveSampler_layered_state * layerStates = malloc(sizeof(*layerStates) * self->layerCount); for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { layerStates[layerIndex].blend = self->layers[layerIndex].blend; layerStates[layerIndex].frequencyOffset = self->layers[layerIndex].frequencyOffset; layerStates[layerIndex].phaseOffset = self->layers[layerIndex].phaseOffset; layerStates[layerIndex].state = call_virtual(initState, self->layers[layerIndex].sampler); } return layerStates; } void WaveSampler_layered_disposeState(WaveSampler_layered * self, SamplerObject_state * state) { struct WaveSampler_layered_state * layerStates = state; for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { call_virtual(disposeState, self->layers[layerIndex].sampler, layerStates[layerIndex].state); } free(layerStates); } unsigned int WaveSampler_layered_getPropertyCount(WaveSampler_layered * self) { unsigned int propertyCount = 0; for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { propertyCount += 3 + call_virtual(getPropertyCount, self->layers[layerIndex].sampler); } return propertyCount; } SynthProperty WaveSampler_layered_getPropertyAtIndex(WaveSampler_layered * self, unsigned int index) { unsigned int layerPropertyIndex = index; for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { unsigned int propertyCount = call_virtual(getPropertyCount, self->layers[layerIndex].sampler); if (layerPropertyIndex < propertyCount + 3) { if (layerPropertyIndex == 0) { SynthProperty property; property.propertyIndex = index; snprintf_safe(property.description, sizeof(property.description), "Layer %u blend", layerIndex + 1); property.minValue = 0.0f; property.maxValue = 1.0f; property.defaultValue = 1.0f; property.stepIncrement = 0.05f; return property; } if (layerPropertyIndex == 1) { SynthProperty property; property.propertyIndex = index; snprintf_safe(property.description, sizeof(property.description), "Layer %u freq offset", layerIndex + 1); property.minValue = -5.0f; property.maxValue = 5.0f; property.defaultValue = 0.0f; property.stepIncrement = 1.0f / 12.0f; return property; } if (layerPropertyIndex == 2) { SynthProperty property; property.propertyIndex = index; snprintf_safe(property.description, sizeof(property.description), "Layer %u phase offset", layerIndex + 1); property.minValue = 0.0f; property.maxValue = 1.0f; property.defaultValue = 0.0f; property.stepIncrement = 0.05f; return property; } SynthProperty property = call_virtual(getPropertyAtIndex, self->layers[layerIndex].sampler, layerPropertyIndex - 3); char description[40]; strncpy_safe(description, property.description, sizeof(description)); snprintf_safe(property.description, sizeof(property.description), "Layer %u: %s", layerIndex + 1, description); property.propertyIndex = index; return property; } layerPropertyIndex -= propertyCount + 3; } return SynthProperty_undefined; } float WaveSampler_layered_getPropertyValueStateless(WaveSampler_layered * self, SynthPropertyIdentifier propertyIdentifier) { for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { unsigned int propertyCount = call_virtual(getPropertyCount, self->layers[layerIndex].sampler); if (propertyIdentifier.propertyIndex < propertyCount + 3) { if (propertyIdentifier.propertyIndex == 0) { return self->layers[layerIndex].blend; } if (propertyIdentifier.propertyIndex == 1) { return self->layers[layerIndex].frequencyOffset; } if (propertyIdentifier.propertyIndex == 2) { return self->layers[layerIndex].phaseOffset; } propertyIdentifier.propertyIndex -= 3; return call_virtual(getPropertyValueStateless, self->layers[layerIndex].sampler, propertyIdentifier); } propertyIdentifier.propertyIndex -= propertyCount + 3; } return 0.0f; } void WaveSampler_layered_setPropertyValue(WaveSampler_layered * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct WaveSampler_layered_state * layerStates = state; for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { unsigned int propertyCount = call_virtual(getPropertyCount, self->layers[layerIndex].sampler); if (propertyIdentifier.propertyIndex < propertyCount + 3) { if (propertyIdentifier.propertyIndex == 0) { layerStates[layerIndex].blend = value; } else if (propertyIdentifier.propertyIndex == 1) { layerStates[layerIndex].frequencyOffset = value; } else if (propertyIdentifier.propertyIndex == 2) { layerStates[layerIndex].phaseOffset = value; } else { propertyIdentifier.propertyIndex -= 3; call_virtual(setPropertyValue, self->layers[layerIndex].sampler, layerStates[layerIndex].state, propertyIdentifier, value); } break; } propertyIdentifier.propertyIndex -= propertyCount + 3; } } float WaveSampler_layered_sample(WaveSampler_layered * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveSampler_layered_state * layerStates = state; for (unsigned int layerIndex = 0; layerIndex < self->layerCount; layerIndex++) { if (self->layers[layerIndex].solo) { float layerPhaseMultiplier = powf(2, layerStates[layerIndex].frequencyOffset); return call_virtual(sample, self->layers[layerIndex].sampler, layerStates[layerIndex].state, phase * layerPhaseMultiplier + layerStates[layerIndex].phaseOffset, phaseDelta * layerPhaseMultiplier, time, timeDelta) * layerStates[layerIndex].blend; } } float blendTotal = layerStates[0].blend * !self->layers[0].mute; float phaseMultiplier = powf(2, layerStates[0].frequencyOffset); float sample = call_virtual(sample, self->layers[0].sampler, layerStates[0].state, phase * phaseMultiplier + layerStates[0].phaseOffset, phaseDelta * phaseMultiplier, time, timeDelta) * blendTotal; float lastSampleBlend = blendTotal; for (unsigned int layerIndex = 1; layerIndex < self->layerCount; layerIndex++) { if (self->layers[layerIndex].mute) { continue; } float layerBlend = layerStates[layerIndex].blend; float layerPhaseMultiplier = powf(2, layerStates[layerIndex].frequencyOffset); float layerSample = call_virtual(sample, self->layers[layerIndex].sampler, layerStates[layerIndex].state, phase * layerPhaseMultiplier + layerStates[layerIndex].phaseOffset, phaseDelta * layerPhaseMultiplier, time, timeDelta); switch (self->blendMode) { case WaveSampler_layered_average: case WaveSampler_layered_add: sample += layerSample * layerBlend; break; case WaveSampler_layered_multiply: sample = sample * (1.0f - layerBlend) + sample * layerSample * layerBlend + layerSample * layerBlend * (1.0f - lastSampleBlend); break; } // This is iffy; not 100% sure fmax is the correct operator here lastSampleBlend = fmaxf(lastSampleBlend, layerBlend); blendTotal += layerBlend; } if (self->blendMode == WaveSampler_layered_average && blendTotal != 0.0f) { sample /= blendTotal; } return sample; } void WaveSampler_layered_addLayer(WaveSampler_layered * self, struct WaveSampler_layered_layer layer) { self->layers = realloc(self->layers, (self->layerCount + 1) * sizeof(*self->layers)); self->layers[self->layerCount++] = layer; } void WaveSampler_layered_removeLayer(WaveSampler_layered * self, unsigned int layerIndex) { if (layerIndex < self->layerCount) { call_virtual(dispose, self->layers[layerIndex].sampler); self->layerCount--; for (; layerIndex < self->layerCount; layerIndex++) { self->layers[layerIndex] = self->layers[layerIndex + 1]; } } }