Initial C-style C++

master
Peter Nelson 2013-02-02 07:16:14 +00:00
parent 5a63fb39fd
commit dab01f34f9
3 changed files with 85 additions and 90 deletions

View File

@ -4,11 +4,13 @@ LDFLAGS := -lm
CFLAGS += `pkg-config lv2core pugl-0 --cflags`
LDFLAGS += `pkg-config lv2core pugl-0 --libs`
PTAPSRC := ptap.c
PTAPSRC += ptapui.c
PTAPOBJ := $(PTAPSRC:.c=.o)
PTAPSRC := ptap.cpp
PTAPSRC += ptapui.cpp
PTAPOBJ := $(PTAPSRC:.cpp=.o)
PTAPO := ptap.so
CPP := g++
all: $(PTAPO)
clean:
@ -20,6 +22,6 @@ depend:
$(PTAPO): $(PTAPOBJ)
$(CC) $(LDFLAGS) -shared -fPIC -Wl,-soname,$(PTAPO) $(PTAPOBJ) -o $@
.c.o:
$(CC) -c -fPIC $(CFLAGS) $< -o $@
.cpp.o:
$(CPP) -c -fPIC $(CFLAGS) $< -o $@

View File

@ -1,7 +1,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
#include <lv2.h>
@ -9,7 +9,7 @@
#define TAPS 6
#define BUFFER_SECONDS 10
struct tap_t {
struct Tap {
float *t_gain[TAPS];
float *l_gain;
float *r_gain;
@ -17,16 +17,16 @@ struct tap_t {
float *delay;
};
static const int CONTROLS_PER_TAP = sizeof (struct tap_t) / sizeof (float *);
static const int CONTROLS_PER_TAP = sizeof (Tap) / sizeof (float *);
struct ptap_t
struct PTap
{
double sample_rate;
size_t buffer_max;
struct tap_t tap[TAPS];
struct tap_t l_out;
struct tap_t r_out;
Tap tap[TAPS];
Tap l_out;
Tap r_out;
float *buffers[TAPS]; ///< Tap audio buffers
float *rp[TAPS]; ///< Read pointers
@ -45,32 +45,23 @@ static LV2_Handle ptap_instantiate(
const LV2_Feature *const *host_features)
{
/* Allocate local data */
struct ptap_t *ptap = malloc(sizeof *ptap);
if (ptap == NULL) {
return NULL;
}
memset(ptap, 0, sizeof *ptap);
PTap *ptap = new PTap();
ptap->sample_rate = sample_rate;
ptap->buffer_max = ptap->sample_rate * BUFFER_SECONDS + 1;
int i, j;
for (i = 0; i < TAPS; i++) {
ptap->buffers[i] = malloc(ptap->buffer_max * sizeof (float));
for (int i = 0; i < TAPS; i++) {
ptap->buffers[i] = new float[ptap->buffer_max]();
if (ptap->buffers[i] == NULL) {
for (j = 0; j < i; j++) {
free(ptap->buffers[i]);
for (int j = 0; j < i; j++) {
delete ptap->buffers[i];
}
free(ptap);
delete ptap;
printf("Failed to allocate buffers\n");
return NULL;
}
memset(ptap->buffers[i], 0, ptap->buffer_max * sizeof (float));
ptap->rp[i] = ptap->buffers[i];
ptap->wp[i] = ptap->buffers[i];
}
@ -80,16 +71,17 @@ static LV2_Handle ptap_instantiate(
static void ptap_connect_port(LV2_Handle lv2instance, uint32_t port, void *data)
{
struct ptap_t *ptap = (struct ptap_t *)lv2instance;
struct tap_t *tap;
PTap *ptap = (PTap *)lv2instance;
Tap *tap;
float *fdata = (float *)data;
/* Audio ports */
if (port < 4) {
switch (port) {
case 0: ptap->in_l = data; break;
case 1: ptap->in_r = data; break;
case 2: ptap->out_l = data; break;
case 3: ptap->out_r = data; break;
case 0: ptap->in_l = fdata; break;
case 1: ptap->in_r = fdata; break;
case 2: ptap->out_l = fdata; break;
case 3: ptap->out_r = fdata; break;
}
return;
}
@ -111,15 +103,15 @@ static void ptap_connect_port(LV2_Handle lv2instance, uint32_t port, void *data)
int tap_port = port % CONTROLS_PER_TAP;
if (tap_port < TAPS) {
tap->t_gain[tap_port] = data;
tap->t_gain[tap_port] = fdata;
} else if (tap_port - TAPS == 0) {
tap->l_gain = data;
tap->l_gain = fdata;
} else if (tap_port - TAPS == 1) {
tap->r_gain = data;
tap->r_gain = fdata;
} else if (tap_port - TAPS == 2) {
tap->gain = data;
tap->gain = fdata;
} else if (tap_port - TAPS == 3) {
tap->delay = data;
tap->delay = fdata;
} else {
return;
}
@ -127,13 +119,12 @@ static void ptap_connect_port(LV2_Handle lv2instance, uint32_t port, void *data)
static void ptap_run(LV2_Handle lv2instance, uint32_t sample_count)
{
struct ptap_t *ptap = (struct ptap_t *)lv2instance;
const struct tap_t *tap;
PTap *ptap = (PTap *)lv2instance;
Tap *tap;
float *wp;
int i, j;
/* Position read pointers behind write pointers */
for (i = 0; i < TAPS; i++) {
for (int i = 0; i < TAPS; i++) {
int delay = *ptap->tap[i].delay * ptap->sample_rate;
ptap->rp[i] = ptap->wp[i] - delay;
if (ptap->rp[i] < ptap->buffers[i]) {
@ -141,19 +132,19 @@ static void ptap_run(LV2_Handle lv2instance, uint32_t sample_count)
}
}
float *il = ptap->in_l;
float *ir = ptap->in_r;
float *ol = ptap->out_l;
float *or = ptap->out_r;
float *in_l = ptap->in_l;
float *in_r = ptap->in_r;
float *out_l = ptap->out_l;
float *out_r = ptap->out_r;
while (sample_count--) {
for (i = 0; i < TAPS; i++) {
for (int i = 0; i < TAPS; i++) {
wp = ptap->wp[i];
tap = &ptap->tap[i];
*wp = *il * *tap->l_gain;
*wp += *ir * *tap->r_gain;
for (j = 0; j < TAPS; j++) {
*wp = *in_l * *tap->l_gain;
*wp += *in_r * *tap->r_gain;
for (int j = 0; j < TAPS; j++) {
*wp += *ptap->rp[j] * *tap->t_gain[j];
}
*wp *= *tap->gain;
@ -161,24 +152,24 @@ static void ptap_run(LV2_Handle lv2instance, uint32_t sample_count)
/* Write to left output */
tap = &ptap->l_out;
*ol = *il * *tap->l_gain;
*ol += *ir * *tap->r_gain;
for (j = 0; j < TAPS; j++) {
*ol += *ptap->rp[j] * *tap->t_gain[j];
*out_l = *in_l * *tap->l_gain;
*out_l += *in_r * *tap->r_gain;
for (int j = 0; j < TAPS; j++) {
*out_l += *ptap->rp[j] * *tap->t_gain[j];
}
*ol *= *tap->gain;
*out_l *= *tap->gain;
/* Write to right output */
tap = &ptap->r_out;
*or = *il * *tap->l_gain;
*or += *ir * *tap->r_gain;
for (j = 0; j < TAPS; j++) {
*or += *ptap->rp[j] * *tap->t_gain[j];
*out_r = *in_l * *tap->l_gain;
*out_r += *in_r * *tap->r_gain;
for (int j = 0; j < TAPS; j++) {
*out_r += *ptap->rp[j] * *tap->t_gain[j];
}
*or *= *tap->gain;
*out_r *= *tap->gain;
/* Progress read pointers */
for (i = 0; i < TAPS; i++) {
for (int i = 0; i < TAPS; i++) {
ptap->wp[i]++;
if (ptap->wp[i] >= ptap->buffers[i] + ptap->buffer_max) {
ptap->wp[i] = ptap->buffers[i];
@ -190,32 +181,34 @@ static void ptap_run(LV2_Handle lv2instance, uint32_t sample_count)
}
}
il++;
ir++;
ol++;
or++;
in_l++;
in_r++;
out_l++;
out_r++;
}
}
static void ptap_cleanup(LV2_Handle lv2instance)
{
struct ptap_t *ptap = (struct ptap_t *)lv2instance;
PTap *ptap = (PTap *)lv2instance;
int i;
for (i = 0; i < TAPS; i++) {
free(ptap->buffers[i]);
for (int i = 0; i < TAPS; i++) {
delete ptap->buffers[i];
}
free(ptap);
delete ptap;
}
static const LV2_Descriptor s_lv2descriptor =
{
.URI = "urn:fuzzle:ptap",
.instantiate = &ptap_instantiate,
.connect_port = &ptap_connect_port,
.run = &ptap_run,
.cleanup = &ptap_cleanup,
"urn:fuzzle:ptap",
&ptap_instantiate,
&ptap_connect_port,
NULL,
&ptap_run,
NULL,
&ptap_cleanup,
NULL,
};
const LV2_Descriptor *lv2_descriptor(uint32_t index)

View File

@ -10,7 +10,7 @@
#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>
#include <pugl/pugl.h>
struct ptapui {
struct PTapUI {
PuglView *view;
pthread_t thread;
LV2UI_Write_Function write;
@ -22,7 +22,7 @@ struct ptapui {
static void *ui_thread(void *ptr)
{
struct ptapui *pui = (struct ptapui *)ptr;
PTapUI *pui = (PTapUI *)ptr;
while (!pui->exit) {
usleep(1000000 / 25);
puglProcessEvents(pui->view);
@ -30,7 +30,7 @@ static void *ui_thread(void *ptr)
return NULL;
}
static void parameterChanged(struct ptapui *pui, uint32_t index, float value)
static void parameterChanged(PTapUI *pui, uint32_t index, float value)
{
pui->write(pui->controller, index, sizeof value, 0, &value);
}
@ -48,7 +48,7 @@ static void onReshape(PuglView *view, int width, int height)
static void onDisplay(PuglView *view)
{
struct ptapui *pui = (struct ptapui *)puglGetHandle(view);
PTapUI *pui = (PTapUI *)puglGetHandle(view);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
@ -106,7 +106,7 @@ static LV2UI_Handle ptapui_instantiate(
return NULL;
}
struct ptapui *pui = malloc(sizeof *pui);
PTapUI *pui = new PTapUI();
pui->write = write_function;
pui->controller = controller;
pui->width = 320;
@ -134,11 +134,11 @@ static LV2UI_Handle ptapui_instantiate(
static void ptapui_cleanup(LV2UI_Handle ui)
{
struct ptapui *pui = (struct ptapui *)ui;
PTapUI *pui = (PTapUI *)ui;
pui->exit = true;
pthread_join(pui->thread, NULL);
puglDestroy(pui->view);
free(ui);
delete pui;
}
static void ptapui_port_event(
@ -157,11 +157,11 @@ static const void *ptapui_extension_data(const char *uri)
static const LV2UI_Descriptor s_lv2uidescriptor =
{
.URI = "urn:fuzzle:ptap#X11UI",
.instantiate = &ptapui_instantiate,
.cleanup = &ptapui_cleanup,
.port_event = &ptapui_port_event,
.extension_data = &ptapui_extension_data,
"urn:fuzzle:ptap#X11UI",
&ptapui_instantiate,
&ptapui_cleanup,
&ptapui_port_event,
&ptapui_extension_data,
};
const LV2UI_Descriptor *lv2ui_descriptor(uint32_t index)