envmix.c 5.84 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 (12*16)

#define AUD_CL_POINTER (ADPCM_TABLE_LOC + ADPCM_TABLE_SIZE)

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

#define PSCALE 0x8000
#define PI 3.1415926

main(int argc, char *argv[])

{
  Task tlist, *tlistp;
  Acmd alist[8192], *alistp;
  int w0, w1, i, outpoint, pitch, ninput, noutput;
  int dmin, dmout, dmoutL, dmoutR, dmauxL, dmauxR;
  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 = 176;
  pitch = (int) 1.0*PSCALE;             /* PSCALE is one */

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

#define LRATE 1.5
#define RRATE 0.5
  
  envsetLrate(LRATE);
  envsetRrate(RRATE);

  outpoint = 0;
  dmin = 0;
  dmout = 0xc8;		/* 200 */
  dmoutL = 0x190;	/* 400 */
  dmoutR = 0x2f0;	/* 752  ( 400  + 176*2) */
  dmauxL = 0x450;	/* 1104 ( 752  + 176*2) */
  dmauxR = 0x5b0;	/* 1456 ( 1104 + 176*2) */

  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);		/* ADPCM table */
  aSegment(alistp++, 5, 0x9f70);	/* env state */
  aSegment(alistp++, 6, 0x20000);	/* outR */
  aSegment(alistp++, 7, 0x30000);	/* auxL */
  aSegment(alistp++, 8, 0x40000);	/* auxR */
  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, dmin, dmout, ninput, 0, 1, &dmout); 

  aSetBuffer(alistp++, A_MAIN, dmout, dmin, noutput<<1); 
  aSetVolume(alistp++, A_LEFT | A_VOL, 1, 0, 0); /* current = 1 */
  aSetVolume(alistp++, A_RIGHT | A_VOL, 32767, 0, 0); /* current = full scale */
  aSetVolume(alistp++, A_LEFT | A_RATE, 32767, 1, 0x0561); /* target = full scale, rate^8 = 1.005212 */
  aSetVolume(alistp++, A_RIGHT | A_RATE, 0, 0, 0xfeaa); /* target = 0, rate^8 = 0.994787 */
  aSetVolume(alistp++, A_AUX, 32767, 0, 16384);
  aResample(alistp++, A_INIT, pitch, 3<<26);
  aSetBuffer(alistp++, A_AUX, dmoutR, dmauxL, dmauxR);
  aSetBuffer(alistp++, A_MAIN, dmin, dmoutL, noutput<<1); 
  aClearBuffer(alistp++, dmoutL, noutput<<1);
  aClearBuffer(alistp++, dmoutR, noutput<<1);
  aClearBuffer(alistp++, dmauxL, noutput<<1);
  aClearBuffer(alistp++, dmauxR, noutput<<1);
  aEnvMixer(alistp++, A_INIT | A_AUX, 5<<26);
  aSaveBuffer(alistp++, 1<<26 | outpoint); 
  aSetBuffer(alistp++, A_MAIN, 0, dmoutR, noutput<<1); 
  aSaveBuffer(alistp++, 6<<26 | outpoint); 
  aSetBuffer(alistp++, A_MAIN, 0, dmauxL, noutput<<1); 
  aSaveBuffer(alistp++, 7<<26 | outpoint); 
  aSetBuffer(alistp++, A_MAIN, 0, dmauxR, noutput<<1); 
  aSaveBuffer(alistp++, 8<<26 | outpoint); 
  outpoint += noutput<<1;

  for (i=0; i<100; 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, dmin, 1280, ninput, 0, 1, &dmout); 

    aSetBuffer(alistp++, A_MAIN, dmout, dmin, noutput<<1); 
    aResample(alistp++, A_CONTINUE, pitch, 3<<26); 
    aSetBuffer(alistp++, A_AUX, dmoutR, dmauxL, dmauxR); 
    aSetBuffer(alistp++, A_MAIN, dmin, dmoutL, noutput<<1); 
    aClearBuffer(alistp++, dmoutL, noutput<<1);
    aClearBuffer(alistp++, dmoutR, noutput<<1);
    aClearBuffer(alistp++, dmauxL, noutput<<1);
    aClearBuffer(alistp++, dmauxR, noutput<<1);
    aEnvMixer(alistp++, A_AUX, 5<<26);
    aSaveBuffer(alistp++, 1<<26 | outpoint); 
    aSetBuffer(alistp++, A_MAIN, 0, dmoutR, noutput<<1); 
    aSaveBuffer(alistp++, 6<<26 | outpoint); 
    aSetBuffer(alistp++, A_MAIN, 0, dmauxL, noutput<<1); 
    aSaveBuffer(alistp++, 7<<26 | outpoint); 
    aSetBuffer(alistp++, A_MAIN, 0, dmauxR, noutput<<1); 
    aSaveBuffer(alistp++, 8<<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);
}