/* 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_chord.h" #include "utilities/IOUtilities.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveModifier_chord 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(); WaveModifier_chord * WaveModifier_chord_create(unsigned int noteCount, struct WaveModifier_chord_note * notes) { stemobject_create_implementation(init, noteCount, notes) } bool WaveModifier_chord_init(WaveModifier_chord * self, unsigned int noteCount, struct WaveModifier_chord_note * notes) { call_super(init, self); self->noteCount = noteCount; self->notes = memdup(notes, noteCount * sizeof(*self->notes)); return true; } void WaveModifier_chord_dispose(WaveModifier_chord * self) { free(self->notes); call_super_virtual(dispose, self); } WaveModifier_chord * WaveModifier_chord_copy(WaveModifier_chord * self) { stemobject_copy_implementation(initCopy) } bool WaveModifier_chord_isEqual(WaveModifier_chord * self, compat_type(WaveModifier_chord *) compareUntyped) { WaveModifier_chord * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare) || self->noteCount != compare->noteCount || memcmp(self->notes, compare->notes, self->noteCount * sizeof(*self->notes))) { return false; } return true; } uint32_t WaveModifier_chord_hash(WaveModifier_chord * self, uint32_t initval) { for (unsigned int noteIndex = 0; noteIndex < self->noteCount; noteIndex++) { initval = hashlittle(&self->notes[noteIndex].note, sizeof(self->notes[noteIndex].note), initval); initval = hashlittle(&self->notes[noteIndex].amplitude, sizeof(self->notes[noteIndex].amplitude), initval); } return initval; } void WaveModifier_chord_initCopy(WaveModifier_chord * self, WaveModifier_chord * original) { call_super(initCopy, self, (WaveModifier *) original); self->noteCount = original->noteCount; self->notes = memdup(original->notes, original->noteCount * sizeof(*original->notes)); } struct WaveModifier_chord_state { float blend; SamplerObject_state * upstreamState; unsigned int upstreamPropertyCount; struct { float amplitude; float octave; SamplerObject_state * state; } * notes; }; SamplerObject_state * WaveModifier_chord_initState(WaveModifier_chord * self) { struct WaveModifier_chord_state * stateStruct = malloc(sizeof(*stateStruct) + sizeof(*stateStruct->notes) * self->noteCount); stateStruct->blend = self->blend; stateStruct->upstreamState = call_virtual(initState, self->upstream); stateStruct->upstreamPropertyCount = call_virtual(getPropertyCount, self->upstream); stateStruct->notes = (void *) (stateStruct + 1); for (unsigned int noteIndex = 0; noteIndex < self->noteCount; noteIndex++) { stateStruct->notes[noteIndex].amplitude = self->notes[noteIndex].amplitude; stateStruct->notes[noteIndex].octave = self->notes[noteIndex].note / 12.0f; stateStruct->notes[noteIndex].state = call_virtual(initState, self->upstream); } return stateStruct; } void WaveModifier_chord_disposeState(WaveModifier_chord * self, SamplerObject_state * state) { struct WaveModifier_chord_state * stateStruct = state; call_virtual(disposeState, self->upstream, stateStruct->upstreamState); for (unsigned int noteIndex = 0; noteIndex < self->noteCount; noteIndex++) { call_virtual(disposeState, self->upstream, stateStruct->notes[noteIndex].state); } free(stateStruct); } unsigned int WaveModifier_chord_getPropertyCount(WaveModifier_chord * self) { return 1 + self->noteCount * (2 + call_virtual(getPropertyCount, self->upstream)); } SynthProperty WaveModifier_chord_getPropertyAtIndex(WaveModifier_chord * self, unsigned int index) { SynthProperty property; property.propertyIndex = index; if (index == 0) { return (SynthProperty) {.propertyIndex = 0, .description = "Blend (chord)", .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}; } index--; unsigned int upstreamPropertyCount = call_virtual(getPropertyCount, self->upstream); if (index < self->noteCount * (2 + upstreamPropertyCount)) { unsigned int noteIndex = index / (2 + upstreamPropertyCount); index %= (2 + upstreamPropertyCount); if (index == 0) { snprintf_safe(property.description, sizeof(property.description), "Note %u amplitude", noteIndex + 1); property.minValue = 0.0f; property.maxValue = 1.0f; property.defaultValue = 1.0f; property.stepIncrement = 0.05f; } else if (index == 1) { snprintf_safe(property.description, sizeof(property.description), "Note %u frequency", noteIndex + 1); property.minValue = 0.0f; property.maxValue = 9.0f; property.defaultValue = 4.0f; property.stepIncrement = 1.0f / 12.0f; } else { SynthProperty upstreamProperty = call_virtual(getPropertyAtIndex, self->upstream, index - 2); snprintf_safe(property.description, sizeof(property.description), "Note %u: %s", noteIndex + 1, upstreamProperty.description); property.minValue = upstreamProperty.minValue; property.maxValue = upstreamProperty.maxValue; property.defaultValue = upstreamProperty.defaultValue; property.stepIncrement = upstreamProperty.stepIncrement; } return property; } return SynthProperty_undefined; } float WaveModifier_chord_getPropertyValueStateless(WaveModifier_chord * self, SynthPropertyIdentifier propertyIdentifier) { if (propertyIdentifier.propertyIndex == 0) { return self->blend; } propertyIdentifier.propertyIndex--; unsigned int upstreamPropertyCount = call_virtual(getPropertyCount, self->upstream); unsigned int noteIndex = propertyIdentifier.propertyIndex / (2 + upstreamPropertyCount); propertyIdentifier.propertyIndex %= (2 + upstreamPropertyCount); if (propertyIdentifier.propertyIndex == 0) { return self->notes[noteIndex].amplitude; } if (propertyIdentifier.propertyIndex == 1) { return self->notes[noteIndex].note / 12.0f; } propertyIdentifier.propertyIndex -= 2; return call_virtual(getPropertyValueStateless, self->upstream, propertyIdentifier); } void WaveModifier_chord_setPropertyValue(WaveModifier_chord * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) { struct WaveModifier_chord_state * stateStruct = state; if (propertyIdentifier.propertyIndex == 0) { stateStruct->blend = value; } else { propertyIdentifier.propertyIndex--; unsigned int upstreamPropertyCount = stateStruct->upstreamPropertyCount; unsigned int noteIndex = propertyIdentifier.propertyIndex / (2 + upstreamPropertyCount); propertyIdentifier.propertyIndex %= (2 + upstreamPropertyCount); if (propertyIdentifier.propertyIndex == 0) { stateStruct->notes[noteIndex].amplitude = value; } else if (propertyIdentifier.propertyIndex == 1) { stateStruct->notes[noteIndex].octave = value; } else { propertyIdentifier.propertyIndex -= 2; call_virtual(setPropertyValue, self->upstream, stateStruct->notes[noteIndex].state, propertyIdentifier, value); } } } float WaveModifier_chord_sample(WaveModifier_chord * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { struct WaveModifier_chord_state * stateStruct = state; float sample = call_virtual(sample, self->upstream, stateStruct->upstreamState, phase, phaseDelta, time, timeDelta); float modifiedSample = 0.0f; for (unsigned int noteIndex = 0; noteIndex < self->noteCount; noteIndex++) { float multiplier = powf(2, stateStruct->notes[noteIndex].octave - 4); modifiedSample += call_virtual(sample, self->upstream, stateStruct->notes[noteIndex].state, phase * multiplier, phaseDelta * multiplier, time, timeDelta) * stateStruct->notes[noteIndex].amplitude; } float blend = stateStruct->blend; return modifiedSample * blend + sample * (1.0f - blend); }