home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / dos / prg / dsik205 / dsik.dat / EXAMPLES / EXAMP07.C < prev    next >
C/C++ Source or Header  |  1995-04-10  |  8KB  |  260 lines

  1. /****************************************************************************
  2. *
  3. *                   Digital Sound Interface Kit (DSIK)
  4. *                            Version 2.00
  5. *
  6. *                           by Carlos Hasan
  7. *
  8. * Filename:     example7.c
  9. * Version:      Revision 1.0
  10. *
  11. * Language:     WATCOM C
  12. * Environment:  IBM PC (DOS/4GW)
  13. *
  14. * Description:  Music module and sound sequencer.
  15. *
  16. * Revision History:
  17. * ----------------
  18. *
  19. * Revision 1.0  94/10/28  23:16:47  chv
  20. * Initial revision
  21. *
  22. ****************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <conio.h>
  29. #include <i86.h>
  30. #include "audio.h"
  31. #include "import.h"
  32. #include "timer.h"
  33.  
  34. #ifdef SHAREWARE
  35. #define NUMVOICES   4
  36. #else
  37. #define NUMVOICES   16
  38. #endif
  39.  
  40. typedef struct {
  41.     int frame,sec,min,hour;
  42. } SMPTE;
  43.  
  44. SMPTE smpte;
  45.  
  46. /* timer interrupt handler called 100 times per second, it will update
  47.    the current SMPTE time and call the dPoll() routine */
  48.  
  49. void timer(void)
  50. {
  51.     if (++smpte.frame >= 100) {
  52.         smpte.frame = 0;
  53.         if (++smpte.sec >= 60) {
  54.             smpte.sec = 0;
  55.             if (++smpte.min >= 60) {
  56.                 smpte.min = 0;
  57.                 if (++smpte.hour >=  24) {
  58.                     smpte.hour = 0;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     dPoll();
  64. }
  65.  
  66. long getcurtime(void)
  67. {
  68.     return smpte.frame+100L*smpte.sec+6000L*smpte.min+360000L*smpte.hour;
  69. }
  70.  
  71. long gettime(int hour, int min, int sec, int frame)
  72. {
  73.     return frame+100L*sec+6000L*min+360000L*hour;
  74. }
  75.  
  76. void waittime(int hour, int min, int sec, int frame)
  77. {
  78.     long n;
  79.     n = gettime(hour,min,sec,frame);
  80.     while (n > getcurtime()) {
  81.         cprintf("[%02d:%02d:%02d.%02d]\r", smpte.hour, smpte.min,
  82.             smpte.sec, smpte.frame);
  83.         if (kbhit()) {
  84.             switch (toupper(getch())) {
  85.                 case 0x1B:
  86.                     cprintf("\nAborted.\n\r");
  87.                     exit(EXIT_FAILURE);
  88.                     break;
  89.                 case 0x0D:
  90.                     cprintf("\n\r");
  91.                     break;
  92.                 case 0x20:
  93.                     smpte.hour = hour;
  94.                     smpte.min = min;
  95.                     smpte.sec = sec;
  96.                     smpte.frame = frame;
  97.                     break;
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. int main(int argc, char *argv[])
  104. {
  105.     FILE *f;
  106.     DSM *mod,*m;
  107.     Sample *smp,*s;
  108.     SoundCard SC;
  109.     SMPTE t;
  110.     char seqpath[_MAX_PATH],path[_MAX_PATH];
  111.     char drive[_MAX_DRIVE],dir[_MAX_DIR],name[_MAX_FNAME],ext[_MAX_EXT];
  112.     int mvol,svol,sbal;
  113.     char buf[128];
  114.     int i,linenum;
  115.  
  116.     printf("\nSound Sequencer V1.00  Copyright (C) 1995 Carlos Hasan\n");
  117.     if (argc < 2) {
  118.         printf("Use: SEQ [options] eventfile[.seq]\n");
  119.         printf("\t-m###\tmusic volume (0-255)\n");
  120.         printf("\t-s###\tsfx volume (0-255)\n");
  121.         printf("\t-b###\tsfx balance (0=left,128=right)\n\n");
  122.         printf("Used to sequence MOD/S3M/MTM/STM/669/DSM music module files\n");
  123.         printf("and WAV/VOC/IFF/RAW/SMP sample files.\n");
  124.         exit(EXIT_FAILURE);
  125.     }
  126.  
  127.     strcpy(seqpath,"default.seq");
  128.     mvol = svol = 255;
  129.     sbal = 64;
  130.     for (i = 1; i < argc; i++) {
  131.         if (argv[i][0] == '-') {
  132.             switch (argv[i][1]) {
  133.                 case 'm':
  134.                     mvol = atoi(&argv[i][2]);
  135.                     break;
  136.                 case 's':
  137.                     svol = atoi(&argv[i][2]);
  138.                     break;
  139.                 case 'b':
  140.                     sbal = atoi(&argv[i][2]);
  141.                     break;
  142.                 default:
  143.                     printf("Unknown -%c option.\n",argv[i][1]);
  144.                     exit(EXIT_FAILURE);
  145.             }
  146.         }
  147.         else {
  148.             strncpy(seqpath,argv[i],sizeof(seqpath));
  149.         }
  150.     }
  151.     strupr(seqpath);
  152.     _splitpath(seqpath,drive,dir,name,ext);
  153.     if (!*ext) strcpy(ext,".SEQ");
  154.     _makepath(seqpath,drive,dir,name,ext);
  155.  
  156.     dRegisterDrivers();
  157.     if (dLoadSetup(&SC,"SETUP.CFG")) {
  158.         printf("Please run SETUP.EXE to configure.\n");
  159.         exit(EXIT_FAILURE);
  160.     }
  161.     printf("%s at Port %03Xh using IRQ %d on DMA channel %d\n\n",
  162.         dGetDriverStruc(SC.ID)->Name, SC.Port, SC.IrqLine, SC.DmaChannel);
  163.     if (dInit(&SC)) {
  164.         printf("Error initializing the sound system.\n");
  165.         exit(EXIT_FAILURE);
  166.     }
  167.     atexit((void(*)(void))dDone);
  168.     dSetupVoices(NUMVOICES+1,128);
  169.     dSetMusicVolume(mvol);
  170.     dSetSoundVolume(svol);
  171.     dSetVoiceBalance(NUMVOICES,sbal);
  172.  
  173.     dInitTimer();
  174.     atexit((void(*)(void))dDoneTimer);
  175.     dStartTimer(timer,TICKS(100));
  176.  
  177.     if (!(f = fopen(seqpath,"rt"))) {
  178.         printf("Error opening %s event file.\n",seqpath);
  179.         exit(EXIT_FAILURE);
  180.     }
  181.  
  182.     mod = NULL;
  183.     smp = NULL;
  184.     for (linenum = 1; fgets(buf,sizeof(buf),f); linenum++) {
  185.         if (buf[0] == ';' || buf[0] == '\n') continue;
  186.         if (sscanf(buf,"%02d:%02d:%02d.%02d %s", &t.hour, &t.min,
  187.           &t.sec, &t.frame, path) != 5) {
  188.             printf("Error reading file %s line %d.\n", seqpath, linenum);
  189.             exit(EXIT_FAILURE);
  190.         }
  191.         strupr(path);
  192.         _splitpath(path,drive,dir,name,ext);
  193.         _makepath(path,drive,dir,name,ext);
  194.  
  195.         if (!strcmp(ext,".DSM")) i = FORM_DSM;
  196.         else if (!strcmp(ext,".MOD")) i = FORM_MOD;
  197.         else if (!strcmp(ext,".S3M")) i = FORM_S3M;
  198.         else if (!strcmp(ext,".MTM")) i = FORM_MTM;
  199.         else if (!strcmp(ext,".669")) i = FORM_669;
  200.         else if (!strcmp(ext,".STM")) i = FORM_STM;
  201.         else if (!strcmp(ext,".WAV")) i = FORM_WAV;
  202.         else if (!strcmp(ext,".VOC")) i = FORM_VOC;
  203.         else if (!strcmp(ext,".IFF")) i = FORM_IFF;
  204.         else if (!strcmp(ext,".RAW")) i = FORM_RAW;
  205.         else if (!strcmp(ext,".SMP")) i = FORM_RAW;
  206.         else {
  207.             printf("Unknown file format: %s\n",path);
  208.             exit(EXIT_FAILURE);
  209.         }
  210.         if (i < FORM_WAV) {
  211.             if (!(m = dImportModule(path,i))) {
  212.                 printf("Error (%03d) loading %s module file: %s.\n",
  213.                     dError, path, dErrorMsg[dError]);
  214.                 exit(EXIT_FAILURE);
  215.             }
  216.             waittime(t.hour,t.min,t.sec,t.frame);
  217.             if (mod) {
  218.                 for (i = mvol; i >= 0; i--) {
  219.                     dSetMusicVolume(i);
  220.                     delay(5);
  221.                 }
  222.                 dStopMusic();
  223.                 dFreeModule(mod);
  224.             }
  225.             printf("Playing module %s%s\n",name,ext);
  226.             mod = m;
  227.             dPlayMusic(mod);
  228.             dSetMusicVolume(mvol);
  229.         }
  230.         else {
  231.             if (!(s = dImportSample(path,i))) {
  232.                 printf("Error (%03d) loading %s sample file: %s.\n",
  233.                     dError, path, dErrorMsg[dError]);
  234.                 exit(EXIT_FAILURE);
  235.             }
  236.             waittime(t.hour,t.min,t.sec,t.frame);
  237.             if (smp) {
  238.                 dStopVoice(NUMVOICES);
  239.                 dFreeSample(smp);
  240.             }
  241.             printf("Playing sample %s%s\n",name,ext);
  242.             smp = s;
  243.             dPlayVoice(NUMVOICES,smp);
  244.         }
  245.     }
  246.     fclose(f);
  247.  
  248.     printf("Press SPACE to quit.\n");
  249.     waittime(23,59,59,0);
  250.  
  251.     dStopMusic();
  252.     dStopVoice(NUMVOICES);
  253.  
  254.     if (mod) dFreeModule(mod);
  255.     if (smp) dFreeSample(smp);
  256.  
  257.     printf("Done.        \n");
  258.     return EXIT_SUCCESS;
  259. }
  260.