areverb.c 4.28 KB
#include <abi.h>
#include <stdio.h>
#include <math.h>

/* Size value come from u-code assembly, locations are arbitrary,
   but should be used for load command in simulator.

   The command list is written by this program and sits right after
   the task header in DRAM, which is loaded at 0x00000000
*/

#define AUD_UCODE_POINTER 0x7000
#define AUD_UCODE_SIZE 4095         /* In bytes */

#define AUD_PDATA_POINTER 0x8000
#define AUD_PDATA_SIZE 1536          /* In bytes */


#define ADPCM_TABLE_LOC 48
#define ADPCM_TABLE_SIZE (8*16)

#define AUD_CL_POINTER (ADPCM_TABLE_LOC + ADPCM_TABLE_SIZE)

short adpcmtable[][16] = {
	{0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0,
         1920, 1800, 1688, 1582, 1483, 1390, 1304, 1222},
	{-1920, -3660, -5177, -6437, -7418, -8105, -8496, -8597,
         3904, 5522, 6866, 7912, 8645, 9062, 9170, 8985},
	{-1664, -2990, -4021, -4795, -5350, -5717, -5925, -6002,
         3680, 4949, 5902, 6584, 7036, 7293, 7388, 7349}
};

#define PSCALE 0x8000
#define PI 3.1415926

main(int argc, char *argv[])

{
  Task tlist, *tlistp;
  Acmd alist[8192], *alistp;
  int w0, w1, i, outpoint, inpoint, pitch, ninput, noutput, dmout;
  int delaybase, delayptr, delaylength;
  int clcount;
  FILE *outp;
  float fninput, delta;
  Acmd *adpcmdec();

  if((outp = fopen(argv[1], "w")) == NULL){
    fprintf(stderr,"Can't open %s\n",argv[1]);
    exit(1);
  }
  tlistp = &tlist;
  alistp = alist;

  noutput = 352;
  pitch = (int) 1.0*PSCALE;             /* PSCALE is one */

  adpcmsetdram(0, 0);                   /* Initialize ADPCM object */
  adpcminit(2, 0);

  inpoint = 0; 
  outpoint = 0;

  delaybase = 0;
  delayptr = delaybase;
  delaylength = 864<<1;
  
  aSegment(alistp++, 0, 0xa000);        /* Bitstream in DRAM 0x4000 */
  aSegment(alistp++, 1, 0x10000);       /* Decoded samples in DRAM 0x100000 */
  aSegment(alistp++, 2, 0x9fe0);        /* Use this for ADPCM state */
  aSegment(alistp++, 3, 0x9fc0);        /* Use this for RESAMPLE state */
  aSegment(alistp++, 4, 0x0);
  aSegment(alistp++, 5, 0x20000);	/* reverb delay line buffer */
  aLoadADPCM(alistp++, ADPCM_TABLE_SIZE, 4<<26 | ADPCM_TABLE_LOC);
  
  /* Number of input samples */
  fninput = (float) noutput * pitch/PSCALE; 
  ninput = (int) floorf(fninput);
  delta = (fninput - ninput);

  alistp = adpcmdec(alistp, 0, 1280, ninput, 0, 1, &dmout); 

  aSetBuffer(alistp++, 0, dmout, 0, noutput<<1); 
  aSetVolume(alistp++, 0, 32767, 32767, 32767);
  aResample(alistp++, A_INIT, pitch, 3<<26);
  /*
   * do a single "all-pass" section reverb
   */
  aLoadBuffer(alistp++, 5<<26 | delayptr);
  aMix(alistp++, A_MIX, 0x3fff, dmout, 0);
  aSaveBuffer(alistp++, 5<<26 | delayptr);
  aMix(alistp++, A_MIX, 0xc001, 0, dmout);
  /*
   * fix up circular buffer pointer(s)..
   */
  delayptr += noutput;
  if (delayptr > (delaybase + delaylength))
      delayptr = delaybase;

  aSaveBuffer(alistp++, 1<<26 | outpoint); 
  outpoint += noutput<<1;

  for (i=0; i<200; i++){ 

    /* Update pitch */
    pitch = (int) PSCALE * (0.1*cos(2*PI*i*noutput/44100) + 0.75);
    fninput = delta + (float) noutput * pitch/PSCALE; 
    ninput = (int) floorf(fninput);
    delta = (fninput - ninput);

    alistp = adpcmdec(alistp, 0, 1280, ninput, 0, 1, &dmout); 

    aSetBuffer(alistp++, 0, dmout, 0, noutput<<1); 
    aResample(alistp++, 0, pitch, 3<<26); 

    aLoadBuffer(alistp++, 5<<26 | delayptr);
    aMix(alistp++, A_MIX, 0x3fff, dmout, 0);
    aSaveBuffer(alistp++, 5<<26 | delayptr);
    aMix(alistp++, A_MIX, 0xc001, 0, dmout);
    delayptr += noutput;
    if (delayptr > (delaybase + delaylength))
	delayptr = delaybase;
    aSaveBuffer(alistp++, 1<<26 | outpoint); 

    outpoint += noutput<<1;

  }

  clcount = (int) (alistp - alist);
  alistp = alist;
  tlistp->t.dl = AUD_CL_POINTER;
  tlistp->t.len = clcount*8;
  tlistp->t.type = M_AUDTASK | ((AUD_UCODE_SIZE)<<12); 
  tlistp->t.ucode = (unsigned long *) AUD_UCODE_POINTER;
  tlistp->t.ucode_data = (unsigned long *) AUD_PDATA_POINTER;
  tlistp->t.ucode_data_len = AUD_PDATA_SIZE;
  fwrite(tlistp, 1, 48, outp);
  fwrite(adpcmtable, 1, ADPCM_TABLE_SIZE, outp);
  for (i=0; i<clcount; i++){ 
    w0 = alistp->words.w0;
    w1 = alistp->words.w1;
    fwrite(&w0, 1, 4, outp);
    fwrite(&w1, 1, 4, outp);
    alistp++;
  }
  fclose(outp);
  fprintf(stderr,"Command list length: %d\n",clcount);
}