/*Amiga Computing*/
/*Code Clinic*/
/*Jan 1992- sound 1*/
/*A500 upwards*/

/*c source code*/
#include <exec/types.h>
#include <hardware/custom.h>

long dosbase=0;
long libraries=0;
char *buffer = 0;                            /*address of waveform data*/ 
long bytes=0;                                /*bytes read*/
long filehandle = 0;
char *filename="sploit";                     /*waveform data file*/
char *waveadd = 0;
char *squareadd = 0;
char *sawadd = 0;
struct Custom *custom;
struct AudChannel *audc0;

char waveform[] =

{ 0,-20,-38,-56,-71,-83,-92,-98,-100,-98,-92,-83,-71,-56,-38,-20,
 0,20,38,56,71,83,92,98,100,98,92,83,71,56,38,20};

char square[] =
{
 0,127,127,120,127,120,127,120,127,120,127,120,127,120,127,120,
 0,-127,-127,-120,-127,-120,-127,-120,
 -127,-127,-120,-127,-120,-127,-120,-127
};
char saw[] =
{
 0,8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,127,
 -120,-112,-104,-96,-88,-80,-72,-64,-56,-48,-40,-32,-24,-16,-8
};

main()
{
 libraries= openlibraries();                 /*open the libraries*/
 if (libraries == 0) 
    {
       cleanup("no libraries"); 
       exit(1);
    };
 allocatememory();                           /*allocate chip memory for data*/
 readfile();                                 /*read the file*/
 copydata(&waveform,waveadd,32);  /*copy wave data into chip memory*/
 copydata(&square,squareadd,32);
 copydata(&saw,sawadd,32);
 custom = (struct Custom *) 0xdff000; 
 custom->dmacon = 0x0001;        /*turn off dma sound channel 0*/
 custom->adkcon = 0xff;          /*disable modulation*/
 audc0 = custom->aud;
 audc0->ac_ptr = (UWORD *)buffer; /*put sound data addressinto register*/
 audc0->ac_len = 1125;          /*data length in words*/ 
 audc0->ac_vol = 64;            /*volume*/
 audc0->ac_per = 508;           /*pitch 440 Hz*/
 makesound();                   /*make the sound*/
 audc0->ac_ptr = (UWORD *)waveadd; /*put sine wave address into register*/
 audc0->ac_len = 16;           /*data length in words*/
 makesound();
 audc0->ac_ptr = (UWORD *)squareadd; /*square wave data address*/
 makesound();
 audc0->ac_ptr = (UWORD *)sawadd; /*saw tooth wave data*/
 makesound();
 cleanup("ok");                           /*close libraries, free buffers*/
 return(0);
}

makesound()                     /*make the sound*/
{
 custom->dmacon = 0x8001;       /*turn on DMA sound channel 0*/
 Delay(100);                    /*short delay*/
 custom->dmacon = 0x0001;       /*turn off DMA sound channel 0*/
 Delay(100);                    /*another delay*/
 return(0);
}

copydata(s,t,n)                 /*copy n bytes of data ,from a block*/
char *s,*t;                     /*starting at address s, to a block*/
long n;                         /*starting at address t*/
{
  long i;
  for (i=0;i<n;i++)
    *t++ = *s++;
  return(0);
}

openlibraries()                             /* open amiga libraries */
{
  dosbase= OpenLibrary("dos.library",0);
  if (dosbase == 0) return(0);
  return(1);
}

cleanup(errormsg)                                   /* leave as you would wish */
{                                                   /*  to find                */
  printf(errormsg);
  if (buffer != 0 ) FreeMem(buffer,2250);
  if (waveadd !=0) FreeMem(waveadd,32);
  if (squareadd !=0) FreeMem(squareadd,32);
  if (sawadd !=0) FreeMem(sawadd,32);
  if (dosbase !=0) CloseLibrary(dosbase);
  return(0);
}
allocatememory()
{
  buffer=(char *)AllocMem(2250,65539);   /*allocate chip memory */
  if (buffer == 0)                      /*for data from file*/
    {
     cleanup("no memory"); 
     exit(1);
    };
  sawadd=(char *)AllocMem(32,65539);    /*allocate chip memory for*/
  if (sawadd == 0)                      /*saw tooth wave data*/
    {
     cleanup("no memory"); 
     exit(1);
    };
  waveadd=(char *)AllocMem(32,65539);  /*allocate chip memory buffer*/
  if (waveadd == 0)                    /*for sine wave data*/
    {
     cleanup("no memory"); 
     exit(1);
    };
  squareadd=(char *)AllocMem(32,65539); /*allocate memory buffer*/
  if (squareadd == 0)                   /*for square wave data*/
    {
     cleanup("no memory"); 
     exit(1);
    };
  return(0);
}

readfile()                            /*read the data from the file*/
{
  filehandle=Open(filename,1005);
  if (filehandle == 0) return(0);
  bytes=Read(filehandle,buffer,2250);
  if (bytes == 0) return(0);
  Close(filehandle);
  return(1);
}


