TonicAudio / Tonic

Easy and efficient audio synthesis in C++
The Unlicense
522 stars 63 forks source link

BitCrusher #239

Closed morganpackard closed 10 years ago

morganpackard commented 10 years ago

Snap values to an arbitrary bit depth.

I think the way to do this is to find the max int for a given bit depth, multiply the current sample by that maximum depth, cast to an int, then divide back down by that max value to get to a float again. Like this:

int bitDepth = 8;
int bitDepthMax = pow(2, bitDepth);
for( float* currentSample in buffer){
    int snapped = bitDepthMax *  (*currentSample);
    (*currentSample) = (float)snapped/bitDepthMax;
}

Any reason I should force the bit depth to be an int? Fractional bit depths doesn't actually make sense, of course, but it could potentially be interesting musically.

ndonald2 commented 10 years ago

That approach works. Fractional bit depths don't make sense in a conventional way, but you're right - that could be pretty interesting.

morganpackard commented 10 years ago

Ok. Wrote the class, but got tangled up in git/compile issues and don't want to jump out of creative time for organization time. Class is here: https://gist.github.com/morganpackard/5a45c855b81a7adca882

morganpackard commented 10 years ago

Done. Branch ready for merge.