aboutsummaryrefslogtreecommitdiffstats
path: root/src/sawtooth.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sawtooth.c')
-rw-r--r--src/sawtooth.c81
1 files changed, 45 insertions, 36 deletions
diff --git a/src/sawtooth.c b/src/sawtooth.c
index 3c242e1..35db21f 100644
--- a/src/sawtooth.c
+++ b/src/sawtooth.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 SAWTOOTH_FREQUENCY 0
@@ -28,7 +30,9 @@ typedef struct {
float* frequency;
float* output;
float phase;
+ uint32_t frequency_is_cv;
Wavedata wdat;
+ URIs uris;
} Sawtooth;
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)
+{
+ Sawtooth* plugin = (Sawtooth*)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,
@@ -55,6 +76,9 @@ instantiate(const LV2_Descriptor* descriptor,
const LV2_Feature* const* features)
{
Sawtooth* plugin = (Sawtooth*)malloc(sizeof(Sawtooth));
+ if (!plugin) {
+ return NULL;
+ }
if (wavedata_load(&plugin->wdat, bundle_path, "sawtooth_data",
BLOP_DLSYM_SAWTOOTH, sample_rate)) {
@@ -62,6 +86,10 @@ instantiate(const LV2_Descriptor* descriptor,
return 0;
}
+ plugin->frequency_is_cv = 0;
+ map_uris(&plugin->uris, features);
+ wavedata_get_table(&plugin->wdat, 440.0);
+
return (LV2_Handle)plugin;
}
@@ -83,12 +111,12 @@ activate(LV2_Handle instance)
}
static void
-runSawtooth_fa_oa(LV2_Handle instance,
- uint32_t sample_count)
+run(LV2_Handle instance,
+ uint32_t sample_count)
{
Sawtooth* plugin = (Sawtooth*)instance;
- /* Frequency (array of float of length sample_count) */
+ /* Frequency (array of float of length 1 or sample_count) */
const float* frequency = plugin->frequency;
/* Output (pointer to float value) */
@@ -99,8 +127,11 @@ runSawtooth_fa_oa(LV2_Handle instance,
float phase = plugin->phase;
for (uint32_t s = 0; s < sample_count; s++) {
- /* Lookup 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);
+ }
output[s] = wavedata_get_sample(wdat, phase);
@@ -115,47 +146,25 @@ runSawtooth_fa_oa(LV2_Handle instance,
plugin->phase = phase;
}
-static void
-runSawtooth_fc_oa(LV2_Handle instance,
- uint32_t sample_count)
+static const void*
+extension_data(const char* uri)
{
- Sawtooth* plugin = (Sawtooth*)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/sawtooth",
+ "http://drobilla.net/plugins/blop/sawtooth",
instantiate,
connect_port,
activate,
- runSawtooth_fc_oa,
+ run,
NULL,
cleanup,
- NULL,
+ extension_data,
};
LV2_SYMBOL_EXPORT const LV2_Descriptor*