crap/util/bench.c

83 lines
1.5 KiB
C
Raw Normal View History

2015-04-04 02:06:13 -07:00
#include <alloca.h>
2013-11-11 08:26:22 -08:00
#include <stdlib.h>
#include <stdio.h>
2013-11-11 08:26:22 -08:00
#include <assert.h>
#include <time.h>
#include "dlfcn.h"
#include "ladspa.h"
#include "util.h"
2013-11-11 08:26:22 -08:00
enum {
BLOCK_SIZE=2048
};
void *plug = NULL;
static float *audio_buffer;
static int audio_count = 0;
2013-11-11 08:26:22 -08:00
static void
2014-02-05 23:47:16 -08:00
cleanup()
{
2013-11-11 08:26:22 -08:00
dlclose(plug);
if (audio_count) free(audio_buffer);
2013-11-11 08:26:22 -08:00
}
static const LADSPA_Descriptor*
2014-02-05 23:47:16 -08:00
load_ladspa(char *path)
{
2013-11-11 08:26:22 -08:00
plug = dlopen(path, RTLD_NOW);
assert(plug);
atexit(cleanup);
LADSPA_Descriptor_Function df = dlsym(plug, "ladspa_descriptor");
assert(df);
const LADSPA_Descriptor *d = df(0);
assert(d);
return d;
}
int
2014-02-05 23:47:16 -08:00
main(int argc, char **argv)
{
2013-11-11 08:26:22 -08:00
assert(argc > 1);
const LADSPA_Descriptor *d = load_ladspa(argv[1]);
LADSPA_Handle h = d->instantiate(d, 44100);
assert(h);
// we're lazy so we don't distinguish inputs and outputs
for (int i = 0; i < d->PortCount; i++)
if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[i]))
audio_count++;
audio_buffer = calloc(audio_count*BLOCK_SIZE, sizeof(float));
int a = 0;
2015-04-04 02:06:13 -07:00
for (int i = 0; i < d->PortCount; i++) {
if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[i])) {
d->connect_port(h, i, audio_buffer + a++*BLOCK_SIZE);
2015-04-04 02:06:13 -07:00
} else {
float *x = alloca(sizeof(float));
*x = 0;
d->connect_port(h, i, x);
}
}
2013-11-11 08:26:22 -08:00
mirand = time(NULL);
for (int i = 0; i < audio_count*BLOCK_SIZE; i++)
audio_buffer[i] = whitenoise();
2013-11-11 08:26:22 -08:00
if (d->activate) d->activate(h);
for (int i = 0; i < 64*64*8; i++)
d->run(h, BLOCK_SIZE);
if (d->deactivate) d->deactivate(h);
d->cleanup(h);
return 0;
}