adpcmobj.c 1.28 KB
#include <abi.h>
#include <stdio.h>

#define ADPCMVSIZE 16
#define FRAMEBYTES 9

static int segin;
static int dramin;
static int segstate;
static int statep;
static int lastsam;
static int first_time;

int adpcmsetdram(int seg, int dram)
{
  segin = seg;
  dramin = dram;
}

int adpcminit(int seg, int astate)
{
  first_time = 1;
  statep = astate;
  lastsam = 0;
  segstate = seg;
}

Acmd *adpcmdec(Acmd *cp, int dmemin, int dmemout, int nsam, 
	     unsigned char flags, int gain, int *outp)
{
  Acmd *cl = cp;
  int tsam;
  int nframes;
  int nbytes;
  
  if (lastsam)
    tsam = nsam - ADPCMVSIZE + lastsam;      /* How many to ask for */
  else
    tsam = nsam;
  nframes = (tsam+ADPCMVSIZE-1)/ADPCMVSIZE;

  nbytes = nframes*FRAMEBYTES + (dramin % 8);
  aSetBuffer(cl++, 0, dmemin, 0, nbytes + 8 - (nbytes % 8));
  aLoadBuffer(cl++, (segin<<26) + dramin - (dramin % 8));

  aSetBuffer(cl++, 0, dmemin + (dramin % 8), dmemout, tsam<<1);
  if (first_time){
    aADPCMdec(cl++, A_INIT | flags, gain, (segstate<<26) + statep);
    first_time=0;
  } else
    aADPCMdec(cl++, flags, gain, (segstate<<26) + statep);

  if (lastsam)
    *outp = (lastsam<<1) + dmemout;
  else
    *outp = dmemout + (ADPCMVSIZE<<1);

  lastsam = (nsam + lastsam) % ADPCMVSIZE;
  dramin += FRAMEBYTES*nframes;
  return(cl);
}