home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / sbbs_src.exe / TONE / TONE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  5.6 KB  |  253 lines

  1. /* TONE.C */
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. /* Test sound freqs and durations from command line */
  6.  
  7. #ifdef __OS2__
  8.     #define INCL_DOS
  9.     #include <os2.h>
  10. #endif
  11. #include <io.h>
  12. #include <dos.h>
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18.  
  19. #ifdef __OS2__
  20.     #define mswait(x) DosSleep(x)
  21.     #define delay(x) DosSleep(x)
  22.     #define sound(x) DosBeep(x,0)
  23.     #define nosound() DosBeep(0,0)
  24. #endif
  25.  
  26. #define NOT_ABORTABLE    (1<<0)
  27. #define SHOW_DOT        (1<<1)
  28. #define SHOW_FREQ        (1<<2)
  29. #define NO_VISUAL        (1<<3)
  30. #define USE_MSWAIT        (1<<4)
  31.  
  32. #ifdef __OS2__
  33.     int mswtyp;
  34. #else
  35.     extern mswtyp;
  36. #endif
  37.  
  38. int aborted=0;    /* Ctrl-C hit? */
  39. int mode=0;     /* Optional modes */
  40. int t=1;        /* Timing */
  41. int s=0;        /* Stacato */
  42. int octave=4;    /* Default octave */
  43.  
  44. double pitch=523.50/32.0;     /* low 'C' */
  45.  
  46. void play(char *freq, char *dur)
  47. {
  48.     char *notes="c d ef g a b";
  49.     char *sharp="BC D EF G A ";
  50.     int i,n,d,o=octave;
  51.     double f;
  52.  
  53. d=atoi(dur);
  54. if(isdigit(freq[0]))
  55.     f=atoi(freq);
  56. else
  57.     switch(toupper(freq[0])) {
  58.         case 'O':               /* default octave */
  59.             if(isdigit(dur[0]))
  60.                 octave=d;
  61.             else
  62.                 octave+=d;
  63.             return;
  64.         case 'P':               /* pitch variation */
  65.             if(isdigit(dur[0]))
  66.                 pitch=atof(dur)/32.0;
  67.             else
  68.                 pitch+=atof(dur);
  69.             return;
  70.         case 'Q':               /* quit */
  71.             nosound();
  72.             exit(0);
  73.         case 'R':               /* rest */
  74.             f=0;
  75.             break;
  76.         case 'S':               /* stacato */
  77.             if(isdigit(dur[0]))
  78.                 s=d;
  79.             else
  80.                 s+=d;
  81.             return;
  82.         case 'T':               /* time adjust */
  83.             t=d;
  84.             return;
  85.         case 'V':
  86.             if(mode&NO_VISUAL)
  87.                 return;
  88.             n=strlen(dur);
  89.             while(n && dur[n]<=' ')
  90.                 n--;
  91.             dur[n+1]=0;
  92.             if(dur[n]=='\\') {
  93.                 dur[n]=0;
  94.                 printf("%s",dur); }
  95.             else
  96.                 printf("%s\r\n",dur);
  97.             return;
  98.         case 'X':               /* exit */
  99.             exit(1);
  100.         default:
  101.             for(n=0;notes[n];n++)
  102.                 if(freq[0]==notes[n] || freq[0]==sharp[n])
  103.                     break;
  104.             if(isdigit(freq[1]))
  105.                 o=(freq[1]&0xf);
  106.             else
  107.                 o=octave;
  108.             f=pitch*pow(2,o+(double)n/12);
  109.             break; }
  110. if(!f)
  111.     nosound();
  112. else
  113.     sound(f);
  114. if(f && mode&SHOW_FREQ) {
  115.     for(i=0;freq[i]>' ';i++)
  116.         ;
  117.     freq[i]=0;
  118.     printf("%-4.4s",freq); }
  119. if(mode&SHOW_DOT)
  120.     printf(".");
  121. if(t>10) {
  122.     if(mode&USE_MSWAIT)
  123.         mswait((d*t)-(d*s));
  124.     else
  125.         delay((d*t)-(d*s)); }
  126. else {
  127.     if(mode&USE_MSWAIT)
  128.         mswait(d*t);
  129.     else
  130.         delay(d*t); }
  131. if(s) {
  132.     nosound();
  133.     if(t>10) {
  134.         if(mode&USE_MSWAIT)
  135.             mswait(d*s);
  136.         else
  137.             delay(d*s); }
  138.     else {
  139.         if(mode&USE_MSWAIT)
  140.             mswait(s);
  141.         else
  142.             delay(s); } }
  143. }
  144.  
  145. void usage(void)
  146. {
  147. printf("usage: tone [/opts] [(note[oct]|freq) dur | (cmd val) [...]] "
  148.     "[+filename]\n\n");
  149. printf("where: note  = a,b,c,d,e,f, or g (naturals) or A,B,C,D,E,F, or "
  150.     "G (sharps)\n");
  151. printf("       oct   = octave 1 through 9 (default=%d)\n",octave);
  152. printf("       freq  = frequency (in Hz) or 0 for silence\n");
  153. printf("       dur   = duration (in timer counts)\n");
  154. printf("       cmd   = o set default octave (+/- to adjust) "
  155.     "(default=%d)\n",octave);
  156. printf("               p set middle c pitch (+/- to adjust) "
  157.     "(default=%.2f)\n",pitch*32.0);
  158. printf("               q quit program immediately\n");
  159. printf("               r rest (silence) for val timer counts\n");
  160. printf("               s set stacato duration (in ms) (+/- to adjust) "
  161.     "(default=%d)\n",s);
  162. printf("               t set timer count value (in ms) "
  163.     "(default=%d)\n",t);
  164. printf("               v visual text diplay of val (no val=cr/lf)\n");
  165. printf("               x quit program immediately (leave tone on)\n");
  166. printf("       opts  = d display dot for each note\n");
  167. printf("               f display frequency or note value\n");
  168. printf("               n not abortable with key-stroke\n");
  169. printf("               v disable visual text commands\n");
  170. printf("               t use time-slice aware delays\n");
  171. exit(0);
  172. }
  173.  
  174. int cbreakh()    /* ctrl-break handler */
  175. {
  176. aborted=1;
  177. return(1);        /* 1 to continue, 0 to abort */
  178. }
  179.  
  180. void main(int argc, char **argv)
  181. {
  182.     char *p,str[128];
  183.     int i,j,file;
  184.     FILE *stream;
  185.  
  186. #ifndef __OS2__
  187. ctrlbrk(cbreakh);
  188. #endif
  189.  
  190. printf("\nTone Generation Utility  v1.01  Developed 1993 Rob Swindell\n\n");
  191.  
  192. if(argc<2)
  193.     usage();
  194.  
  195. mswtyp=0;
  196. delay(0);
  197. for(i=1;i<argc;i++) {
  198.     if(argv[i][0]=='/') {
  199.         for(j=1;argv[i][j];j++)
  200.             switch(toupper(argv[i][j])) {
  201.                 case 'D':
  202.                     mode^=SHOW_DOT;
  203.                     break;
  204.                 case 'F':
  205.                     mode^=SHOW_FREQ;
  206.                     break;
  207.                 case 'N':
  208.                     mode^=NOT_ABORTABLE;
  209.                     break;
  210.                 case 'V':
  211.                     mode^=NO_VISUAL;
  212.                     break;
  213.                 case 'T':
  214.                     mode^=USE_MSWAIT;
  215.                     mswtyp=atoi(argv[i]+j+1);
  216.                     while(isdigit(argv[i][j+1]))
  217.                         j++;
  218.                     break;
  219.                 default:
  220.                     usage(); }
  221.         continue; }
  222.     if(argv[i][0]=='+') {
  223.         if((file=open(argv[i]+1,O_RDONLY|O_DENYNONE|O_BINARY))==-1
  224.             || (stream=fdopen(file,"rb"))==NULL) {
  225.             strcpy(str,argv[0]);
  226.             p=strrchr(str,'\\');
  227.             if(p)
  228.                 *(p+1)=0;
  229.             strcat(str,argv[i]+1);
  230.             if((file=open(str,O_RDONLY|O_DENYNONE|O_BINARY))==-1
  231.                 || (stream=fdopen(file,"rb"))==NULL) {
  232.                 printf("\7Error opening %s\n",argv[i]+1);
  233.                 exit(1); } }
  234.         while(mode&NOT_ABORTABLE || !kbhit()) {
  235.             if(!fgets(str,81,stream))
  236.                 break;
  237.             if(!isalnum(str[0]))
  238.                 continue;
  239.             p=str;
  240.             while(*p>' ')
  241.                 p++;
  242.             while(*p && *p<=' ')
  243.                 p++;
  244.             play(str,p); }
  245.         fclose(stream);
  246.         continue; }
  247.     play(argv[i],argv[i+1]);
  248.     i++;
  249.     if(aborted)
  250.         break; }
  251. nosound();
  252. }
  253.