home *** CD-ROM | disk | FTP | other *** search
- #include <tos.h>
- #include <aes.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- #include "snddefs.h"
- #include "mp2audio.h"
- #include "mp2wind.h"
- #include "mp2info.h"
- #include "libshoe.h"
-
- /* #define DEBUG */
-
- /* Functions in this module */
- char *open_file(char *);
- int reopen_file(int fd);
- void close_file(int fd);
- void reset_file(int fd);
- void real_load(long l, char *b);
- void load(int q);
- void set_stop(long *buf);
- int has_stopped(int state);
-
- /* global variables */
- char path[512]="\0", filename[512]="\0";
- long filesize,filepos,bufend=0,bufferpos=0;
-
- /* global variables from mp2audio.c */
- extern int file_open,fd,quit,replay,dom;
- extern long buffer,block;
- extern WINDFORM windforms[5];
- #ifdef STATIC_BUFFER
- extern char buffer_mem[(long)BLOCK_SIZE];
- #else
- extern char *buffer_mem;
- #endif
-
- /* global variables from mp2event.c */
- extern long total_time;
- extern int looping;
-
- /* global variable from mp2init.c */
- extern int buf_init;
-
- /* Function from mp2info.c */
- extern int getmp2info(int);
-
- /* Function from mp2exit.c */
- extern void exit_replay(void);
-
- /* Function from mp2init.c */
- extern void init_replay(void);
-
- /* Functions from mp2event.c */
- extern void toggle_object(WINDFORM *, int, int);
- extern void update_time(void);
-
- int check_file(int tfd)
- {
- int error;
-
- fd=tfd;
- if((error=getmp2info(tfd))==MP2_NOERR)
- update_time();
- else
- close_file(fd);
-
- return error;
- }
-
- int load_file(char *file)
- {
- int ret;
- char *filep;
-
- if(replay)
- exit_replay();
- if(file_open)
- close_file(fd);
- if((ret=(int)Fopen(file, FO_READ)) > 0)
- {
- file_open=1;
- filesize=Fseek(0L,ret,2);
- Fseek(0L,ret,0);
- bufferpos = filepos = 0;
- if((filep = strrchr(file, '\\')) != NULL)
- strcpy(filename, filep+1);
- else
- strcpy(filename, file);
- }
- return ret;
- }
-
- char *open_file(char *pattern)
- {
- int button;
- static char file[1024],tfn[512];
-
- strcpy(tfn,filename);
- strcat(path, pattern);
- fsel_exinput(path, tfn, &button, "Load MPEG");
- strrchr(path, '\\')[1] = '\0';
- if(button == 1 && *tfn) {
- strcpy(file,path);
- strcat(file, tfn);
- return file;
- }
- return NULL;
- }
-
- int reopen_file(int tfd)
- {
- char tmp[1024];
-
- strcpy(tmp,path);
- close_file(tfd);
- if((tfd=(int)Fopen(strcat(tmp, filename), FO_READ)) > 0) {
- file_open = 1;
- (void)Fseek(filepos,tfd,0);
- }
-
- return tfd;
- }
-
- void close_file(int fd)
- {
- file_open=0;
- Fclose(fd);
- }
-
- void reset_file(int fd)
- {
- Fseek(0L,fd,0);
- buf_init = 0;
- bufferpos = filepos = 0;
- }
-
-
- void real_load(long l, char *b)
- {
- long a=l;
- long tmp,t=(long)b;
-
- #ifdef DEBUG
- long ffp;
- char tt[128];
- #endif
-
- while (a > 0)
- {
-
- #ifdef DEBUG
- ffp=Fseek(0L,fd,1);
- sprintf(tt,"[1][%d: a=%ld|t=%lx fpos=%ld][Ok]",fd,a,t,ffp);
- puts(tt);
- #endif
-
- tmp=Fread(fd, a & 0xfffffffeL, (void *)t);
-
- #ifdef DEBUG
- ffp=Fseek(0L,fd,1);
- sprintf(tt,"[1][%d Read tmp=%ld|a=%ld|t=%lx fpos=%ld][Ok]",fd,tmp,a,t,ffp);
- puts(tt);
- #endif
-
- if (tmp < 0)
- {
- #ifdef DEBUG
- ffp=Fseek(0L,fd,1);
- sprintf(tt,"[1][%d reset tmp<0|a=%ld t=%lx|tmp=%ld fpos=%ld][Ok]",fd,a,t,tmp,ffp);
- puts(tt);
- #endif
- bufend=t;
- for( ; a>0 ; a--,t++)
- *((char *)t) = 0x42;
- }
- else
- {
- filepos += tmp;
-
- a-=tmp;
- t+=tmp;
- if(a >= 1)
- {
-
- #ifdef DEBUG
- ffp=Fseek(0L,fd,1);
- sprintf(tt,"[1][%d reset a>0 a=%ld|t=%lx tmp=%ld|fpos=%ld][Ok]",fd,a,t,tmp,ffp);
- puts(tt);
- #endif
- bufend=t;
- #ifdef DEBUG
- printf("bufend = %lx\n",bufend);
- #endif
- for( ; a>0 ; a--,t++)
- *((char *)t) = 0x42;
- }
- }
- }
- }
-
- void load(int q)
- {
- static int state = 0;
- static long lastbuf=0;
- long ptr[4];
-
- #ifdef DEBUG
- long bo;
- char tt[128];
- #endif
-
- buffptr(ptr);
- buffer = ptr[0];
-
- if(buffer < lastbuf)
- bufferpos+=block;
-
- lastbuf=buffer;
-
- if(!q) {
- bufferpos=filepos;
- lastbuf=0;
- state = 0;
- bufend=0;
- } else {
- if(has_stopped(state)) {
- if(bufend)
- eval("(mp2-hook-finitum)");
- else {
- #ifdef DEBUG
- fprintf(stderr,"Song continues! filepos=%lx bufferpos=%lx\n",
- filepos,bufferpos);
- #endif
- init_replay();
- update_time();
- }
- return;
- }
- }
-
- if(!bufend)
- {
- if(state && (buffer < ((long)buffer_mem + block/2)) &&
- (buffer > ((long)buffer_mem + STOP_BLOCK)))
- {
- real_load(block/2, (char *) ((long)buffer_mem + block/2));
- set_stop((long *)buffer_mem);
- state = !state;
- }
- else if(!q || (!state && (buffer >= ((long)buffer_mem + block/2 + STOP_BLOCK))))
- {
- real_load(block/2, buffer_mem);
- set_stop((long *) ((long)buffer_mem + block/2));
- state = !state;
- }
- }
- }
-
- void set_stop(long *buf)
- {
- int i;
-
- for (i=0 ; i<(STOP_BLOCK/4) ; i++)
- buf[i] = 0x42424242L;
- }
-
- int has_stopped(int state)
- {
- if(bufend)
- {
- if(bufend < ((long)buffer_mem + block/2))
- {
- if((buffer > bufend) && (buffer < ((long)buffer_mem + block/2)))
- return 1;
- }
- else
- {
- if(buffer > bufend)
- return 1;
- }
- }
- else
- {
- if(state && (buffer > ((long)buffer_mem + block/2)) &&
- (buffer < ((long)buffer_mem + block/2 + STOP_BLOCK)))
- return 1;
- else if(!state && (buffer > (long)buffer_mem) &&
- (buffer < ((long)buffer_mem + STOP_BLOCK)))
- return 1;
- }
-
- return 0;
- }
-