DSP
This commit is contained in:
parent
de364d3538
commit
434b8be151
87
source/engine/dsp.c
Normal file
87
source/engine/dsp.c
Normal file
|
@ -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] =
|
||||
}
|
||||
}
|
||||
}
|
12
source/engine/dsp.h
Normal file
12
source/engine/dsp.h
Normal file
|
@ -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
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue