home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ELYVER10.ZIP / MIKXMAS.ZIP / source / MIKMOD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-12  |  6.3 KB  |  312 lines

  1. /*
  2.  
  3. Name:
  4. MIKMOD.C
  5.  
  6. Description:
  7. Modplaying example of mikmod.
  8.  
  9. MSDOS:    BC(y)    Watcom(y)    DJGPP(y)
  10. Win95:    BC(y*)
  11. Os2:    y
  12. Linux:    n
  13.  
  14. * console mode only
  15. (y) - yes
  16. (n) - no (not possible or not useful)
  17. (?) - may be possible, but not tested
  18.  
  19. */
  20. #ifdef __WIN32__
  21. #include <windows.h>
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <conio.h>
  27. #include <string.h>
  28.  
  29. #ifndef __OS2__
  30. #include <dos.h>
  31. #endif
  32.  
  33. #include "wildfile.h"
  34. #include "mikmod.h"
  35.  
  36. char helptext[]=
  37.  
  38. "Available switches (CaSe SeNsItIvE!):\n"
  39. "\n"
  40. "  /d x    use device-driver #x for output (0 is autodetect). Default=0\n"
  41. "  /ld     List all available device-drivers\n"
  42. "  /ll     List all available loaders\n"
  43. "  /x      disables protracker extended speed\n"
  44. "  /p      disables panning effects (9fingers.mod)\n"
  45. "  /v xx   Sets volume from 0 (silence) to 100. Default=100\n"
  46. "  /f xxxx Sets mixing frequency. Default=44100\n"
  47. "  /m      Force mono output (so sb-pro can mix at 44100)\n"
  48. "  /8      Force 8 bit output\n"
  49. "  /i      Use interpolated mixing\n"
  50. "  /r      Restart a module when it's done playing";
  51.  
  52.  
  53. /*
  54.     declarations for boring old sys-v style getopt *yawn*:
  55. */
  56. int     getopt(int argc, char *argv[], char *optionS);
  57. extern char *optarg;
  58. extern int optind;
  59. extern int opterr;
  60.  
  61.  
  62. void tickhandler(void)
  63. {
  64.     MP_HandleTick();    /* play 1 tick of the module */
  65.     MD_SetBPM(mp_bpm);
  66. }
  67.  
  68.  
  69. int main(int argc,char *argv[])
  70. {
  71.     UNIMOD *mf;
  72.     int cmderr=0;                   /* error in commandline flag */
  73.     int morehelp=0;                 /* set if user wants more help */
  74.     int quit;
  75.     int t;
  76.     static int nargc;
  77.     static char **nargv;
  78.  
  79.     puts(mikbanner);
  80.  
  81.     /* Expand wildcards on commandline */
  82.  
  83.     nargc=argc; nargv=argv;
  84. #ifndef __DJGPP__
  85.     MyGlob(&nargc,&nargv,0);
  86. #else
  87.     setvbuf(stdout, NULL, _IONBF, 0);
  88.     __djgpp_set_ctrl_c(0);
  89. #endif
  90.  
  91.     /*
  92.         Initialize soundcard parameters.. you _have_ to do this
  93.         before calling MD_Init(), and it's illegal to change them
  94.         after you've called MD_Init()
  95.     */
  96.  
  97.     md_mixfreq      =44100;                     /* standard mixing freq */
  98.     md_dmabufsize   =20000;                     /* standard dma buf size */
  99.     md_mode         =DMODE_16BITS|DMODE_STEREO; /* standard mixing mode */
  100.     md_device       =0;                                                     /* standard device: autodetect */
  101.  
  102.     /*
  103.         Register the loaders we want to use..
  104.     */
  105.  
  106.     ML_RegisterLoader(&load_m15);    /* if you use m15load, register it as first! */
  107.     ML_RegisterLoader(&load_mod);
  108.     ML_RegisterLoader(&load_mtm);
  109.     ML_RegisterLoader(&load_s3m);
  110.     ML_RegisterLoader(&load_stm);
  111.     ML_RegisterLoader(&load_ult);
  112.     ML_RegisterLoader(&load_uni);
  113.     ML_RegisterLoader(&load_xm);
  114.  
  115.     /*
  116.         Register the drivers we want to use:
  117.     */
  118.  
  119.     MD_RegisterDriver(&drv_nos);
  120. #ifdef __OS2__
  121.     MD_RegisterDriver(&drv_os2);
  122. #elif defined(__WIN32__)
  123.     MD_RegisterDriver(&drv_w95);
  124. #else
  125.     MD_RegisterDriver(&drv_ss);
  126.     MD_RegisterDriver(&drv_sb);
  127.     MD_RegisterDriver(&drv_gus);
  128. #endif
  129.  
  130.     MD_RegisterPlayer(tickhandler);
  131.  
  132.     /* Parse option switches using standard getopt function: */
  133.  
  134.     opterr=0;
  135.  
  136.     while( !cmderr &&
  137.           (t=getopt(nargc,nargv,"ohxpm8irv:f:l:d:")) != EOF ){
  138.  
  139.         switch(t){
  140.  
  141.             case 'd':
  142.                 md_device=atoi(optarg);
  143.                 break;
  144.  
  145.             case 'l':
  146.                 if(optarg[0]=='d') MD_InfoDriver();
  147.                 else if(optarg[0]=='l') ML_InfoLoader();
  148.                 else{
  149.                     cmderr=1;
  150.                     break;
  151.                 }
  152.                 exit(0);
  153.  
  154.             case 'r':
  155.                 mp_loop=1;
  156.                 break;
  157.  
  158.             case 'm':
  159.                 md_mode&=~DMODE_STEREO;
  160.                 break;
  161.  
  162.             case '8':
  163.                 md_mode&=~DMODE_16BITS;
  164.                 break;
  165.  
  166.             case 'i':
  167.                 md_mode|=DMODE_INTERP;
  168.                 break;
  169.  
  170.             case 'x':
  171.                 mp_extspd=0;
  172.                 break;
  173.  
  174.             case 'p':
  175.                 mp_panning=0;
  176.                 break;
  177.  
  178.             case 'v':
  179.                 if((mp_volume=atoi(optarg))>100) mp_volume=100;
  180.                 break;
  181.  
  182.             case 'f':
  183.                 md_mixfreq=atol(optarg);
  184.                 break;
  185.  
  186.             case 'h':
  187.                 morehelp=1;
  188.                 cmderr=1;
  189.                 break;
  190.  
  191.             case '?':
  192.                 puts("\07Invalid switch or option needs an argument\n");
  193.                 cmderr=1;
  194.                 break;
  195.         }
  196.     }
  197.  
  198.     if(cmderr || optind>=nargc){
  199.  
  200.         /*
  201.             there was an error in the commandline, or there were no true
  202.             arguments, so display a usage message
  203.         */
  204.  
  205.         puts("Usage: MIKMOD [switches] <fletch.mod> ... \n");
  206.  
  207.         if(morehelp)
  208.             puts(helptext);
  209.         else
  210.             puts("Type MIKMOD /h for more help.");
  211.  
  212.         exit(-1);
  213.     }
  214.  
  215.     /*  initialize soundcard */
  216.  
  217.     if(!MD_Init()){
  218.         printf("Driver error: %s.\n",myerr);
  219.         return 0;
  220.     }
  221.  
  222.     printf("Using %s for %d bit %s %s sound at %u Hz\n\n",
  223.             md_driver->Name,
  224.             (md_mode&DMODE_16BITS) ? 16:8,
  225.             (md_mode&DMODE_INTERP) ? "interpolated":"normal",
  226.             (md_mode&DMODE_STEREO) ? "stereo":"mono",
  227.             md_mixfreq);
  228.  
  229. #ifdef __OS2__
  230.     DosSetPriority( PRTYS_THREAD, PRTYC_TIMECRITICAL, 5L, 0UL );
  231. #endif
  232.  
  233.     for(quit=0; !quit && optind<nargc; optind++){
  234.  
  235.         printf("File    : %s\n",nargv[optind]);
  236.  
  237.         /* load the module */
  238.  
  239.         mf=ML_LoadFN(nargv[optind]);
  240.  
  241.         /* didn't work -> exit with errormsg. */
  242.  
  243.         if(mf==NULL){
  244.             printf("MikMod Error: %s\n",myerr);
  245.             break;
  246.         }
  247.  
  248.         /*      initialize modplayer to play this module */
  249.  
  250.         MP_Init(mf);
  251.  
  252.         printf( "Songname: %s\n"
  253.                 "Modtype : %s\n"
  254.                 "Periods : %s,%s\n",
  255.                 mf->songname,
  256.                 mf->modtype,
  257.                 (mf->flags&UF_XMPERIODS) ? "XM type" : "mod type",
  258.                 (mf->flags&UF_LINEAR) ? "Linear" : "Log");
  259.  
  260.         /*
  261.             set the number of voices to use.. you
  262.             could add extra channels here (e.g. md_numchn=mf->numchn+4; )
  263.             to use for your own soundeffects:
  264.         */
  265.  
  266.         md_numchn=mf->numchn;
  267.  
  268.         /*  start playing the module: */
  269.  
  270.         MD_PlayStart();
  271.  
  272.         while(!MP_Ready()){
  273.  
  274.             char c;
  275.  
  276.             c=kbhit() ? getch() : 0;
  277.  
  278.             if(c=='+')
  279.                 MP_NextPosition();
  280.             else if(c=='-')
  281.                 MP_PrevPosition();
  282.             else if(c==0x1b){
  283.                 quit=1;
  284.                 break;
  285.             }
  286.             else if(c==' ') break;
  287.  
  288.             MD_Update();
  289.  
  290.             /* wait a bit */
  291.  
  292. #if defined(__OS2__)
  293.             DosSleep(40);   /* hmmmm */
  294. #elif defined(__WIN32__)
  295.             Sleep(10);
  296. #elif defined(__DJGPP__)
  297.                         /* no wait for djgpp ? */
  298. #else
  299.                         delay(10);
  300. #endif
  301.                         printf("\rsngpos:%d patpos:%d sngspd %d bpm %d   ",mp_sngpos,mp_patpos,mp_sngspd,mp_bpm);
  302.         }
  303.  
  304.         MD_PlayStop();          /* stop playing */
  305.         ML_Free(mf);            /* and free the module */
  306.         puts("\n");
  307.     }
  308.  
  309.     MD_Exit();
  310.     return 0;
  311. }
  312.