-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitcrush.c
43 lines (39 loc) · 937 Bytes
/
bitcrush.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include <math.h>
#include "soundpipe.h"
int sp_bitcrush_create(sp_bitcrush **p)
{
*p = malloc(sizeof(sp_bitcrush));
return SP_OK;
}
int sp_bitcrush_destroy(sp_bitcrush **p)
{
sp_bitcrush *pp = *p;
sp_fold_destroy(&pp->fold);
free(*p);
return SP_OK;
}
int sp_bitcrush_init(sp_data *sp, sp_bitcrush *p)
{
p->bitdepth = 8;
p->srate = 10000;
sp_fold_create(&p->fold);
sp_fold_init(sp, p->fold);
return SP_OK;
}
int sp_bitcrush_compute(sp_data *sp, sp_bitcrush *p, SPFLOAT *in, SPFLOAT *out)
{
SPFLOAT bits = pow(2, floor(p->bitdepth));
SPFLOAT foldamt = sp->sr / p->srate;
SPFLOAT sig;
*out = *in * 65536.0;
*out += 32768;
*out *= (bits / 65536.0);
*out = floor(*out);
*out = *out * (65536.0 / bits) - 32768;
sig = *out;
p->fold->incr = foldamt;
sp_fold_compute(sp, p->fold, &sig, out);
*out /= 65536.0;
return SP_OK;
}