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

  1. #include <tos.h>
  2. #include <aes.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include "snddefs.h"
  8. #include "mp2audio.h"
  9. #include "mp2wind.h"
  10. #include "mp2info.h"
  11.  
  12. /* #define DEBUG */
  13.  
  14. /* Functions in this module */
  15. int open_file(void);
  16. int reopen_file(void);
  17. void close_file(int fd);
  18. void reset_file(int fd);
  19. void real_load(long l, char *b);
  20. void load(int q);
  21. void set_stop(long *buf);
  22. int has_stopped(int state);
  23. int match(char *, char *, int);
  24. int next_song(char *, char *, int);
  25.  
  26. /* global variables */
  27. char path[512]="\0", filename[512]="\0";
  28. long filesize,filepos,bufend=0,bufferpos=0;
  29.  
  30. /* global variables from mp2audio.c */
  31. extern int file_open,fd,quit,replay;
  32. extern long buffer,block;
  33. extern WINDFORM windforms[5];
  34. #ifdef STATIC_BUFFER
  35. extern char buffer_mem[(long)BLOCK_SIZE];
  36. #else
  37. extern char *buffer_mem;
  38. #endif
  39.  
  40. /* global variables from mp2event.c */
  41. extern long total_time;
  42. extern int looping;
  43.  
  44. /* global variable from mp2init.c */
  45. extern int buf_init;
  46.  
  47. /* Function from mp2info.c */
  48. extern int getmp2info(int);
  49.  
  50. /* Function from mp2exit.c */
  51. extern void exit_replay(void);
  52.  
  53. /* Function from mp2init.c */
  54. extern void init_replay(void);
  55.  
  56. /* Functions from mp2event.c */
  57. extern void toggle_object(WINDFORM *, int, int);
  58. extern void update_time(void);
  59. extern void setfilename(char *);
  60.  
  61. int open_file(void)
  62. {
  63.     int button,ret;
  64.     char tmp[1024],tfn[512];
  65.  
  66.     strcpy(tfn,filename);
  67.     fsel_exinput(path, tfn, &button, "Load MPEG");
  68.     if (button == 1) {
  69.         if(*tfn)
  70.         {
  71.             if(replay)
  72.                 exit_replay();
  73.             if(file_open)
  74.                 close_file(fd);
  75.             strcpy(tmp,path);
  76.             strrchr(tmp, '\\')[1] = '\0';
  77.             if((ret=(int) Fopen(strcat(tmp, tfn), FO_READ)) > 0)
  78.             {
  79.                 file_open=1;
  80.                 filesize=Fseek(0L,ret,2);
  81.                 Fseek(0L,ret,0);
  82.                 bufferpos = filepos = 0;
  83.                 strcpy(filename,tfn);
  84.             }
  85.             return ret;
  86.         }
  87.     }
  88.     return 0;
  89. }
  90.  
  91. int reopen_file()
  92. {
  93.     char tmp[1024];
  94.     int tfd;
  95.     long fp;
  96.  
  97.     strcpy(tmp,path);
  98.     strrchr(tmp, '\\')[1] = '\0';
  99.     file_open=1;
  100.     tfd=(int)Fopen(strcat(tmp, filename), FO_READ);
  101.     fp=Fseek(filepos,tfd,0);
  102.  
  103.     fp=fp;
  104. #ifdef DEBUG
  105.     sprintf(tmp,"[1][New filepos: %ld|Should be: %ld][Ok]",fp,filepos);
  106.     form_alert(1,tmp);
  107. #endif
  108.     return tfd;
  109. }
  110.  
  111. void close_file(int fd)
  112. {
  113.     file_open=0;
  114.     Fclose(fd);
  115. }
  116.  
  117. void reset_file(int fd)
  118. {
  119.     Fseek(0L,fd,0);
  120.     buf_init = 0;
  121.     bufferpos = filepos = 0;
  122. }
  123.  
  124.  
  125.  
  126. void real_load(long l, char *b)
  127. {
  128.     long a=l;
  129.     long tmp,t=(long)b;
  130.  
  131. #ifdef DEBUG
  132.     long ffp;
  133.     char tt[128];
  134. #endif
  135.  
  136.     while (a > 0)
  137.     {
  138.  
  139. #ifdef DEBUG
  140.         ffp=Fseek(0L,fd,1);
  141.         sprintf(tt,"[1][%d: a=%ld|t=%lx fpos=%ld][Ok]",fd,a,t,ffp);
  142.         puts(tt);
  143. #endif
  144.             
  145.         tmp=Fread(fd, a & 0xfffffffeL, (void *)t);
  146.  
  147. #ifdef DEBUG
  148.         ffp=Fseek(0L,fd,1);
  149.         sprintf(tt,"[1][%d Read tmp=%ld|a=%ld|t=%lx fpos=%ld][Ok]",fd,tmp,a,t,ffp);
  150.         puts(tt);
  151. #endif
  152.  
  153.         if (tmp < 0)
  154.         {
  155. #ifdef DEBUG
  156.             ffp=Fseek(0L,fd,1);
  157.             sprintf(tt,"[1][%d reset tmp<0|a=%ld t=%lx|tmp=%ld fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  158.             puts(tt);
  159. #endif
  160.             bufend=t;
  161.             for( ; a>0 ; a--,t++)
  162.                 *((char *)t) = 0x42;
  163.         }
  164.         else
  165.         {
  166.             filepos += tmp;
  167.         
  168.             a-=tmp;
  169.             t+=tmp;
  170.             if(a >= 1)
  171.             {
  172.  
  173. #ifdef DEBUG
  174.                 ffp=Fseek(0L,fd,1);
  175.                 sprintf(tt,"[1][%d reset a>0 a=%ld|t=%lx tmp=%ld|fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  176.                 puts(tt);
  177. #endif
  178.                 bufend=t;
  179. #ifdef DEBUG
  180.                 printf("bufend = %lx\n",bufend);
  181. #endif
  182.                 for( ; a>0 ; a--,t++)
  183.                     *((char *)t) = 0x42;
  184.             }
  185.         }
  186.     }
  187. }
  188.  
  189.  
  190. int match(char *s1, char *s2, int dir)
  191. {
  192.     int n=0;
  193.  
  194.     if(dir) /* Find next */
  195.     {
  196.         if(strcmp(s1,s2) < 0)
  197.         {
  198.             n++;
  199.             while(*(s1++) == *(s2++))
  200.                 n++;
  201.         }
  202.     }
  203.     else /* Find previous */
  204.     {
  205.         if(strcmp(s1,s2) > 0)
  206.         {
  207.             n++;
  208.             while(*(s1++) == *(s2++))
  209.                 n++;
  210.         }
  211.     }
  212.     return n;
  213. }
  214.  
  215. int next_song(char *path, char *filename, int dir)
  216. {
  217.     int next_fd,ok;
  218.     DTA *dta;
  219.     char tmp[1024];
  220.     int q, score=0;
  221.  
  222.     dta = Fgetdta();
  223.     strcpy(tmp, path);
  224.     strrchr(tmp, '\\')[1] = '\0';
  225.     ok = Fsfirst(strcat(tmp, "*.MP?"), 0);
  226.     while (ok == 0) {
  227.         if ((dta->d_attrib & (FA_VOLUME | FA_SUBDIR)) == 0) {
  228.             q = match(filename, dta->d_fname, dir);
  229. #ifdef DEBUG
  230.             fprintf(stderr,"Matching %s and %s with match=%d\n",filename,dta->d_fname,q);
  231. #endif
  232.             if (score < q)
  233.             {
  234.                 score = q;
  235.                 strcpy(tmp, dta->d_fname);
  236. #ifdef DEBUG
  237.                 fprintf(stderr,"Score choice = %s\n",tmp);
  238. #endif
  239.             }
  240.             else if(score == q)
  241.             {
  242.                 if(dir) /* Find next */
  243.                 {
  244.                     if(strcmp(tmp, dta->d_fname) > 0)
  245.                     {
  246.                         strcpy(tmp, dta->d_fname);
  247. #ifdef DEBUG
  248.                         fprintf(stderr,"Diff choice = %s\n",tmp);
  249. #endif
  250.                     }
  251.                 }
  252.                 else /* Find previous */
  253.                 {
  254.                     if(strcmp(tmp, dta->d_fname) < 0)
  255.                     {
  256.                         strcpy(tmp, dta->d_fname);
  257. #ifdef DEBUG
  258.                         fprintf(stderr,"Diff choice = %s\n",tmp);
  259. #endif
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.         ok = Fsnext();
  265.     }
  266.     
  267.     if (score) {
  268.         strcpy(filename, tmp);
  269.         strcpy(tmp, path);
  270.         strrchr(tmp, '\\')[1] = '\0';
  271.         if((next_fd=(int) Fopen(strcat(tmp, filename), FO_READ)) > 0)
  272.         {
  273.             filesize=Fseek(0L,next_fd,2);
  274.             Fseek(0L,next_fd,0);
  275.             return next_fd;
  276.         }
  277.     }
  278.     return 0;  /* No next file */
  279. }
  280.  
  281.  
  282. void load(int q)
  283. {
  284.     static int state = 0;
  285.     static long lastbuf=0;
  286.     long ptr[4];
  287.     
  288. #ifdef DEBUG
  289.     long bo;
  290.     char tt[128];
  291. #endif
  292.  
  293.     buffptr(ptr);
  294.     buffer = ptr[0];
  295.  
  296.     if(buffer < lastbuf)
  297.         bufferpos+=block;
  298.         
  299.     lastbuf=buffer;
  300.     
  301.     if(!q)
  302.     {
  303.         bufferpos=filepos;
  304.         lastbuf=0;
  305.         state = 0;
  306.         bufend=0;
  307.     }
  308.     else
  309.     {
  310.         if (has_stopped(state))
  311.         {
  312.             if(bufend)
  313.             {
  314. #ifdef DEBUG
  315.                 printf("Song reset!\n");
  316. #endif
  317.                 if(looping)
  318.                 {
  319.                     int ff;
  320.                     
  321.                     reset_file(fd);
  322.                     update_time();
  323.                     ff=Dsp_Hf1(-1);
  324.                     init_replay();
  325.                     Dsp_Hf1(ff);
  326.                 } else
  327.                 {
  328.                     int next_fd,ff;
  329.  
  330.                     ff=Dsp_Hf1(-1);
  331.                     while((next_fd=next_song(path, filename, 1))!=0)
  332.                     {
  333.                         if (getmp2info(next_fd)!=MP2_NOERR) {
  334.                             close_file(next_fd);
  335.                         } else
  336.                         {
  337.                             if(file_open)
  338.                                 close_file(fd);
  339.                             fd = next_fd;
  340.                             file_open=1;
  341.                             buf_init = 0;
  342.                             bufferpos = filepos = 0;
  343.                             update_time();
  344.                             setfilename(filename);
  345.                             init_replay();
  346.                             Dsp_Hf1(ff);
  347.                             break;
  348.                         }
  349.                     }
  350.                     if(!next_fd)
  351.                     {
  352.                         if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  353.                             toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  354.                         Dsp_Hf1(0);                
  355.                         
  356.                         exit_replay();
  357.                         update_time();
  358.                     }
  359.                 }
  360.             }
  361.             else
  362.             {
  363. #ifdef DEBUG
  364.                 fprintf(stderr,"Song continues! filepos=%lx bufferpos=%lx\n",
  365.                     filepos,bufferpos);
  366. #endif
  367.                 init_replay();
  368.                 update_time();
  369.             }
  370.             return;
  371.         }
  372.     }
  373.  
  374.     if(!bufend)
  375.     {
  376.         if(state && (buffer < ((long)buffer_mem + block/2)) &&
  377.             (buffer > ((long)buffer_mem + STOP_BLOCK)))
  378.         {
  379.             real_load(block/2, (char *) ((long)buffer_mem + block/2));
  380.             set_stop((long *)buffer_mem);
  381.             state = !state;
  382.         }
  383.         else if(!q || (!state && (buffer >= ((long)buffer_mem + block/2 + STOP_BLOCK))))
  384.         {
  385.             real_load(block/2, buffer_mem);
  386.             set_stop((long *) ((long)buffer_mem + block/2));
  387.             state = !state;
  388.         }
  389.     }
  390. }
  391.  
  392. void set_stop(long *buf)
  393. {
  394.     int i;
  395.  
  396.     for (i=0 ; i<(STOP_BLOCK/4) ; i++)
  397.         buf[i] = 0x42424242L;
  398. }
  399.  
  400. int has_stopped(int state)
  401. {
  402.     if(bufend)
  403.     {
  404.         if(bufend < ((long)buffer_mem + block/2))
  405.         {
  406.             if((buffer > bufend) && (buffer < ((long)buffer_mem + block/2)))
  407.                 return 1;
  408.         }
  409.         else
  410.         {
  411.             if(buffer > bufend)
  412.                 return 1;
  413.         }
  414.     }
  415.     else
  416.     {
  417.         if(state && (buffer > ((long)buffer_mem + block/2)) &&
  418.             (buffer < ((long)buffer_mem + block/2 + STOP_BLOCK)))
  419.             return 1;
  420.         else if(!state && (buffer > (long)buffer_mem) &&
  421.             (buffer < ((long)buffer_mem + STOP_BLOCK)))
  422.             return 1;
  423.     }
  424.  
  425.     return 0;
  426. }
  427.