crap/template/ladspa.cpp

194 lines
4.6 KiB
C++
Raw Normal View History

#include <stdlib.h>
2015-06-06 10:42:14 -07:00
#include "ladspa.hpp"
//#INCLUDE
//#REDEFINE
2014-01-29 19:36:08 -08:00
#ifndef PARAM_NAME_LEN
#define PARAM_NAME_LEN 24
#endif
#define PLUG_INPUT_L 0
#define PLUG_INPUT_R 1
#define PLUG_OUTPUT_L 2
#define PLUG_OUTPUT_R 3
#define PCOUNT (PARAMETERS + 4)
2014-01-29 19:36:08 -08:00
static void __attribute__ ((constructor)) plug_init();
2014-01-29 19:36:08 -08:00
LADSPA_PortDescriptor p_descs[PCOUNT];
LADSPA_PortRangeHint p_hints[PCOUNT];
char p_default_strings[4][PARAM_NAME_LEN + 1] = {
"Input L", "Input R",
"Output L", "Output R"
};
2014-01-29 19:36:08 -08:00
#if (PARAMETERS > 0)
static param global_params[PARAMETERS];
char p_name_strings[PARAMETERS][PARAM_NAME_LEN + 1];
#endif
char *p_names[PCOUNT];
typedef struct {
LADSPA_Data *input_L;
LADSPA_Data *input_R;
LADSPA_Data *output_L;
LADSPA_Data *output_R;
Crap *crap;
2014-01-29 19:36:08 -08:00
#if (PARAMETERS > 0)
2014-06-19 08:27:25 -07:00
LADSPA_Data *values[PARAMETERS];
2014-01-29 19:36:08 -08:00
param params[PARAMETERS];
#endif
} plug_t;
static void
2014-02-05 23:47:16 -08:00
plug_connect(LADSPA_Handle instance, unsigned long port, LADSPA_Data *data)
{
plug_t *plug = (plug_t *)instance;
if (port == PLUG_INPUT_L)
plug->input_L = data;
else if (port == PLUG_INPUT_R)
plug->input_R = data;
else if (port == PLUG_OUTPUT_L)
plug->output_L = data;
else if (port == PLUG_OUTPUT_R)
plug->output_R = data;
2014-01-29 19:36:08 -08:00
#if (PARAMETERS > 0)
else if (port < PARAMETERS + 4)
plug->values[port - 4] = data;
#endif
}
static void
2014-02-05 23:47:16 -08:00
plug_resume(LADSPA_Handle instance)
{
plug_t *plug = (plug_t *)instance;
plug->crap->resume();
}
static void
2014-02-05 23:47:16 -08:00
plug_pause(LADSPA_Handle instance)
{
plug_t *plug = (plug_t *)instance;
plug->crap->pause();
}
static LADSPA_Handle
2014-02-05 23:47:16 -08:00
plug_construct(const LADSPA_Descriptor *descriptor, unsigned long fs)
{
plug_t *plug = (plug_t *) calloc(1, sizeof(plug_t));
plug->crap = new CrapPlug();
plug->crap->construct();
2014-01-29 19:36:08 -08:00
#if (PARAMETERS > 0)
memcpy(plug->params, global_params, sizeof(param)*PARAMETERS);
plug->crap->adjust(plug->params, fs);
2014-01-29 19:36:08 -08:00
#else
plug->crap->adjust(NULL, fs);
2014-01-29 19:36:08 -08:00
#endif
return (LADSPA_Handle) plug;
}
static void
2014-02-05 23:47:16 -08:00
plug_destruct(LADSPA_Handle instance)
{
plug_t *plug = (plug_t *)instance;
plug->crap->destruct();
delete plug->crap;
free(plug);
}
static void
2014-02-05 23:47:16 -08:00
plug_process(LADSPA_Handle instance, unsigned long count)
{
plug_t *plug = (plug_t *)instance;
2014-01-29 19:36:08 -08:00
#if (PARAMETERS > 0)
for (int i = 0; i < PARAMETERS; i++) {
2014-06-19 08:27:25 -07:00
if (!plug->values[i])
continue;
2014-01-29 19:36:08 -08:00
if (*plug->values[i] != plug->params[i].value) {
plug->params[i].value = *plug->values[i];
plug->crap->adjust_one(plug->params, i);
2014-01-29 19:36:08 -08:00
}
}
#endif
plug->crap->process(
plug->input_L, plug->input_R,
plug->output_L, plug->output_R,
count);
}
static const LADSPA_Descriptor plug_desc = {
.UniqueID = ID,
.Label = LABEL,
.Properties = 0,
.Name = NAME,
.Maker = AUTHOR,
.Copyright = COPYRIGHT,
.PortCount = PCOUNT,
2014-01-29 19:36:08 -08:00
.PortDescriptors = p_descs,
.PortRangeHints = p_hints,
.PortNames = (const char * const *) p_names,
.instantiate = plug_construct,
.cleanup = plug_destruct,
.activate = plug_resume,
.deactivate = plug_pause,
.connect_port = plug_connect,
.run = plug_process,
.run_adding = NULL,
.set_run_adding_gain = NULL
};
const LADSPA_Descriptor *
2014-02-05 23:47:16 -08:00
ladspa_descriptor(unsigned long index)
{
if (index != 0)
return NULL;
return &plug_desc;
}
2014-01-29 19:36:08 -08:00
static void
2014-02-05 23:47:16 -08:00
plug_init()
{
2014-01-29 19:36:08 -08:00
for (int i = 0; i < 4; i++) {
p_names[i] = p_default_strings[i];
p_descs[i] = LADSPA_PORT_AUDIO;
p_descs[i] |= (i < 2) ? LADSPA_PORT_INPUT : LADSPA_PORT_OUTPUT;
p_hints[i] = (LADSPA_PortRangeHint){.HintDescriptor = 0};
}
#if (PARAMETERS > 0)
CrapPlug::construct_params(global_params);
2014-01-29 19:36:08 -08:00
for (int i = 0; i < PARAMETERS; i++) {
int j = i + 4;
param *p = &global_params[i];
memcpy(p_name_strings[i], p->name, PARAM_NAME_LEN + 1);
p_names[j] = p_name_strings[i];
p_descs[j] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
int hint = LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
p_hints[j].LowerBound = p->min;
p_hints[j].UpperBound = p->max;
if (p->scale == SCALE_INT) hint |= LADSPA_HINT_INTEGER;
if (p->scale == SCALE_TOGGLE) hint |= LADSPA_HINT_TOGGLED;
if (p->scale >= SCALE_LOG) hint |= LADSPA_HINT_LOGARITHMIC;
switch (p->def) {
case DEFAULT_0: hint |= LADSPA_HINT_DEFAULT_0; break;
case DEFAULT_1: hint |= LADSPA_HINT_DEFAULT_1; break;
case DEFAULT_100: hint |= LADSPA_HINT_DEFAULT_100; break;
case DEFAULT_440: hint |= LADSPA_HINT_DEFAULT_440; break;
case DEFAULT_MIN: hint |= LADSPA_HINT_DEFAULT_MINIMUM; break;
case DEFAULT_LOW: hint |= LADSPA_HINT_DEFAULT_LOW; break;
case DEFAULT_HIGH: hint |= LADSPA_HINT_DEFAULT_HIGH; break;
case DEFAULT_MAX: hint |= LADSPA_HINT_DEFAULT_MAXIMUM; break;
case DEFAULT_HALF: hint |= LADSPA_HINT_DEFAULT_MIDDLE; break;
}
p_hints[j].HintDescriptor = hint;
}
#endif
}