home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / bass / c / multi / multi.c < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-14  |  3.7 KB  |  143 lines

  1. // BASS Multiple output example, copyright (c) 2001-2005 Ian Luck.
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5.  
  6. #include "bass.h"
  7.  
  8. HWND win=NULL;
  9.  
  10. DWORD outdev[2];    // output devices
  11. HSTREAM chan[2];    // the streams
  12.  
  13. /* display error messages */
  14. void Error(char *es)
  15. {
  16.     char mes[200];
  17.     sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
  18.     MessageBox(win,mes,"Error",0);
  19. }
  20.  
  21. #define MESS(id,m,w,l) SendDlgItemMessage(win,id,m,(WPARAM)w,(LPARAM)l)
  22.  
  23. BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  24. {
  25.     static OPENFILENAME ofn;
  26.     static char path[MAX_PATH];
  27.  
  28.     switch (m) {
  29.         case WM_COMMAND:
  30.             switch (LOWORD(w)) {
  31.                 case IDCANCEL:
  32.                     DestroyWindow(h);
  33.                     break;
  34.                 case 10: // open a file to play on device #1
  35.                 case 11: // open a file to play on device #2
  36.                     {
  37.                         int devn=LOWORD(w)-10;
  38.                         char file[MAX_PATH]="";
  39.                         ofn.lpstrFilter="streamable files\0*.mp3;*.mp2;*.mp1;*.ogg;*.wav;*.aif\0All files\0*.*\0\0";
  40.                         ofn.lpstrFile=file;
  41.                         if (GetOpenFileName(&ofn)) {
  42.                             memcpy(path,file,ofn.nFileOffset);
  43.                             path[ofn.nFileOffset-1]=0;
  44.                             BASS_StreamFree(chan[devn]);
  45.                             BASS_SetDevice(outdev[devn]); // set the device to create stream on
  46.                             if (!(chan[devn]=BASS_StreamCreateFile(FALSE,file,0,0,BASS_SAMPLE_LOOP))) {
  47.                                 MESS(10+devn,WM_SETTEXT,0,"click here to open a file...");
  48.                                 Error("Can't play the file");
  49.                                 break;
  50.                             }
  51.                             MESS(10+devn,WM_SETTEXT,0,file);
  52.                             BASS_ChannelPlay(chan[devn],FALSE);
  53.                         }
  54.                     }
  55.                     break;
  56.             }
  57.             break;
  58.  
  59.         case WM_INITDIALOG:
  60.             win=h;
  61.             GetCurrentDirectory(MAX_PATH,path);
  62.             memset(&ofn,0,sizeof(ofn));
  63.             ofn.lStructSize=sizeof(ofn);
  64.             ofn.hwndOwner=h;
  65.             ofn.nMaxFile=MAX_PATH;
  66.             ofn.lpstrInitialDir=path;
  67.             ofn.Flags=OFN_HIDEREADONLY|OFN_EXPLORER;
  68.             // setup output devices
  69.             if (!BASS_Init(outdev[0],44100,0,win,NULL)) {
  70.                 Error("Can't initialize device 1");
  71.                 DestroyWindow(win);
  72.             }
  73.             if (!BASS_Init(outdev[1],44100,0,win,NULL)) {
  74.                 Error("Can't initialize device 2");
  75.                 DestroyWindow(win);
  76.             }
  77.             MESS(20,WM_SETTEXT,0,BASS_GetDeviceDescription(outdev[0]));
  78.             MESS(21,WM_SETTEXT,0,BASS_GetDeviceDescription(outdev[1]));
  79.             return 1;
  80.  
  81.         case WM_DESTROY:
  82.             // release both devices
  83.             BASS_SetDevice(outdev[0]);
  84.             BASS_Free();
  85.             BASS_SetDevice(outdev[1]);
  86.             BASS_Free();
  87.             break;
  88.     }
  89.     return 0;
  90. }
  91.  
  92.  
  93. // Simple device selector dialog stuff begins here
  94. BOOL CALLBACK devicedialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  95. {
  96.     switch (m) {
  97.         case WM_COMMAND:
  98.             switch (LOWORD(w)) {
  99.                 case 10:
  100.                     if (HIWORD(w)!=LBN_DBLCLK) break;
  101.                 case IDOK:
  102.                     {
  103.                         int device=SendDlgItemMessage(h,10,LB_GETCURSEL,0,0)+1;
  104.                         EndDialog(h,device);
  105.                     }
  106.                     break;
  107.             }
  108.             break;
  109.  
  110.         case WM_INITDIALOG:
  111.             {
  112.                 char buf[30],*d;
  113.                 int c;
  114.                 sprintf(buf,"Select output device #%d",l);
  115.                 SetWindowText(h,buf);
  116.                 for (c=1;d=BASS_GetDeviceDescription(c);c++) // device 1 = 1st real device
  117.                     SendDlgItemMessage(h,10,LB_ADDSTRING,0,(int)d);
  118.                 SendDlgItemMessage(h,10,LB_SETCURSEL,0,0);
  119.             }
  120.             return 1;
  121.     }
  122.     return 0;
  123. }
  124. // Device selector stuff ends here
  125.  
  126. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  127. {
  128.     /* Check that BASS 2.2 was loaded */
  129.     if (BASS_GetVersion()!=MAKELONG(2,2)) {
  130.         MessageBox(0,"BASS version 2.2 was not loaded","Incorrect BASS.DLL",0);
  131.         return 0;
  132.     }
  133.  
  134.     /* Let the user choose the output devices */
  135.     outdev[0]=DialogBoxParam(hInstance,(char*)2000,win,&devicedialogproc,1);
  136.     outdev[1]=DialogBoxParam(hInstance,(char*)2000,win,&devicedialogproc,2);
  137.  
  138.     /* main dialog */
  139.     DialogBox(hInstance,(char*)1000,0,&dialogproc);
  140.  
  141.     return 0;
  142. }
  143.