/* A modified version of BASSMOD's contest.c example, with support for MO3 using UNMO3. Requires BASSMOD, available at www.un4seen.com */ #include #include "bassmod.h" #ifdef WIN32 #include #include #define PIPEMODE "rb" #else #include #include #include #define PIPEMODE "r" #define Sleep(x) usleep(x*1000) DWORD timeGetTime() { struct timeval tv; gettimeofday(&tv,0); return tv.tv_sec*1000+tv.tv_usec/1000; } int _kbhit() { int r; fd_set rfds; struct timeval tv; struct termios term,oterm; tcgetattr(0,&oterm); memcpy(&term,&oterm,sizeof(term)); cfmakeraw(&term); tcsetattr(0,TCSANOW,&term); FD_ZERO(&rfds); FD_SET(0,&rfds); tv.tv_sec=tv.tv_usec=0; r=select(1,&rfds,NULL,NULL,&tv); tcsetattr(0,TCSANOW,&oterm); return r; } #endif // display error messages void Error(char *text) { printf("Error(%d): %s\n",BASSMOD_ErrorGetCode(),text); BASSMOD_Free(); exit(0); } int starttime; void CALLBACK LoopSync(HSYNC handle, DWORD data, DWORD user) { starttime=timeGetTime(); } void main(int argc, char **argv) { int t,p; printf("Simple console BASSMOD example : IT/XM/S3M/MTM/MOD/UMX & MO3 player\n" "-------------------------------------------------------------------\n"); if (argc!=2) { printf("\tusage: contest \n"); return; } if (!BASSMOD_Init(-1,44100,0)) Error("Can't initialize device"); // try loading the MOD (with looping, sensitive ramping, surround sound and calculate the duration) if (!BASSMOD_MusicLoad(FALSE,argv[1],0,0,BASS_MUSIC_LOOP|BASS_MUSIC_RAMPS|BASS_MUSIC_SURROUND|BASS_MUSIC_CALCLEN)) { // can't load it as it is, so try unmo3'ing it char com[300]; FILE *fp; sprintf(com,"unmo3 -s \"%s\" STDOUT",argv[1]); fp=popen(com,PIPEMODE); if (fp) { char *buf=malloc(50000); int bufpos=0,n; while ((n=fread(buf+bufpos,1,50000,fp))>0) { bufpos+=n; buf=realloc(buf,bufpos+50000); } t=pclose(fp) || !BASSMOD_MusicLoad(TRUE,buf,0,bufpos,BASS_MUSIC_LOOP|BASS_MUSIC_RAMPS|BASS_MUSIC_SURROUND|BASS_MUSIC_CALCLEN); free(buf); } else t=1; if (t) Error("Can't play the file"); } // set a synchronizer for when the MOD reaches the end BASSMOD_MusicSetSync(BASS_SYNC_END,0,&LoopSync,0); for (t=0;BASSMOD_MusicGetAttribute(BASS_MUSIC_ATTRIB_VOL_CHAN+t)!=-1;t++); // count channels printf("playing \"%s\" [%d chans, %d orders]",BASSMOD_MusicGetName(),t,BASSMOD_MusicGetLength(FALSE)); // display the time length if (t=BASSMOD_MusicGetLength(TRUE)) { t/=176400; printf(" %d:%02d\n",t/60,t%60); } else // no time length available printf("\n"); BASSMOD_MusicPlay(); starttime=timeGetTime(); while (!_kbhit()) { p=BASSMOD_MusicGetPosition(); t=(timeGetTime()-starttime)/1000; printf("pos: %03d.%03d - time: %d:%02d - cpu: %.2f%% \r",LOWORD(p),HIWORD(p),t/60,t%60,BASSMOD_GetCPU()); fflush(stdout); Sleep(50); } printf(" \n"); BASSMOD_Free(); }