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

View File

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

View File

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