home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / sound / mp2_099 / src / gem / mp2file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  5.2 KB  |  296 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. #include "libshoe.h"
  12.  
  13. /* #define DEBUG */
  14.  
  15. /* Functions in this module */
  16. char *open_file(char *);
  17. int reopen_file(int fd);
  18. void close_file(int fd);
  19. void reset_file(int fd);
  20. void real_load(long l, char *b);
  21. void load(int q);
  22. void set_stop(long *buf);
  23. int has_stopped(int state);
  24.  
  25. /* global variables */
  26. char path[512]="\0", filename[512]="\0";
  27. long filesize,filepos,bufend=0,bufferpos=0;
  28.  
  29. /* global variables from mp2audio.c */
  30. extern int file_open,fd,quit,replay,dom;
  31. extern long buffer,block;
  32. extern WINDFORM windforms[5];
  33. #ifdef STATIC_BUFFER
  34. extern char buffer_mem[(long)BLOCK_SIZE];
  35. #else
  36. extern char *buffer_mem;
  37. #endif
  38.  
  39. /* global variables from mp2event.c */
  40. extern long total_time;
  41. extern int looping;
  42.  
  43. /* global variable from mp2init.c */
  44. extern int buf_init;
  45.  
  46. /* Function from mp2info.c */
  47. extern int getmp2info(int);
  48.  
  49. /* Function from mp2exit.c */
  50. extern void exit_replay(void);
  51.  
  52. /* Function from mp2init.c */
  53. extern void init_replay(void);
  54.  
  55. /* Functions from mp2event.c */
  56. extern void toggle_object(WINDFORM *, int, int);
  57. extern void update_time(void);
  58.  
  59. int check_file(int tfd)
  60. {
  61.     int error;
  62.  
  63.     fd=tfd;
  64.     if((error=getmp2info(tfd))==MP2_NOERR)
  65.         update_time();
  66.     else
  67.         close_file(fd);
  68.     
  69.     return error;
  70. }
  71.  
  72. int load_file(char *file)
  73. {
  74.     int ret;
  75.     char *filep;
  76.  
  77.     if(replay)
  78.         exit_replay();
  79.     if(file_open)
  80.         close_file(fd);
  81.     if((ret=(int)Fopen(file, FO_READ)) > 0)
  82.     {
  83.         file_open=1;
  84.         filesize=Fseek(0L,ret,2);
  85.         Fseek(0L,ret,0);
  86.         bufferpos = filepos = 0;
  87.         if((filep = strrchr(file, '\\')) != NULL)
  88.             strcpy(filename, filep+1);
  89.         else
  90.             strcpy(filename, file);
  91.     }
  92.     return ret;
  93. }
  94.  
  95. char *open_file(char *pattern)
  96. {
  97.     int button;
  98.     static char file[1024],tfn[512];
  99.  
  100.     strcpy(tfn,filename);
  101.     strcat(path, pattern);
  102.     fsel_exinput(path, tfn, &button, "Load MPEG");
  103.     strrchr(path, '\\')[1] = '\0';
  104.     if(button == 1 && *tfn) {
  105.         strcpy(file,path);
  106.         strcat(file, tfn);
  107.         return file;
  108.     }
  109.     return NULL;
  110. }
  111.  
  112. int reopen_file(int tfd)
  113. {
  114.     char tmp[1024];
  115.  
  116.     strcpy(tmp,path);
  117.     close_file(tfd);
  118.     if((tfd=(int)Fopen(strcat(tmp, filename), FO_READ)) > 0) {
  119.         file_open = 1;
  120.         (void)Fseek(filepos,tfd,0);
  121.     }
  122.  
  123.     return tfd;
  124. }
  125.  
  126. void close_file(int fd)
  127. {
  128.     file_open=0;
  129.     Fclose(fd);
  130. }
  131.  
  132. void reset_file(int fd)
  133. {
  134.     Fseek(0L,fd,0);
  135.     buf_init = 0;
  136.     bufferpos = filepos = 0;
  137. }
  138.  
  139.  
  140. void real_load(long l, char *b)
  141. {
  142.     long a=l;
  143.     long tmp,t=(long)b;
  144.  
  145. #ifdef DEBUG
  146.     long ffp;
  147.     char tt[128];
  148. #endif
  149.  
  150.     while (a > 0)
  151.     {
  152.  
  153. #ifdef DEBUG
  154.         ffp=Fseek(0L,fd,1);
  155.         sprintf(tt,"[1][%d: a=%ld|t=%lx fpos=%ld][Ok]",fd,a,t,ffp);
  156.         puts(tt);
  157. #endif
  158.             
  159.         tmp=Fread(fd, a & 0xfffffffeL, (void *)t);
  160.  
  161. #ifdef DEBUG
  162.         ffp=Fseek(0L,fd,1);
  163.         sprintf(tt,"[1][%d Read tmp=%ld|a=%ld|t=%lx fpos=%ld][Ok]",fd,tmp,a,t,ffp);
  164.         puts(tt);
  165. #endif
  166.  
  167.         if (tmp < 0)
  168.         {
  169. #ifdef DEBUG
  170.             ffp=Fseek(0L,fd,1);
  171.             sprintf(tt,"[1][%d reset tmp<0|a=%ld t=%lx|tmp=%ld fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  172.             puts(tt);
  173. #endif
  174.             bufend=t;
  175.             for( ; a>0 ; a--,t++)
  176.                 *((char *)t) = 0x42;
  177.         }
  178.         else
  179.         {
  180.             filepos += tmp;
  181.         
  182.             a-=tmp;
  183.             t+=tmp;
  184.             if(a >= 1)
  185.             {
  186.  
  187. #ifdef DEBUG
  188.                 ffp=Fseek(0L,fd,1);
  189.                 sprintf(tt,"[1][%d reset a>0 a=%ld|t=%lx tmp=%ld|fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  190.                 puts(tt);
  191. #endif
  192.                 bufend=t;
  193. #ifdef DEBUG
  194.                 printf("bufend = %lx\n",bufend);
  195. #endif
  196.                 for( ; a>0 ; a--,t++)
  197.                     *((char *)t) = 0x42;
  198.             }
  199.         }
  200.     }
  201. }
  202.  
  203. void load(int q)
  204. {
  205.     static int state = 0;
  206.     static long lastbuf=0;
  207.     long ptr[4];
  208.     
  209. #ifdef DEBUG
  210.     long bo;
  211.     char tt[128];
  212. #endif
  213.  
  214.     buffptr(ptr);
  215.     buffer = ptr[0];
  216.  
  217.     if(buffer < lastbuf)
  218.         bufferpos+=block;
  219.         
  220.     lastbuf=buffer;
  221.     
  222.     if(!q) {
  223.         bufferpos=filepos;
  224.         lastbuf=0;
  225.         state = 0;
  226.         bufend=0;
  227.     } else {
  228.         if(has_stopped(state)) {
  229.             if(bufend)
  230.                 eval("(mp2-hook-finitum)");
  231.             else {
  232. #ifdef DEBUG
  233.                 fprintf(stderr,"Song continues! filepos=%lx bufferpos=%lx\n",
  234.                     filepos,bufferpos);
  235. #endif
  236.                 init_replay();
  237.                 update_time();
  238.             }
  239.             return;
  240.         }
  241.     }
  242.  
  243.     if(!bufend)
  244.     {
  245.         if(state && (buffer < ((long)buffer_mem + block/2)) &&
  246.             (buffer > ((long)buffer_mem + STOP_BLOCK)))
  247.         {
  248.             real_load(block/2, (char *) ((long)buffer_mem + block/2));
  249.             set_stop((long *)buffer_mem);
  250.             state = !state;
  251.         }
  252.         else if(!q || (!state && (buffer >= ((long)buffer_mem + block/2 + STOP_BLOCK))))
  253.         {
  254.             real_load(block/2, buffer_mem);
  255.             set_stop((long *) ((long)buffer_mem + block/2));
  256.             state = !state;
  257.         }
  258.     }
  259. }
  260.  
  261. void set_stop(long *buf)
  262. {
  263.     int i;
  264.  
  265.     for (i=0 ; i<(STOP_BLOCK/4) ; i++)
  266.         buf[i] = 0x42424242L;
  267. }
  268.  
  269. int has_stopped(int state)
  270. {
  271.     if(bufend)
  272.     {
  273.         if(bufend < ((long)buffer_mem + block/2))
  274.         {
  275.             if((buffer > bufend) && (buffer < ((long)buffer_mem + block/2)))
  276.                 return 1;
  277.         }
  278.         else
  279.         {
  280.             if(buffer > bufend)
  281.                 return 1;
  282.         }
  283.     }
  284.     else
  285.     {
  286.         if(state && (buffer > ((long)buffer_mem + block/2)) &&
  287.             (buffer < ((long)buffer_mem + block/2 + STOP_BLOCK)))
  288.             return 1;
  289.         else if(!state && (buffer > (long)buffer_mem) &&
  290.             (buffer < ((long)buffer_mem + STOP_BLOCK)))
  291.             return 1;
  292.     }
  293.  
  294.     return 0;
  295. }
  296.