// an efficient noise shaper to get the maximum from low cost I2S DACs
// even if using 16 bit mode.
class NoiseShaper{
public:
float dc=0;
int32_t rnd=0;
float e=0;
int16_t s=1;
int16_t quands(float x) {
// DC remove
dc+=0.00261f*(x-dc);
x-=dc;
// saturation [-1 +1]
if(x>1) x=1;
else if(x<-1) x=-1;
//pseudo random generator
rnd=rnd*69069+1;
// 16bit scaling
float v=x*32760;
// error feedback + slight random dithering
// to avoid repetitive patterns
float vFbDither = v - e + 0.06f*q_to_float(rnd,32);
int16_t iv=float_to_s16(vFbDither,0);
// safe guard against clicky silence detectors when using PCM5102 <-----------------------------------
if(iv==0) {
iv=s;
s=-s;
}
// error used for feedback dithering
e=iv-v;
return iv;
}
};