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

  1. // BASS Internet radio example, copyright (c) 2002-2005 Ian Luck.
  2.  
  3. #include <windows.h>
  4. #include <process.h>
  5. #include <stdio.h>
  6. #include "bass.h"
  7.  
  8.  
  9. HWND win=NULL;
  10. DWORD cthread=0;
  11.  
  12. HSTREAM chan;
  13.  
  14. char *urls[10]={ // stream URLs
  15.     "http://64.236.34.196/stream/1048","http://205.188.234.129:8024",
  16.     "http://64.236.34.97/stream/1006","http://206.98.167.99:8406",
  17.     "http://160.79.1.141:8000","http://streams.riverhosting.net:8012",
  18.     "http://205.188.234.4:8016","http://205.188.234.4:8014",
  19.     "http://207.200.96.225:8010","http://64.202.98.91:8082"
  20. };
  21.  
  22. // display error messages
  23. void Error(char *es)
  24. {
  25.     char mes[200];
  26.     sprintf(mes,"%s\n(error code: %d)",es,BASS_ErrorGetCode());
  27.     MessageBox(win,mes,"Error",0);
  28. }
  29.  
  30. #define MESS(id,m,w,l) SendDlgItemMessage(win,id,m,(WPARAM)w,(LPARAM)l)
  31.  
  32. // update stream title from metadata
  33. void DoMeta(char *meta)
  34. {
  35.     char *p;
  36.     if (meta && (p=strstr(meta,"StreamTitle='"))) {
  37.         p=strdup(p+13);
  38.         strchr(p,';')[-1]=0;
  39.         MESS(30,WM_SETTEXT,0,p);
  40.         free(p);
  41.     }
  42. }
  43.  
  44. void CALLBACK MetaSync(HSYNC handle, DWORD channel, DWORD data, DWORD user)
  45. {
  46.     DoMeta((char*)data);
  47. }
  48.  
  49. void CALLBACK StatusProc(void *buffer,DWORD length,DWORD user)
  50. {
  51.     if (buffer && !length)
  52.         MESS(32,WM_SETTEXT,0,buffer); // display connection status
  53. }
  54.  
  55. void __cdecl OpenURL(char *url)
  56. {
  57.     BASS_StreamFree(chan); // close old stream
  58.     MESS(31,WM_SETTEXT,0,"connecting...");
  59.     MESS(30,WM_SETTEXT,0,"");
  60.     MESS(32,WM_SETTEXT,0,"");
  61.     if (!(chan=BASS_StreamCreateURL(url,0,BASS_STREAM_STATUS,StatusProc,0))) {
  62.         MESS(31,WM_SETTEXT,0,"not playing");
  63.         Error("Can't play the stream");
  64.     } else {
  65.         while (1) { // pre-buffer...
  66.             char text[20];
  67.             DWORD progress,len=BASS_StreamGetFilePosition(chan,BASS_FILEPOS_END);
  68.             if (len==-1) goto done; // something's gone wrong! (eg. BASS_Free called)
  69.             progress=(BASS_StreamGetFilePosition(chan,BASS_FILEPOS_DOWNLOAD)
  70.                 -BASS_StreamGetFilePosition(chan,BASS_FILEPOS_CURRENT))*100/len; // percentage of buffer filled
  71.             if (progress>75) break; // over 75% full, enough
  72.             sprintf(text,"buffering... %d%%",progress);
  73.             MESS(31,WM_SETTEXT,0,text);
  74.             Sleep(50);
  75.         }
  76.         { // get the broadcast name and bitrate
  77.             char *icy=BASS_StreamGetTags(chan,BASS_TAG_ICY);
  78.             if (icy)
  79.                 for (;*icy;icy+=strlen(icy)+1) {
  80.                     if (!memcmp(icy,"icy-name:",9))
  81.                         MESS(31,WM_SETTEXT,0,icy+9);
  82.                     if (!memcmp(icy,"icy-br:",7)) {
  83.                         char br[30]="bitrate: ";
  84.                         strcat(br,icy+7);
  85.                         MESS(32,WM_SETTEXT,0,br);
  86.                     }
  87.                 }
  88.             else
  89.                 MESS(31,WM_SETTEXT,0,"");
  90.         }
  91.         // get the stream title and set sync for subsequent titles
  92.         DoMeta(BASS_StreamGetTags(chan,BASS_TAG_META));
  93.         BASS_ChannelSetSync(chan,BASS_SYNC_META,0,&MetaSync,0);
  94.         // play it!
  95.         BASS_ChannelPlay(chan,FALSE);
  96.     }
  97. done:
  98.     cthread=0;
  99. }
  100.  
  101. BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  102. {
  103.     switch (m) {
  104.         case WM_COMMAND:
  105.             switch (LOWORD(w)) {
  106.                 case IDCANCEL:
  107.                     DestroyWindow(h);
  108.                     return 1;
  109.                 default:
  110.                     if (LOWORD(w)>=10 && LOWORD(w)<20) {
  111.                         if (cthread) { // already connecting
  112.                             MessageBeep(0);
  113.                             break;
  114.                         }
  115.                         // open URL in a new thread (so that main thread is free)
  116.                         cthread=_beginthread(OpenURL,0,urls[LOWORD(w)-10]);
  117.                     }
  118.             }
  119.             break;
  120.  
  121.         case WM_INITDIALOG:
  122.             win=h;
  123.             // setup output device
  124.             if (!BASS_Init(-1,44100,0,win,NULL)) {
  125.                 Error("Can't initialize device");
  126.                 DestroyWindow(win);
  127.             }
  128.             BASS_SetConfig(BASS_CONFIG_NET_PREBUF,0); // minimize automatic pre-buffering, so we can do it (and display it) instead
  129.             return 1;
  130.  
  131.         case WM_DESTROY:
  132.             BASS_Free();
  133.             break;
  134.     }
  135.     return 0;
  136. }
  137.  
  138. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  139. {
  140.     // check that BASS 2.2 was loaded
  141.     if (BASS_GetVersion()!=MAKELONG(2,2)) {
  142.         MessageBox(0,"BASS version 2.2 was not loaded","Incorrect BASS.DLL",0);
  143.         return 0;
  144.     }
  145.  
  146.     // display the window
  147.     DialogBox(hInstance,MAKEINTRESOURCE(1000),0,&dialogproc);
  148.  
  149.     return 0;
  150. }
  151.