fourdgsQuantizeSh function

int fourdgsQuantizeSh(
  1. int value,
  2. int bits
)

Round one coefficient byte onto the grid bits names, at bin centres.

Centring is what makes the bound half the pitch rather than the whole of it, and it keeps the operation idempotent: a coefficient already on the grid is left alone, so re-encoding a file at the depth it already carries changes no byte.

Implementation

int fourdgsQuantizeSh(int value, int bits) {
  final step = fourdgsShStep(bits);
  if (step == 1) return value;
  return (value ~/ step) * step + step ~/ 2;
}