2022-07-04 14:19:52 -05:00
|
|
|
#ifndef DSP_H
|
|
|
|
#define DSP_H
|
|
|
|
|
2023-08-31 13:00:33 -05:00
|
|
|
#define SAMPLERATE 44100
|
2023-09-04 01:20:55 -05:00
|
|
|
#define BUF_FRAMES 2048 /* At 48k, 128 needed for 240fps consistency */
|
2022-07-06 17:17:06 -05:00
|
|
|
#define CHANNELS 2
|
2023-01-16 13:20:07 -06:00
|
|
|
|
2023-08-30 18:22:32 -05:00
|
|
|
#include "sound.h"
|
2023-11-27 14:29:55 -06:00
|
|
|
#include "cbuf.h"
|
|
|
|
#include "script.h"
|
|
|
|
|
|
|
|
/* a DSP node, when processed, sums its inputs, and stores the result of proc in its cache */
|
|
|
|
typedef struct dsp_node {
|
|
|
|
void (*proc)(void *dsp, soundbyte *buf, int samples); /* processor */
|
|
|
|
void *data; /* Node specific data to use in the proc function, passed in as dsp */
|
|
|
|
void (*data_free)(void *data);
|
|
|
|
soundbyte cache[BUF_FRAMES*CHANNELS]; /* Cached process */
|
|
|
|
struct dsp_node **ins; /* Array of in nodes */
|
|
|
|
struct dsp_node *out; /* node this one is connected to */
|
|
|
|
int pass; /* True if the filter should be bypassed */
|
|
|
|
int off; /* True if the filter shouldn't output */
|
|
|
|
float gain; /* Between 0 and 1, to attenuate this output */
|
|
|
|
float pan; /* Between -100 and +100, panning left to right in the speakers */
|
|
|
|
} dsp_node;
|
|
|
|
|
|
|
|
void dsp_init();
|
|
|
|
|
|
|
|
/* Get the output of a node */
|
|
|
|
soundbyte *dsp_node_out(dsp_node *node);
|
|
|
|
void dsp_node_run(dsp_node *node);
|
|
|
|
dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int samples));
|
|
|
|
void plugin_node(dsp_node *from, dsp_node *to);
|
|
|
|
void unplug_node(dsp_node *node);
|
|
|
|
void node_free(dsp_node *node);
|
|
|
|
|
|
|
|
void scale_soundbytes(soundbyte *a, float scale, int frames);
|
|
|
|
void sum_soundbytes(soundbyte *a, soundbyte *b, int frames);
|
|
|
|
void zero_soundbytes(soundbyte *a, int frames);
|
|
|
|
void set_soundbytes(soundbyte *a, soundbyte *b, int frames);
|
|
|
|
|
|
|
|
dsp_node *dsp_mixer_node();
|
|
|
|
dsp_node *dsp_am_mod(dsp_node *mod);
|
|
|
|
dsp_node *dsp_rectify();
|
|
|
|
|
|
|
|
extern dsp_node *masterbus;
|
|
|
|
|
|
|
|
dsp_node *dsp_hpf(float freq);
|
|
|
|
dsp_node *dsp_lpf(float freq);
|
2022-07-08 13:54:45 -05:00
|
|
|
|
2022-07-10 17:59:15 -05:00
|
|
|
/* atk, dec, sus, rls specify the time, in miliseconds, the phase begins */
|
|
|
|
struct dsp_adsr {
|
|
|
|
unsigned int atk;
|
|
|
|
double atk_t;
|
|
|
|
unsigned int dec;
|
|
|
|
double dec_t;
|
|
|
|
unsigned int sus;
|
|
|
|
float sus_pwr; // Between 0 and 1
|
|
|
|
unsigned int rls;
|
|
|
|
double rls_t;
|
|
|
|
|
|
|
|
double time; /* Current time of the filter */
|
|
|
|
float out;
|
|
|
|
};
|
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
dsp_node *dsp_adsr(unsigned int atk, unsigned int dec, unsigned int sus, unsigned int rls);
|
2022-07-10 17:59:15 -05:00
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
typedef struct {
|
|
|
|
unsigned int ms_delay;
|
|
|
|
float decay; /* Each echo should be multiplied by this number */
|
|
|
|
soundbyte *ring;
|
|
|
|
} delay;
|
2022-07-06 17:17:06 -05:00
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
dsp_node *dsp_delay(double sec, double decay);
|
2022-07-06 17:17:06 -05:00
|
|
|
|
|
|
|
struct dsp_compressor {
|
2022-07-11 23:21:57 -05:00
|
|
|
double ratio;
|
|
|
|
double threshold;
|
|
|
|
float target;
|
|
|
|
unsigned int atk; /* Milliseconds */
|
|
|
|
double atk_tau;
|
|
|
|
unsigned int rls; /* MIlliseconds */
|
|
|
|
double rls_tau;
|
2022-07-06 17:17:06 -05:00
|
|
|
};
|
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
dsp_node *dsp_compressor();
|
2022-07-10 17:59:15 -05:00
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
dsp_node *dsp_limiter(float ceil);
|
|
|
|
dsp_node *dsp_noise_gate(float floor);
|
2022-07-06 17:17:06 -05:00
|
|
|
|
|
|
|
struct phasor phasor_make(unsigned int sr, float freq);
|
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
dsp_node *dsp_whitenoise();
|
|
|
|
dsp_node *dsp_pinknoise();
|
2022-07-06 17:17:06 -05:00
|
|
|
|
|
|
|
float sin_phasor(float p);
|
|
|
|
float square_phasor(float p);
|
|
|
|
float saw_phasor(float p);
|
|
|
|
float tri_phasor(float p);
|
|
|
|
|
2023-11-27 14:29:55 -06:00
|
|
|
dsp_node *dsp_reverb();
|
|
|
|
dsp_node *dsp_sinewave(float amp, float freq);
|
|
|
|
dsp_node *dsp_square(float amp, float freq, int sr, int ch);
|
|
|
|
dsp_node *dsp_bitcrush(float sr, float res);
|
2023-08-30 18:22:32 -05:00
|
|
|
void dsp_mono(void *p, soundbyte *out, int n);
|
2023-11-27 14:29:55 -06:00
|
|
|
void pan_frames(soundbyte *out, float deg, int frames);
|
2022-12-14 13:01:42 -06:00
|
|
|
|
2022-11-25 07:12:31 -06:00
|
|
|
#endif
|