home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / bass / c / contest / Contest.c next >
Encoding:
C/C++ Source or Header  |  2005-09-22  |  3.9 KB  |  149 lines

  1. // BASS Simple Console Test, copyright (c) 1999-2005 Ian Luck.
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include "bass.h"
  6.  
  7. #ifdef WIN32 // Windows
  8. #include <conio.h>
  9. #else // OSX
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <termios.h>
  13. #include <string.h>
  14.  
  15. #define Sleep(x) usleep(x*1000)
  16.  
  17. int _kbhit()
  18. {
  19.     int r;
  20.     fd_set rfds;
  21.     struct timeval tv={0};
  22.     struct termios term,oterm;
  23.     tcgetattr(0,&oterm);
  24.     memcpy(&term,&oterm,sizeof(term));
  25.     cfmakeraw(&term);
  26.     tcsetattr(0,TCSANOW,&term);
  27.     FD_ZERO(&rfds);
  28.     FD_SET(0,&rfds);
  29.     r=select(1,&rfds,NULL,NULL,&tv);
  30.     tcsetattr(0,TCSANOW,&oterm);
  31.     return r;
  32. }
  33. #endif
  34.  
  35. // display error messages
  36. void Error(char *text) 
  37. {
  38.     printf("Error(%d): %s\n",BASS_ErrorGetCode(),text);
  39.     BASS_Free();
  40.     exit(0);
  41. }
  42.  
  43. DWORD starttime;
  44.  
  45. void main(int argc, char **argv)
  46. {
  47.     DWORD chan,act,time,level;
  48.     BOOL ismod;
  49.     QWORD pos;
  50.     int a;
  51.  
  52.     printf("Simple console mode BASS example : MOD/MPx/OGG/WAV player\n"
  53.             "---------------------------------------------------------\n");
  54.     // check that BASS 2.2 was loaded
  55.     if (BASS_GetVersion()!=MAKELONG(2,2)) {
  56.         printf("BASS version 2.2 was not loaded\n");
  57.         return;
  58.     }
  59.  
  60.     if (argc!=2) {
  61.         printf("\tusage: contest <file>\n");
  62.         return;
  63.     }
  64.  
  65.     // setup output - default device
  66.     if (!BASS_Init(-1,44100,0,0,NULL))
  67.         Error("Can't initialize device");
  68.  
  69.     // try streaming the file/url
  70.     if ((chan=BASS_StreamCreateFile(FALSE,argv[1],0,0,BASS_SAMPLE_LOOP))
  71.         || (chan=BASS_StreamCreateURL(argv[1],0,BASS_SAMPLE_LOOP,0,0))) {
  72.         pos=BASS_ChannelGetLength(chan);
  73.         if (BASS_StreamGetFilePosition(chan,BASS_FILEPOS_DOWNLOAD)!=-1) {
  74.             // streaming from the internet
  75.             if (pos!=-1)
  76. #ifdef WIN32
  77.                 printf("streaming internet file [%I64u bytes]",pos);
  78. #else
  79.                 printf("streaming internet file [%llu bytes]",pos);
  80. #endif
  81.             else
  82.                 printf("streaming internet file");
  83.         } else
  84. #ifdef WIN32
  85.             printf("streaming file [%I64u bytes]",pos);
  86. #else
  87.             printf("streaming file [%llu bytes]",pos);
  88. #endif
  89.         ismod=FALSE;
  90.     } else {
  91.         // try loading the MOD (with looping, sensitive ramping, and calculate the duration)
  92.         if (!(chan=BASS_MusicLoad(FALSE,argv[1],0,0,BASS_SAMPLE_LOOP|BASS_MUSIC_RAMPS|BASS_MUSIC_PRESCAN|BASS_MUSIC_SURROUND,0)))
  93.             // not a MOD either
  94.             Error("Can't play the file");
  95.         // count channels
  96.         for (a=0;BASS_MusicGetAttribute(chan,BASS_MUSIC_ATTRIB_VOL_CHAN+a)!=-1;a++);
  97.         printf("playing MOD music \"%s\" [%u chans, %u orders]",BASS_MusicGetName(chan),a,BASS_MusicGetOrders(chan));
  98.         pos=BASS_ChannelGetLength(chan);
  99.         ismod=TRUE;
  100.     }
  101.  
  102.     // display the time length
  103.     if (pos!=-1) {
  104.         time=(DWORD)BASS_ChannelBytes2Seconds(chan,pos);
  105.         printf(" %u:%02u\n",time/60,time%60);
  106.     } else // no time length available
  107.         printf("\n");
  108.  
  109.     BASS_ChannelPlay(chan,FALSE);
  110.  
  111.     while (!_kbhit() && (act=BASS_ChannelIsActive(chan))) {
  112.         // display some stuff and wait a bit
  113.         level=BASS_ChannelGetLevel(chan);
  114.         pos=BASS_ChannelGetPosition(chan);
  115.         time=BASS_ChannelBytes2Seconds(chan,pos);
  116. #ifdef WIN32
  117.         printf("pos %09I64u",pos);
  118. #else
  119.         printf("pos %09llu",pos);
  120. #endif
  121.         if (ismod) {
  122.             pos=BASS_MusicGetOrderPosition(chan);
  123.             printf(" (%03u:%03u)",LOWORD(pos),HIWORD(pos));
  124.         }
  125.         printf(" - %u:%02u - L ",time/60,time%60);
  126.         if (act==BASS_ACTIVE_STALLED) { // playback has stalled
  127.             printf("-- buffering : %05u --",
  128.                 BASS_StreamGetFilePosition(chan,BASS_FILEPOS_DOWNLOAD)-BASS_StreamGetFilePosition(chan,BASS_FILEPOS_DECODE));
  129.         } else {
  130.             for (a=27204;a>200;a=a*2/3) putchar(LOWORD(level)>=a?'*':'-');
  131.             putchar(' ');
  132.             for (a=210;a<32768;a=a*3/2) putchar(HIWORD(level)>=a?'*':'-');
  133.         }
  134.         printf(" R - cpu %.2f%%  \r",BASS_GetCPU());
  135.         fflush(stdout);
  136.         Sleep(50);
  137.     }
  138.     printf("                                                                             \r");
  139.  
  140.     // wind the frequency down...
  141.     BASS_ChannelSlideAttributes(chan,1000,-1,-101,500);
  142.     Sleep(300);
  143.     // ...and fade-out to avoid a "click"
  144.     BASS_ChannelSlideAttributes(chan,-1,-2,-101,200);
  145.     while (BASS_ChannelIsSliding(chan)) Sleep(1);
  146.  
  147.     BASS_Free();
  148. }
  149.