home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / bass / c / basstest / Basstest.c next >
Encoding:
C/C++ Source or Header  |  2005-09-21  |  6.7 KB  |  255 lines

  1. // BASS Simple Test, copyright (c) 1999-2005 Ian Luck.
  2.  
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include "bass.h"
  6. #include "basstest.h"
  7.  
  8. HWND win=NULL;
  9.  
  10. HSTREAM *strs=NULL;
  11. int strc=0;
  12. HMUSIC *mods=NULL;
  13. int modc=0;
  14. HSAMPLE *sams=NULL;
  15. int samc=0;
  16.  
  17. /* Display error messages */
  18. void Error(char *es)
  19. {
  20.     char mes[200];
  21.     sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
  22.     MessageBox(win,mes,"Error",0);
  23. }
  24.  
  25. /* Messaging macros */
  26. #define MESS(id,m,w,l) SendDlgItemMessage(win,id,m,(WPARAM)w,(LPARAM)l)
  27. #define MLM(m,w,l) MESS(ID_MODLIST,m,w,l)
  28. #define SLM(m,w,l) MESS(ID_SAMLIST,m,w,l)
  29. #define STLM(m,w,l) MESS(ID_STRLIST,m,w,l)
  30. #define GETMOD() MLM(LB_GETCURSEL,0,0)
  31. #define GETSAM() SLM(LB_GETCURSEL,0,0)
  32. #define GETSTR() STLM(LB_GETCURSEL,0,0)
  33.  
  34. BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  35. {
  36.     static OPENFILENAME ofn;
  37.     static char path[MAX_PATH];
  38.     switch (m) {
  39.         case WM_TIMER:
  40.             { /* update the CPU usage % display */
  41.                 char text[10];
  42.                 sprintf(text,"%.2f",BASS_GetCPU());
  43.                 MESS(ID_CPU,WM_SETTEXT,0,text);
  44.             }
  45.             break;
  46.  
  47.         case WM_COMMAND:
  48.             switch (LOWORD(w)) {
  49.                 case IDCANCEL:
  50.                     DestroyWindow(h);
  51.                     break;
  52.  
  53.                 case ID_STRADD:
  54.                     {
  55.                         char file[MAX_PATH]="";
  56.                         HSTREAM str;
  57.                         ofn.lpstrFilter="Streamable files (wav/aif/mp3/mp2/mp1/ogg)\0*.wav;*.aif;*.mp3;*.mp2;*.mp1;*.ogg\0All files\0*.*\0\0";
  58.                         ofn.lpstrFile=file;
  59.                         if (GetOpenFileName(&ofn)) {
  60.                             memcpy(path,file,ofn.nFileOffset);
  61.                             path[ofn.nFileOffset-1]=0;
  62.                             if (str=BASS_StreamCreateFile(FALSE,file,0,0,0)) {
  63.                                 strc++;
  64.                                 strs=(HSTREAM*)realloc((void*)strs,strc*sizeof(*strs));
  65.                                 strs[strc-1]=str;
  66.                                 STLM(LB_ADDSTRING,0,strrchr(file,'\\')+1);
  67.                             } else
  68.                                 Error("Can't open stream");
  69.                         }
  70.                     }
  71.                     break;
  72.                 case ID_STRREMOVE:
  73.                     {
  74.                         int s=GETSTR();
  75.                         if (s!=LB_ERR) {
  76.                             STLM(LB_DELETESTRING,s,0);
  77.                             /* Free the stream from memory */
  78.                             BASS_StreamFree(strs[s]);
  79.                             strc--;
  80.                             memcpy(strs+s,strs+s+1,(strc-s)*sizeof(*strs));
  81.                         }
  82.                     }
  83.                     break;
  84.                 case ID_STRPLAY:
  85.                     {
  86.                         int s=GETSTR();
  87.                         /* Play the stream (continue from current position) */
  88.                         if (s!=LB_ERR)
  89.                             if (!BASS_ChannelPlay(strs[s],FALSE)) Error("Can't play stream");
  90.                     }
  91.                     break;
  92.                 case ID_STRSTOP:
  93.                     {
  94.                         int s=GETSTR();
  95.                         /* Stop the stream */
  96.                         if (s!=LB_ERR) BASS_ChannelStop(strs[s]);
  97.                     }
  98.                     break;
  99.                 case ID_STRRESTART:
  100.                     {
  101.                         int s=GETSTR();
  102.                         /* Play the stream from the start */
  103.                         if (s!=LB_ERR) BASS_ChannelPlay(strs[s],TRUE);
  104.                     }
  105.                     break;
  106.  
  107.                 case ID_MODADD:
  108.                     {
  109.                         char file[MAX_PATH]="";
  110.                         HMUSIC mod;
  111.                         ofn.lpstrFilter="MOD music files (mo3/xm/mod/s3m/it/mtm/umx)\0*.mo3;*.xm;*.mod;*.s3m;*.it;*.mtm;*.umx\0All files\0*.*\0\0";
  112.                         ofn.lpstrFile=file;
  113.                         if (GetOpenFileName(&ofn)) {
  114.                             memcpy(path,file,ofn.nFileOffset);
  115.                             path[ofn.nFileOffset-1]=0;
  116.                             /* Load a music from "file" and make it use ramping */
  117.                             if (mod=BASS_MusicLoad(FALSE,file,0,0,BASS_MUSIC_RAMP |BASS_MUSIC_POSRESET |BASS_SAMPLE_FLOAT |BASS_MUSIC_FT2MOD,0)) {
  118.                                 modc++;
  119.                                 mods=(HMUSIC*)realloc((void*)mods,modc*sizeof(*mods));
  120.                                 mods[modc-1]=mod;
  121.                                 MLM(LB_ADDSTRING,0,strrchr(file,'\\')+1);
  122.                             } else
  123.                                 Error("Can't load music");
  124.                         }
  125.                     }
  126.                     break;
  127.                 case ID_MODREMOVE:
  128.                     {
  129.                         int s=GETMOD();
  130.                         if (s!=LB_ERR) {
  131.                             MLM(LB_DELETESTRING,s,0);
  132.                             /* Free the music from memory */
  133.                             BASS_MusicFree(mods[s]);
  134.                             modc--;
  135.                             memcpy(mods+s,mods+s+1,(modc-s)*sizeof(*mods));
  136.                         }
  137.                     }
  138.                     break;
  139.                 case ID_MODPLAY:
  140.                     {
  141.                         int s=GETMOD();
  142.                         /* Play the music (continue from current position) */
  143.                         if (s!=LB_ERR)
  144.                             if (!BASS_ChannelPlay(mods[s],FALSE)) Error("Can't play music");
  145.                     }
  146.                     break;
  147.                 case ID_MODSTOP:
  148.                     {
  149.                         int s=GETMOD();
  150.                         /* Stop the music */
  151.                         if (s!=LB_ERR) BASS_ChannelStop(mods[s]);
  152.                     }
  153.                     break;
  154.                 case ID_MODRESTART:
  155.                     {
  156.                         int s=GETMOD();
  157.                         /* Play the music from the start */
  158.                         if (s!=LB_ERR) BASS_ChannelPlay(mods[s],TRUE);
  159.                     }
  160.                     break;
  161.  
  162.                 case ID_SAMADD:
  163.                     {
  164.                         char file[MAX_PATH]="";
  165.                         HSAMPLE sam;
  166.                         ofn.lpstrFilter="Sample files (wav/aif)\0*.wav;*/aif\0All files\0*.*\0\0";
  167.                         ofn.lpstrFile=file;
  168.                         if (GetOpenFileName(&ofn)) {
  169.                             memcpy(path,file,ofn.nFileOffset);
  170.                             path[ofn.nFileOffset-1]=0;
  171.                             /* Load a sample from "file" and give it a max of 3 simultaneous
  172.                             playings using playback position as override decider */
  173.                             if (sam=BASS_SampleLoad(FALSE,file,0,0,3,BASS_SAMPLE_OVER_POS)) {
  174.                                 samc++;
  175.                                 sams=(HSAMPLE*)realloc((void*)sams,samc*sizeof(*sams));
  176.                                 sams[samc-1]=sam;
  177.                                 SLM(LB_ADDSTRING,0,strrchr(file,'\\')+1);
  178.                             } else
  179.                                 Error("Can't load sample");
  180.                         }
  181.                     }
  182.                     break;
  183.                 case ID_SAMREMOVE:
  184.                     {
  185.                         int s=GETSAM();
  186.                         if (s!=LB_ERR) {
  187.                             SLM(LB_DELETESTRING,s,0);
  188.                             /* Free the sample from memory */
  189.                             BASS_SampleFree(sams[s]);
  190.                             samc--;
  191.                             memcpy(sams+s,sams+s+1,(samc-s)*sizeof(*sams));
  192.                         }
  193.                     }
  194.                     break;
  195.                 case ID_SAMPLAY:
  196.                     {
  197.                         int s=GETSAM();
  198.                         // Play the sample at default rate, volume=50, random pan position
  199.                         if (s!=LB_ERR) {
  200.                             HCHANNEL ch=BASS_SampleGetChannel(sams[s],FALSE);
  201.                             BASS_ChannelSetAttributes(ch,-1,50,(rand()%201)-100);
  202.                             if (!BASS_ChannelPlay(ch,FALSE)) Error("Can't play sample");
  203.                         }
  204.                     }
  205.                     break;
  206.  
  207.                 case ID_STOP:
  208.                     /* Pause output */
  209.                     BASS_Pause();
  210.                     break;
  211.                 case ID_RESUME:
  212.                     /* Resume output */
  213.                     BASS_Start();
  214.                     break;
  215.             }
  216.             break;
  217.  
  218.         case WM_INITDIALOG:
  219.             win=h;
  220.             /* Initialize default output device */
  221.             if (!BASS_Init(-1,44100,0,win,NULL))
  222.                 Error("Can't initialize device");
  223.             SetTimer(h,1,250,NULL);
  224.             GetCurrentDirectory(MAX_PATH,path);
  225.             memset(&ofn,0,sizeof(ofn));
  226.             ofn.lStructSize=sizeof(ofn);
  227.             ofn.hwndOwner=h;
  228.             ofn.nMaxFile=MAX_PATH;
  229.             ofn.lpstrInitialDir=path;
  230.             ofn.Flags=OFN_HIDEREADONLY|OFN_EXPLORER;
  231.             return 1;
  232.  
  233.         case WM_DESTROY:
  234.             KillTimer(h,1);
  235.             /* Close output */
  236.             BASS_Free();
  237.             break;
  238.     }
  239.     return 0;
  240. }
  241.  
  242. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  243. {
  244.     /* Check that BASS 2.2 was loaded */
  245.     if (BASS_GetVersion()!=MAKELONG(2,2)) {
  246.         MessageBox(0,"BASS version 2.2 was not loaded","Incorrect BASS.DLL",0);
  247.         return 0;
  248.     }
  249.  
  250.     /* display the window */
  251.     DialogBox(hInstance,MAKEINTRESOURCE(1000),NULL,&dialogproc);
  252.  
  253.     return 0;
  254. }
  255.