diff options
author | David Robillard <d@drobilla.net> | 2012-05-31 06:04:50 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-05-31 06:04:50 +0000 |
commit | d7d39078187917779c5474909b62d2f5421e6788 (patch) | |
tree | dc13d94784660d4752a3cbe9470627c0792abe79 /src/random.c | |
parent | 8b46fd1ff51b29cded517c9c2d166611e8c453fc (diff) | |
parent | 9529f203c09d6dc8f4c2f12c3add9ec906f4bdba (diff) | |
download | blop.lv2-d7d39078187917779c5474909b62d2f5421e6788.tar.gz blop.lv2-d7d39078187917779c5474909b62d2f5421e6788.tar.bz2 blop.lv2-d7d39078187917779c5474909b62d2f5421e6788.zip |
Blip => Blop. I give up trying to give ports new names.
git-svn-id: http://svn.drobilla.net/lad/trunk/plugins/blop.lv2@4485 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/random.c')
-rw-r--r-- | src/random.c | 250 |
1 files changed, 188 insertions, 62 deletions
diff --git a/src/random.c b/src/random.c index 0981583..4a6f5b8 100644 --- a/src/random.c +++ b/src/random.c @@ -18,29 +18,24 @@ */ #include <stdlib.h> -#include "lv2/lv2plug.in/ns/ext/morph/morph.h" #include "lv2/lv2plug.in/ns/lv2core/lv2.h" #include <time.h> #include "math_func.h" #include "common.h" -#include "uris.h" #define RANDOM_FREQUENCY 0 #define RANDOM_SMOOTH 1 #define RANDOM_OUTPUT 2 typedef struct { - float* frequency; - float* smooth; - float* output; - float nyquist; - float inv_nyquist; - float phase; - float value1; - float value2; - uint32_t frequency_is_cv; - uint32_t smooth_is_cv; - URIs uris; + float* frequency; + float* smooth; + float* output; + float nyquist; + float inv_nyquist; + float phase; + float value1; + float value2; } Random; float inv_rand_max; @@ -71,33 +66,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) -{ - Random* plugin = (Random*)instance; - - if (type != plugin->uris.lv2_ControlPort && - type != plugin->uris.lv2_CVPort) { - return LV2_MORPH_ERR_BAD_TYPE; - } - - switch (port) { - case RANDOM_FREQUENCY: - plugin->frequency_is_cv = (type == plugin->uris.lv2_CVPort); - break; - case RANDOM_SMOOTH: - plugin->smooth_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, @@ -116,9 +84,6 @@ instantiate(const LV2_Descriptor* descriptor, plugin->value1 = rand() * inv_rand_max - 1.0f; plugin->value2 = rand() * inv_rand_max - 1.0f; - plugin->frequency_is_cv = 0; - plugin->smooth_is_cv = 0; - return (LV2_Handle)plugin; } @@ -131,15 +96,15 @@ activate(LV2_Handle instance) } static void -run(LV2_Handle instance, - uint32_t sample_count) +runRandom_fasa_oa(LV2_Handle instance, + uint32_t sample_count) { Random* plugin = (Random*)instance; - /* Frequency (Hz) (array of floats of length 1 or sample_count) */ + /* Frequency (Hz) (array of floats of length sample_count) */ const float* frequency = plugin->frequency; - /* Wave smoothness (array of floats of length 1 or sample_count) */ + /* Wave smoothness (array of floats of length sample_count) */ const float* smooth = plugin->smooth; /* Output (array of floats of length sample_count) */ @@ -152,16 +117,16 @@ run(LV2_Handle instance, float value1 = plugin->value1; float value2 = plugin->value2; + float freq; + float smth; + float interval; float result; for (uint32_t s = 0; s < sample_count; ++s) { - const float freq = f_clip(frequency[s * plugin->frequency_is_cv], - 0.0f, nyquist); + freq = f_clip(frequency[s], 0.0f, nyquist); - const float smth = f_clip(smooth[s * plugin->smooth_is_cv], - 0.0f, 1.0f); - - const float interval = (1.0f - smth) * 0.5f; + smth = f_clip(smooth[s], 0.0f, 1.0f); + interval = (1.0f - smth) * 0.5f; if (phase < interval) { result = 1.0f; @@ -191,25 +156,186 @@ run(LV2_Handle instance, plugin->value2 = value2; } -static const void* -extension_data(const char* uri) +static void +runRandom_fasc_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; + Random* plugin = (Random*)instance; + + /* Frequency (Hz) (array of floats of length sample_count) */ + const float* frequency = plugin->frequency; + + /* Wave smoothness (float value) */ + const float smooth = f_clip(*(plugin->smooth), 0.0f, 1.0f); + + /* Output (array of floats of length sample_count) */ + float* output = plugin->output; + + /* Instance data */ + float nyquist = plugin->nyquist; + float inv_nyquist = plugin->inv_nyquist; + float phase = plugin->phase; + float value1 = plugin->value1; + float value2 = plugin->value2; + + float freq; + float interval = (1.0f - smooth) * 0.5f; + float result; + + for (uint32_t s = 0; s < sample_count; ++s) { + freq = f_clip(frequency[s], 0.0f, nyquist); + + if (phase < interval) { + result = 1.0f; + } else if (phase > (1.0f - interval)) { + result = -1.0f; + } else if (interval > 0.0f) { + result = COSF((phase - interval) / smooth * M_PI); + } else { + result = COSF(phase * M_PI); + } + + result *= (value2 - value1) * 0.5f; + result -= (value2 + value1) * 0.5f; + + output[s] = result; + + phase += freq * inv_nyquist; + if (phase > 1.0f) { + phase -= 1.0f; + value1 = value2; + value2 = (float)rand() * inv_rand_max - 1.0f; + } } - return NULL; + + plugin->phase = phase; + plugin->value1 = value1; + plugin->value2 = value2; +} + +static void +runRandom_fcsa_oa(LV2_Handle instance, + uint32_t sample_count) +{ + Random* plugin = (Random*)instance; + + /* Frequency (Hz) (float value) */ + const float frequency = *(plugin->frequency); + + /* Wave smoothness (array of floats of length sample_count) */ + const float* smooth = plugin->smooth; + + /* Output (pointer to float value) */ + float* output = plugin->output; + + /* Instance data */ + float nyquist = plugin->nyquist; + float inv_nyquist = plugin->inv_nyquist; + float phase = plugin->phase; + float value1 = plugin->value1; + float value2 = plugin->value2; + + float phase_scale = f_clip(frequency, 0.0f, nyquist) * inv_nyquist; + float smth; + float interval; + float result; + + for (uint32_t s = 0; s < sample_count; ++s) { + smth = f_clip(smooth[s], 0.0f, 1.0f); + interval = (1.0f - smth) * 0.5f; + + if (phase < interval) { + result = 1.0f; + } else if (phase > (1.0f - interval)) { + result = -1.0f; + } else if (interval > 0.0f) { + result = COSF((phase - interval) / smth * M_PI); + } else { + result = COSF(phase * M_PI); + } + + result *= (value2 - value1) * 0.5f; + result -= (value2 + value1) * 0.5f; + + output[s] = result; + + phase += phase_scale; + if (phase > 1.0f) { + phase -= 1.0f; + value1 = value2; + value2 = (float)rand() * inv_rand_max - 1.0f; + } + } + + plugin->phase = phase; + plugin->value1 = value1; + plugin->value2 = value2; +} + +static void +runRandom_fcsc_oa(LV2_Handle instance, + uint32_t sample_count) +{ + Random* plugin = (Random*)instance; + + /* Frequency (Hz) (float value) */ + const float frequency = *(plugin->frequency); + + /* Wave smoothness (float value) */ + const float smooth = f_clip(*(plugin->smooth), 0.0f, 1.0f); + + /* Output (array of floats of length sample_count) */ + float* output = plugin->output; + + /* Instance data */ + float nyquist = plugin->nyquist; + float inv_nyquist = plugin->inv_nyquist; + float phase = plugin->phase; + float value1 = plugin->value1; + float value2 = plugin->value2; + + float phase_scale = f_clip(frequency, 0.0f, nyquist) * inv_nyquist; + float interval = (1.0f - smooth) * 0.5f; + float result; + + for (uint32_t s = 0; s < sample_count; ++s) { + if (phase < interval) { + result = 1.0f; + } else if (phase > (1.0f - interval)) { + result = -1.0f; + } else if (interval > 0.0f) { + result = COSF((phase - interval) / smooth * M_PI); + } else { + result = COSF(phase * M_PI); + } + + result *= (value2 - value1) * 0.5f; + result -= (value2 + value1) * 0.5f; + + output[s] = result; + + phase += phase_scale; + if (phase > 1.0f) { + phase -= 1.0f; + value1 = value2; + value2 = (float)rand() * inv_rand_max - 1.0f; + } + } + + plugin->phase = phase; + plugin->value1 = value1; + plugin->value2 = value2; } static const LV2_Descriptor descriptor = { - "http://drobilla.net/plugins/blop/random", + "http://drobilla.net/plugins/blip/random", instantiate, connect_port, activate, - run, + runRandom_fcsc_oa, NULL, cleanup, - extension_data, + NULL, }; LV2_SYMBOL_EXPORT const LV2_Descriptor* |