diff options
Diffstat (limited to 'src/square.c')
-rw-r--r-- | src/square.c | 76 |
1 files changed, 41 insertions, 35 deletions
diff --git a/src/square.c b/src/square.c index fb78859..3e7f78f 100644 --- a/src/square.c +++ b/src/square.c @@ -18,7 +18,9 @@ */ #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 SQUARE_FREQUENCY 0 @@ -28,7 +30,9 @@ typedef struct { float* frequency; float* output; float phase; + uint32_t frequency_is_cv; Wavedata wdat; + URIs uris; } Square; static void @@ -48,6 +52,23 @@ 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) +{ + Square* plugin = (Square*)instance; + if (type == plugin->uris.lv2_ControlPort) { + plugin->frequency_is_cv = 0; + } else if (type == plugin->uris.lv2_CVPort) { + plugin->frequency_is_cv = 1; + } else { + return LV2_MORPH_ERR_BAD_TYPE; + } + return LV2_MORPH_SUCCESS; +} + static LV2_Handle instantiate(const LV2_Descriptor* descriptor, double sample_rate, @@ -62,6 +83,10 @@ instantiate(const LV2_Descriptor* descriptor, return NULL; } + plugin->frequency_is_cv = 0; + map_uris(&plugin->uris, features); + wavedata_get_table(&plugin->wdat, 440.0); + return (LV2_Handle)plugin; } @@ -83,8 +108,8 @@ activate(LV2_Handle instance) } static void -runSquare_fa_oa(LV2_Handle instance, - uint32_t sample_count) +run(LV2_Handle instance, + uint32_t sample_count) { Square* plugin = (Square*)instance; @@ -99,8 +124,11 @@ runSquare_fa_oa(LV2_Handle instance, float phase = plugin->phase; for (uint32_t s = 0; s < sample_count; ++s) { - /* Get table to play */ - wavedata_get_table(wdat, frequency[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); + } /* Get interpolated sample */ output[s] = wavedata_get_sample(wdat, phase); @@ -116,47 +144,25 @@ runSquare_fa_oa(LV2_Handle instance, plugin->phase = phase; } -static void -runSquare_fc_oa(LV2_Handle instance, - uint32_t sample_count) +static const void* +extension_data(const char* uri) { - Square* plugin = (Square*)instance; - - /* Frequency (float value) */ - const float frequency = *(plugin->frequency); - - /* Output (pointer to float value) */ - float* output = plugin->output; - - /* Instance data */ - Wavedata* wdat = &plugin->wdat; - float phase = plugin->phase; - - wavedata_get_table(wdat, frequency); - - for (uint32_t s = 0; s < sample_count; ++s) { - output[s] = wavedata_get_sample(wdat, phase); - - /* 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; - } + static const LV2_Morph_Interface morph = { morph_port, NULL }; + if (!strcmp(uri, LV2_MORPH__interface)) { + return &morph; } - plugin->phase = phase; + return NULL; } static const LV2_Descriptor descriptor = { - "http://drobilla.net/plugins/blip/square", + "http://drobilla.net/plugins/blop/square", instantiate, connect_port, activate, - runSquare_fc_oa, + run, NULL, cleanup, - NULL, + extension_data, }; LV2_SYMBOL_EXPORT const LV2_Descriptor* |