prosperon/source/engine/sound/dsp.h

161 lines
3.8 KiB
C
Raw Normal View History

2022-07-04 14:19:52 -05:00
#ifndef DSP_H
#define DSP_H
#define SAMPLERATE 44100
#define BUF_FRAMES 2048 /* At 48k, 128 needed for 240fps consistency */
2022-07-06 17:17:06 -05:00
#define CHANNELS 2
2023-08-30 18:22:32 -05:00
#include "sound.h"
2023-05-30 11:39:22 -05:00
//#include "circbuf.h"
2022-07-05 15:12:48 -05:00
2022-08-01 13:32:58 -05:00
struct dsp_iir;
2022-07-06 17:17:06 -05:00
2023-08-30 18:22:32 -05:00
void dsp_rectify(soundbyte *in, soundbyte *out, int n);
2022-07-04 14:19:52 -05:00
2022-07-05 15:12:48 -05:00
struct dsp_filter {
2023-08-30 18:22:32 -05:00
void (*filter)(void *data, soundbyte *out, int samples);
2022-07-05 15:12:48 -05:00
void *data;
2022-07-09 21:46:23 -05:00
int inputs;
struct dsp_filter *in[6];
struct bus *bus;
2022-07-08 13:54:45 -05:00
2023-08-30 18:22:32 -05:00
soundbyte cache[CHANNELS*BUF_FRAMES];
2022-07-09 21:46:23 -05:00
int dirty;
2022-07-08 13:54:45 -05:00
};
2023-08-30 18:22:32 -05:00
struct dsp_filter dsp_filter(void *data, void (*filter)(void *data, soundbyte *out, int samples));
2022-07-12 15:43:02 -05:00
2022-07-08 13:54:45 -05:00
struct dsp_fir {
float freq;
int n;
int head;
float *cof;
float *dx;
struct dsp_filter in;
};
2022-07-10 13:04:24 -05:00
void dsp_filter_addin(struct dsp_filter filter, struct dsp_filter *in);
2022-07-09 21:46:23 -05:00
2022-07-08 13:54:45 -05:00
struct dsp_filter lp_fir_make(float freq);
2023-08-30 18:22:32 -05:00
void dsp_iir_fillbuf(struct dsp_iir *iir, soundbyte *out, int n);
2022-07-08 13:54:45 -05:00
struct dsp_filter hpf_make(int poles, float freq);
struct dsp_filter lpf_make(int poles, float freq);
struct dsp_filter bpf_make(int poles, float freq1, float freq2);
struct dsp_filter npf_make(int poles, float freq1, float freq2);
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-08-30 18:22:32 -05:00
void dsp_adsr_fillbuf(struct dsp_adsr *adsr, soundbyte *out, int n);
2022-07-10 17:59:15 -05:00
struct dsp_filter make_adsr(unsigned int atk, unsigned int dec, unsigned int sus, unsigned int rls);
2022-07-05 15:12:48 -05:00
struct dsp_delay {
unsigned int ms_delay;
2023-05-30 11:39:22 -05:00
// struct circbuf buf;
2022-07-06 17:17:06 -05:00
struct dsp_filter in;
};
struct dsp_delay dsp_delay_make(unsigned int ms_delay);
2023-08-30 18:22:32 -05:00
void dsp_delay_filbuf(struct dsp_delay *delay, soundbyte *buf, int n);
2022-07-06 17:17:06 -05:00
struct dsp_ammod {
struct dsp_filter ina;
struct dsp_filter inb;
2023-08-30 18:22:32 -05:00
soundbyte abuf[BUF_FRAMES*CHANNELS];
soundbyte bbuf[BUF_FRAMES*CHANNELS];
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
};
2022-07-10 17:59:15 -05:00
struct dsp_filter dsp_make_compressor();
2023-08-30 18:22:32 -05:00
void dsp_compressor_fillbuf(struct dsp_compressor *comp, soundbyte *out, int n);
2022-07-10 17:59:15 -05:00
2022-07-06 17:17:06 -05:00
struct dsp_limiter {
};
2022-07-10 17:59:15 -05:00
struct dsp_filter dsp_make_limiter();
2023-08-30 18:22:32 -05:00
void dsp_limiter_fillbuf(struct dsp_limiter *lim, soundbyte *out, int n);
2022-07-06 17:17:06 -05:00
struct phasor {
unsigned int sr;
float cur;
float freq;
unsigned int clen;
unsigned int cstep;
float *cache;
};
struct phasor phasor_make(unsigned int sr, float freq);
struct osc {
float (*f)(float p);
struct phasor p;
unsigned int frames;
unsigned int frame;
2023-08-30 18:22:32 -05:00
soundbyte *cache;
2022-07-05 15:12:48 -05:00
};
2022-07-04 14:19:52 -05:00
struct wav;
struct wav gen_sine(float amp, float freq, int sr, int ch);
struct wav gen_square(float amp, float freq, int sr, int ch);
struct wav gen_triangle(float amp, float freq, int sr, int ch);
2022-07-05 15:12:48 -05:00
struct wav gen_saw(float amp, float freq, int sr, int ch);
2022-07-04 14:19:52 -05:00
2023-08-30 18:22:32 -05:00
void gen_whitenoise(void *data, soundbyte *out, int n);
void gen_pinknoise(void *data, soundbyte *out, int n);
2022-07-08 13:54:45 -05:00
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-08-30 18:22:32 -05:00
void osc_fillbuf(struct osc *osc, soundbyte *buf, int n);
2022-07-06 17:17:06 -05:00
2023-08-30 18:22:32 -05:00
void am_mod(struct dsp_ammod *mod, soundbyte *c, int n);
2022-07-06 17:17:06 -05:00
2022-07-10 19:29:52 -05:00
struct dsp_reverb {
unsigned int time; /* Time in miliseconds for the sound to decay out */
};
struct dsp_filter make_reverb();
2023-08-30 18:22:32 -05:00
void dsp_reverb_fillbuf(struct dsp_reverb *r, soundbyte *out, int n);
2022-07-10 19:29:52 -05:00
2023-08-30 18:22:32 -05:00
void dsp_pan(float *deg, soundbyte *out, int n);
2022-07-11 23:21:57 -05:00
2023-08-30 18:22:32 -05:00
void dsp_mono(void *p, soundbyte *out, int n);
2022-07-11 23:21:57 -05:00
2023-08-30 18:22:32 -05:00
void dsp_bitcrush(void *p, soundbyte *out, int n);
2022-07-12 15:43:02 -05:00
2023-08-30 18:22:32 -05:00
void dsp_run(struct dsp_filter filter, soundbyte *out, int n);
#endif