randomBits2 method

int randomBits2(
  1. int numBits,
  2. int amp
)

Implementation

int randomBits2(int numBits, int amp) {
  var diff = _table[_index1] - _table[_index2];
  if (diff < 0) {
    diff += (1 << 31);
  }

  _table[_index1] = diff;

  if (++_index1 == RANDOM_TABLE_SIZE) {
    _index1 = 0;
  }
  if (++_index2 == RANDOM_TABLE_SIZE) {
    _index2 = 0;
  }

  // sign-extend, 0-center
  diff = (diff << 1) >> (32 - numBits);
  // restrict range
  diff = (diff * amp) >> RANDOM_DITHER_FIX;
  // shift back to 0.5-center
  diff += 1 << (numBits - 1);

  return diff;
}