home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / amfm / amfm11 / techcorner / exmain.c.pp / exmain.c
C/C++ Source or Header  |  1994-06-20  |  2KB  |  84 lines

  1. /* AM/FM 8 channel example - loader part */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <dos/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/dos.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. extern void __asm play(void),endplay(void);
  12.  
  13. UWORD *sample[2],period1,period2;
  14. ULONG slen[2];
  15.  
  16. /* halvesample() halves the volume of the sample, it has to be done
  17.    in advance */
  18.  
  19. void halvesample(BYTE *sample,ULONG length)
  20. {
  21.     while(length--) *sample++ /= 2;
  22. }
  23.  
  24. /* loadsample() load a sample into the memory */
  25.  
  26. BOOL loadsample(UWORD num,char *name)
  27. {
  28.     BPTR file;
  29.     LONG len;
  30.     if(file = Open(name,MODE_OLDFILE)) {
  31.         Seek(file,0,OFFSET_END);
  32.         if((len = Seek(file,0,OFFSET_BEGINNING)) > 0 &&
  33.             (sample[num] = AllocMem(len,MEMF_CHIP|MEMF_PUBLIC))) {
  34.             slen[num] = len;
  35.             if(Read(file,sample[num],len) != len) {
  36.                 printf("read error!\n");
  37.                 Close(file);
  38.                 return(TRUE);
  39.             }
  40.         } else {
  41.             printf("no memory!\n");
  42.             Close(file);
  43.             return(TRUE);
  44.         }
  45.         Close(file);
  46.     } else {
  47.         printf("file open failure! ('%s')\n",name);
  48.         return(TRUE);
  49.     }
  50.     halvesample((UBYTE *)sample[num],len);
  51.     return(FALSE);
  52. }
  53.  
  54. /* frees the samples */
  55.  
  56. void freesamples(void)
  57. {
  58.     if(slen[0]) FreeMem(sample[0],slen[0]);
  59.     if(slen[1]) FreeMem(sample[1],slen[1]);
  60. }
  61.  
  62. /* the main routine */
  63.  
  64. void main(int argc,char *argv[])
  65. {
  66.     if(argc != 5) {
  67.         printf("usage: example <sample1> <period1> <sample2> <period2>\n");
  68.         return;
  69.     }
  70.     if((period1 = atoi(argv[2])) == 0 || (period2 = atoi(argv[4])) == 0) {
  71.         printf("periods must be > 0!\n");
  72.         return;
  73.     }
  74.     if(!loadsample(0,argv[1]) && !loadsample(1,argv[3])) {
  75.         printf("Playing '%s' (period %d) and '%s' (period %d)\n",
  76.             argv[1],period1,argv[3],period2);
  77.         play();
  78.         Delay(100);
  79.         endplay();
  80.     }
  81.     freesamples();
  82.     exit(0);
  83. }
  84.