From 7341d8a8cfd1989eb49b21eedea0808b0e47b5d8 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 5 Jul 2022 02:44:39 +0000 Subject: [PATCH] synths --- source/engine/dsp.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/source/engine/dsp.c b/source/engine/dsp.c index 30d832a..b8d9b5a 100644 --- a/source/engine/dsp.c +++ b/source/engine/dsp.c @@ -34,8 +34,9 @@ struct wav gen_sine(float amp, float freq, int sr, int ch) short *data = (short*)new.data; for (int i = 0; i < new.frames; i++) { + short val = amp * sin(2*PI*((float)i / new.frames)); for (int j = 0; j < new.ch; j++) { - data[i+j] =amp * sin(2*PI*((float)i / new.frames)); + data[i+j] = val; } } @@ -55,15 +56,10 @@ struct wav gen_square(float amp, float freq, int sr, int ch) short *data = (short*)new.data; - for (int i = 0; i < crossover; i++) { + for (int i = 0; i < new.frames; i++) { + short val = -2 * floor(2 * i / new.frames) + 1; 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; + data[i+j] = val; } } @@ -77,11 +73,25 @@ struct wav gen_triangle(float amp, float freq, int sr, int ch) if (amp > 1) amp = 1; if (amp < 0) amp = 0; + for (int i = 0; i < new.frames; i++) { + short val = 2 * abs( (i/new.frames) - floor( (i/new.frames) + 0.5)); + for (int j = 0; j < new.ch; j++) { + new.data[i+j] = val; + } + } +} +struct wav gen_saw(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++) { + short val = 2 * ( (i/new.frames) - floor( (0.5 + (i/new.frames)))); for (int j = 0; j < new.ch; j++) { - //new.data[i+j] = + new.data[i+j] = val; } } } \ No newline at end of file