/* 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 */ #ifndef __SamplerObject_H__ #define __SamplerObject_H__ #ifdef __cplusplus extern "C" { #endif typedef struct SamplerObject SamplerObject; #define SamplerObject_superclass StemObject #include "stemobject/StemObject.h" #include "utilities/IOUtilities.h" #include #include #include typedef void SamplerObject_state; typedef enum SynthPropertyRootType { SYNTH_PROPERTY_ROOT_UNDEFINED, SYNTH_PROPERTY_ROOT_AMPLITUDE_ENVELOPE, SYNTH_PROPERTY_ROOT_FREQUENCY_CURVE, SYNTH_PROPERTY_ROOT_WAVE_SAMPLER, SYNTH_PROPERTY_ROOT_SIGNAL_FILTER } SynthPropertyRootType; typedef struct SynthProperty { unsigned int propertyIndex; char description[40]; float minValue; float maxValue; float defaultValue; float stepIncrement; size_t offset; } SynthProperty; #define SynthProperty_undefined ((SynthProperty) {.propertyIndex = 0, .description[0] = 0, .minValue = -1.0f, .maxValue = 1.0f, .defaultValue = 0.0f, .stepIncrement = 0.05f, .offset = 0}) typedef struct SynthPropertyIdentifier { SynthPropertyRootType rootType; unsigned int objectIndex; unsigned int propertyIndex; } SynthPropertyIdentifier; #define SynthPropertyIdentifier_undefined ((SynthPropertyIdentifier) {SYNTH_PROPERTY_ROOT_UNDEFINED, 0, 0}) #define SYNTH_PROPERTY_FUNCTION_IMPLEMENTATIONS() \ unsigned int stemobject_cat(stemobject_implementation, getPropertyCount)(stemobject_implementation * self) { \ return sizeof(synthProperties) / sizeof(synthProperties[0]); \ } \ SynthProperty stemobject_cat(stemobject_implementation, getPropertyAtIndex)(stemobject_implementation * self, unsigned int index) { \ static bool initialized = false; \ if (!initialized) { \ initSynthProperties(); \ initialized = true; \ } \ SynthProperty property = SynthProperty_undefined; \ if (index < sizeof(synthProperties) / sizeof(synthProperties[0])) { \ property = synthProperties[index]; \ } \ return property; \ } #define SYNTH_PROPERTY(property_index, struct_field, human_readable_name, min_value, max_value, default_value, step_increment) \ synthProperties[property_index].propertyIndex = property_index; \ strncpy_safe(synthProperties[property_index].description, human_readable_name, sizeof(synthProperties[property_index].description)); \ synthProperties[property_index].minValue = min_value; \ synthProperties[property_index].maxValue = max_value; \ synthProperties[property_index].defaultValue = default_value; \ synthProperties[property_index].stepIncrement = step_increment; \ synthProperties[property_index].offset = 0; #define SamplerObject_ivars \ StemObject_ivars \ \ unsigned int runtimeUniqueID; #define SamplerObject_vtable(self_type) \ StemObject_vtable(self_type) \ \ self_type * (* copy)(self_type * self); \ bool (* isEqual)(self_type * self, compat_type(self_type *) compare); \ uint32_t (* hash)(self_type * self, uint32_t initval); \ SamplerObject_state * (* initState)(self_type * self); \ void (* disposeState)(self_type * self, SamplerObject_state * state); \ unsigned int (* getPropertyCount)(self_type * self); \ SynthProperty (* getPropertyAtIndex)(self_type * self, unsigned int index); \ float (* getPropertyValueStateless)(self_type * self, SynthPropertyIdentifier propertyIdentifier); \ void (* setPropertyValue)(self_type * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value); \ float (* getLength)(self_type * self); \ float (* sample)(self_type * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta); stemobject_declare(SamplerObject) bool SamplerObject_init(SamplerObject * self); void SamplerObject_dispose(SamplerObject * self); SamplerObject * SamplerObject_copy(SamplerObject * self); void SamplerObject_initCopy(SamplerObject * self, SamplerObject * original); bool SamplerObject_isEqual(SamplerObject * self, compat_type(SamplerObject *) compare); uint32_t SamplerObject_hash(SamplerObject * self, uint32_t initval); SamplerObject_state * SamplerObject_initState(SamplerObject * self); void SamplerObject_disposeState(SamplerObject * self, SamplerObject_state * state); unsigned int SamplerObject_getPropertyCount(SamplerObject * self); SynthProperty SamplerObject_getPropertyAtIndex(SamplerObject * self, unsigned int index); float SamplerObject_getPropertyValueStateless(SamplerObject * self, SynthPropertyIdentifier propertyIdentifier); void SamplerObject_setPropertyValue(SamplerObject * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value); float SamplerObject_getLength(SamplerObject * self); float SamplerObject_sample(SamplerObject * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta); #ifdef __cplusplus } #endif #endif