amixtask.c 3.55 KB
#include <abi.h>
/* #include <os.h>*/
#include <stdio.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}
};

main(int argc, char *argv[])

{
  Task tlist, *tlistp;
  Acmd alist[4096], *alistp;
  int w0, w1, i, outpoint, inpoint, clcount, lastsam;
  FILE *outp;
  
  if((outp = fopen(argv[1], "w")) == NULL){
    fprintf(stderr,"Can't open %s\n",argv[1]);
    exit(1);
  }

  tlistp = &tlist;
  alistp = alist;

  lastsam = 0;
  inpoint = 0; /* This is in samples */
  outpoint = 0;
  aSegment(alistp++, 0, 0xa000); 
  aSegment(alistp++, 1, 0x100000);
  aSegment(alistp++, 2, 0x8fa0); /* Use this for ADPCM state */
  aSegment(alistp++, 3, 0x40000);
  aSegment(alistp++, 4, 0x0);
  aLoadADPCM(alistp++, ADPCM_TABLE_SIZE-1, 4<<26 | ADPCM_TABLE_LOC);

  aSetBuffer(alistp++, 0, 0, 0, 184);      /* 180 + 4 to reach 8 byte boundary */
  aLoadBuffer(alistp++, inpoint); 

  aSetBuffer(alistp++, 0, 0, 640, 640);    /* 20 frames of 16 samples */
  aADPCMdec(alistp++, A_INIT, 1, (2<<26));
  aMix(alistp++, 0, 0x8000, 336<<1, 688<<1);

  aSetBuffer(alistp++, 0, 0, 0, 184); 
  aLoadBuffer(alistp++, (3<<26) | inpoint);
  
  aSetBuffer(alistp++, 0, 0, 640, 640);    
  aADPCMdec(alistp++, A_INIT, 1, (2<<26) | 32); 
  aMix(alistp++, A_MIX, 0x8000, 336<<1, 688<<1);
  
  aSetBuffer(alistp++, 0, 0, 688<<1, 320<<1); /* 20 frames of 16 samples */
  aSaveBuffer(alistp++, 1<<26 | outpoint);
  
  outpoint += 640;
  inpoint += 180;
  
  for (i=0; i<100; i++){
    aSetBuffer(alistp++, 0, 0, 0, 184);
    aLoadBuffer(alistp++, inpoint - (inpoint % 8));

    aSetBuffer(alistp++, 0, inpoint % 8, 640, 640);
    aADPCMdec(alistp++, 0, 1, (2<<26));
    aMix(alistp++, 0, 0x8000, 336<<1, 688<<1);

    aSetBuffer(alistp++, 0, 0, 0, 180); 
    aLoadBuffer(alistp++, (3<<26) | inpoint - (inpoint % 8));
    
    aSetBuffer(alistp++, 0, inpoint % 8, 640, 640); 
    aADPCMdec(alistp++, 0, 1, (2<<26) | 32); 
    aMix(alistp++, A_MIX, 0x8000, 336<<1, 688<<1);

    aSetBuffer(alistp++, 0, 0, 688<<1, 320<<1);
    aSaveBuffer(alistp++, 1<<26 | outpoint); 

    outpoint += 640;
    inpoint += 180;
  }

  clcount = (int) (alistp - alist);
  fprintf(stderr,"Command list length: %d\n",clcount);
  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);
}