Subversion Repositories Kolibri OS

Rev

Rev 7174 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //
  2. //   This file is part of the AC97 mp3 player.
  3. //   (C) copyright Serge 2006-2007 infinity_sound@mail.ru
  4. //   (C) copyright Quantum 2007    ufmod@users.sf.net
  5. //
  6. //   This program is free software; you can redistribute it and/or modify
  7. //   it under the terms of the GNU General Public License as published by
  8. //   the Free Software Foundation; either version 2 of the License, or
  9. //   (at your option) any later version.
  10. //
  11. //   This program is distributed in the hope that it will be useful,
  12. //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. //   GNU General Public License for more details.
  15.  
  16. #include "../kolibri.h"
  17. #include "string.h"
  18. #include "ac97wav.h"
  19. #include "../mpg/mpg123.h"
  20. #include "../sound.h"
  21. #include "../ufmod-codec.h"                   /* uFMOD integration */
  22. void exit();                                  /* uFMOD integration */
  23.  
  24. #define DOCKABLE_WINDOW
  25. #define MP3_ERROR_OUT_OF_BUFFER  5
  26. int m_last_error;
  27.  
  28. void _stdcall thread_proc(void *param);
  29. int _stdcall create_thread(void *proc, void *param, int stack_size);
  30.  
  31. #ifdef DOCKABLE_WINDOW
  32. void GetThreadInfo (char *info, int slot); //Asper+
  33. #endif
  34.  
  35. void touch(char *buf, int size);
  36. int mp3FindSync(byte* buf, int size, int* sync);
  37. int stream_read_raw(struct reader *rd,unsigned char *buf, int size);
  38.  
  39. int __cdecl _stricmp (const char * dst, const char * src);
  40. char *__cdecl strrchr (const char * string,int ch);
  41. int _strncmp(char *src, char *dst, DWORD n); //Asper+
  42. int _strncpy (char *dst, char *src, int n); //Asper+
  43. void uint2str(unsigned int value, char *string); //Asper+
  44.  
  45. struct reader rd;
  46. struct frame fr;
  47.  
  48. DWORD hDrv;
  49. DWORD hSound;
  50. SNDBUF hBuff;
  51.  
  52. CTRL_INFO info;
  53.  
  54. FILEINFO   fileinfo;
  55. const char filename[256];
  56. const char *fileext;
  57. char full_filename[4096];
  58.  
  59. int m_vol;
  60. int l_vol=-700;     //-7db
  61. int r_vol=-700;
  62. int pan =0;
  63.  
  64. DWORD status;
  65. DWORD first_sync;
  66. DWORD PLStatus=0; //Asper+
  67. byte replay=0;
  68. byte hidden=0;
  69.  
  70. #ifdef DOCKABLE_WINDOW
  71. byte thread_info[1024]; //Asper+
  72. #endif
  73.  
  74. int tid, pl_tid;
  75. const DWORD main_wh=92, pl_ww=300, pl_wh=382; //Asper+
  76. DWORD pl_wx=100, pl_wy=101+92;  //Asper+
  77. //DWORD main_wc=0x404040, main_bc=0x808080;  //Asper+ ac97snd Classic style
  78. //DWORD main_wc=0x002040, main_bc=0x008080, main_ic=0x002040, selected_ic=0x1010F0;  //Asper+
  79. DWORD main_wc=0x101030, main_bc=0x008080, main_ic=0x000000, selected_ic=0x1010F0;  //Asper+
  80.  
  81. unsigned char *testbuff;
  82. unsigned char *outbuf;
  83. unsigned char *inpbuf;
  84. unsigned char *outPtr;
  85.  
  86. int inpsize;
  87. int outsize;
  88. int outremain;
  89. int totalout;
  90. int done;
  91.  
  92. char header[] = "AC97 MP3 player";
  93. char header_PL[] = "PLAYLIST";
  94. char buttons_xm[]  = " Play    Stop                    Vol-    Vol+"; /* uFMOD integration */
  95. char buttons_wav[] = " Play    Stop     <<      >>     Vol-    Vol+"; /* uFMOD integration */
  96. char button_PL[] = "PL"; //Asper+
  97. char button_R[] = "R"; //Asper+
  98. char *buttons_text = buttons_wav;                                     /* uFMOD integration */
  99.  
  100. void play_xm();                                                       /* uFMOD integration */
  101. void (*snd_play)();
  102.  
  103.  
  104. //Asper_____________________Play List code start_____________________________
  105. #define PLI_BUTTON_HEIGHT    13
  106. #define PL_MAX_SHOWN_ITEMS   (pl_wh-PLI_BUTTON_HEIGHT-40)/PLI_BUTTON_HEIGHT
  107. #define MAX_TEXT_WIDTH       46
  108. #define MAX_PATH_LEN         1024
  109.  
  110. int currSelected, currActive, currFirstShowed;
  111. unsigned char *pl_buff;
  112. char pl_path[4096];
  113. int pl_items_number;
  114.  
  115. int ShowPLContent(char *filebuffer);
  116. int GetFileNameFromPL(const char *fbuff, int index, char *name);
  117. void Win2Dos (char *st, int len);
  118.  
  119. void HidePLWindow()
  120. {
  121.    BeginDraw();
  122.    ResizeReplaceWindow(pl_wx,pl_wy,0,0);
  123.    EndDraw();
  124. }
  125.  
  126. void ShowPLWindow()
  127. {
  128.    unsigned int i;
  129.  
  130.    BeginDraw();
  131.    DrawWindow(pl_wx,pl_wy,pl_ww,pl_wh,main_ic,4,0,0,0);
  132.  
  133.    for (i=0; i<PL_MAX_SHOWN_ITEMS; i++)
  134.            make_button(7,24+i*(PLI_BUTTON_HEIGHT+1),285,PLI_BUTTON_HEIGHT, (0x10+i)|BT_NORMAL|BT_NOFRAME, main_ic);
  135.  
  136.    write_text(8,8,FONT0, header_PL, sizeof(header_PL)-1);
  137.    ShowPLContent(pl_buff);
  138.    EndDraw();
  139. }
  140.  
  141. int LoadTrack(int i)
  142. {
  143.         if (GetFileNameFromPL(pl_buff, i, filename))
  144.         {
  145.                 strcpy (full_filename, pl_path);
  146.                 strcat (full_filename, filename);
  147.                 return 1;
  148.         }
  149.         return 0;
  150. }
  151.  
  152. void _stdcall pl_thread_proc(void *param)
  153. {  int evnt;
  154.    int key, button;
  155.    DWORD tmp_x, tmp_y, i; //Asper+
  156.    char ipc_buff[16]="";
  157.  
  158.    set_event_mask(0x47); //Asper + IPC event
  159.    ipc_init(ipc_buff, 16);
  160.    
  161.   _asm
  162.   {
  163.     mov eax, 66
  164.     mov ebx, 1
  165.     mov ecx, 1
  166.     int 0x40
  167.   };
  168.    
  169.   ShowPLWindow();
  170.  
  171.   while(1)
  172.   {
  173.         if (PLStatus&0xF0)
  174.     {
  175.                 switch(PLStatus)
  176.                 {
  177.                         case 0x11:
  178.                                 HidePLWindow();
  179.                         break;
  180.                         case 0x12:
  181.                                 ResizeReplaceWindow(pl_wx,pl_wy,pl_ww,pl_wh);                  
  182.                         break;
  183.                 }
  184.                 PLStatus&=0x0F;
  185.         }
  186.         switch (status)
  187.         {
  188.                 case ST_TRACK:
  189.                         break;
  190.                 case ST_EXIT:
  191.                         PLStatus=0x00;
  192.                         exit();
  193.         }
  194.  
  195. #ifdef DOCKABLE_WINDOW
  196.         tmp_x = (DWORD)thread_info[35]*0x100+(DWORD)thread_info[34];
  197.         tmp_y = (DWORD)thread_info[38]+main_wh+1;
  198.         if (pl_wx!= tmp_x || pl_wy!=tmp_y)
  199.         {
  200.                 pl_wx=tmp_x;
  201.                 pl_wy=tmp_y;
  202.                 ResizeReplaceWindow(pl_wx,pl_wy,-1,-1);
  203.         }
  204. #endif
  205.  
  206.     evnt = wait_for_event(20);
  207.  
  208.     switch(evnt)
  209.     {
  210.       case EV_REDRAW:
  211.                           ShowPLWindow();
  212.         break;
  213.  
  214.       case EV_KEY:
  215.         if(!get_key(&key))
  216.         {
  217.        
  218.           switch(key)
  219.           {  case 0xE0:
  220.              case 0xE1:
  221.                break;
  222.              default:
  223.                switch (key)
  224.                {
  225.                  case 0x01:  //Esc
  226.                                          PLStatus=0x00;
  227.                                          exit();
  228.                                          break;
  229.  
  230.                                  case 0x19: //P - playlist
  231.                                    switch (PLStatus)
  232.                                    {    case 0x00: //PL not started.
  233.                                                         pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  234.                                                         PLStatus=0x12;
  235.                                                 break;
  236.                                                 case 0x01: //PL started, but hidden.
  237.                                                         PLStatus=0x12;
  238.                                                 break;
  239.                                                 case 0x02: //PL started and showed.
  240.                                                         PLStatus=0x11;
  241.                                                 break;
  242.                                    }
  243.                                    break;
  244.  
  245.                                  case 0x1C:  //Enter                  
  246.                                          currActive=currFirstShowed+currSelected-1;
  247.                                          status = ST_TRACK;
  248.                                          break;
  249.  
  250.                                  case 0x2C: //Z - previous
  251.                                    currActive-=2;
  252.                                    status = ST_TRACK;
  253.                                    break;
  254.                                  
  255.                                  case 0x2D: //X - play
  256.                                    status = ST_PLAY;
  257.                                    break;
  258.                                  
  259.                                  case 0x2E: //C - pause
  260.                                    break;
  261.                                  
  262.                                  case 0x2F: //V - stop
  263.                    status = ST_STOP;
  264.                                    break;
  265.                                  
  266.                                  case 0x30: //B - next
  267.                                    status = ST_TRACK;
  268.                                    break;
  269.  
  270.                  case 0x47:  //Home
  271.                                          if(l_vol < 0)
  272.                                          { l_vol+=100;
  273.                                            r_vol+=100;  
  274.                                            SetVolume(hBuff,l_vol,r_vol);
  275.                                          };
  276.                                    break;
  277.                  case 0x48:  //Up
  278.                                          if (currSelected==0)
  279.                                          {
  280.                                                  if (currFirstShowed>0)
  281.                                                          currFirstShowed--;
  282.                                                  else break;
  283.                                                  ShowPLContent(pl_buff);
  284.                                                  break;
  285.                                          }
  286.                                          currSelected--;
  287.                                          ShowPLContent(pl_buff);
  288.                                          break;
  289.                  case 0x50:  //Down
  290.                                          if (currSelected+currFirstShowed > pl_items_number-2) break;
  291.                                          if (currSelected==PL_MAX_SHOWN_ITEMS-1)
  292.                                          {
  293.                                                  //if (currFirstShowed<pl_items_number)
  294.                                                          currFirstShowed++;
  295.                                                  ShowPLContent(pl_buff);
  296.                                                  break;
  297.                                          }
  298.                                          //if (currSelected<PL_MAX_SHOWN_ITEMS)
  299.                                          currSelected++;
  300.                                          ShowPLContent(pl_buff);
  301.                                          break;
  302.                  case 0x4F:  //End                
  303.                                          if(l_vol > -10000)
  304.                                          { l_vol-=100;
  305.                                            r_vol-=100;  
  306.                                            SetVolume(hBuff,l_vol,r_vol);
  307.                                          };
  308.                                          break;
  309.                  case 0x53:
  310.                                          if(pan > -10000)
  311.                                          { pan -=100;
  312.                                            SetPan(hBuff,pan);
  313.                                          };
  314.                                          break;  
  315.                  case 0x51:
  316.                                          if(pan < 10000)
  317.                                          { pan +=100;
  318.                                            SetPan(hBuff,pan);
  319.                                          };
  320.                                          break;  
  321.                            }
  322.                   };    
  323.                 };  
  324.                 break;
  325.  
  326.       case EV_BUTTON:
  327.                   button=get_button_id();
  328.                   if (button==1)
  329.                   {
  330.                           PLStatus=0x00;
  331.                           exit();
  332.                   }
  333.                   currActive=currFirstShowed+button-0x10-1;
  334.                   status = ST_TRACK;
  335.                   break;
  336.           case EV_IPC:
  337.                   *ipc_buff='\0';
  338.                   ShowPLContent(pl_buff);
  339.                   break;
  340.  
  341.         }
  342.   }
  343. }
  344.  
  345. void Win2Dos (char *st, int len)
  346. {
  347.         int i;
  348.         unsigned char ch;
  349.  
  350.         for (i=0; i<len; i++)
  351.         {
  352.                 ch = st[i];
  353.                 if (ch>=192)
  354.                 {
  355.                         if (ch>=240) ch-=16;
  356.                         else ch-=64;
  357.                 }
  358.                 else
  359.                 {
  360.                         if (ch==168) ch = 240;
  361.                         if (ch==184) ch = 241;
  362.                         if (ch==185) ch = 252;
  363.                         if ((ch==147) || (ch==148) || (ch==171) || (ch==187)) ch = 34;
  364.                         if ((ch==150) || (ch==151)) ch = 45;
  365.                 }
  366.                 st[i]=ch;
  367.         }
  368. }
  369.  
  370. int GetFileNameFromPL(const char *plbuff, int index, char *name)
  371. {
  372.         int count=0,i=0,j=0;
  373.         char ch;
  374.  
  375.         do{            
  376.                 ch=plbuff[i];
  377.                 if (ch!='#' && i && plbuff[i-1]=='\n')
  378.                         count++;
  379.                 if (count-1==index)
  380.                 {
  381.                         if (j>MAX_PATH_LEN || ch=='\r' || ch=='\n')
  382.                         {
  383.                                 name[j]='\0';
  384.                                 break;
  385.                         }
  386.                         if (ch=='\\') ch='/';
  387.                         name[j++]=ch;
  388.                 }
  389.                 else if (count-1>index) break;
  390.                 i++;
  391.         }while (ch);
  392.         if (!ch) return 0;
  393.         return j;
  394. }
  395.  
  396. int CountFileNamesInPL(const char *plbuff)
  397. {
  398.         int count=0,i=0;
  399.         char ch;
  400.  
  401.         do{
  402.                 ch=plbuff[i];
  403.                 if (ch!='#' && i && plbuff[i-1]=='\n')
  404.                         count++;
  405.                 i++;
  406.         }while (ch);
  407.         if (count) count--;
  408.         return count;
  409. }
  410.  
  411. int ShowPLContent(char *filebuffer)
  412. {
  413.         char st[MAX_PATH_LEN+10]="", tmp[MAX_PATH_LEN+1]="";
  414.         unsigned int len=8,i;
  415.         DWORD text_color;
  416.  
  417.         draw_bar(7,24,285,PLI_BUTTON_HEIGHT*(PL_MAX_SHOWN_ITEMS+2), main_ic);
  418.         draw_bar(7,24+currSelected*(PLI_BUTTON_HEIGHT+1),285,PLI_BUTTON_HEIGHT, selected_ic);
  419.         for (i=0; i<PL_MAX_SHOWN_ITEMS; i++)
  420.         {
  421.                 text_color = (currFirstShowed+i==currActive?0xFFFFFF|FONT0:0x00FF20|FONT0);
  422.                 if (!GetFileNameFromPL(filebuffer, i+currFirstShowed, tmp)) break;
  423.                 uint2str(currFirstShowed+i+1, st);
  424.                 strcat(st, ". ");
  425.                 strcat(st, tmp);
  426.                 len = strlen(st);
  427.                 if (len > MAX_TEXT_WIDTH)
  428.                         len = MAX_TEXT_WIDTH;
  429.                 write_text(11,i*(PLI_BUTTON_HEIGHT+1)+27,text_color, st, len);
  430.         }
  431.         return 1;
  432. }
  433. //Asper_____________________Play List code end_____________________________
  434. void redraw_R_button() //Asper +
  435. {
  436.         DWORD rc = 0xA0FFA0; //R button text color
  437.         if (!replay) rc = 0x404040;
  438.         write_text(14,74,rc|FONT0,button_R,sizeof(button_R)-1);
  439. }
  440.  
  441. void update_dynamic_content() //Asper +
  442. {
  443.         int len = strlen(filename);
  444.         if (len > 47) len = 47;
  445.         draw_bar(7,41,286,11,main_wc);
  446.         draw_bar(7,55,286,11,main_wc);
  447.         write_text(11,57,0x00FF20|FONT0, filename, len);
  448. }
  449.  
  450. void draw_window()
  451. {
  452.    if (!hidden)
  453.    {
  454.       BeginDraw();
  455.       DrawWindow(100,100,299,main_wh,main_wc,4,0,0,0); //Asper+  
  456.  
  457.       make_button(7,24,45,13, 0x10|BT_NORMAL,main_bc);
  458.       make_button(56,24,45,13, 0x11|BT_NORMAL,main_bc);
  459.       make_button(104,24,45,13, 0x12|BT_NORMAL,main_bc);
  460.       make_button(152,24,45,13, 0x13|BT_NORMAL,main_bc);
  461.       make_button(200,24,45,13, 0x14|BT_NORMAL,main_bc);
  462.       make_button(248,24,45,13, 0x15|BT_NORMAL,main_bc);
  463.  
  464.       make_button(268,70,25,13, 0x16|BT_NORMAL,main_bc); //Asper+ PL button
  465.       make_button(10,70,12,13, 0x17|BT_NORMAL,main_wc); //Asper+ R button
  466.  
  467.       make_button(7,41,286,11, 0x30|BT_HIDE|BT_NOFRAME,main_wc);
  468.  
  469.       update_dynamic_content();
  470.       write_text(8,8,FONT0, header, sizeof(header)-1);                     /* uFMOD integration */
  471.       write_text(12,28,main_wc|FONT0,buttons_text,sizeof(buttons_wav)-1); /* uFMOD integration */
  472.       write_text(11,27,0xA0FFA0|FONT0,buttons_text,sizeof(buttons_wav)-1); /* uFMOD integration */
  473.  
  474.       write_text(276,74,main_wc|FONT0,button_PL,sizeof(button_PL)-1); //Asper+ PL button text
  475.       write_text(275,73,0xA0FFA0|FONT0,button_PL,sizeof(button_PL)-1); //Asper+
  476.       redraw_R_button();
  477.       EndDraw();
  478.    }
  479. };
  480.  
  481. void draw_progress_bar()
  482. {  DWORD x;
  483.    x = (DWORD)(287.0f * (float)(rd.filepos-rd.strremain)/(float)fileinfo.size); /* uFMOD integration */
  484.    if(x==0) return;
  485.    draw_bar(7,41,x,11,0xA0A0A0);
  486.    draw_bar(x+7,41,287-x,11,main_wc);
  487. };
  488.  
  489. void debug_out_str(const char* str)
  490. {
  491.   while (*str != 0)
  492.   {
  493.     debug_out(*str);
  494.     str++;
  495.   }
  496. }
  497.  
  498. void ExitInHiddenMode()
  499. {
  500.         uFMOD_StopSong();
  501.         StopBuffer(hBuff);
  502.         DestroyBuffer(hBuff);
  503.         exit();
  504. }
  505.  
  506. int LoadPL(char *fname)
  507. {
  508.         DWORD fmt;
  509.         DWORD r_bytes;
  510.         int retval;
  511.         int i;
  512. //              char st[100]="";
  513.  
  514.         char *pch;
  515.  
  516.         r_bytes=0;
  517.         pch=strrchr(fname, '/');
  518.         if (pch)
  519.                 i=pch-fname+1;
  520.         else
  521.                 i=strlen(fname);
  522.  
  523.         _strncpy (pl_path, fname, i);
  524.         pl_path[i]='\0';
  525.  
  526.         if (!pl_buff)
  527.                 pl_buff = UserAlloc(0x40000);
  528.         retval=read_file (fname,pl_buff,0,0x40000,&r_bytes);
  529.  
  530.         if ( retval && (r_bytes==0))
  531.                 return 0;
  532.  
  533.         Win2Dos(pl_buff, r_bytes);
  534.         pl_items_number=CountFileNamesInPL(pl_buff);
  535. /*
  536.         debug_out_str("\n\rPlay List files number = ");
  537.         itoa(pl_items_number, st, 10);
  538.         debug_out_str(st);
  539. */
  540.         fmt = test_m3u(pl_buff);
  541.  
  542.         if(!fmt)
  543.         {
  544.                 debug_out_str("\n\rInvalid M3U file");
  545.                 return 0;
  546.         }
  547.         debug_out_str("\n\rValid M3U file");
  548.         currSelected=currFirstShowed=0;
  549.         currActive=-1;
  550.         return 1;
  551. }
  552.  
  553. int LoadFile(char *fname)
  554. {
  555.    DWORD fmt;
  556.    DWORD r_bytes;
  557.    int retval;
  558.    int err;
  559.    unsigned char *ttl, *cur;               /* uFMOD integration */
  560.  
  561.    debug_out_str("\n\rPlay file ");
  562.    debug_out_str(fname);
  563.    debug_out_str("\n\r");
  564.  
  565.    if(get_fileinfo(fname, &fileinfo)==FILE_NOT_FOUND)
  566.    {  debug_out_str("\n\rfile not found\n\r");
  567.       return 0;
  568.    }
  569.  
  570.    r_bytes=0;
  571.    strcpy(filename, strrchr(fname,'/')+1);
  572.    if( !(fileext = strrchr(filename,'.')))
  573.            return 0;
  574.  
  575.    if(!_stricmp(fileext,".m3u"))
  576.    {
  577.            LoadPL(fname);
  578.            status=ST_TRACK;
  579.            return 1;
  580.    }
  581.  
  582.    if (!testbuff)
  583.            testbuff = UserAlloc(4096);
  584.  
  585.    retval=read_file (fname,testbuff,0,2048,&r_bytes);
  586.    if ( retval && (r_bytes==0))
  587.            return 0;
  588.  
  589.    if (!inpbuf)
  590.    {
  591.            inpbuf = UserAlloc(0x10000);
  592.            touch(inpbuf, 0x10000);
  593.    }
  594.    create_reader(&rd, inpbuf, 0x10000);
  595.    init_reader(&rd,fname);
  596.  
  597.    if(!_stricmp(fileext,".mp3"))
  598.    {   
  599.                         fmt = test_mp3(testbuff);              
  600.                         if(!fmt)
  601.                         {
  602.                           debug_out_str("\n\rInvalid MP3 file");
  603.                           return 0;                    
  604.                         };
  605.                         snd_play = &play_mp3;
  606.                         outremain = 0x40000;
  607.                         if (!outbuf)
  608.                         {
  609.                                 outbuf = UserAlloc(outremain);
  610.                                 touch(outbuf, outremain);
  611.                         }
  612.                         make_decode_tables(32767);
  613.                         init_layer2();
  614.                         init_layer3(32);
  615.                         fr.single = -1;
  616.                         goto play;
  617.    };
  618.  
  619.    if(!_stricmp(fileext,".xm"))
  620.    {
  621.            if(uFMOD_LoadSong(fname))
  622.            {
  623.               buttons_text = buttons_xm;               /* uFMOD integration */
  624.               fmt = PCM_2_16_48;                       /* uFMOD integration */
  625.               snd_play = &play_xm;                     /* uFMOD integration */
  626.               ttl = uFMOD_GetTitle();                  /* uFMOD integration */
  627.               cur = ttl;                               /* uFMOD integration */
  628.               err = 0;                                 /* uFMOD integration */
  629.               while(*cur && *cur++ != ' ') err++;      /* uFMOD integration */
  630.               if(err){                                 /* uFMOD integration */
  631.                       cur = fname;                             /* uFMOD integration */
  632.                              while(*cur) cur++;                       /* uFMOD integration */
  633.                              *cur++ = ' ';                            /* uFMOD integration */
  634.                              *cur++ = '|';                            /* uFMOD integration */
  635.                              *cur++ = ' ';                            /* uFMOD integration */
  636.                              while(*ttl) *cur++ = *ttl++;             /* uFMOD integration */
  637.                     }
  638.                     goto play;
  639.                  }  
  640.                  debug_out_str("\n\rInvalid XM file");
  641.      return 0;
  642.          };
  643.          
  644.          if(!_stricmp(fileext, ".wav"))
  645.          {
  646.        fmt = test_wav((WAVEHEADER*)testbuff);
  647.            if(fmt)
  648.                  {                                  
  649.                    snd_play = &play_wave;            
  650.                    set_reader(&rd, 44);              
  651.                    outbuf = UserAlloc(32*1024);
  652.                    touch(outbuf, 32768);
  653.                    goto play;
  654.                  }
  655.      debug_out_str("\n\rInvalid WAV file");
  656.      return 0;
  657.          };    
  658.  
  659.    debug_out_str("\n\rUnsupported file");
  660.    return 0;
  661.  
  662. play:
  663.  
  664.    status = ST_PLAY;
  665.    SetFormat(hBuff, fmt);
  666.    SetVolume(hBuff,l_vol,r_vol);
  667.    GetVolume(hBuff,&l_vol,&r_vol);
  668.  
  669.    return 1;
  670. }
  671.  
  672. int main(int argc, char *argv[])
  673. {
  674.    int err, ver;
  675.    int i;
  676.    char ipc_msg[2]="\0\0";
  677.    pl_items_number=0;
  678.  
  679.    if (argv[1][0]=='-' && argv[1][1]=='h')
  680.    {
  681.       hidden = 1;
  682.           argv[1]+=3;
  683.    }
  684.    strcpy (full_filename, argv[1]);
  685.    
  686.    InitHeap(1024*1024);
  687.    
  688.    if(err = InitSound(&ver))
  689.    {  
  690.      debug_out_str("Sound service not installed\n\r");
  691.      return 0;
  692.    }
  693.    
  694.    if( (SOUND_VERSION>(ver&0xFFFF)) ||
  695.        (SOUND_VERSION<(ver >> 16)))
  696.    {  
  697.      debug_out_str("Sound service version mismatch\n\r");
  698.      return 0;
  699.    }
  700.  
  701.    if (err = CreateBuffer(PCM_2_16_48, 0, &hBuff))
  702.    {
  703.      debug_out_str("create buffer return error\n\r");
  704.      return 0;
  705.    }
  706.  
  707.    if (!LoadFile(full_filename))
  708.            return 0;
  709.    tid=create_thread(thread_proc, 0, 4096);
  710.    
  711.    while(1)
  712.    {  delay(10);
  713.       switch(status)
  714.       {  case ST_TRACK:
  715.                         StopBuffer(hBuff);
  716.                         if (hidden) ExitInHiddenMode();
  717.                         if (LoadTrack(++currActive))
  718.                         {
  719.                                 if (LoadFile(full_filename))
  720.                                         status = ST_PLAY;
  721.                         }
  722.                         else
  723.                         {
  724.                                 currSelected=currFirstShowed=0;
  725.                                 currActive=-1;
  726.                                 if (!replay) status = ST_STOP;
  727.                                 continue;
  728.                         }
  729.  
  730.                         //Update ac97snd and PL windows
  731.                         i=currActive-currFirstShowed;
  732.                         if (i>PL_MAX_SHOWN_ITEMS-1)
  733.                                 currFirstShowed = currActive - PL_MAX_SHOWN_ITEMS/2;
  734.                         ipc_send_msg(tid, ipc_msg);
  735.                         ipc_send_msg(pl_tid, ipc_msg);
  736.                         continue;
  737.          
  738.           case ST_PLAY:
  739.                   snd_play();
  740.                   continue;
  741.  
  742.           case ST_STOP:
  743.                   StopBuffer(hBuff);
  744.                   status = ST_DONE;
  745.                   if (hidden) ExitInHiddenMode();
  746.                   continue;
  747.  
  748.           case ST_EXIT:
  749.                   uFMOD_StopSong();          /* uFMOD integration */
  750.                   StopBuffer(hBuff);
  751.                   DestroyBuffer(hBuff);
  752.                   return 0;
  753.           };
  754.    };
  755.    return 0;
  756. };
  757.  
  758. void touch(char *buf, int size)
  759. { int i;
  760.    char a;
  761.     for ( i = 0;i < size; i+=4096)     //alloc all pages
  762.       a = buf[i];
  763. };
  764.  
  765. DWORD test_m3u(char *buf) //Asper+
  766. {
  767.         char  *sign="#EXTM3U";
  768.         return _strncmp(buf, sign, 7);
  769. }
  770.  
  771. DWORD test_mp3(char *buf)
  772. {  unsigned long hdr;
  773.     WAVEHEADER whdr;
  774.     int attempts = 0;
  775.      
  776.     for (;;attempts++)
  777.     {  if(attempts > 1000)
  778.           return 0;
  779.         if(!rd.head_read(&rd,&hdr))
  780.                         return 0;
  781.         if(!decode_header(&fr,hdr))
  782.         {
  783.          if((hdr & 0xffffff00) == 0x49443300)
  784.               {
  785.                     int id3length = 0;
  786.                     id3length = parse_new_id3(&rd, hdr);
  787.                     continue;
  788.               };
  789.           rd.strpos-=3;
  790.           rd.stream-=3;
  791.           rd.strremain+=3;
  792.           continue;
  793.         };
  794.         break;
  795.     };
  796.          
  797.     first_sync = rd.filepos-rd.strremain-4;
  798.          
  799.     whdr.riff_id = 0x46464952;
  800.     whdr.riff_format = 0x45564157;
  801.     whdr.wFormatTag = 0x01;
  802.     whdr.nSamplesPerSec = freqs[fr.sampling_frequency];
  803.     whdr.nChannels = 2;
  804.     whdr.wBitsPerSample = 16;
  805.    
  806.     return test_wav(&whdr);
  807. };
  808.  
  809.  
  810. void play_mp3()
  811. {  char *outPtr;
  812.     int totalout;
  813.     int outcount;
  814.  
  815.  //   memset(&fr,0,sizeof(fr));
  816.     fr.down_sample_sblimit = 32;
  817.     fr.single = -1;
  818.     reset_mpg();
  819.  
  820.     outPtr = outbuf;
  821.     totalout=0;
  822.     done = 0;
  823.     outremain=0x40000;
  824.  
  825.     memset(outbuf,0,0x40000);
  826.     set_reader(&rd, 0);    //;first_sync);
  827.  
  828.     while(1)
  829.     { if(status!=ST_PLAY)
  830.              break;
  831.  
  832.      for(;;)
  833.      {   outcount = 0;                          
  834.           if( !read_frame(&rd, &fr))
  835.           {  done = 1;
  836.               break;
  837.           };
  838.           fr.do_layer(&fr, outPtr,&outcount);
  839.           outPtr+= outcount;
  840.           totalout+=outcount;
  841.           outremain-=outcount;
  842.           if(outremain < outcount*2)
  843.             break;  
  844.     };
  845.  
  846.     if(done)
  847.     { if(totalout < 4096)
  848.       {  memset(outPtr,0,4096-totalout);
  849.                 totalout = 4096;
  850.       };
  851.     }
  852.     else
  853.       if(totalout < 8192)
  854.         continue;
  855.  
  856.     outPtr = outbuf;      
  857.     while (totalout >= 4096)
  858.     {
  859.    
  860.       WaveOut(hBuff,outPtr,4096);
  861.       if(status!=ST_PLAY)
  862.       { if ((status != ST_EXIT) && (status != ST_STOP))
  863.          status = ST_TRACK;
  864.         return;
  865.       };
  866.       totalout-=4096;
  867.       outPtr+=4096;
  868.       outremain+=4096;
  869.     };
  870.     if(done)
  871.       break;
  872.      
  873.     memmove(outbuf,outPtr, totalout);
  874.     outPtr = outbuf+totalout;
  875.    }
  876.  
  877.     if ((status != ST_EXIT) && (status != ST_STOP))
  878.       status =  ST_TRACK;
  879. };
  880.  
  881. void play_wave()
  882. {  int count;
  883.  
  884.    set_reader(&rd,44);
  885.    while(1)
  886.    {
  887.       if(status!=ST_PLAY)
  888.         break;
  889.  
  890.       if( count=stream_read_raw(&rd,outbuf,32768))
  891.       {
  892.         WaveOut(hBuff,outbuf,count);
  893.         continue;
  894.       }
  895.       done = 1;
  896.       break;
  897.    };
  898.  
  899.    if ((status != ST_EXIT) && (status != ST_STOP))
  900.      status =  ST_TRACK;
  901. };
  902.  
  903. void play_xm(){                             /* uFMOD integration */
  904.         while(status == ST_PLAY){                 /* uFMOD integration */
  905.                 uFMOD_WaveOut(hBuff);                   /* uFMOD integration */
  906.                 delay(8);                               /* uFMOD integration */
  907.         }                                         /* uFMOD integration */
  908.         if ((status != ST_EXIT) && (status != ST_STOP)) status = ST_TRACK;   /* uFMOD integration */
  909. }                                           /* uFMOD integration */
  910.  
  911. void snd_stop()
  912. {
  913.   StopBuffer(hBuff);
  914. };
  915.  
  916. void _stdcall thread_proc(void *param)
  917. {  int evnt;
  918.    int pos;
  919.    int key;
  920.    DWORD offset;
  921.    char ipc_buff[16];
  922.  
  923.    set_event_mask(0x47); //Asper + IPC event
  924.    ipc_init(ipc_buff, 16);
  925.  
  926.   _asm
  927.   {
  928.     mov eax, 66
  929.     mov ebx, 1
  930.     mov ecx, 1
  931.     int 0x40
  932.   };
  933.    
  934.   draw_window();
  935.  
  936.   while(1)
  937.   {
  938.          if(status==ST_PLAY)
  939.      {  draw_progress_bar();
  940.         evnt = wait_for_event(80);             
  941.      }
  942.      else
  943.                  evnt = wait_for_event_infinite();
  944.  
  945. #ifdef DOCKABLE_WINDOW
  946.      GetThreadInfo(thread_info, -1);
  947. #endif
  948.  
  949.     switch(evnt)
  950.     {
  951.       case EV_REDRAW:
  952.                   draw_window();
  953.                   break;
  954.  
  955.       case EV_KEY:
  956.         if(!get_key(&key))
  957.         {
  958.        
  959.           switch(key)
  960.           {  case 0xE0:
  961.              case 0xE1:
  962.                break;
  963.              default:
  964.                switch (key)
  965.                {
  966.                  case 0x01:  //Esc
  967.                    status = ST_EXIT;
  968.                    exit();
  969.                    break;
  970.                
  971.                                  case 0x13: //R - repeat on/off
  972.                                    replay=!replay;
  973.                                    redraw_R_button();
  974.                                    break;
  975.  
  976.                                  case 0x19: //P - playlist
  977.                                    switch (PLStatus)
  978.                                    {    case 0x00: //PL not started.
  979.                                                         pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  980.                                                         PLStatus=0x12;
  981.                                                 break;
  982.                                                 case 0x01: //PL started, but hidden.
  983.                                                         PLStatus=0x12;
  984.                                                 break;
  985.                                                 case 0x02: //PL started and showed.
  986.                                                         PLStatus=0x11;
  987.                                                 break;
  988.                                    }
  989.                                    break;
  990.  
  991.                                  case 0x2C: //Z - previous
  992.                                    currActive-=2;
  993.                                    status = ST_TRACK;
  994.                                    break;
  995.                                  
  996.                                  case 0x2D: //X - play
  997.                                    status = ST_PLAY;
  998.                                    break;
  999.                                  
  1000.                                  case 0x2E: //C - pause
  1001.                                    break;
  1002.                                  
  1003.                                  case 0x2F: //V - stop
  1004.                    status = ST_STOP;
  1005.                                    break;
  1006.                                  
  1007.                                  case 0x30: //B - next
  1008.                                    status = ST_TRACK;
  1009.                                    break;
  1010.  
  1011.                                  case 0x47:  //Home
  1012.                    if(l_vol < 0)
  1013.                    { l_vol+=100;
  1014.                      r_vol+=100;  
  1015.                      SetVolume(hBuff,l_vol,r_vol);
  1016.                    };
  1017.                    break;
  1018.                  case 0x4F:  //End                
  1019.                    if(l_vol > -10000)
  1020.                    { l_vol-=100;
  1021.                      r_vol-=100;  
  1022.                      SetVolume(hBuff,l_vol,r_vol);
  1023.                    };
  1024.                    break;
  1025.                  case 0x53:
  1026.                    if(pan > -10000)
  1027.                    { pan -=100;
  1028.                      SetPan(hBuff,pan);
  1029.                    };
  1030.                    break;  
  1031.                  case 0x51:
  1032.                    if(pan < 10000)
  1033.                    { pan +=100;
  1034.                      SetPan(hBuff,pan);
  1035.                    };
  1036.                    break;  
  1037.                }
  1038.           };    
  1039.         };  
  1040.         break;
  1041.  
  1042.       case EV_BUTTON:
  1043.         switch(get_button_id())
  1044.         {  case 1:
  1045.              status = ST_EXIT;
  1046.              exit();
  1047.              break;
  1048.              
  1049.            case 0x10:
  1050.              status = ST_PLAY;
  1051.              break;//continue;
  1052.  
  1053.            case 0x11:
  1054.              status = ST_STOP;
  1055.              break;
  1056.            case 0x12:
  1057.                          currActive-=2;
  1058.                          status = ST_TRACK;
  1059.                          break;
  1060.            case 0x13:
  1061.                          status = ST_TRACK;
  1062.                          break;
  1063.            case 0x14:
  1064.             if(l_vol > -10000)
  1065.             {
  1066.               l_vol-=100;
  1067.               r_vol-=100;  
  1068.               SetVolume(hBuff,l_vol,r_vol);
  1069.             };
  1070.             break;
  1071.  
  1072.            case 0x15:
  1073.             if(l_vol < 0)
  1074.             { l_vol+=100;
  1075.               r_vol+=100;  
  1076.               SetVolume(hBuff,l_vol,r_vol);
  1077.             };
  1078.             break;
  1079.  
  1080.            case 0x16: //Asper+ PL button action
  1081.                            switch (PLStatus)
  1082.                            {    case 0x00: //PL not started.
  1083.                                                 pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  1084.                                                 PLStatus=0x12;
  1085.                                     break;
  1086.                                         case 0x01: //PL started, but hidden.
  1087.                                                 PLStatus=0x12;
  1088.                                     break;
  1089.                                         case 0x02: //PL started and showed.
  1090.                                                 PLStatus=0x11;
  1091.                                         break;
  1092.                            }
  1093.             break;
  1094.            case 0x17: //Asper+ PL button action
  1095.                            replay=!replay;
  1096.                            redraw_R_button();
  1097.             break;
  1098.  
  1099.            case 0x30:
  1100.             if(status==ST_DONE)
  1101.               break;
  1102.             pos = (GetMousePos(REL_WINDOW)>>16)-7;
  1103.             offset = ((fileinfo.size-44)/286*pos+44)&0xFFFFFFFC;
  1104.             set_reader(&rd, offset);
  1105.             draw_progress_bar();
  1106.             break;
  1107.         };
  1108.                 break;
  1109.  
  1110.           case EV_IPC:
  1111.                   *ipc_buff='\0';
  1112.                   update_dynamic_content();
  1113.                   break;
  1114.  
  1115.     };
  1116.   };
  1117. };
  1118.  
  1119. void delay (int val)
  1120. {
  1121.   _asm
  1122.  {    
  1123.       mov   eax,5
  1124.       mov   ebx, [val]
  1125.       int   0x40
  1126.   };  
  1127. }
  1128.  
  1129. int wait_for_event(int time)
  1130. { int retval;
  1131.   _asm
  1132.  {  
  1133.      mov  eax,23
  1134.      mov  ebx,[time]
  1135.      int  0x40
  1136.      mov [retval], eax
  1137.  };
  1138.  return retval;
  1139. };
  1140.  
  1141. int wait_for_event_infinite()
  1142. { int retval;
  1143.   _asm
  1144.   {  
  1145.       mov  eax,10
  1146.       int  0x40
  1147.       mov [retval], eax
  1148.   };
  1149.   return retval;
  1150. };
  1151.  
  1152. void BeginDraw()
  1153. {_asm
  1154.  {  
  1155.     mov   eax,12
  1156.     mov   ebx, 1
  1157.     int   0x40
  1158.   };  
  1159. };
  1160.  
  1161. void EndDraw()
  1162. { _asm
  1163.  {  
  1164.     mov   eax,12
  1165.     mov   ebx, 2
  1166.     int   0x40
  1167.   };  
  1168. };
  1169.  
  1170. //Asper+_______start KolibriOS sys functions___________________
  1171. void ResizeReplaceWindow (DWORD x, DWORD y, DWORD w, DWORD h) //Asper+
  1172. {
  1173.   _asm
  1174.  {    
  1175.       mov   eax, 67
  1176.       mov   ebx, [x]
  1177.       mov   ecx, [y]
  1178.       mov   edx, [w]
  1179.       mov   esi, [h]
  1180.       int   0x40
  1181.   };  
  1182. }
  1183.  
  1184. #ifdef DOCKABLE_WINDOW
  1185. void GetThreadInfo (char *info, int slot) //Asper+
  1186. {
  1187.         _asm
  1188.         {    
  1189.                 mov   eax, 9
  1190.                 mov   ebx, [info]
  1191.                 mov   ecx, [slot]
  1192.                 int   0x40
  1193.         }  
  1194. }
  1195. #endif
  1196.  
  1197. void set_event_mask(int mask)
  1198. {
  1199.         _asm
  1200.         {  
  1201.                 mov  eax, 40
  1202.                 mov  ebx, [mask]
  1203.                 int  0x40    
  1204.         }
  1205. }
  1206.  
  1207. void ipc_init(char *buf, int bufsize)
  1208. {
  1209.         _asm
  1210.         {  
  1211.                 mov  eax, 60
  1212.                 mov  ebx, 1
  1213.                 mov  ecx, [buf]
  1214.                 mov  edx, [bufsize]
  1215.                 int  0x40    
  1216.         }
  1217. }
  1218.  
  1219. int ipc_send_msg(int PID, char *msg)
  1220. {
  1221.         int len = strlen(msg);
  1222.         int retval;
  1223.         _asm
  1224.         {  
  1225.                 mov  eax, 60
  1226.                 mov  ebx, 2
  1227.                 mov  ecx, [PID]
  1228.                 mov  edx, [msg]
  1229.                 mov  esi, [len]
  1230.                 int  0x40    
  1231.                 mov [retval], eax
  1232.         }
  1233. }
  1234. //Asper+_______end KolibriOS sys functions___________________
  1235.  
  1236. //Asper+_______start strings routines___________________
  1237. int _strncmp(char *src, char *dst, DWORD n)
  1238. {
  1239.         _asm{
  1240.                 mov             esi, src
  1241.                 mov             edi, dst
  1242.                 mov             ecx, n
  1243.         }
  1244.  l1:
  1245.         _asm{
  1246.                 cmpsb
  1247.                 jne     err
  1248.                 loop    l1     
  1249.         }
  1250.         return 1;
  1251.  err:
  1252.         return 0;
  1253. }
  1254.  
  1255. int _strncpy (char *dst, char *src, int n)
  1256. {
  1257.         int  i;
  1258.         for (i=0; i<n; i++)
  1259.         {
  1260.                 dst[i]=src[i];
  1261.                 if (src[i]=='\0') break;
  1262.         }
  1263.         return 0;
  1264. }
  1265.  
  1266. void uint2str(unsigned int value, char *string)
  1267. {
  1268.   char tmp[33];
  1269.   int i, j;
  1270.   unsigned v;
  1271.  
  1272.   v = (unsigned)value;
  1273.   j = 0;
  1274.   do{
  1275.     i = v % 10;
  1276.     v = v / 10;
  1277.     if (i < 10)
  1278.       tmp[j] = i+'0';
  1279.     else
  1280.       tmp[j] = i + 'a' - 10;
  1281.         j++;
  1282.   }while (v);
  1283.  
  1284.   for (i=0; i<j; i++)
  1285.     string[i] = tmp[j-i-1];
  1286.   string[i] = '\0';
  1287. }
  1288.  
  1289. //Asper+_______end strings routines___________________
  1290.  
  1291.  
  1292. ///*********
  1293. void * __cdecl memmove ( void * dst, const void * src, unsigned int count)  /* uFMOD integration */
  1294. { void *ret;
  1295.   ret = dst;
  1296.  
  1297.   if (dst <= src || (char *)dst >= ((char *)src + count))
  1298.   {
  1299.       while (count--)
  1300.       { *(char *)dst = *(char *)src;
  1301.           dst = (char *)dst + 1;
  1302.           src = (char *)src + 1;
  1303.       }
  1304.    }
  1305.    else
  1306.     {
  1307.         dst = (char *)dst + count - 1;
  1308.         src = (char *)src + count - 1;
  1309.          while (count--)
  1310.           {  *(char *)dst = *(char *)src;
  1311.               dst = (char *)dst - 1;
  1312.               src = (char *)src - 1;
  1313.           }
  1314.     }
  1315.     return ret;
  1316. };
  1317. //**********/
  1318.  
  1319. void * __cdecl mem_cpy(void * dst,const void * src,size_t count)
  1320. {    void * ret = dst;
  1321.       while (count--)
  1322.       {  *(char *)dst = *(char *)src;
  1323.           dst = (char *)dst + 1;
  1324.           src = (char *)src + 1;
  1325.       };
  1326.       return(ret);
  1327. }
  1328.  
  1329. char * __cdecl strrchr (const char * string,int ch)
  1330. {
  1331.         char *start = (char *)string;
  1332.  
  1333.         while (*string++)                       /* find end of string */
  1334.                 ;
  1335.                                                 /* search towards front */
  1336.         while (--string != start && *string != (char)ch)
  1337.                 ;
  1338.  
  1339.         if (*string == (char)ch)                /* char found ? */
  1340.                 return( (char *)string );
  1341.  
  1342.         return(NULL);
  1343. }
  1344.  
  1345. int __cdecl _stricmp (const char * dst, const char * src)
  1346. {
  1347.     int f, l;
  1348.  
  1349.     do
  1350.     {
  1351.         if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') )
  1352.             f -= 'A' - 'a';
  1353.         if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') )
  1354.             l -= 'A' - 'a';
  1355.     }
  1356.     while ( f && (f == l) );
  1357.  
  1358.     return(f - l);
  1359. }
  1360.  
  1361.  
  1362.