diff options
Diffstat (limited to 'src/triangle.c')
-rw-r--r-- | src/triangle.c | 231 |
1 files changed, 160 insertions, 71 deletions
diff --git a/src/triangle.c b/src/triangle.c index 8cb32a6..0a06f6c 100644 --- a/src/triangle.c +++ b/src/triangle.c @@ -18,9 +18,7 @@ */ #include <stdlib.h> -#include "lv2/lv2plug.in/ns/ext/morph/morph.h" #include "lv2/lv2plug.in/ns/lv2core/lv2.h" -#include "uris.h" #include "wavedata.h" #define TRIANGLE_FREQUENCY 0 @@ -34,10 +32,7 @@ typedef struct { float phase; float min_slope; float max_slope; - uint32_t frequency_is_cv; - uint32_t slope_is_cv; Wavedata wdat; - URIs uris; } Triangle; static void @@ -60,33 +55,6 @@ connect_port(LV2_Handle instance, } } -static LV2_Morph_Status -morph_port(LV2_Handle instance, - uint32_t port, - LV2_URID type, - const LV2_Morph_Property*const* properties) -{ - Triangle* plugin = (Triangle*)instance; - - if (type != plugin->uris.lv2_ControlPort && - type != plugin->uris.lv2_CVPort) { - return LV2_MORPH_ERR_BAD_TYPE; - } - - switch (port) { - case TRIANGLE_FREQUENCY: - plugin->frequency_is_cv = (type == plugin->uris.lv2_CVPort); - break; - case TRIANGLE_SLOPE: - plugin->slope_is_cv = (type == plugin->uris.lv2_CVPort); - break; - default: - return LV2_MORPH_ERR_BAD_PORT; - } - - return LV2_MORPH_SUCCESS; -} - static LV2_Handle instantiate(const LV2_Descriptor* descriptor, double sample_rate, @@ -104,12 +72,6 @@ instantiate(const LV2_Descriptor* descriptor, plugin->min_slope = 2.0f / plugin->wdat.sample_rate; plugin->max_slope = 1.0f - plugin->min_slope; - plugin->frequency_is_cv = 0; - plugin->slope_is_cv = 0; - - map_uris(&plugin->uris, features); - wavedata_get_table(&plugin->wdat, 440.0); - return (LV2_Handle)plugin; } @@ -131,46 +93,89 @@ activate(LV2_Handle instance) } static void -run(LV2_Handle instance, - uint32_t sample_count) +runTriangle_fasa_oa(LV2_Handle instance, + uint32_t sample_count) { Triangle* plugin = (Triangle*)instance; - /* Frequency (array of float of length 1 or sample_count) */ + /* Frequency (array of float of length sample_count) */ const float* frequency = plugin->frequency; - /* Slope (array of float of length 1 or sample_count) */ + /* Slope (array of float of length sample_count) */ const float* slope = plugin->slope; /* Output (pointer to float value) */ float* output = plugin->output; /* Instance data */ - Wavedata* wdat = &plugin->wdat; - float phase = plugin->phase; - const float min_slope = plugin->min_slope; - const float max_slope = plugin->max_slope; + Wavedata* wdat = &plugin->wdat; + float phase = plugin->phase; + float min_slope = plugin->min_slope; + float max_slope = plugin->max_slope; - float last_slope = slope[0]; - float slp = f_clip(last_slope, min_slope, max_slope); - float phase_shift = slp * wdat->sample_rate; - float scale = 1.0f / (8.0f * (slp - (slp * slp))); + float freq; + float slp; + float phase_shift; for (uint32_t s = 0; s < sample_count; ++s) { - const float freq = frequency[s * plugin->frequency_is_cv]; - if (freq != wdat->frequency) { - /* Frequency changed, look up table to play */ - wavedata_get_table(wdat, freq); - } + freq = frequency[s]; + slp = f_clip(slope[s], min_slope, max_slope); + phase_shift = slp * wdat->sample_rate; + + /* Lookup which table to use from frequency */ + wavedata_get_table(wdat, freq); - const float this_slope = slope[s * plugin->slope_is_cv]; - if (this_slope != last_slope) { - /* Slope changed, recalculate */ - last_slope = this_slope; - slp = f_clip(this_slope, min_slope, max_slope); - phase_shift = slp * wdat->sample_rate; - scale = 1.0f / (8.0f * (slp - (slp * slp))); + /* Get samples from parabola and phase shifted inverted parabola, + and scale to compensate */ + output[s] = (wavedata_get_sample(wdat, phase) + - wavedata_get_sample(wdat, phase + phase_shift)) + / (8.0f * (slp - (slp * slp))); + + /* Update phase, wrapping if necessary */ + phase += wdat->frequency; + if (phase < 0.0f) { + phase += wdat->sample_rate; + } else if (phase > wdat->sample_rate) { + phase -= wdat->sample_rate; } + } + plugin->phase = phase; +} + +static void +runTriangle_fasc_oa(LV2_Handle instance, + uint32_t sample_count) +{ + Triangle* plugin = (Triangle*)instance; + + /* Frequency (array of float of length sample_count) */ + const float* frequency = plugin->frequency; + + /* Slope (float value) */ + float slope = *(plugin->slope); + + /* Output (pointer to float value) */ + float* output = plugin->output; + + /* Instance data */ + Wavedata* wdat = &plugin->wdat; + float phase = plugin->phase; + float min_slope = plugin->min_slope; + float max_slope = plugin->max_slope; + + float freq; + float phase_shift; + float scale; + + slope = f_clip(slope, min_slope, max_slope); + scale = 1.0f / (8.0f * (slope - (slope * slope))); + phase_shift = slope * wdat->sample_rate; + + for (uint32_t s = 0; s < sample_count; ++s) { + freq = frequency[s]; + + /* Lookup which table to use from frequency */ + wavedata_get_table(wdat, freq); /* Get samples from parabola and phase shifted inverted parabola, and scale to compensate */ @@ -188,25 +193,109 @@ run(LV2_Handle instance, plugin->phase = phase; } -static const void* -extension_data(const char* uri) +static void +runTriangle_fcsa_oa(LV2_Handle instance, + uint32_t sample_count) { - static const LV2_Morph_Interface morph = { morph_port, NULL }; - if (!strcmp(uri, LV2_MORPH__interface)) { - return &morph; + Triangle* plugin = (Triangle*)instance; + + /* Frequency (float value) */ + const float frequency = *(plugin->frequency); + + /* Slope (array of float of length sample_count) */ + const float* slope = plugin->slope; + + /* Output (pointer to float value) */ + float* output = plugin->output; + + /* Instance data */ + Wavedata* wdat = &plugin->wdat; + float phase = plugin->phase; + float min_slope = plugin->min_slope; + float max_slope = plugin->max_slope; + + float slp; + float phase_shift; + + wavedata_get_table(wdat, frequency); + + for (uint32_t s = 0; s < sample_count; ++s) { + slp = f_clip(slope[s], min_slope, max_slope); + phase_shift = slp * wdat->sample_rate; + + /* Get samples from parabola and phase shifted inverted parabola, + and scale to compensate */ + output[s] = (wavedata_get_sample(wdat, phase) + - wavedata_get_sample(wdat, phase + phase_shift)) + / (8.0f * (slp - (slp * slp))); + + /* Update phase, wrapping if necessary */ + phase += wdat->frequency; + if (phase < 0.0f) { + phase += wdat->sample_rate; + } else if (phase > wdat->sample_rate) { + phase -= wdat->sample_rate; + } } - return NULL; + plugin->phase = phase; +} + +static void +runTriangle_fcsc_oa(LV2_Handle instance, + uint32_t sample_count) +{ + Triangle* plugin = (Triangle*)instance; + + /* Frequency (float value) */ + const float frequency = *(plugin->frequency); + + /* Slope (float value) */ + float slope = *(plugin->slope); + + /* Output (pointer to float value) */ + float* output = plugin->output; + + /* Instance data */ + Wavedata* wdat = &plugin->wdat; + float phase = plugin->phase; + float min_slope = plugin->min_slope; + float max_slope = plugin->max_slope; + + float scale; + float phase_shift; + + slope = f_clip(slope, min_slope, max_slope); + scale = 1.0f / (8.0f * (slope - (slope * slope))); + phase_shift = slope * wdat->sample_rate; + + wavedata_get_table(wdat, frequency); + + for (uint32_t s = 0; s < sample_count; ++s) { + /* Get samples from parabola and phase shifted inverted parabola, + and scale to compensate */ + output[s] = (wavedata_get_sample(wdat, phase) + - wavedata_get_sample(wdat, phase + phase_shift)) * scale; + + /* Update phase, wrapping if necessary */ + phase += wdat->frequency; + if (phase < 0.0f) { + phase += wdat->sample_rate; + } else if (phase > wdat->sample_rate) { + phase -= wdat->sample_rate; + } + } + plugin->phase = phase; } static const LV2_Descriptor descriptor = { - "http://drobilla.net/plugins/blop/triangle", + "http://drobilla.net/plugins/blip/triangle", instantiate, connect_port, activate, - run, + runTriangle_fcsc_oa, NULL, cleanup, - extension_data, + NULL, }; LV2_SYMBOL_EXPORT const LV2_Descriptor* |