home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / ModBass / c / Basstest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-20  |  8.0 KB  |  303 lines

  1. /* BASS Simple Test, copyright (c) 1999-2000 Ian Luck.
  2. ======================================================
  3. Other source: basstest.rc
  4. Imports: bass.lib, kernel32.lib, user32.lib, comdlg32.lib
  5. */
  6.  
  7. #include <windows.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "bass.h"
  11. #include "basstest.h"
  12.  
  13. static HWND win=NULL;
  14. static HINSTANCE inst;
  15.  
  16. static HMUSIC *mods=NULL;
  17. static int modc=0;
  18. static HSAMPLE *sams=NULL;
  19. static int samc=0;
  20. static HSTREAM str;
  21. static BOOL cdplaying=FALSE;
  22.  
  23. /* Display error dialogs */
  24. static void Error(char *es)
  25. {
  26.     char mes[200];
  27.     sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
  28.     MessageBox(win,mes,"Error",0);
  29. }
  30.  
  31. /* Messaging macros */
  32. #define MESS(id,m,w,l) SendDlgItemMessage(win,id,m,(WPARAM)w,(LPARAM)l)
  33. #define MLM(m,w,l) MESS(ID_MODLIST,m,w,l)
  34. #define SLM(m,w,l) MESS(ID_SAMLIST,m,w,l)
  35. #define GETMOD() MLM(LB_GETCURSEL,0,0)
  36. #define GETSAM() SLM(LB_GETCURSEL,0,0)
  37.  
  38. BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  39. {
  40.     static OPENFILENAME ofn;
  41.     static char path[MAX_PATH];
  42.  
  43.     switch (m) {
  44.         case WM_TIMER:
  45.             {
  46.                 char text[10];
  47.                 int p;
  48.                 /* update the CD status */
  49.                 MESS(ID_CDINDRIVE,BM_SETCHECK,BASS_CDInDrive(),0);
  50.                 p=BASS_ChannelGetPosition(CDCHANNEL);
  51.                 sprintf(text,"%d:%02d",p/60000,(p/1000)%60);
  52.                 MESS(ID_CDPOS,WM_SETTEXT,0,text);
  53.                 /* update the CPU usage % display */
  54.                 sprintf(text,"%.1f",BASS_GetCPU());
  55.                 MESS(ID_CPU,WM_SETTEXT,0,text);
  56.             }
  57.             break;
  58.  
  59.         case WM_COMMAND:
  60.             switch (LOWORD(w)) {
  61.                 case IDCANCEL:
  62.                     DestroyWindow(h);
  63.                     return 1;
  64.                 case ID_MODADD:
  65.                     {
  66.                         char file[MAX_PATH]="";
  67.                         HMUSIC mod;
  68.                         ofn.lpstrFilter="MOD music files (mo3/xm/mod/s3m/it/mtm)\0*.mo3;*.xm;*.mod;*.s3m;*.it;*.mtm\0All files\0*.*\0\0";
  69.                         ofn.lpstrFile=file;
  70.                         if (GetOpenFileName(&ofn)) {
  71.                             memcpy(path,file,ofn.nFileOffset);
  72.                             path[ofn.nFileOffset-1]=0;
  73.                             /* Load a music from "file" and make it use ramping */
  74.                             if (mod=BASS_MusicLoad(FALSE,file,0,0,BASS_MUSIC_RAMP)) {
  75.                                 modc++;
  76.                                 mods=(HMUSIC*)realloc((void*)mods,modc*sizeof(HMUSIC));
  77.                                 mods[modc-1]=mod;
  78.                                 MLM(LB_ADDSTRING,0,file);
  79.                             } else
  80.                                 Error("Can't load music");
  81.                         }
  82.                     }
  83.                     return 1;
  84.                 case ID_MODREMOVE:
  85.                     {
  86.                         int s=GETMOD();
  87.                         if (s!=LB_ERR) {
  88.                             MLM(LB_DELETESTRING,s,0);
  89.                             /* Free the music from memory */
  90.                             BASS_MusicFree(mods[s]);
  91.                             memcpy(mods+s,mods+s+1,(modc-s-1)*sizeof(HMUSIC));
  92.                             modc--;
  93.                         }
  94.                     }
  95.                     return 1;
  96.                 case ID_MODPLAY:
  97.                     {
  98.                         int s=GETMOD();
  99.                         /* Play the music (continue from current position) */
  100.                         if (s!=LB_ERR)
  101.                             if (!BASS_MusicPlay(mods[s])) Error("Can't play music");
  102.                     }
  103.                     return 1;
  104.                 case ID_MODSTOP:
  105.                     {
  106.                         int s=GETMOD();
  107.                         /* Stop the music */
  108.                         if (s!=LB_ERR) BASS_ChannelStop(mods[s]);
  109.                     }
  110.                     return 1;
  111.                 case ID_MODRESTART:
  112.                     {
  113.                         int s=GETMOD();
  114.                         /* Play the music from the start */
  115.                         if (s!=LB_ERR) BASS_MusicPlayEx(mods[s],0,-1,TRUE);
  116.                     }
  117.                     return 1;
  118.  
  119.                 case ID_SAMADD:
  120.                     {
  121.                         char file[MAX_PATH]="";
  122.                         HSAMPLE sam;
  123.                         ofn.lpstrFilter="WAVE sample files (wav)\0*.wav\0All files\0*.*\0\0";
  124.                         ofn.lpstrFile=file;
  125.                         if (GetOpenFileName(&ofn)) {
  126.                             memcpy(path,file,ofn.nFileOffset);
  127.                             path[ofn.nFileOffset-1]=0;
  128.                             /* Load a sample from "file" and give it a max of 3 simultaneous
  129.                             playings using playback position as override decider */
  130.                             if (sam=BASS_SampleLoad(FALSE,file,0,0,3,BASS_SAMPLE_OVER_POS)) {
  131.                                 samc++;
  132.                                 sams=(HSAMPLE*)realloc((void*)sams,samc*sizeof(HSAMPLE));
  133.                                 sams[samc-1]=sam;
  134.                                 SLM(LB_ADDSTRING,0,file);
  135.                             } else
  136.                                 Error("Can't load sample");
  137.                         }
  138.                     }
  139.                     return 1;
  140.                 case ID_SAMREMOVE:
  141.                     {
  142.                         int s=GETSAM();
  143.                         if (s!=LB_ERR) {
  144.                             SLM(LB_DELETESTRING,s,0);
  145.                             /* Free the sample from memory */
  146.                             BASS_SampleFree(sams[s]);
  147.                             memcpy(sams+s,sams+s+1,(samc-s-1)*sizeof(HSAMPLE));
  148.                             samc--;
  149.                         }
  150.                     }
  151.                     return 1;
  152.                 case ID_SAMPLAY:
  153.                     {
  154.                         int s=GETSAM();
  155.                         /* Play the sample from the start, volume=50, random pan position,
  156.                         using the default frequency and looping settings */
  157.                         if (s!=LB_ERR) {
  158.                             if (!BASS_SamplePlayEx(sams[s],0,-1,50,(rand()%201)-100,-1))
  159.                                 Error("Can't play sample");
  160.                         }
  161.                     }
  162.                     return 1;
  163.  
  164.                 case ID_STRNEW:
  165.                     {
  166.                         char file[MAX_PATH]="";
  167.                         ofn.lpstrFilter="Streamable files (wav/mp3)\0*.wav;*.mp3\0All files\0*.*\0\0";
  168.                         ofn.lpstrFile=file;
  169.                         if (GetOpenFileName(&ofn)) {
  170.                             memcpy(path,file,ofn.nFileOffset);
  171.                             path[ofn.nFileOffset-1]=0;
  172.                             /* Free old stream (if any) and create new one */
  173.                             BASS_StreamFree(str);
  174.                             MESS(ID_STRFILE,WM_SETTEXT,0,"");
  175.                             if (!(str=BASS_StreamCreateFile(FALSE,file,0,0,0)))
  176.                                 Error("Can't create stream");
  177.                             else
  178.                                 MESS(ID_STRFILE,WM_SETTEXT,0,file);
  179.                         }
  180.                     }
  181.                     return 1;
  182.                 case ID_STRPLAY:
  183.                     /* Play stream, not flushed */
  184.                     if (!BASS_StreamPlay(str,FALSE,0))
  185.                         Error("Can't play stream");
  186.                     return 1;
  187.                 case ID_STRSTOP:
  188.                     /* Stop the stream */
  189.                     BASS_ChannelStop(str);
  190.                     return 1;
  191.  
  192.                 case ID_CDPLAY:
  193.                     /* Play CD track (looped) */
  194.                     {
  195.                         char track[4];
  196.                         MESS(ID_CDTRACK,WM_GETTEXT,4,track);
  197.                         if (!BASS_CDPlay(atoi(track),TRUE,FALSE))
  198.                             Error("Can't play CD");
  199.                         else
  200.                             cdplaying=TRUE;
  201.                     }
  202.                     return 1;
  203.                 case ID_CDSTOP:
  204.                     /* Pause CD */
  205.                     BASS_ChannelPause(CDCHANNEL);
  206.                     cdplaying=FALSE;
  207.                     return 1;
  208.                 case ID_CDRESUME:
  209.                     /* Resume CD */
  210.                     if (BASS_ChannelResume(CDCHANNEL))
  211.                         cdplaying=TRUE;
  212.                     return 1;
  213.  
  214.                 case ID_STOP:
  215.                     /* Pause digital output and CD */
  216.                     BASS_Pause();
  217.                     BASS_ChannelPause(CDCHANNEL);
  218.                     return 1;
  219.                 case ID_RESUME:
  220.                     /* Resume digital output and CD */
  221.                     if (cdplaying) BASS_ChannelResume(CDCHANNEL);
  222.                     BASS_Start();
  223.                     return 1;
  224.             }
  225.             break;
  226.  
  227.         case WM_INITDIALOG:
  228.             win=h;
  229.             MESS(ID_CDTRACK,WM_SETTEXT,0,"1");
  230.             SetTimer(h,1,250,NULL);
  231.             GetCurrentDirectory(MAX_PATH,path);
  232.             memset(&ofn,0,sizeof(ofn));
  233.             ofn.lStructSize=sizeof(ofn);
  234.             ofn.hwndOwner=h;
  235.             ofn.hInstance=inst;
  236.             ofn.nMaxFile=MAX_PATH;
  237.             ofn.lpstrInitialDir=path;
  238.             ofn.Flags=OFN_HIDEREADONLY|OFN_EXPLORER;
  239.             return 1;
  240.  
  241.         case WM_DESTROY:
  242.             KillTimer(h,1);
  243.             PostQuitMessage(0);
  244.             return 1;
  245.     }
  246.     return 0;
  247. }
  248.  
  249. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  250. {
  251.     MSG msg;
  252.     inst=hInstance;
  253.  
  254. /* Check that BASS 0.8 was loaded */
  255.     if (BASS_GetVersion()!=MAKELONG(0,8)) {
  256.         MessageBox(0,"BASS version 0.8 was not loaded","Incorrect BASS.DLL",0);
  257.         return 0;
  258.     }
  259.  
  260.     if (!CreateDialog(hInstance,MAKEINTRESOURCE(1000),NULL,&dialogproc)) {
  261.         Error("Can't create window");
  262.         return 0;
  263.     }
  264.  
  265. /* Initialize digital sound - default device, 44100hz, stereo, 16 bits, no syncs used */
  266.     if (!BASS_Init(-1,44100,BASS_DEVICE_NOSYNC,win)) Error("Can't initialize digital sound system");
  267. /* Initialize CD */
  268.     if (!BASS_CDInit(NULL)) Error("Can't initialize CD system");
  269.  
  270.     BASS_Start();    /* Start digital output */
  271.  
  272.     while (1) {
  273.         if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  274.             if (!GetMessage(&msg, NULL, 0, 0))
  275.                 break;
  276.             TranslateMessage(&msg);
  277.             DispatchMessage(&msg);
  278.         } else
  279.             WaitMessage();
  280.     }
  281.     
  282.     BASS_Stop();    /* Stop digital output */
  283. /* Free the stream */
  284.     BASS_StreamFree(str);
  285. /* It's not actually necessary to free the musics and samples
  286. because they are automatically freed by BASS_Free() */
  287. /* Free musics */
  288.     if (mods) {
  289.         int a;
  290.         for (a=0;a<modc;a++) BASS_MusicFree(mods[a]);
  291.         free(mods);
  292.     }
  293. /* Free samples */
  294.     if (sams) {
  295.         int a;
  296.         for (a=0;a<samc;a++) BASS_SampleFree(sams[a]);
  297.         free(sams);
  298.     }
  299.     BASS_Free();    /* Close digital sound system */
  300.     BASS_CDFree();    /* Close CD system */
  301.     return 0;
  302. }
  303.