home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / ModBass / c / loadnget.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-17  |  5.8 KB  |  194 lines

  1. /* BASS Console Test (LL/GPA version), copyright (c) 1999-2000 Ian Luck.
  2. ========================================================================
  3. Demonstrates how you can use LoadLibrary and GetProcAddress to import
  4. BASS, instead of using BASS.LIB. Also demonstrates including BASS.DLL
  5. in the EXE as a resource, instead of seperate as a file. It's basically
  6. the same as the CONTEST.C version, with LoadBASS and FreeBASS functions
  7. added, to import and free BASS respectively.
  8. Other source: loadnget.rc
  9. Imports: kernel32.lib, user32.lib, winmm.lib
  10. */
  11.  
  12. #include <windows.h>
  13. #include <mmsystem.h>
  14. #include <stdio.h>
  15. #include <conio.h>
  16.  
  17. #define BASSDEF(f) (WINAPI *f)    // define the functions as pointers
  18. #include "bass.h"
  19.  
  20. char tempfile[MAX_PATH];    // temporary BASS.DLL
  21. HINSTANCE bass=0;            // bass handle
  22.  
  23. /* load BASS and the required functions */
  24. void LoadBASS()
  25. {
  26.     BYTE *data;
  27.     HANDLE hres,hfile;
  28.     DWORD len,c;
  29.     char temppath[MAX_PATH];
  30.     /* get the BASS.DLL resource */
  31.     if (!(hres=FindResource(GetModuleHandle(NULL),"BASS_DLL",RT_RCDATA))
  32.         || !(len=SizeofResource(NULL,hres))
  33.         || !(hres=LoadResource(NULL,hres))
  34.         || !(data=LockResource(hres))) {
  35.         printf("Error: Can't get the BASS.DLL resource\n");
  36.         ExitProcess(0);
  37.     }
  38.     /* get a temporary filename */
  39.     GetTempPath(MAX_PATH,temppath);
  40.     GetTempFileName(temppath,"bas",0,tempfile);
  41.     /* write BASS.DLL to the temporary file */
  42.     if (!(hfile=CreateFile(tempfile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,NULL))) {
  43.         printf("Error: Can't write BASS.DLL\n");
  44.         ExitProcess(0);
  45.     }
  46.     WriteFile(hfile,data,len,&c,NULL);
  47.     CloseHandle(hfile);
  48.  
  49.     /* load the temporary BASS.DLL library */
  50.     if (!(bass=LoadLibrary(tempfile))) {
  51.         printf("Error: Can't load BASS.DLL\n");
  52.         ExitProcess(0);
  53.     }
  54.     /* "load" all the BASS functions that are to be used */
  55. #ifdef __cplusplus
  56.     #define LOADBASSFUNCTION(f) {\
  57.         char *func=#f;\
  58.         __asm {push func}\
  59.         __asm {push bass}\
  60.         __asm {mov eax,GetProcAddress}\
  61.         __asm {call eax}\
  62.         __asm {mov f,eax}\
  63.     }
  64. #else
  65.     #define LOADBASSFUNCTION(f) (FARPROC)f=GetProcAddress(bass,#f)
  66. #endif
  67.     LOADBASSFUNCTION(BASS_ErrorGetCode);
  68.     LOADBASSFUNCTION(BASS_Init);
  69.     LOADBASSFUNCTION(BASS_Free);
  70.     LOADBASSFUNCTION(BASS_GetCPU);
  71.     LOADBASSFUNCTION(BASS_Start);
  72.     LOADBASSFUNCTION(BASS_SetVolume);
  73.     LOADBASSFUNCTION(BASS_MusicLoad);
  74.     LOADBASSFUNCTION(BASS_MusicPlay);
  75.     LOADBASSFUNCTION(BASS_StreamCreateFile);
  76.     LOADBASSFUNCTION(BASS_StreamPlay);
  77.     LOADBASSFUNCTION(BASS_ChannelIsActive);
  78.     LOADBASSFUNCTION(BASS_ChannelGetFlags);
  79.     LOADBASSFUNCTION(BASS_ChannelSetAttributes);
  80.     LOADBASSFUNCTION(BASS_ChannelGetAttributes);
  81.     LOADBASSFUNCTION(BASS_ChannelGetPosition);
  82.     LOADBASSFUNCTION(BASS_ChannelGetLevel);
  83.     LOADBASSFUNCTION(BASS_ChannelSetSync);
  84. }
  85.  
  86. /* free the BASS library from memory and delete the temporary file */
  87. void FreeBASS()
  88. {
  89.     if (!bass) return;
  90.     FreeLibrary(bass);
  91.     bass=0;
  92.     DeleteFile(tempfile);
  93. }
  94.  
  95. /* display error messages */
  96. void Error(char *text) 
  97. {
  98.     printf("Error(%d): %s\n",BASS_ErrorGetCode(),text);
  99.     BASS_Free();
  100.     FreeBASS();
  101.     ExitProcess(0);
  102. }
  103.  
  104. static DWORD starttime;
  105.  
  106. /* looping synchronizer, resets the clock */
  107. void CALLBACK LoopSync(HSYNC handle, DWORD channel, DWORD data)
  108. {
  109.     starttime=timeGetTime();    // reset the clock
  110. }
  111.  
  112. void main(int argc, char **argv)
  113. {
  114.     HMUSIC mod;
  115.     HSTREAM str;
  116.     DWORD time,pos,level;
  117.     int a,freq;
  118.     BOOL mono=FALSE;
  119.  
  120.     printf("LoadLibrary/GetProcAddress example : MOD/MP3/WAV player\n"
  121.         "-------------------------------------------------------\n");
  122.  
  123.     LoadBASS();
  124.  
  125.     if (argc<2 || argc>3) {
  126.         printf("\tusage: loadnget <file>\n");
  127.         return;
  128.     }
  129.  
  130.     /* setup output - default device, 44100hz, stereo, 16 bits */
  131.     if (!BASS_Init(-1,44100,0,GetForegroundWindow()))
  132.         Error("Can't initialize device");
  133.     /* try streaming the file */
  134.     if (str=BASS_StreamCreateFile(FALSE,argv[1],0,0,0)) {
  135.         /* check if the stream is mono (for the level indicator) */
  136.         mono=BASS_ChannelGetFlags(str)&BASS_SAMPLE_MONO;
  137.         /* set a synchronizer for when the stream reaches the end */
  138.         BASS_ChannelSetSync(str,BASS_SYNC_END,0,&LoopSync,NULL);
  139.     } else {
  140.         /* load the MOD (with looping and normal ramping) */
  141.         if (!(mod=BASS_MusicLoad(FALSE,argv[1],0,0,BASS_MUSIC_LOOP|BASS_MUSIC_RAMP)))
  142.             /* not a MOD either */
  143.             Error("Can't play the file");
  144.         /* set a synchronizer for when the MOD reaches the end */
  145.         BASS_ChannelSetSync(mod,BASS_SYNC_END,0,&LoopSync,NULL);
  146.     }
  147.  
  148.     BASS_Start();
  149.     if (str)
  150.         BASS_StreamPlay(str,FALSE,BASS_SAMPLE_LOOP);
  151.     else
  152.         BASS_MusicPlay(mod);
  153.     starttime=timeGetTime();
  154.     printf("now playing... press any key to stop\n");
  155.  
  156.     /* NOTE: some compilers don't support _kbhit */
  157.     while (!_kbhit() && BASS_ChannelIsActive(str?str:mod)) {
  158.         /* display some stuff and wait a bit */
  159.         time=timeGetTime()-starttime;
  160.         level=BASS_ChannelGetLevel(str?str:mod);
  161.         pos=BASS_ChannelGetPosition(str?str:mod);
  162.         if (str)
  163.             printf("pos %09d - time %d:%02d - L ",pos,time/60000,(time/1000)%60);
  164.         else
  165.             printf("pos %03d:%03d - time %d:%02d - L ",LOWORD(pos),HIWORD(pos),time/60000,(time/1000)%60);
  166.         for (a=93;a;a=a*2/3) putchar(LOWORD(level)>=a?'*':'-');
  167.         putchar(' ');
  168.         if (mono)
  169.             for (a=1;a<128;a+=a-(a>>1)) putchar(LOWORD(level)>=a?'*':'-');
  170.         else
  171.             for (a=1;a<128;a+=a-(a>>1)) putchar(HIWORD(level)>=a?'*':'-');
  172.         printf(" R - cpu %.1f%%  \r",BASS_GetCPU());
  173.         Sleep(50);
  174.     }
  175.     printf("                                                                   \r");
  176.  
  177.     /* get the frequency... and wind it down */
  178.     BASS_ChannelGetAttributes(str?str:mod,&freq,NULL,NULL);
  179.     level=freq/40;
  180.     for (;freq>2000;freq-=level) {
  181.         BASS_ChannelSetAttributes(str?str:mod,freq,-1,-101);
  182.         Sleep(5);
  183.     }
  184.  
  185.     /* fade-out to avoid a "click" */
  186.     for (a=100;a>=0;a-=5) {
  187.         BASS_SetVolume(a);
  188.         Sleep(1);
  189.     }
  190.  
  191.     BASS_Free();
  192.     FreeBASS();
  193. }
  194.