home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / sound / mp2 / src / gem / mp2audio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-08  |  7.4 KB  |  369 lines

  1. /* MPEG Audio Layer II Decoder */
  2.  
  3. #include <tos.h>
  4. #include <vdi.h>
  5. #include <aes.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #include "version.h"
  12. #include "snddefs.h"
  13. #include "mp2audio.h"
  14. #include "mp2info.h"
  15. #include "mp2conf.h"
  16.  
  17. #include "mp2ctrl.rsh"    /* rsc file */
  18. #include "mp2ctrl.rh"
  19. #include "mp2wind.h"
  20.  
  21. /* global variables */
  22. int time_slice=DEFAULT_TIME_SLICE,count_dir=DEFAULT_COUNT_DIR;
  23. int find_first=0,play_first=0;
  24. long block=(long)DEFAULT_BLOCK_SIZE*1024L;
  25. int fd,replay,file_open,ext,quit;
  26. int app_id, acc_id, vdi_id;
  27. long buffer;
  28. WINDFORM windforms[5];
  29. #ifdef STATIC_BUFFER
  30. char buffer_mem[(long)BLOCK_SIZE];
  31. #else
  32. char *buffer_mem;
  33. #endif
  34.  
  35. extern int _app;
  36.  
  37. /* global variables from mp2file.c */
  38. extern char path[512], filename[512];
  39. extern long filesize,filepos;
  40.  
  41. /* global variables from mp2event.c */
  42. extern long total_time;
  43.  
  44. /* Function in this module */
  45. void setup_rsrc(int nbpl);
  46. void readconfig(void);
  47. int interpret(char *, char **);
  48. void unix2dos(char *);
  49.  
  50. /* Functions from mp2init.c */
  51. extern int external_clock(void);
  52. extern void init_replay(void);
  53. extern int open_file(void);
  54.  
  55. /* Functions from mp2exit.c */
  56. extern void exit_replay(void);
  57.  
  58. /* Functions from mp2file.c */
  59. extern void close_file(int fd);
  60. extern int reopen_file(void);
  61. extern int next_song(char *, char *, int);
  62.  
  63. /* Functions from mp2event.c */
  64. extern void main_event_loop(void);
  65. extern void fg_init(WINDFORM *wind);
  66. extern void update_time(void);
  67. extern void setfilename(char *);
  68.  
  69. /* Functions from mp2info.c */
  70. extern int getmp2info(int);
  71. extern void show_mp2_error(int);
  72.  
  73. int main(int argc, char *argv[])
  74. {
  75.     int wchar,hchar,wbar,hbar;
  76.     int work_in[11]={ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 };
  77.     int work_out[57];
  78.  
  79.     replay = 0;
  80.     
  81.     /* default clock is automatically choosen */
  82.     ext=external_clock();
  83.  
  84.     /* default path is current */
  85.     path[0] = 'A' + (char)Dgetdrv();
  86.     path[1] = ':';
  87.     Dgetpath(path+2, 0);
  88.     strcat(path, "\\*.MP?");
  89.  
  90.     readconfig();
  91.  
  92.     if ((app_id = appl_init()) >= 0)
  93.     {
  94. #ifndef STATIC_BUFFER
  95.         if((buffer_mem=(char *)malloc((size_t)block))==NULL)
  96.         {
  97.             form_alert(1,"[1][Not enough memory!][OK]");
  98.             appl_exit();
  99.             return 1;
  100.         }
  101. #endif
  102.  
  103.         if(!_app)
  104.             acc_id = menu_register(app_id, "  MPEG2 Audio ");
  105.         
  106.         work_in[0]=graf_handle(&wchar,&hchar,&wbar,&hbar);
  107.         v_opnvwk(work_in,&vdi_id,work_out);
  108.         if(vdi_id > 0)
  109.         {
  110.             vq_extnd(vdi_id,1,work_out);
  111.             v_clsvwk(vdi_id);
  112.             setup_rsrc(work_out[4]);
  113.             if (_app) 
  114.             {
  115.                 fg_init(&windforms[WIND_CTRL]);
  116.                 graf_mouse(ARROW,NULL);
  117.                 if(argc>1)
  118.                 {
  119.                     int error;
  120.                     char *o;
  121.                     
  122.                     if((o=strrchr(argv[1],'\\'))==NULL)
  123.                     {
  124.                         strcpy(filename,argv[1]);
  125.                     }
  126.                     else
  127.                     {
  128.                         strcpy(path,argv[1]);
  129.                         strcpy(filename,&o[1]);
  130.                         o=strrchr(path,'\\');
  131.                         strncpy(o,"\\*.MP?",6);
  132.                         o[6] = '\0';
  133.                     }
  134.                     filepos=0;
  135.                     fd=reopen_file();
  136.                     filesize=Fseek(0L,fd,2);
  137.                     Fseek(0L,fd,0);
  138.                     if((error=getmp2info(fd))==MP2_NOERR)
  139.                     {
  140.                         update_time();
  141.                         setfilename(filename);
  142.                         init_replay();
  143.                     }
  144.                     else
  145.                     {
  146.                         show_mp2_error(error);
  147.                         close_file(fd);
  148.                         strcpy(windforms[WIND_CTRL].wind_title,"MPEG");
  149.                         wind_set(windforms[WIND_CTRL].whandle,WF_NAME,
  150.                             windforms[WIND_CTRL].wind_title);
  151.                         setfilename("MPEGFILE");
  152.                     }
  153.                 }
  154.                 else if(find_first)
  155.                 {
  156.                     int tfd;
  157.  
  158.                     filename[0] = '\0';
  159.  
  160.                     while((tfd=next_song(path, filename, 1))!=0)
  161.                     {
  162.                         if(getmp2info(tfd)!=MP2_NOERR)
  163.                         {
  164.                             close_file(tfd);
  165.                         } 
  166.                         else
  167.                         {
  168.                             fd = tfd;
  169.                             file_open=1;
  170.                             update_time();
  171.                             setfilename(filename);
  172.                             if(play_first)
  173.                                 init_replay();
  174.                             break;
  175.                         }
  176.                     }
  177.                 }
  178.                 main_event_loop();
  179.                 if (replay)
  180.                     exit_replay();
  181.             } 
  182.             else
  183.             {
  184.                 if (acc_id >= 0)
  185.                     main_event_loop();
  186.             }
  187.         }
  188.         appl_exit();
  189. #ifndef STATIC_BUFFER
  190.         free(buffer_mem);
  191. #endif
  192.     }
  193.     else
  194.         fprintf(stderr, "Que pasa?\n");
  195.     return 0;
  196. }
  197.  
  198. void setup_rsrc(int nbpl)
  199. {
  200.     int ob;
  201.     char ver[64];
  202.     
  203.     for(ob=0 ; ob<NUM_OBS ; ob++)
  204.         rsrc_obfix(rs_object,ob);
  205.  
  206.     switch(nbpl)
  207.     {
  208.         case 1:
  209.         case 2:
  210.             windforms[WIND_CTRL].formtree=rs_trindex[FORM_CTRL1];
  211.             windforms[WIND_INFO].formtree=rs_trindex[FORM_INFO1];
  212.             break;
  213.         case 4:
  214.         default:
  215.             windforms[WIND_CTRL].formtree=rs_trindex[FORM_CTRL4];
  216.             windforms[WIND_INFO].formtree=rs_trindex[FORM_INFO4];
  217.     }
  218.  
  219.     strcpy(windforms[WIND_CTRL].wind_title,"MPEG");
  220.     strcpy(windforms[WIND_INFO].wind_title,"Info");
  221.     strcpy(ver,"Version ");
  222.     strcat(ver,VERSION_TEXT);
  223.     strcpy(windforms[WIND_INFO].formtree[INFO_VERSION].ob_spec.tedinfo->te_ptext,ver);
  224.     windforms[WIND_CTRL].windkind=(MOVER | CLOSER | NAME);
  225.     windforms[WIND_INFO].windkind=(MOVER | CLOSER | NAME);
  226.     windforms[WIND_CTRL].firstobj=CTRL_FIRST;
  227.     windforms[WIND_INFO].firstobj=INFO_FIRST;
  228.     windforms[WIND_CTRL].objdepth=2;
  229.     windforms[WIND_INFO].objdepth=1;
  230.     
  231.     if(!count_dir) /* default set to count up */
  232.     {
  233.         objc_change(windforms[WIND_CTRL].formtree,CTRL_TIME,0,
  234.             windforms[WIND_CTRL].form.x,windforms[WIND_CTRL].form.y,
  235.             windforms[WIND_CTRL].form.w,windforms[WIND_CTRL].form.h,
  236.             SELECTED,0);
  237.         windforms[WIND_CTRL].formtree[windforms[WIND_CTRL].formtree[CTRL_TIME].ob_head].ob_x++;
  238.         windforms[WIND_CTRL].formtree[windforms[WIND_CTRL].formtree[CTRL_TIME].ob_head].ob_y++;
  239.     }
  240. }
  241.  
  242. void readconfig()
  243. {
  244.     FILE *cf;
  245.     char line[256],*value=line;
  246.     
  247.     cf=fopen("mp2audio.cnf","r");
  248.     if(cf == NULL)
  249.         cf=fopen("c:\\mp2audio.cnf","r");
  250.  
  251.     if(cf != NULL)
  252.     {
  253.         do
  254.         {
  255.             fgets(line,255,cf);
  256.             if(line[0]!='#' || isspace((int)line[0]))
  257.             {
  258.                 switch(interpret(line,&value))
  259.                 {
  260.                     case CF_BLOCK_SIZE:
  261. #ifdef STATIC_BUFFER
  262.                         block = (long)BLOCK_SIZE;
  263. #else
  264.                         /* config value is in kb */
  265.                         block = atol(value)*1024L;
  266. #endif
  267.                         break;
  268.                     case CF_COUNT_DIR:
  269.                         if(!strcmp(strlwr(value),"up"))
  270.                             count_dir=0;
  271.                         else if(!strcmp(strlwr(value),"down"))
  272.                             count_dir=1;
  273.                         break;
  274.                     case CF_TIME_SLICE:
  275.                         time_slice = atoi(value);
  276.                         break;
  277.                     case CF_DEFAULT_PATH:
  278.                         strupr(value);
  279.                         strcpy(path,value);
  280.                         unix2dos(path);
  281.                         strcat(path,"*.MP?");
  282.                         break;
  283.                     case CF_FORCE_CLOCK:
  284.                         if(!strcmp(strlwr(value),"internal"))
  285.                             ext=0;
  286.                         else if(!strcmp(strlwr(value),"external"))
  287.                             ext=1;
  288.                         break;
  289.                     case CF_FIND_FIRST:
  290.                         if(!strcmp(value,"yes"))
  291.                             find_first=1;
  292.                         break;
  293.                     case CF_PLAY_FIRST:
  294.                         if(!strcmp(value,"yes"))
  295.                         {
  296.                             play_first=1;
  297.                             find_first=1;
  298.                         }
  299.                     default:
  300.                         /* Unknown or invalid config, ignored */
  301.                         break;
  302.                 }
  303.             }
  304.         } while(!feof(cf));
  305.         fclose(cf);
  306.     }
  307. }
  308.  
  309. int interpret(char *line,char **value)
  310. {
  311.     int i=0;
  312.     char *tmp,*tt;
  313.  
  314.     tt=strchr(line,'=');
  315.         
  316.     if(tt!=NULL)
  317.     {
  318.         /* remove possible spaces before '=' */
  319.         tmp=tt;
  320.         tmp--;
  321.         while(*tmp==' ' || *tmp=='\t') tmp--;
  322.         tmp[1]='\0';
  323.  
  324.         /* remove possible spaces after '=' */
  325.         tmp=tt;
  326.         tmp++;
  327.         while((*tmp==' ' || *tmp=='\t') && *tmp) tmp++;
  328.         if(!*tmp) /* invalid value to argument */
  329.             return 0;
  330.         *value=tmp;
  331.  
  332.         /* remove possible spaces after value */
  333.         while(!isspace((int)*tmp)) tmp++;
  334.         *tmp='\0';
  335.         
  336.         while(config_args[i].arg)
  337.         {
  338.             if(!strcmp(line,config_args[i].arg))
  339.                 return config_args[i].define;
  340.             i++;
  341.         }
  342.     }
  343.     /* unknown or invalid config argument */
  344.     return 0;
  345. }
  346.  
  347. void unix2dos(char *path)
  348. {
  349.     int i=0;
  350.     char tmp[512];
  351.  
  352.     if(path[0] == '/')
  353.     {
  354.         strcpy(tmp,&path[1]);
  355.         strcpy(path,"U:\\");
  356.         strcat(path,tmp);
  357.         while(path[i])
  358.         {
  359.             if(path[i] == '/')
  360.                 path[i] = '\\';
  361.             i++;
  362.         }
  363.     }
  364.     if(path[strlen(path)-1] != '\\')
  365.         strcat(path,"\\");
  366. }
  367.  
  368.  
  369.