quant.c 723 Bytes
#include "vadpcm.h"

short
qsample(float x, int scale)
{
    if (x>0)
      return (short) (x/(float) scale + 0.4999999);
    else
      return (short) (x/(float) scale - 0.4999999);
}

void
clamp(int fs, float *e, int *ie, int bits)
{
  int i;
  float ulevel, llevel;
  llevel = - (float) (1L<<(bits-1));
  ulevel = -llevel - 1;
  
  for(i=0; i<fs; i++){
    if (e[i]>ulevel)
      e[i] = ulevel;
    if (e[i]<llevel)
      e[i] = llevel;
    if (e[i]>0)
      ie[i] = (int) (e[i]+0.5);
    else
      ie[i] = (int) (e[i]-0.5);
  }
}

int
clip(int ix, int llevel, int ulevel)

{
    if ((ix<llevel) || (ix>ulevel)){
        if (ix<llevel) return llevel;
        if (ix>ulevel) return ulevel;
    }
    return ix;
}