From 7c7e2918bdd051fb213944d53043a457a0d9ac34 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 4 Oct 2011 03:19:25 +0000 Subject: Build oscillator data and load it from LV2 bundle. git-svn-id: http://svn.drobilla.net/lad/trunk/plugins/blip.lv2@3526 a436a847-0d15-0410-975c-d299462d15a1 --- src/include/wavedata.h | 8 +-- src/include/wdatutil.h | 4 +- src/pulse.c | 3 +- src/sawtooth.c | 3 +- src/square.c | 3 +- src/triangle.c | 3 +- src/wavedata.c | 133 +++++++++++-------------------------------------- src/wavegen.c | 2 + src/wdatutil.c | 29 +++++------ 9 files changed, 59 insertions(+), 129 deletions(-) (limited to 'src') diff --git a/src/include/wavedata.h b/src/include/wavedata.h index 82bc70f..32a8a5b 100644 --- a/src/include/wavedata.h +++ b/src/include/wavedata.h @@ -74,9 +74,11 @@ typedef struct { extern "C" { #endif -int wavedata_load(Wavedata* w, - const char* wdat_descriptor_name, - unsigned long sample_rate); +int wavedata_load(Wavedata* w, + const char* bundle_path, + const char* lib_name, + const char* wdat_descriptor_name, + double sample_rate); void wavedata_unload(Wavedata* w); diff --git a/src/include/wdatutil.h b/src/include/wdatutil.h index d7bca80..931a7f3 100644 --- a/src/include/wdatutil.h +++ b/src/include/wdatutil.h @@ -78,7 +78,7 @@ extern "C" { * * Notes: No wavetables are allocated. Use wavedata_add_table ******************************************************************************/ -Wavedata* wavedata_new(unsigned long sample_rate); +Wavedata* wavedata_new(double sample_rate); /******************************************************************************* * Description: Destroy allocated wavedata and any tables @@ -98,7 +98,7 @@ void wavedata_cleanup(Wavedata* w); * -1 otherwise ******************************************************************************/ int wavedata_add_table(Wavedata* w, - unsigned long sample_count, + uint32_t sample_count, unsigned long harmonics); /******************************************************************************* diff --git a/src/pulse.c b/src/pulse.c index 17c8525..722527b 100644 --- a/src/pulse.c +++ b/src/pulse.c @@ -61,7 +61,8 @@ instantiate(const LV2_Descriptor* descriptor, { Pulse* plugin = (Pulse*)malloc(sizeof(Pulse)); - if (wavedata_load(&plugin->wdat, BLOP_DLSYM_SAWTOOTH, sample_rate)) { + if (wavedata_load(&plugin->wdat, bundle_path, "sawtooth_data", + BLOP_DLSYM_SAWTOOTH, sample_rate)) { free(plugin); return 0; } diff --git a/src/sawtooth.c b/src/sawtooth.c index 2e1a3ec..fcef953 100644 --- a/src/sawtooth.c +++ b/src/sawtooth.c @@ -56,7 +56,8 @@ instantiate(const LV2_Descriptor* descriptor, { Sawtooth* plugin = (Sawtooth*)malloc(sizeof(Sawtooth)); - if (wavedata_load(&plugin->wdat, BLOP_DLSYM_SAWTOOTH, sample_rate)) { + if (wavedata_load(&plugin->wdat, bundle_path, "sawtooth_data", + BLOP_DLSYM_SAWTOOTH, sample_rate)) { free(plugin); return 0; } diff --git a/src/square.c b/src/square.c index 001a47e..8267803 100644 --- a/src/square.c +++ b/src/square.c @@ -56,7 +56,8 @@ instantiate(const LV2_Descriptor* descriptor, { Square* plugin = (Square*)malloc(sizeof(Square)); - if (wavedata_load(&plugin->wdat, BLOP_DLSYM_SQUARE, sample_rate)) { + if (wavedata_load(&plugin->wdat, bundle_path, "square_data", + BLOP_DLSYM_SQUARE, sample_rate)) { free(plugin); return NULL; } diff --git a/src/triangle.c b/src/triangle.c index 718369a..00964df 100644 --- a/src/triangle.c +++ b/src/triangle.c @@ -63,7 +63,8 @@ instantiate(const LV2_Descriptor* descriptor, { Triangle* plugin = (Triangle*)malloc(sizeof(Triangle)); - if (wavedata_load(&plugin->wdat, BLOP_DLSYM_PARABOLA, sample_rate)) { + if (wavedata_load(&plugin->wdat, bundle_path, "parabola_data", + BLOP_DLSYM_PARABOLA, sample_rate)) { free(plugin); return 0; } diff --git a/src/wavedata.c b/src/wavedata.c index 8f7e359..7bf8e3b 100644 --- a/src/wavedata.c +++ b/src/wavedata.c @@ -1,5 +1,5 @@ /* - Oscillator wave data generation. + Oscillator wave data loading. Copyright 2011 David Robillard Copyright 2002 Mike Rawes @@ -21,116 +21,43 @@ #include #include #include -#include #include +#include +#include #include "lv2/lv2plug.in/ns/lv2core/lv2.h" -#include +#include "config.h" #include "wavedata.h" -#ifndef WAVEDATA_SUBDIR -#warning *** No wavedata subdir given, using default 'blip_files' -#define WAVEDATA_SUBDIR "blip_files" -#endif - int -wavedata_load(Wavedata* w, - const char* wdat_descriptor_name, - unsigned long sample_rate) +wavedata_load(Wavedata* w, + const char* bundle_path, + const char* lib_name, + const char* wdat_descriptor_name, + double sample_rate) { - const char* subdir = WAVEDATA_SUBDIR; - char* ladspa_path; - const char* start; - const char* end; - int extra; - size_t subdirlen = strlen(WAVEDATA_SUBDIR); - size_t length; - size_t pathlen; - char* path; - char* filename; - DIR* dp; - struct dirent* ep; - struct stat sb; - void* handle; - int (*desc_func)(Wavedata*, unsigned long); - int retval = -1; - - /* Get LADPSA_PATH, if available */ - ladspa_path = getenv("LV2_PATH"); - if (!ladspa_path) { - ladspa_path = "/usr/lib/ladspa:/usr/local/lib/ladspa"; - } - - start = ladspa_path; - while (*start != '\0') { - while (*start == ':') { - start++; - } - end = start; - while (*end != ':' && *end != '\0') { - end++; - } - if (end - start > 0) { - extra = (*(end - 1) == '/') ? 0 : 1; - path = (char*)malloc(end - start + extra + subdirlen + 1 + 1); - if (path) { - strncpy(path, start, end - start); - if (extra == 1) { - path[end - start] = '/'; - } - - path[end - start + extra] = '\0'; - - if (subdirlen > 0) { - strncat(path, subdir, subdirlen); - path[end - start + extra + subdirlen] = '/'; - path[end - start + extra + subdirlen + 1] = '\0'; - } else { - path[end - start + extra + subdirlen] = '\0'; - } - - dp = opendir(path); - if (dp) { - pathlen = strlen(path); - while ((ep = readdir(dp))) { - /* Stat file to get type */ - length = pathlen + strlen(ep->d_name); - filename = (char*)malloc(length + 1); - if (filename) { - strncpy(filename, path, pathlen); - - filename[pathlen] = '\0'; - filename = strncat(filename, ep->d_name, strlen(ep->d_name)); - filename[length] = '\0'; - - if (!stat(filename, &sb)) { - /* We only want regular files */ - if (S_ISREG(sb.st_mode)) { - /* Whew. Now see if we've got the right dll */ - handle = dlopen(filename, RTLD_NOW); - - if (handle) { - desc_func = dlsym(handle, wdat_descriptor_name); - - if (desc_func) { - free(filename); - free(path); - retval = desc_func(w, sample_rate); - w->data_handle = handle; - return retval; - } - } - } - } - free(filename); - } - } - closedir(dp); - } - free(path); - } + const size_t bundle_len = strlen(bundle_path); + const size_t lib_name_len = strlen(lib_name); + const size_t ext_len = strlen(BLIP_SHLIB_EXT); + const size_t path_len = bundle_len + lib_name_len + ext_len + 2; + int retval = -1; + + char* lib_path = malloc(path_len); + snprintf(lib_path, path_len, "%s%s%s", + bundle_path, lib_name, BLIP_SHLIB_EXT); + + void* handle = dlopen(lib_path, RTLD_NOW); + + if (handle) { + int (*desc_func)(Wavedata*, unsigned long); + desc_func = dlsym(handle, wdat_descriptor_name); + if (desc_func) { + retval = desc_func(w, sample_rate); + w->data_handle = handle; + return retval; } - start = end; } + + free(lib_path); return retval; } diff --git a/src/wavegen.c b/src/wavegen.c index 3728920..740ed3c 100644 --- a/src/wavegen.c +++ b/src/wavegen.c @@ -17,6 +17,8 @@ along with this software. If not, see . */ +#define _GNU_SOURCE 1 + #include #include #include diff --git a/src/wdatutil.c b/src/wdatutil.c index 190e8f5..b6a9409 100644 --- a/src/wdatutil.c +++ b/src/wdatutil.c @@ -74,9 +74,7 @@ unsigned long wave_harmonic_intervals[] = { }; Wavedata* -wavedata_new(double sample_rate, - const char* bundle_path, - const LV2_Feature* const* features) +wavedata_new(double sample_rate) { Wavedata* w; @@ -97,7 +95,7 @@ wavedata_new(double sample_rate, return w; } -static void +void wavedata_cleanup(Wavedata* w) { unsigned long ti; @@ -180,7 +178,7 @@ wavedata_add_table(Wavedata* w, return 0; } -static void +void wavedata_generate_tables(Wavedata* w, Wavetype wavetype, float gibbs_comp) @@ -190,7 +188,6 @@ wavedata_generate_tables(Wavedata* w, float* samples_hf; unsigned long h_lf; unsigned long h_hf; - unsigned long s; unsigned long i; for (i = 0; i < w->table_count; i++) { @@ -261,7 +258,7 @@ wavedata_write(Wavedata* w, */ table_count = w->table_count + 1; - fprintf(wdat_fp, "#include "lv 2 / lv2plug.in / ns / lv2core / lv2.h "\n"); + fprintf(wdat_fp, "#include \"lv2/lv2plug.in/ns/lv2core/lv2.h\"\n"); fprintf(wdat_fp, "#include \n"); fprintf(wdat_fp, "#include \"wavedata.h\"\n"); fprintf(wdat_fp, "\n"); @@ -286,7 +283,7 @@ wavedata_write(Wavedata* w, fprintf(wdat_fp, "static float samples_lf_%ld[%ld] = {\n", i, t->sample_count + 3); column = 0; - for (uint32_t s = 0; s < t->sample_count + 3 - 1; s++, column++) { + for (s = 0; s < t->sample_count + 3 - 1; s++, column++) { if (column == 5) { fprintf(wdat_fp, "\n"); column = 0; @@ -305,7 +302,7 @@ wavedata_write(Wavedata* w, fprintf(wdat_fp, "static float samples_hf_%ld[%ld] = {\n", i, t->sample_count + 3); column = 0; - for (uint32_t s = 0; s < t->sample_count + 3 - 1; s++, column++) { + for (s = 0; s < t->sample_count + 3 - 1; s++, column++) { if (column == 5) { fprintf(wdat_fp, "\n"); column = 0; @@ -331,9 +328,7 @@ wavedata_write(Wavedata* w, fprintf(wdat_fp, "int\n"); fprintf( wdat_fp, - "blip_get_%s (Wavedata * w, double sample_rate, - const char* bundle_path, - const LV2_Feature* const* features)\n" , + "blip_get_%s (Wavedata * w, double sample_rate, const char* bundle_path, const LV2_Feature* const* features)\n", data_name); fprintf(wdat_fp, "{\n"); fprintf(wdat_fp, "\tWavetable * t;\n"); @@ -415,12 +410,12 @@ wavedata_write(Wavedata* w, * Assemble tables and lookup */ fprintf(wdat_fp, "void\n"); - fprintf(wdat_fp, "_init (void)\n"); + fprintf(wdat_fp, "__attribute__ ((constructor))\n"); + fprintf(wdat_fp, "init (void)\n"); fprintf(wdat_fp, "{\n"); fprintf(wdat_fp, "\tunsigned long max_harmonic;\n"); fprintf(wdat_fp, "\tunsigned long ti;\n"); fprintf(wdat_fp, "\tunsigned long li;\n"); - fprintf(wdat_fp, "\tunsigned long s;\n"); fprintf(wdat_fp, "\n"); for (i = 0; i < w->table_count; i++) { @@ -488,7 +483,7 @@ wavedata_write(Wavedata* w, return 0; } -static void +void generate_sawtooth(float* samples, uint32_t sample_count, unsigned long harmonics, @@ -547,7 +542,7 @@ generate_sawtooth(float* samples, } } -static void +void generate_square(float* samples, uint32_t sample_count, unsigned long harmonics, @@ -606,7 +601,7 @@ generate_square(float* samples, } } -static void +void generate_parabola(float* samples, uint32_t sample_count, unsigned long harmonics, -- cgit v1.2.1