prosperon/source/engine/sound/dsp.h

127 lines
3.5 KiB
C
Raw Normal View History

2022-07-04 14:19:52 -05:00
#ifndef DSP_H
#define DSP_H
2024-04-10 16:21:46 -05:00
extern int SAMPLERATE;
extern int BUF_FRAMES;
extern int CHANNELS;
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"
2023-11-28 22:48:32 -06:00
#include "iir.h"
2023-11-27 14:29:55 -06:00
/* 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);
2024-04-10 16:21:46 -05:00
soundbyte *cache;
2023-11-27 14:29:55 -06:00
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);
2023-11-28 22:48:32 -06:00
dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int samples), void (*fr)(void *data));
2023-11-27 14:29:55 -06:00
void plugin_node(dsp_node *from, dsp_node *to);
void unplug_node(dsp_node *node);
void node_free(dsp_node *node);
2023-11-29 07:32:32 -06:00
void dsp_node_free(dsp_node *node);
2023-11-28 22:48:32 -06:00
void filter_iir(struct dsp_iir *iir, soundbyte *buffer, int frames);
2023-11-27 14:29:55 -06:00
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 */
2024-05-21 18:50:53 -05:00
typedef struct dsp_adsr {
2022-07-10 17:59:15 -05:00
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;
2024-05-21 18:50:53 -05:00
} adsr;
2022-07-10 17:59:15 -05:00
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);
2023-11-28 22:48:32 -06:00
dsp_node *dsp_fwd_delay(double sec, double decay);
dsp_node *dsp_pitchshift(float octaves);
2022-07-06 17:17:06 -05:00
2024-05-21 18:50:53 -05:00
typedef 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;
2024-05-21 18:50:53 -05:00
} compressor;
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();
2023-11-28 22:48:32 -06:00
dsp_node *dsp_rednoise();
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);
2024-05-21 18:50:53 -05:00
typedef struct {
float amp;
float freq;
float phase; /* from 0 to 1, marking where we are */
float (*filter)(float phase);
} phasor;
typedef struct bitcrush {
float sr;
float depth;
} bitcrush;
2023-11-27 14:29:55 -06:00
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);
#endif