From a05477f429e1c9739a9deab7195bff20e91c94b8 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 11 Nov 2013 07:59:19 -0800 Subject: [PATCH] white noise generator --- Makefile | 2 +- crap_noise.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ crap_util.h | 3 ++ crap_util_def.h | 14 +++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100755 crap_noise.c diff --git a/Makefile b/Makefile index aa8ce6b..5648fc0 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ DISTNAME = crap VERSION = git EXE = design -SHOBJ = crap_eq.so crap_eq_const.so +SHOBJ = crap_eq.so crap_eq_const.so crap_noise.so MID = HEADERS = crap_util.h crap_util_def.h ladspa.h diff --git a/crap_noise.c b/crap_noise.c new file mode 100755 index 0000000..0cdeb66 --- /dev/null +++ b/crap_noise.c @@ -0,0 +1,84 @@ +#include "ladspa.h" +#include "crap_util.h" + +typedef unsigned long ulong; + +#define PLUG_INPUT 0 +#define PLUG_OUTPUT 1 +#define PCOUNT 2 + +const LADSPA_PortDescriptor p_discs[PCOUNT] = { + LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO, + LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO +}; +const LADSPA_PortRangeHint p_hints[PCOUNT] = { + {.HintDescriptor = 0}, + {.HintDescriptor = 0} +}; +const char *p_names[PCOUNT] = {"Input", "Output"}; + +typedef struct { + LADSPA_Data *input; + LADSPA_Data *output; + LADSPA_Data fs; +} plug_t; + +static void +plug_cleanup(LADSPA_Handle instance) { + free(instance); +} + +static void +plug_connect(LADSPA_Handle instance, ulong port, LADSPA_Data *data) { + plug_t *plug = (plug_t *)instance; + if (port == PLUG_INPUT) + plug->input = data; + else if (port == PLUG_OUTPUT) + plug->output = data; +} + +static LADSPA_Handle +plug_instantiate(const LADSPA_Descriptor *descriptor, ulong s_rate) { + plug_t *plug = (plug_t *) calloc(1, sizeof(plug_t)); + plug->fs = s_rate; + return (LADSPA_Handle) plug; +} + +static void +plug_run(LADSPA_Handle instance, ulong sample_count) { + plug_t *plug = (plug_t *) instance; + //const LADSPA_Data *input = plug->input; + LADSPA_Data *output = plug->output; + + for (ulong pos = 0; pos < sample_count; pos++) + output[pos] = whitenoise(); +} + +static const LADSPA_Descriptor eqDescriptor = { + .UniqueID = 0xEC57A71C, + .Label = "crap_noise", + .Properties = 0, + .Name = "crap noise generator", + .Maker = "Connor Olding", + .Copyright = "MIT", + .PortCount = PCOUNT, + .PortDescriptors = p_discs, + .PortRangeHints = p_hints, + .PortNames = p_names, + + .activate = NULL, + .cleanup = plug_cleanup, + .connect_port = plug_connect, + .deactivate = NULL, + .instantiate = plug_instantiate, + .run = plug_run, + .run_adding = NULL, + .set_run_adding_gain = NULL +}; + +const LADSPA_Descriptor * +ladspa_descriptor(ulong index) { + if (index != 0) + return NULL; + return &eqDescriptor; +} diff --git a/crap_util.h b/crap_util.h index 8dc1c26..3d84d2d 100644 --- a/crap_util.h +++ b/crap_util.h @@ -23,6 +23,9 @@ typedef struct { double b0, b1, b2, a0, a1, a2; } biquad_interim; +static float +whitenoise(); + static void biquad_init(biquad *bq); diff --git a/crap_util_def.h b/crap_util_def.h index 78da2e8..9b32c5e 100644 --- a/crap_util_def.h +++ b/crap_util_def.h @@ -2,6 +2,20 @@ #include #include +/* via http://www.rgba.org/articles/sfrand/sfrand.htm */ +static unsigned int mirand = 1; + +static float +whitenoise() { + union either { + float f; + unsigned int i; + } white; + mirand *= 16807; + white.i = (mirand & 0x007FFFFF) | 0x40000000; + return white.f - 3; +} + /* used to resemble https://github.com/swh/ladspa/blob/master/util/biquad.h */ static void