home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / ModBass / c / Contest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-28  |  3.4 KB  |  115 lines

  1. /* BASS Simple Console Test, copyright (c) 1999-2000 Ian Luck.
  2. ==============================================================
  3. Imports: bass.lib, kernel32.lib, user32.lib, winmm.lib
  4. */
  5.  
  6. #include <windows.h>
  7. #include <mmsystem.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include "bass.h"
  11.  
  12. /* display error messages */
  13. void Error(char *text) 
  14. {
  15.     printf("Error(%d): %s\n",BASS_ErrorGetCode(),text);
  16.     BASS_Free();
  17.     ExitProcess(0);
  18. }
  19.  
  20. static DWORD starttime;
  21.  
  22. /* looping synchronizer, resets the clock */
  23. void CALLBACK LoopSync(HSYNC handle, DWORD channel, DWORD data, DWORD user)
  24. {
  25.     starttime=timeGetTime();
  26. }
  27.  
  28. void main(int argc, char **argv)
  29. {
  30.     HMUSIC mod;
  31.     HSTREAM str;
  32.     DWORD time,pos,level;
  33.     int a,freq;
  34.     BOOL mono=FALSE;
  35.  
  36.     printf("Simple console mode BASS example : MOD/MP3/WAV player\n"
  37.             "-----------------------------------------------------\n");
  38.  
  39.     /* check that BASS 0.8 was loaded */
  40.     if (BASS_GetVersion()!=MAKELONG(0,8)) {
  41.         printf("BASS version 0.8 was not loaded\n");
  42.         return;
  43.     }
  44.  
  45.     if (argc!=2) {
  46.         printf("\tusage: contest <file>\n");
  47.         return;
  48.     }
  49.  
  50.     /* setup output - default device, 44100hz, stereo, 16 bits */
  51.     if (!BASS_Init(-1,44100,0,GetForegroundWindow()))
  52.         Error("Can't initialize device");
  53.  
  54.     /* try streaming the file */
  55.     if (str=BASS_StreamCreateFile(FALSE,argv[1],0,0,0)) {
  56.         /* check if the stream is mono (for the level indicator) */
  57.         mono=BASS_ChannelGetFlags(str)&BASS_SAMPLE_MONO;
  58.         /* set a synchronizer for when the stream reaches the end */
  59.         BASS_ChannelSetSync(str,BASS_SYNC_END,0,&LoopSync,0);
  60.         printf("streaming file [%d bytes]\n",BASS_StreamGetLength(str));
  61.     } else {
  62.         /* load the MOD (with looping and normal ramping) */
  63.         if (!(mod=BASS_MusicLoad(FALSE,argv[1],0,0,BASS_MUSIC_LOOP|BASS_MUSIC_RAMP)))
  64.             /* not a MOD either */
  65.             Error("Can't play the file");
  66.         /* set a synchronizer for when the MOD reaches the end */
  67.         BASS_ChannelSetSync(mod,BASS_SYNC_END,0,&LoopSync,0);
  68.         printf("playing MOD music \"%s\" [%d orders]\n",BASS_MusicGetName(mod),BASS_MusicGetLength(mod));
  69.     }
  70.  
  71.     BASS_Start();
  72.     if (str)
  73.         BASS_StreamPlay(str,FALSE,BASS_SAMPLE_LOOP);
  74.     else
  75.         BASS_MusicPlayEx(mod,0,-1,1);
  76.     starttime=timeGetTime();
  77.  
  78.     /* NOTE: some compilers don't support _kbhit */
  79.     while (!_kbhit() && BASS_ChannelIsActive(str?str:mod)) {
  80.         /* display some stuff and wait a bit */
  81.         time=timeGetTime()-starttime;
  82.         level=BASS_ChannelGetLevel(str?str:mod);
  83.         pos=BASS_ChannelGetPosition(str?str:mod);
  84.         if (str)
  85.             printf("pos %09d - time %d:%02d - L ",pos,time/60000,(time/1000)%60);
  86.         else
  87.             printf("pos %03d:%03d - time %d:%02d - L ",LOWORD(pos),HIWORD(pos),time/60000,(time/1000)%60);
  88.         for (a=93;a;a=a*2/3) putchar(LOWORD(level)>=a?'*':'-');
  89.         putchar(' ');
  90.         if (mono)
  91.             for (a=1;a<128;a+=a-(a>>1)) putchar(LOWORD(level)>=a?'*':'-');
  92.         else
  93.             for (a=1;a<128;a+=a-(a>>1)) putchar(HIWORD(level)>=a?'*':'-');
  94.         printf(" R - cpu %.1f%%  \r",BASS_GetCPU());
  95.         Sleep(50);
  96.     }
  97.     printf("                                                                   \r");
  98.  
  99.     /* get the frequency... and wind it down */
  100.     BASS_ChannelGetAttributes(str?str:mod,&freq,NULL,NULL);
  101.     level=freq/40;
  102.     for (;freq>2000;freq-=level) {
  103.         BASS_ChannelSetAttributes(str?str:mod,freq,-1,-101);
  104.         Sleep(5);
  105.     }
  106.  
  107.     /* fade-out to avoid a "click" */
  108.     for (a=100;a>=0;a-=5) {
  109.         BASS_SetVolume(a);
  110.         Sleep(1);
  111.     }
  112.  
  113.     BASS_Free();
  114. }
  115.