shithub: neindaw

ref: 79bf1a7b1c60aa3efc5c6fad853294eba8c88d2f
dir: /dsp/kick_drum.c/

View raw version
/* ------------------------------------------------------------
name: "Kick Drum"
Code generated with Faust 2.23.10 (https://faust.grame.fr)
Compilation options: -lang c -scal -ftz 0
------------------------------------------------------------ */

#ifndef  __KickDrum_H__
#define  __KickDrum_H__

/* KickDrum Plan 9 C architecture for Faust. */

#include <u.h>
#include <libc.h>
#include "uiglue.h"

#define max(x,y) (((x) > (y)) ? (x) : (y))
#define min(x,y) (((x) < (y)) ? (x) : (y))

#ifndef FAUSTFLOAT
#define FAUSTFLOAT float
#endif 

#ifdef __cplusplus
extern "C" {
#endif

#include <math.h>
#include <stdlib.h>

typedef struct {
	int iRec0[2];
} KickDrumSIG0;

static KickDrumSIG0* newKickDrumSIG0(void) { return (KickDrumSIG0*)calloc(1, sizeof(KickDrumSIG0)); }
static void deleteKickDrumSIG0(KickDrumSIG0* dsp) {
	USED(dsp); free(dsp); }

int getNumInputsKickDrumSIG0(KickDrumSIG0* dsp) {
	USED(dsp);
	return 0;
}
int getNumOutputsKickDrumSIG0(KickDrumSIG0* dsp) {
	USED(dsp);
	return 1;
}
int getInputRateKickDrumSIG0(KickDrumSIG0* dsp, int channel) {
	USED(dsp);
	int rate;
	switch ((channel)) {
		default: {
			rate = -1;
			break;
		}
	}
	return rate;
}
int getOutputRateKickDrumSIG0(KickDrumSIG0* dsp, int channel) {
	USED(dsp);
	int rate;
	switch ((channel)) {
		case 0: {
			rate = 0;
			break;
		}
		default: {
			rate = -1;
			break;
		}
	}
	return rate;
}

static void instanceInitKickDrumSIG0(KickDrumSIG0* dsp, int sample_rate) {
	USED(sample_rate);
	USED(dsp);
	/* C99 loop */
	{
		int l0;
		for (l0 = 0; (l0 < 2); l0 = (l0 + 1)) {
			dsp->iRec0[l0] = 0;
		}
	}
}

static void fillKickDrumSIG0(KickDrumSIG0* dsp, int count, float* table) {
	USED(dsp);
	/* C99 loop */
	{
		int i;
		for (i = 0; (i < count); i = (i + 1)) {
			dsp->iRec0[0] = (dsp->iRec0[1] + 1);
			table[i] = sinf((9.58738019e-05f * (float)(dsp->iRec0[0] + -1)));
			dsp->iRec0[1] = dsp->iRec0[0];
		}
	}
}

static float ftbl0KickDrumSIG0[65536];

#ifndef FAUSTCLASS 
#define FAUSTCLASS KickDrum
#endif
#ifdef __APPLE__ 
#define exp10f __exp10f
#define exp10 __exp10
#endif

typedef struct {
	FAUSTFLOAT fHslider0;
	int fSampleRate;
	float fConst0;
	float fConst1;
	FAUSTFLOAT fHslider1;
	FAUSTFLOAT fCheckbox0;
	FAUSTFLOAT fHslider2;
	FAUSTFLOAT fHslider3;
	FAUSTFLOAT fButton0;
	float fVec0[2];
	int iRec2[2];
	FAUSTFLOAT fHslider4;
	float fRec1[2];
	FAUSTFLOAT fHslider5;
	FAUSTFLOAT fHslider6;
} KickDrum;

KickDrum* newKickDrum(void) { 
	KickDrum* dsp = (KickDrum*)calloc(1, sizeof(KickDrum));
	return dsp;
}

void deleteKickDrum(KickDrum* dsp) {
	USED(dsp); 
	free(dsp);
}

void metadataKickDrum(MetaGlue* m) { 
	m->declare(m->metaInterface, "basics.lib/name", "Faust Basic Element Library");
	m->declare(m->metaInterface, "basics.lib/version", "0.1");
	m->declare(m->metaInterface, "envelopes.lib/ar:author", "Yann Orlarey, Stéphane Letz");
	m->declare(m->metaInterface, "envelopes.lib/author", "GRAME");
	m->declare(m->metaInterface, "envelopes.lib/copyright", "GRAME");
	m->declare(m->metaInterface, "envelopes.lib/license", "LGPL with exception");
	m->declare(m->metaInterface, "envelopes.lib/name", "Faust Envelope Library");
	m->declare(m->metaInterface, "envelopes.lib/version", "0.1");
	m->declare(m->metaInterface, "filename", "kick_drum.dsp");
	m->declare(m->metaInterface, "group", "Synthesis");
	m->declare(m->metaInterface, "maths.lib/author", "GRAME");
	m->declare(m->metaInterface, "maths.lib/copyright", "GRAME");
	m->declare(m->metaInterface, "maths.lib/license", "LGPL with exception");
	m->declare(m->metaInterface, "maths.lib/name", "Faust Math Library");
	m->declare(m->metaInterface, "maths.lib/version", "2.3");
	m->declare(m->metaInterface, "name", "Kick Drum");
	m->declare(m->metaInterface, "oscillators.lib/name", "Faust Oscillator Library");
	m->declare(m->metaInterface, "oscillators.lib/version", "0.1");
	m->declare(m->metaInterface, "platform.lib/name", "Generic Platform Library");
	m->declare(m->metaInterface, "platform.lib/version", "0.1");
}

int getSampleRateKickDrum(KickDrum* dsp) {
	USED(dsp);
	return dsp->fSampleRate;
}

int getNumInputsKickDrum(KickDrum* dsp) {
	USED(dsp);
	return 0;
}
int getNumOutputsKickDrum(KickDrum* dsp) {
	USED(dsp);
	return 1;
}
int getInputRateKickDrum(KickDrum* dsp, int channel) {
	USED(dsp);
	int rate;
	switch ((channel)) {
		default: {
			rate = -1;
			break;
		}
	}
	return rate;
}
int getOutputRateKickDrum(KickDrum* dsp, int channel) {
	USED(dsp);
	int rate;
	switch ((channel)) {
		case 0: {
			rate = 1;
			break;
		}
		default: {
			rate = -1;
			break;
		}
	}
	return rate;
}

void classInitKickDrum(int sample_rate) {
	USED(sample_rate);
	KickDrumSIG0* sig0 = newKickDrumSIG0();
	instanceInitKickDrumSIG0(sig0, sample_rate);
	fillKickDrumSIG0(sig0, 65536, ftbl0KickDrumSIG0);
	deleteKickDrumSIG0(sig0);
}

void instanceResetUserInterfaceKickDrum(KickDrum* dsp) {
	USED(dsp);
	dsp->fHslider0 = (FAUSTFLOAT)1.0f;
	dsp->fHslider1 = (FAUSTFLOAT)100.0f;
	dsp->fCheckbox0 = (FAUSTFLOAT)0.0f;
	dsp->fHslider2 = (FAUSTFLOAT)-5.0f;
	dsp->fHslider3 = (FAUSTFLOAT)0.001f;
	dsp->fButton0 = (FAUSTFLOAT)0.0f;
	dsp->fHslider4 = (FAUSTFLOAT)0.001f;
	dsp->fHslider5 = (FAUSTFLOAT)0.001f;
	dsp->fHslider6 = (FAUSTFLOAT)0.001f;
}

void instanceClearKickDrum(KickDrum* dsp) {
	USED(dsp);
	/* C99 loop */
	{
		int l1;
		for (l1 = 0; (l1 < 2); l1 = (l1 + 1)) {
			dsp->fVec0[l1] = 0.0f;
		}
	}
	/* C99 loop */
	{
		int l2;
		for (l2 = 0; (l2 < 2); l2 = (l2 + 1)) {
			dsp->iRec2[l2] = 0;
		}
	}
	/* C99 loop */
	{
		int l3;
		for (l3 = 0; (l3 < 2); l3 = (l3 + 1)) {
			dsp->fRec1[l3] = 0.0f;
		}
	}
}

void instanceConstantsKickDrum(KickDrum* dsp, int sample_rate) {
	USED(sample_rate);
	USED(dsp);
	dsp->fSampleRate = sample_rate;
	dsp->fConst0 = fminf(192000.0f, fmaxf(1.0f, (float)dsp->fSampleRate));
	dsp->fConst1 = (1.0f / dsp->fConst0);
}

void instanceInitKickDrum(KickDrum* dsp, int sample_rate) {
	USED(sample_rate);
	USED(dsp);
	instanceConstantsKickDrum(dsp, sample_rate);
	instanceResetUserInterfaceKickDrum(dsp);
	instanceClearKickDrum(dsp);
}

void initKickDrum(KickDrum* dsp, int sample_rate) {
	USED(sample_rate);
	USED(dsp);
	classInitKickDrum(sample_rate);
	instanceInitKickDrum(dsp, sample_rate);
}

void buildUserInterfaceKickDrum(KickDrum* dsp, UIGlue* ui_interface) {
	USED(dsp);
	ui_interface->openVerticalBox(ui_interface->uiInterface, "Kick Drum");
	ui_interface->openVerticalBox(ui_interface->uiInterface, "A");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider1, "0", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider1, "unit", "Hz");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Frequency", &dsp->fHslider1, 100.0f, 5.0f, 200.0f, 1.0f);
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider5, "1", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider5, "unit", "s");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Attack", &dsp->fHslider5, 0.00100000005f, 9.99999975e-06f, 0.200000003f, 0.00100000005f);
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider6, "3", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider6, "unit", "s");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Release", &dsp->fHslider6, 0.00100000005f, 9.99999975e-06f, 1.0f, 0.00100000005f);
	ui_interface->closeBox(ui_interface->uiInterface);
	ui_interface->openVerticalBox(ui_interface->uiInterface, "B");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider2, "0", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider2, "unit", "Hz");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Frequency", &dsp->fHslider2, -5.0f, -200.0f, 200.0f, 1.0f);
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider3, "1", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider3, "unit", "s");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Attack", &dsp->fHslider3, 0.00100000005f, 9.99999975e-06f, 0.200000003f, 0.00100000005f);
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider4, "2", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider4, "unit", "s");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Release", &dsp->fHslider4, 0.00100000005f, 9.99999975e-06f, 1.0f, 0.00100000005f);
	ui_interface->declare(ui_interface->uiInterface, &dsp->fCheckbox0, "3", "");
	ui_interface->addCheckButton(ui_interface->uiInterface, "Enable", &dsp->fCheckbox0);
	ui_interface->closeBox(ui_interface->uiInterface);
	ui_interface->openVerticalBox(ui_interface->uiInterface, "Control");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider0, "0", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fHslider0, "style", "knob");
	ui_interface->addHorizontalSlider(ui_interface->uiInterface, "Gain", &dsp->fHslider0, 1.0f, 0.0f, 1.0f, 0.00999999978f);
	ui_interface->declare(ui_interface->uiInterface, &dsp->fButton0, "1", "");
	ui_interface->declare(ui_interface->uiInterface, &dsp->fButton0, "autovoice", "");
	ui_interface->addButton(ui_interface->uiInterface, "Gate", &dsp->fButton0);
	ui_interface->closeBox(ui_interface->uiInterface);
	ui_interface->closeBox(ui_interface->uiInterface);
}

void computeKickDrum(KickDrum* dsp, int count, FAUSTFLOAT** inputs, FAUSTFLOAT** outputs) {
	USED(inputs);
	USED(dsp);
	FAUSTFLOAT* output0 = outputs[0];
	float fSlow0 = (float)dsp->fHslider0;
	float fSlow1 = (float)dsp->fHslider1;
	float fSlow2 = ((float)dsp->fCheckbox0 * (float)dsp->fHslider2);
	float fSlow3 = fmaxf(1.0f, (dsp->fConst0 * (float)dsp->fHslider3));
	float fSlow4 = (1.0f / fSlow3);
	float fSlow5 = (float)dsp->fButton0;
	float fSlow6 = (1.0f / fmaxf(1.0f, (dsp->fConst0 * (float)dsp->fHslider4)));
	float fSlow7 = fmaxf(1.0f, (dsp->fConst0 * (float)dsp->fHslider5));
	float fSlow8 = (1.0f / fSlow7);
	float fSlow9 = (1.0f / fmaxf(1.0f, (dsp->fConst0 * (float)dsp->fHslider6)));
	/* C99 loop */
	{
		int i;
		for (i = 0; (i < count); i = (i + 1)) {
			dsp->fVec0[0] = fSlow5;
			dsp->iRec2[0] = ((fSlow5 > dsp->fVec0[1]) + ((fSlow5 <= dsp->fVec0[1]) * (dsp->iRec2[1] + (dsp->iRec2[1] > 0))));
			float fTemp0 = (float)dsp->iRec2[0];
			float fTemp1 = (dsp->fConst1 * (fSlow1 + (fSlow2 * fmaxf(0.0f, fminf((fSlow4 * fTemp0), (1.0f - (fSlow6 * (fTemp0 - fSlow3))))))));
			float fTemp2 = (fSlow5 - dsp->fVec0[1]);
			float fTemp3 = (dsp->fRec1[1] * ((float)((fTemp2 * (float)(fTemp2 > 0.0f)) > 0.0f) + -1.0f));
			dsp->fRec1[0] = (fTemp1 - (fTemp3 + floorf((fTemp1 - fTemp3))));
			output0[i] = (FAUSTFLOAT)(fSlow0 * (ftbl0KickDrumSIG0[(int)(65536.0f * dsp->fRec1[0])] * fmaxf(0.0f, fminf((fSlow8 * fTemp0), ((fSlow9 * (fSlow7 - fTemp0)) + 1.0f)))));
			dsp->fVec0[1] = dsp->fVec0[0];
			dsp->iRec2[1] = dsp->iRec2[0];
			dsp->fRec1[1] = dsp->fRec1[0];
		}
	}
}

#ifdef __cplusplus
}
#endif

static KickDrum *cloneKickDrum(KickDrum *dsp) {
	return memmove(newKickDrum(), dsp, sizeof(*dsp));
}

static void *stateKickDrum(KickDrum *dsp, int *sz) {
	*sz = sizeof(*dsp);
	return dsp;
}

#define DSP KickDrum

#include "dspf.h"

static DSPf dspf = {
	.new = newKickDrum,
	.clone = cloneKickDrum,
	.state = stateKickDrum,
	.init = instanceInitKickDrum,
	.delete = deleteKickDrum,
	.metadata = metadataKickDrum,
	.num_in = getNumInputsKickDrum,
	.num_out = getNumOutputsKickDrum,
	.clear = instanceClearKickDrum,
	.reset_ui = instanceResetUserInterfaceKickDrum,
	.build_ui = buildUserInterfaceKickDrum,
	.compute = computeKickDrum,
};

void *
class_init(int rate)
{
	classInitKickDrum(rate);
	return &dspf;
}

#endif