Annotation of sbbs/tone/tone.c, revision 1.1.1.2

1.1       root        1: /* TONE.C */
                      2: 
                      3: /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
                      4: 
1.1.1.2 ! root        5: /* Linux modifications by Casey Martin 2000.
        !             6: 
        !             7:    Note:  Permissions on /dev/console must be at least 006 for this
        !             8:    to work properly
        !             9: */
        !            10: 
        !            11: /****************************************************************************
        !            12:  * @format.tab-size 4          (Plain Text/Source Code File Header)                    *
        !            13:  * @format.use-tabs true       (see http://www.synchro.net/ptsc_hdr.html)              *
        !            14:  *                                                                                                                                                     *
        !            15:  * Note: If this box doesn't appear square, then you need to fix your tabs.    *
        !            16:  ****************************************************************************/
        !            17: 
1.1       root       18: /* Test sound freqs and durations from command line */
                     19: 
                     20: #ifdef __OS2__
                     21:        #define INCL_DOS
                     22:        #include <os2.h>
                     23: #endif
1.1.1.2 ! root       24: 
        !            25: #ifndef __unix__
        !            26:        #include <io.h>
        !            27:        #include <dos.h>
        !            28: #else
        !            29:        #include <unistd.h>
        !            30:        #include <math.h>
        !            31:        #include <time.h>
        !            32:        #include <sys/ioctl.h>
        !            33:        #include <sys/kd.h>
        !            34:        #include <sys/time.h>
        !            35:        #include <sys/types.h>
        !            36:        #include <termios.h>
        !            37:        #include <signal.h>
        !            38: #endif
        !            39: 
1.1       root       40: #include <math.h>
                     41: #include <stdio.h>
                     42: #include <string.h>
                     43: #include <stdlib.h>
                     44: #include <fcntl.h>
                     45: 
                     46: #ifdef __OS2__
                     47:        #define mswait(x) DosSleep(x)
                     48:        #define delay(x) DosSleep(x)
                     49:        #define sound(x) DosBeep(x,0)
                     50:        #define nosound() DosBeep(0,0)
                     51: #endif
                     52: 
                     53: #define NOT_ABORTABLE  (1<<0)
                     54: #define SHOW_DOT               (1<<1)
                     55: #define SHOW_FREQ              (1<<2)
                     56: #define NO_VISUAL              (1<<3)
                     57: #define USE_MSWAIT             (1<<4)
                     58: 
1.1.1.2 ! root       59: #ifdef __unix__
        !            60:        #define nosound() ioctl(fd, KIOCSOUND, 0)
        !            61:        #define sound(f)  ioctl(fd, KIOCSOUND, (int) (1193180 / f))
        !            62:        #define mswait(x) delay(x)
        !            63:        #define kbhit()   select(1, &inp, NULL, NULL, &timeout)
        !            64:        #define delay(ms) usleep(ms * 1000)
        !            65:        #define OPENARGS O_RDONLY
        !            66: #else
        !            67:        #define OPENARGS (O_RDONLY | O_BINARY | O_DENYNONE)
        !            68: #endif
        !            69: 
        !            70: 
        !            71: #if defined __OS2__ || defined __unix__
1.1       root       72:        int mswtyp;
                     73: #else
                     74:        extern mswtyp;
                     75: #endif
                     76: 
                     77: int aborted=0; /* Ctrl-C hit? */
                     78: int mode=0;    /* Optional modes */
                     79: int t=1;               /* Timing */
                     80: int s=0;               /* Stacato */
                     81: int octave=4;  /* Default octave */
                     82: 
1.1.1.2 ! root       83: 
        !            84: #ifdef __unix__
        !            85:        int fd = 0;              // file descriptor for the console
        !            86:        struct termios stored;   // for storing old term settings
        !            87:        struct timeval timeout = {0, 0}; // passed in select() call
        !            88:        fd_set inp;              // passes in select() call
        !            89: #endif
        !            90: 
1.1       root       91: double pitch=523.50/32.0;       /* low 'C' */
                     92: 
1.1.1.2 ! root       93: 
        !            94: 
1.1       root       95: void play(char *freq, char *dur)
                     96: {
                     97:        char *notes="c d ef g a b";
                     98:        char *sharp="BC D EF G A ";
                     99:        int i,n,d,o=octave;
                    100:        double f;
                    101: 
1.1.1.2 ! root      102:        d=atoi(dur);
        !           103:        if(isdigit(freq[0]))
        !           104:                f=atoi(freq);
        !           105:   
1.1       root      106:        else
1.1.1.2 ! root      107:                switch(toupper(freq[0])) {
        !           108:                        case 'O':               /* default octave */
        !           109:                                if(isdigit(dur[0]))
        !           110:                                        octave=d;
        !           111:                                else
        !           112:                                        octave+=d;
        !           113:                                return;
        !           114:                        case 'P':               /* pitch variation */
        !           115:                                if(isdigit(dur[0]))
        !           116:                                        pitch=atof(dur)/32.0;
        !           117:                                else
        !           118:                                        pitch+=atof(dur);
        !           119:                                return;
        !           120:                        case 'Q':               /* quit */
        !           121:                                nosound();
        !           122:                                exit(0);
        !           123:                        case 'R':               /* rest */
        !           124:                                f=0;
        !           125:                                break;
        !           126:                        case 'S':               /* stacato */
        !           127:                                if(isdigit(dur[0]))
        !           128:                                        s=d;
        !           129:                                else
        !           130:                                        s+=d;
        !           131:                                return;
        !           132:                        case 'T':               /* time adjust */
        !           133:                                t=d;
        !           134:                                return;
        !           135:                        case 'V':
        !           136:                                if(mode&NO_VISUAL)
        !           137:                                        return;
        !           138:                                n=strlen(dur);
        !           139:                                while(n && dur[n]<=' ')
        !           140:                                        n--;
        !           141:                                dur[n+1]=0;
        !           142:                                if(dur[n]=='\\') {
        !           143:                                        dur[n]=0;
        !           144:                                        printf("%s",dur); }
        !           145:                                else
        !           146:                                        printf("%s\r\n",dur);
        !           147:                                return;
        !           148:                        case 'X':               /* exit */
        !           149:                                exit(1);
        !           150:                        default:
        !           151:                                for(n=0;notes[n];n++)
        !           152:                                        if(freq[0]==notes[n] || freq[0]==sharp[n])
        !           153:                                                break;
        !           154:                                if(isdigit(freq[1]))
        !           155:                                        o=(freq[1]&0xf);
        !           156:                                else
        !           157:                                        o=octave;
        !           158:                                f=pitch*pow(2,o+(double)n/12);
        !           159:                                break; }
        !           160:        if(!f)
        !           161:                nosound();
1.1       root      162:        else
1.1.1.2 ! root      163:                sound(f);
        !           164:        if(f && mode&SHOW_FREQ) {
        !           165:                for(i=0;freq[i]>' ';i++)
        !           166:                        ;
        !           167:                freq[i]=0;
        !           168:                printf("%-4.4s",freq); }
        !           169:        if(mode&SHOW_DOT)
        !           170:                printf(".");
1.1       root      171:        if(t>10) {
                    172:                if(mode&USE_MSWAIT)
1.1.1.2 ! root      173:                        mswait((d*t)-(d*s));
1.1       root      174:                else
1.1.1.2 ! root      175:                        delay((d*t)-(d*s)); }
1.1       root      176:        else {
                    177:                if(mode&USE_MSWAIT)
1.1.1.2 ! root      178:                        mswait(d*t);
1.1       root      179:                else
1.1.1.2 ! root      180:                        delay(d*t); }
        !           181:        if(s) {
        !           182:                nosound();
        !           183:                if(t>10) {
        !           184:                        if(mode&USE_MSWAIT)
        !           185:                                mswait(d*s);
        !           186:                        else
        !           187:                                delay(d*s); }
        !           188:                else {
        !           189:                        if(mode&USE_MSWAIT)
        !           190:                                mswait(s);
        !           191:                        else
        !           192:                                delay(s); } }
1.1       root      193: }
                    194: 
                    195: void usage(void)
                    196: {
1.1.1.2 ! root      197:        printf("usage: tone [/opts] [(note[oct]|freq) dur | (cmd val) [...]] "
        !           198:                "[+filename]\n\n");
        !           199:        printf("where: note  = a,b,c,d,e,f, or g (naturals) or A,B,C,D,E,F, or "
        !           200:                "G (sharps)\n");
        !           201:        printf("       oct   = octave 1 through 9 (default=%d)\n",octave);
        !           202:        printf("       freq  = frequency (in Hz) or 0 for silence\n");
        !           203:        printf("       dur   = duration (in timer counts)\n");
        !           204:        printf("       cmd   = o set default octave (+/- to adjust) "
        !           205:                "(default=%d)\n",octave);
        !           206:        printf("               p set middle c pitch (+/- to adjust) "
        !           207:                "(default=%.2f)\n",pitch*32.0);
        !           208:        printf("               q quit program immediately\n");
        !           209:        printf("               r rest (silence) for val timer counts\n");
        !           210:        printf("               s set stacato duration (in ms) (+/- to adjust) "
        !           211:                "(default=%d)\n",s);
        !           212:        printf("               t set timer count value (in ms) "
        !           213:                "(default=%d)\n",t);
        !           214:        printf("               v visual text diplay of val (no val=cr/lf)\n");
        !           215:        printf("               x quit program immediately (leave tone on)\n");
        !           216:        printf("       opts  = d display dot for each note\n");
        !           217:        printf("               f display frequency or note value\n");
        !           218:        printf("               n not abortable with key-stroke\n");
        !           219:        printf("               v disable visual text commands\n");
        !           220:        printf("               t use time-slice aware delays\n");
        !           221:        exit(0);
        !           222: }
        !           223: 
        !           224: #ifdef __unix__
        !           225: 
        !           226: void cbreakh(int sig)
        !           227: {
        !           228:        nosound();
        !           229:        exit(0);
1.1       root      230: }
                    231: 
1.1.1.2 ! root      232: #else /* !unix */
        !           233: 
1.1       root      234: int cbreakh()  /* ctrl-break handler */
                    235: {
1.1.1.2 ! root      236:        aborted=1;
        !           237:        return(1);              /* 1 to continue, 0 to abort */
1.1       root      238: }
                    239: 
1.1.1.2 ! root      240: #endif
        !           241: 
        !           242: int main(int argc, char **argv)
1.1       root      243: {
1.1.1.2 ! root      244:        char *p,str[128];
1.1       root      245:        int i,j,file;
                    246:        FILE *stream;
                    247: 
1.1.1.2 ! root      248: #ifdef __unix__
        !           249: 
        !           250:        // sets up the terminal for one key entry (to cancel playback)
        !           251:        struct termios newterm;
        !           252:        tcgetattr(0,&stored);
        !           253:   
        !           254:        memcpy(&newterm,&stored,sizeof(struct termios));
        !           255:        newterm.c_lflag &= (~ICANON);
        !           256:        newterm.c_cc[VTIME] = 0;
        !           257:        newterm.c_cc[VMIN] = 1;
        !           258:        tcsetattr(0,TCSANOW,&newterm);
        !           259: 
        !           260:        // set up select() args
        !           261:        FD_ZERO(&inp);
        !           262:        FD_SET(0, &inp);
        !           263: 
        !           264:        // install Ctrl-C handler...
        !           265:        signal(SIGINT, cbreakh);
        !           266: 
        !           267: #endif
        !           268:   
1.1       root      269: #ifndef __OS2__
1.1.1.2 ! root      270:        #ifndef __unix__
        !           271:                ctrlbrk(cbreakh);
        !           272:        #else
        !           273:                fd = open("/dev/console", O_NOCTTY);
        !           274:                if (fd < 0) {
        !           275:                perror("open(\"/dev/console\"");
        !           276:                exit(-1);
        !           277:                }
        !           278:        #endif
1.1       root      279: #endif
                    280: 
1.1.1.2 ! root      281:        printf("\nTone Generation Utility  v1.01  Developed 1993 Rob Swindell\n\n");
1.1       root      282: 
1.1.1.2 ! root      283:        if(argc<2)
        !           284:                usage();
1.1       root      285: 
1.1.1.2 ! root      286:        mswtyp=0;
        !           287:        delay(0);
        !           288:        for(i=1;i<argc;i++) {
        !           289:                if(argv[i][0]=='/') {
        !           290:                        for(j=1;argv[i][j];j++)
        !           291:                                switch(toupper(argv[i][j])) {
        !           292:                                        case 'D':
        !           293:                                                mode^=SHOW_DOT;
        !           294:                                                break;
        !           295:                                        case 'F':
        !           296:                                                mode^=SHOW_FREQ;
        !           297:                                                break;
        !           298:                                        case 'N':
        !           299:                                                mode^=NOT_ABORTABLE;
        !           300:                                                break;
        !           301:                                        case 'V':
        !           302:                                                mode^=NO_VISUAL;
        !           303:                                                break;
        !           304:                                        case 'T':
        !           305:                                                mode^=USE_MSWAIT;
        !           306:                                                mswtyp=atoi(argv[i]+j+1);
        !           307:                                                while(isdigit(argv[i][j+1]))
        !           308:                                                        j++;
        !           309:                                                break;
        !           310:                                        default:
        !           311:                                                usage(); }
        !           312:                        continue; }
        !           313:                if(argv[i][0]=='+') {
        !           314:                        if((file=open(argv[i]+1, OPENARGS))==-1 || (stream=fdopen(file,"rb"))==NULL) {
        !           315:                                strcpy(str,argv[0]);
        !           316:                                p=strrchr(str,'\\');
        !           317:                                if(p)
        !           318:                                        *(p+1)=0;
        !           319:                                strcat(str,argv[i]+1);
        !           320:                                if((file=open(str, OPENARGS))==-1
        !           321:                                        || (stream=fdopen(file,"rb"))==NULL) {
        !           322:                                        printf("\7Error opening %s\n",argv[i]+1);
        !           323:                                        exit(1); } }
        !           324:                        while(mode&NOT_ABORTABLE || !kbhit()) {
        !           325:                                if(!fgets(str,81,stream))
1.1       root      326:                                        break;
1.1.1.2 ! root      327:                                if(!isalnum(str[0]))
        !           328:                                        continue;
        !           329:                                p=str;
        !           330:                                while(*p>' ')
        !           331:                                        p++;
        !           332:                                while(*p && *p<=' ')
        !           333:                                        p++;
        !           334:                                play(str,p); }
        !           335:                        fclose(stream);
        !           336:                        continue; }
        !           337:                play(argv[i],argv[i+1]);
        !           338:                i++;
        !           339:                if(aborted)
        !           340:                        break; }
        !           341:        nosound();
        !           342: 
        !           343: #ifdef __unix__
        !           344:        close(fd);                      // close /dev/console
        !           345:        tcsetattr(0,TCSANOW,&stored);   // reset terminal to previous state
        !           346: #endif
        !           347: 
        !           348: return(0);
1.1       root      349: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.