home *** CD-ROM | disk | FTP | other *** search
- #include <tos.h>
- #include <aes.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "snddefs.h"
- #include "mp2audio.h"
-
- /* #define DEBUG */
-
- typedef struct
- {
- long bitrate,sample_frequency,filelength,timelength;
- } MP2INFO;
-
- /* Functions in this module */
- int open_file(void);
- int reopen_file(void);
- 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;
-
- /* global variables from mp2audio.c */
- extern int file_open,fd,quit,replay;
- extern long buffer,block;
- extern char buffer_mem[(long)BLOCK_SIZE];
-
- /* global variable from mp2event.c */
- extern long total_time,savedtimerc;
- extern int looping;
-
- /* global variable from mp2info.c */
- extern MP2INFO mp2info;
-
- /* Function from mp2exit.c */
- extern void exit_replay(void);
-
- /* Function from mp2init.c */
- extern void init_replay(void);
-
- /* Function from mp2event.c */
- extern long set_timerc(void);
- extern long reset_timerc(void);
- extern void update_time(void);
-
- int open_file(void)
- {
- int button,ret;
- char tmp[1024];
-
- if(!path[0])
- {
- path[0] = 'A' + Dgetdrv(); /* initialize directory pathname */
- path[1] = ':';
- Dgetpath( path+2, 0 );
- strcat( path, "\\*.MP2" );
- }
-
- fsel_exinput(path, filename, &button, "Load MPEG");
- if (button == 1) {
- if(replay)
- exit_replay();
- if(file_open)
- close_file(fd);
- strcpy(tmp,path);
- strrchr(tmp, '\\')[1] = '\0';
- if((ret=(int) Fopen(strcat(tmp, filename), FO_READ)) > 0)
- {
- file_open=1;
- filesize=Fseek(0L,ret,2);
- Fseek(0L,ret,0);
- }
- return ret;
- }
- return 0;
- }
-
- int reopen_file()
- {
- char tmp[1024];
- int tfd;
- long fp;
-
- strcpy(tmp,path);
- strrchr(tmp, '\\')[1] = '\0';
- file_open=1;
- tfd=(int)Fopen(strcat(tmp, filename), FO_READ);
- fp=Fseek(filepos,tfd,0);
-
- fp=fp;
- #ifdef DEBUG
- sprintf(tmp,"[1][New filepos: %ld|Should be: %ld][Ok]",fp,filepos);
- form_alert(1,tmp);
- #endif
- return tfd;
- }
-
- void close_file(int fd)
- {
- file_open=0;
- Fclose(fd);
- }
-
- void reset_file(int fd)
- {
- Fseek(0L,fd,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
- {
- 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 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
- reset_file(fd);
- a&=0xfffffffeL;
-
- }
- else
- {
- 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
- reset_file(fd);
- if(tmp & 0x1L)
- {
- a--; /* a&=0xfffffffeL; */
- t++; /* t=(t&0xfffffffeL)+1; */
- }
- }
- }
- }
- }
- */
-
-
- void load(int q)
- {
- static int state = 0;
- long ptr[4];
-
- #ifdef DEBUG
- long bo;
- char tt[128];
- #endif
-
- buffptr(ptr);
- buffer = ptr[0];
- #ifdef DEBUG
- bo=buffoper(-1);
- sprintf(tt,"buffer=%lx, buffoper=%lx",ptr[0],bo);
- puts(tt);
- #endif
- if (!q)
- {
- state = 0;
- bufend=0;
- }
- else
- {
- if (has_stopped(state))
- {
- if(bufend)
- {
- #ifdef DEBUG
- printf("Song reset!\n");
- #endif
- if(looping)
- {
- reset_file(fd);
- Supexec(reset_timerc);
- update_time();
- init_replay();
- }
- else
- {
- exit_replay();
- Supexec(reset_timerc);
- update_time();
- }
- }
- else
- {
- #ifdef DEBUG
- printf("Song continues!\n");
- #endif
- savedtimerc=(200*Fseek(0L,fd,1))/(mp2info.bitrate/8);
- init_replay();
- Supexec(set_timerc);
- 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;
- }
-