home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / phreak_utils_pc / tmaster.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-18  |  5.9 KB  |  266 lines

  1. #include<conio.h>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #include"timer.h"
  7. extern "C"
  8. {
  9. #include<forte.h>
  10. #include<gf1proto.h>
  11. #include<extern.h>
  12. #include<ultraerr.h>
  13. }
  14.  
  15. enum Command {TC_TONESTART,TC_TONEEND,TC_WAIT,TC_END};
  16.  
  17. struct toneCommand
  18. {Command tc;
  19.  float fParam;
  20.  int iParam1,iParam2;
  21. };
  22.  
  23. struct toneDef
  24. {struct toneCommand *tcoms;
  25.  char key;
  26.  char name[40];
  27. };
  28.  
  29. #define MAXNMTONEDEFS 50
  30. #define SINESIZE 100
  31.  
  32. int nmToneDefs;
  33. struct toneDef *toneDef[MAXNMTONEDEFS];
  34. int basePort;
  35. int masterVolume;
  36.  
  37. void readToneFile(char *name)
  38. {FILE *ifile;
  39.  int nmTcomms;
  40.  char line[80];
  41.  char toneName[50];
  42.  char command[50];
  43.  char key[5];
  44.  int lineNm;
  45.  int iParam1,iParam2;
  46.  float fParam;
  47.  ifile=fopen(name,"r");
  48.  if (!ifile)
  49.     {printf("Can't open %s\n",name);
  50.      exit(-1);
  51.     }
  52.  lineNm=-1;
  53.  nmToneDefs=0;
  54.  while ((lineNm++,fgets(line,80,ifile)))
  55.     {if (sscanf(line," start %50s %5s ",toneName,key)==2)
  56.     {struct toneDef *newTD;
  57.      newTD=new (struct toneDef);
  58.      if (!newTD)
  59.         {printf("Out of memory!\n");
  60.          exit(-1);
  61.         }
  62.      strcpy(newTD->name,toneName);
  63.      newTD->key=key[0];
  64.      newTD->tcoms=(struct toneCommand *)
  65.         malloc(sizeof(struct toneCommand)*100);
  66.      if (nmToneDefs>MAXNMTONEDEFS)
  67.         {printf("Too many tone definitions\n");
  68.          exit(-1);
  69.         }
  70.      toneDef[nmToneDefs++]=newTD;
  71.      nmTcomms=0;
  72.      while (1)
  73.         {if (nmTcomms>=100)
  74.         {printf("Tone definition too complex\n");
  75.          exit(-1);
  76.         }
  77.          if (!(lineNm++,fgets(line,80,ifile)))
  78.         {printf("Unexpected end of file.\n");
  79.          exit(-1);
  80.         }
  81.          sscanf(line, " %50s ",command);
  82.          if (!strcmp(command,"tstart"))
  83.         {if (sscanf(line," %50s %d %d ",command,&iParam1,&iParam2)!=3)
  84.             {printf("Error parsing line %d\n",lineNm);
  85.              exit(-1);
  86.             }
  87.          newTD->tcoms[nmTcomms].tc=TC_TONESTART;
  88.          newTD->tcoms[nmTcomms].iParam1=iParam1;
  89.          newTD->tcoms[nmTcomms].iParam2=iParam2;
  90.          nmTcomms++;
  91.          continue;
  92.         }
  93.          if (!strcmp(command,"tend"))
  94.         {if (sscanf(line," %50s %d ",command,&iParam1)!=2)
  95.             {printf("Error parsing line %d\n",lineNm);
  96.              exit(-1);
  97.             }
  98.          newTD->tcoms[nmTcomms].tc=TC_TONEEND;
  99.          newTD->tcoms[nmTcomms].iParam1=iParam1;
  100.          nmTcomms++;
  101.          continue;
  102.         }
  103.          if (!strcmp(command,"wait"))
  104.         {if (sscanf(line," %50s %f ",command,&fParam)!=2)
  105.             {printf("Error parsing line %d\n",lineNm);
  106.              exit(-1);
  107.             }
  108.          newTD->tcoms[nmTcomms].tc=TC_WAIT;
  109.          newTD->tcoms[nmTcomms].fParam=fParam;
  110.          nmTcomms++;
  111.          continue;
  112.         }
  113.          if (!strcmp(command,"end"))
  114.         {newTD->tcoms[nmTcomms].tc=TC_END;
  115.          nmTcomms++;
  116.          newTD->tcoms=(struct toneCommand *)realloc(newTD->tcoms,
  117.                       sizeof(struct toneCommand)*nmTcomms);
  118.          break;
  119.         }
  120.          printf("Error parsing line %d\n",lineNm);
  121.          exit(-1);
  122.         }
  123.     }
  124.     }
  125. }
  126.          
  127. int UltraGetCfg(ULTRA_CFG *config)
  128. {char *ptr;
  129.  config->base_port = 0x220;
  130.  config->dram_dma_chan = 1;
  131.  config->adc_dma_chan = 1;
  132.  config->gf1_irq_num = 11;
  133.  config->midi_irq_num = 5;
  134.  ptr = getenv("ULTRASND");
  135.  if (!ptr)
  136.     return(0);
  137.  sscanf(ptr,"%x,%d,%d,%d,%d",&config->base_port,
  138.     &config->dram_dma_chan,
  139.     &config->adc_dma_chan,
  140.     &config->gf1_irq_num,
  141.     &config->midi_irq_num);
  142.  return(1);
  143. }
  144.  
  145. void setupUltrasound(void)
  146. {ULTRA_CFG config;
  147.  int i;
  148.  float f;
  149.  if (!UltraGetCfg(&config))
  150.     {printf("ULTRASND environment variable not defined.\n");
  151.      exit(-1);
  152.     }
  153.  if (UltraProbe(config.base_port) == NO_ULTRA)
  154.     {printf("Couldn't initialize GUS.\n");
  155.      exit(-1);
  156.     }
  157.  basePort=config.base_port;
  158.  if (UltraOpen(&config,14) == NO_ULTRA)
  159.     {printf("Couldn't initialize GUS.\n");
  160.      exit(-1);
  161.     }
  162.  int w;
  163.  for (i=0;i<SINESIZE+5;i++)
  164.     {f=3.1415926535*2.0*(((float)i)/((float)SINESIZE));
  165.      w=15000.0*sin(f);
  166.      UltraPokeData(basePort,i*2,w & 0x0f);
  167.      UltraPokeData(basePort,i*2+1,w>>8);
  168.     }
  169. }
  170.  
  171. void doTone(char c)
  172. {struct toneDef *td;
  173.  int i;
  174.  volatile int timer=0;
  175.  for (i=0;i<nmToneDefs && toneDef[i]->key!=c;i++) ;
  176.  if (i==nmToneDefs)
  177.     return;
  178.  td=toneDef[i];
  179.  globalTimers.addTimerExact(&timer,1);
  180.  for (i=0;;i++)
  181.     {switch (td->tcoms[i].tc)
  182.     {case TC_TONESTART:
  183.         UltraSetLinearVolume(td->tcoms[i].iParam1,masterVolume);
  184.         UltraVoiceOn(td->tcoms[i].iParam1,0,0,2*SINESIZE-2,8+4,
  185.              ((long)td->tcoms[i].iParam2)*((long)SINESIZE));
  186.         break;
  187.      case TC_TONEEND:
  188.         UltraVoiceOff(td->tcoms[i].iParam1,1);
  189.         break;
  190.      case TC_WAIT:
  191.         timer=td->tcoms[i].fParam*582.5422;
  192.         while (timer) ;
  193.         break;
  194.      case TC_END:
  195.         globalTimers.removeTimer(&timer);
  196.         return;
  197.        }
  198.     }
  199. }
  200.  
  201. void closeUltrasound(void)
  202. {UltraClose();
  203. }
  204.  
  205. void printList(void)
  206. {int i;
  207.  for (i=0;i<nmToneDefs;i++)
  208.     {printf("%c - %-20s  ",toneDef[i]->key,toneDef[i]->name);
  209.      if (i%3==2)
  210.     printf("\n");
  211.     }
  212.  printf("\n");
  213.  printf(" ... and `x' to quit.\n");
  214. }
  215.  
  216. void go(void)
  217. {char input[80],*c;
  218.  
  219.  while (1)
  220.     {gotoxy(1,24);
  221.      clreol();
  222.      printf(">");
  223.      input[0]=70;
  224.      cgets(input);
  225.      for (c=input+2;*c;c++)
  226.     {if (*c=='x')
  227.         return;
  228.      doTone(*c);
  229.     }
  230.     }
  231. }
  232.  
  233. void main(int argc, char **argv)
  234. {int i;
  235.  char fname[40];
  236.  clrscr();
  237.  printf("Tone Master - v1.0\n");
  238.  printf("  sine wave synthesis for the Gravis Ultrasound.\n");
  239.  strcpy(fname,"tone.def");
  240.  masterVolume=300;
  241.  for (i=1;i<argc;i++)
  242.     {if (argv[i][0]!='-' && argv[i][0]!='/')
  243.     strcpy(fname,argv[i]);
  244.     else
  245.        {switch (argv[i][1])
  246.        {case 'v':
  247.            masterVolume=atoi(argv[i]+2);
  248.            if (masterVolume<0) masterVolume=0;
  249.            if (masterVolume>500) masterVolume=500;
  250.            break;
  251.         case '?':
  252.            printf("Usage: %s [tone file] [-v# volume 0-500]\n",argv[0]);
  253.            exit(0);
  254.            break;
  255.           }
  256.        }
  257.     }       
  258.  readToneFile(fname);
  259.  setupUltrasound();
  260.  printf("\n");
  261.  printList();
  262.  go();
  263.  closeUltrasound();
  264. }
  265.  
  266.