From 434b8be1518a4e706c1a88d934cae54e0b8540dd Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 4 Jul 2022 19:19:52 +0000 Subject: [PATCH] DSP --- source/engine/dsp.c | 87 +++++++++++++++++++++++++++++++++++++++++++ source/engine/dsp.h | 12 ++++++ source/engine/sound.c | 9 +++-- 3 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 source/engine/dsp.c create mode 100644 source/engine/dsp.h diff --git a/source/engine/dsp.c b/source/engine/dsp.c new file mode 100644 index 0000000..30d832a --- /dev/null +++ b/source/engine/dsp.c @@ -0,0 +1,87 @@ +#include "dsp.h" + +#include "sound.h" +#include "limits.h" +#include "math.h" + +#define PI 3.14159265 + +void am_mod(short *a, short *b, short *c, int n) +{ + for (int i = 0; i < n; i++) { + c[i] = (a[i]*b[i])>>15; + } +} + +static struct wav make_wav(float freq, int sr, int ch) { + struct wav new; + new.ch = ch; + new.samplerate = sr; + new.frames = sr/freq; + new.data = calloc(new.frames*new.ch, sizeof(short)); + + return new; +} + +struct wav gen_sine(float amp, float freq, int sr, int ch) +{ + struct wav new = make_wav(freq, sr, ch); + + if (amp > 1) amp = 1; + if (amp < 0) amp = 0; + short samp = amp*SHRT_MAX; + + short *data = (short*)new.data; + + for (int i = 0; i < new.frames; i++) { + for (int j = 0; j < new.ch; j++) { + data[i+j] =amp * sin(2*PI*((float)i / new.frames)); + } + } + + return new; +} + +struct wav gen_square(float amp, float freq, int sr, int ch) +{ + struct wav new = make_wav(freq, sr, ch); + + int crossover = new.frames/2; + + if (amp > 1) amp = 1; + if (amp < 0) amp = 0; + + short samp = amp * SHRT_MAX; + + short *data = (short*)new.data; + + for (int i = 0; i < crossover; i++) { + for (int j = 0; j < new.ch; j++) { + data[i+j] = samp; + } + } + + for (int i = crossover; i < new.frames; i++) { + for (int j = 0; j < new.ch; j++) { + data[i+j] = samp * -1; + } + } + + return new; +} + +struct wav gen_triangle(float amp, float freq, int sr, int ch) +{ + struct wav new = make_wav(freq, sr, ch); + + if (amp > 1) amp = 1; + if (amp < 0) amp = 0; + + + + for (int i = 0; i < new.frames; i++) { + for (int j = 0; j < new.ch; j++) { + //new.data[i+j] = + } + } +} \ No newline at end of file diff --git a/source/engine/dsp.h b/source/engine/dsp.h new file mode 100644 index 0000000..41c1be6 --- /dev/null +++ b/source/engine/dsp.h @@ -0,0 +1,12 @@ +#ifndef DSP_H +#define DSP_H + +void am_mod(short *a, short *b, short *c, int n); + +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); + +#endif \ No newline at end of file diff --git a/source/engine/sound.c b/source/engine/sound.c index 6da07f2..226ac90 100755 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -7,6 +7,8 @@ #include "limits.h" #include "time.h" +#include "dsp.h" + #define DR_WAV_IMPLEMENTATION #include "dr_wav.h" @@ -133,15 +135,16 @@ void sound_init() printf("Loaded wav: ch %i, sr %i, fr %i.\n", mwav.ch, mwav.samplerate, mwav.frames); + mwav = gen_square(1, 150, 48000, 2); + +/* short *tdata = mwav.data; mwav.frames /= 2; short *newdata = calloc(mwav.frames * 2, sizeof(short)); for (int i = 0; i < mwav.frames; i++) { newdata[i] = tdata[i*2]; } - - free(mwav.data); - mwav.data = newdata; +*/ wavsound.data = &mwav; wavsound.loop = 1;