Subversion Repositories Kolibri OS

Rev

Rev 5485 | Rev 7175 | 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. int LoadPL(char *fname)
  499. {
  500.         DWORD fmt;
  501.         DWORD r_bytes;
  502.         int retval;
  503.         int i;
  504. //              char st[100]="";
  505.  
  506.         char *pch;
  507.  
  508.         r_bytes=0;
  509.         pch=strrchr(fname, '/');
  510.         if (pch)
  511.                 i=pch-fname+1;
  512.         else
  513.                 i=strlen(fname);
  514.  
  515.         _strncpy (pl_path, fname, i);
  516.         pl_path[i]='\0';
  517.  
  518.         if (!pl_buff)
  519.                 pl_buff = UserAlloc(0x40000);
  520.         retval=read_file (fname,pl_buff,0,0x40000,&r_bytes);
  521.  
  522.         if ( retval && (r_bytes==0))
  523.                 return 0;
  524.  
  525.         Win2Dos(pl_buff, r_bytes);
  526.         pl_items_number=CountFileNamesInPL(pl_buff);
  527. /*
  528.         debug_out_str("\n\rPlay List files number = ");
  529.         itoa(pl_items_number, st, 10);
  530.         debug_out_str(st);
  531. */
  532.         fmt = test_m3u(pl_buff);
  533.  
  534.         if(!fmt)
  535.         {
  536.                 debug_out_str("\n\rInvalid M3U file");
  537.                 return 0;
  538.         }
  539.         debug_out_str("\n\rValid M3U file");
  540.         currSelected=currFirstShowed=0;
  541.         currActive=-1;
  542.         return 1;
  543. }
  544.  
  545. int LoadFile(char *fname)
  546. {
  547.    DWORD fmt;
  548.    DWORD r_bytes;
  549.    int retval;
  550.    int err;
  551.    unsigned char *ttl, *cur;               /* uFMOD integration */
  552.  
  553.    debug_out_str("\n\rPlay file ");
  554.    debug_out_str(fname);
  555.    debug_out_str("\n\r");
  556.  
  557.    if(get_fileinfo(fname, &fileinfo)==FILE_NOT_FOUND)
  558.    {  debug_out_str("\n\rfile not found\n\r");
  559.       return 0;
  560.    }
  561.  
  562.    r_bytes=0;
  563.    strcpy(filename, strrchr(fname,'/')+1);
  564.    if( !(fileext = strrchr(filename,'.')))
  565.            return 0;
  566.  
  567.    if(!_stricmp(fileext,".m3u"))
  568.    {
  569.            LoadPL(fname);
  570.            status=ST_TRACK;
  571.            return 1;
  572.    }
  573.  
  574.    if (!testbuff)
  575.            testbuff = UserAlloc(4096);
  576.  
  577.    retval=read_file (fname,testbuff,0,2048,&r_bytes);
  578.    if ( retval && (r_bytes==0))
  579.            return 0;
  580.  
  581.    if (!inpbuf)
  582.    {
  583.            inpbuf = UserAlloc(0x10000);
  584.            touch(inpbuf, 0x10000);
  585.    }
  586.    create_reader(&rd, inpbuf, 0x10000);
  587.    init_reader(&rd,fname);
  588.  
  589.    if(!_stricmp(fileext,".mp3"))
  590.    {   
  591.                         fmt = test_mp3(testbuff);              
  592.                         if(!fmt)
  593.                         {
  594.                           debug_out_str("\n\rInvalid MP3 file");
  595.                           return 0;                    
  596.                         };
  597.                         snd_play = &play_mp3;
  598.                         outremain = 0x40000;
  599.                         if (!outbuf)
  600.                         {
  601.                                 outbuf = UserAlloc(outremain);
  602.                                 touch(outbuf, outremain);
  603.                         }
  604.                         make_decode_tables(32767);
  605.                         init_layer2();
  606.                         init_layer3(32);
  607.                         fr.single = -1;
  608.                         goto play;
  609.    };
  610.  
  611.    if(!_stricmp(fileext,".xm"))
  612.    {
  613.            if(uFMOD_LoadSong(fname))
  614.            {
  615.               buttons_text = buttons_xm;               /* uFMOD integration */
  616.               fmt = PCM_2_16_48;                       /* uFMOD integration */
  617.               snd_play = &play_xm;                     /* uFMOD integration */
  618.               ttl = uFMOD_GetTitle();                  /* uFMOD integration */
  619.               cur = ttl;                               /* uFMOD integration */
  620.               err = 0;                                 /* uFMOD integration */
  621.               while(*cur && *cur++ != ' ') err++;      /* uFMOD integration */
  622.               if(err){                                 /* uFMOD integration */
  623.                       cur = fname;                             /* uFMOD integration */
  624.                              while(*cur) cur++;                       /* uFMOD integration */
  625.                              *cur++ = ' ';                            /* uFMOD integration */
  626.                              *cur++ = '|';                            /* uFMOD integration */
  627.                              *cur++ = ' ';                            /* uFMOD integration */
  628.                              while(*ttl) *cur++ = *ttl++;             /* uFMOD integration */
  629.                     }
  630.                     goto play;
  631.                  }  
  632.                  debug_out_str("\n\rInvalid XM file");
  633.      return 0;
  634.          };
  635.          
  636.          if(!_stricmp(fileext, ".wav"))
  637.          {
  638.        fmt = test_wav((WAVEHEADER*)testbuff);
  639.            if(fmt)
  640.                  {                                  
  641.                    snd_play = &play_wave;            
  642.                    set_reader(&rd, 44);              
  643.                    outbuf = UserAlloc(32*1024);
  644.                    touch(outbuf, 32768);
  645.                    goto play;
  646.                  }
  647.      debug_out_str("\n\rInvalid WAV file");
  648.      return 0;
  649.          };    
  650.  
  651.    debug_out_str("\n\rUnsupported file");
  652.    return 0;
  653.  
  654. play:
  655.  
  656.    status = ST_PLAY;
  657.    SetFormat(hBuff, fmt);
  658.    SetVolume(hBuff,l_vol,r_vol);
  659.    GetVolume(hBuff,&l_vol,&r_vol);
  660.  
  661.    return 1;
  662. }
  663.  
  664. int main(int argc, char *argv[])
  665. {
  666.    int err, ver;
  667.    int i;
  668.    char ipc_msg[2]="\0\0";
  669.    pl_items_number=0;
  670.  
  671.    if (argv[1][0]=='-' && argv[1][1]=='h')
  672.    {
  673.       hidden = 1;
  674.           argv[1]+=3;
  675.    }
  676.    strcpy (full_filename, argv[1]);
  677.    
  678.    InitHeap(1024*1024);
  679.    
  680.    if(err = InitSound(&ver))
  681.    {  
  682.      debug_out_str("Sound service not installed\n\r");
  683.      return 0;
  684.    }
  685.    
  686.    if( (SOUND_VERSION>(ver&0xFFFF)) ||
  687.        (SOUND_VERSION<(ver >> 16)))
  688.    {  
  689.      debug_out_str("Sound service version mismatch\n\r");
  690.      return 0;
  691.    }
  692.  
  693.    if (err = CreateBuffer(PCM_2_16_48, 0, &hBuff))
  694.    {
  695.      debug_out_str("create buffer return error\n\r");
  696.      return 0;
  697.    }
  698.  
  699.    if (!LoadFile(full_filename))
  700.            return 0;
  701.    tid=create_thread(thread_proc, 0, 4096);
  702.    
  703.    while(1)
  704.    {  delay(10);
  705.       switch(status)
  706.       {  case ST_TRACK:
  707.                         StopBuffer(hBuff);
  708.                         if (hidden) ExitInHiddenMode();
  709.                         if (LoadTrack(++currActive))
  710.                         {
  711.                                 if (LoadFile(full_filename))
  712.                                         status = ST_PLAY;
  713.                         }
  714.                         else
  715.                         {
  716.                                 currSelected=currFirstShowed=0;
  717.                                 currActive=-1;
  718.                                 if (!replay) status = ST_STOP;
  719.                                 continue;
  720.                         }
  721.  
  722.                         //Update ac97snd and PL windows
  723.                         i=currActive-currFirstShowed;
  724.                         if (i>PL_MAX_SHOWN_ITEMS-1)
  725.                                 currFirstShowed = currActive - PL_MAX_SHOWN_ITEMS/2;
  726.                         ipc_send_msg(tid, ipc_msg);
  727.                         ipc_send_msg(pl_tid, ipc_msg);
  728.                         continue;
  729.          
  730.           case ST_PLAY:
  731.                   snd_play();
  732.                   continue;
  733.  
  734.           case ST_STOP:
  735.                   StopBuffer(hBuff);
  736.                   status = ST_DONE;
  737.                   if (hidden) ExitInHiddenMode();
  738.                   continue;
  739.  
  740.           case ST_EXIT:
  741.                   uFMOD_StopSong();          /* uFMOD integration */
  742.                   StopBuffer(hBuff);
  743.                   DestroyBuffer(hBuff);
  744.                   return 0;
  745.           };
  746.    };
  747.    return 0;
  748. };
  749.  
  750. void touch(char *buf, int size)
  751. { int i;
  752.    char a;
  753.     for ( i = 0;i < size; i+=4096)     //alloc all pages
  754.       a = buf[i];
  755. };
  756.  
  757. DWORD test_m3u(char *buf) //Asper+
  758. {
  759.         char  *sign="#EXTM3U";
  760.         return _strncmp(buf, sign, 7);
  761. }
  762.  
  763. DWORD test_mp3(char *buf)
  764. {  unsigned long hdr;
  765.     WAVEHEADER whdr;
  766.     int attempts = 0;
  767.      
  768.     for (;;attempts++)
  769.     {  if(attempts > 1000)
  770.           return 0;
  771.         if(!rd.head_read(&rd,&hdr))
  772.                         return 0;
  773.         if(!decode_header(&fr,hdr))
  774.         {
  775.          if((hdr & 0xffffff00) == 0x49443300)
  776.               {
  777.                     int id3length = 0;
  778.                     id3length = parse_new_id3(&rd, hdr);
  779.                     continue;
  780.               };
  781.           rd.strpos-=3;
  782.           rd.stream-=3;
  783.           rd.strremain+=3;
  784.           continue;
  785.         };
  786.         break;
  787.     };
  788.          
  789.     first_sync = rd.filepos-rd.strremain-4;
  790.          
  791.     whdr.riff_id = 0x46464952;
  792.     whdr.riff_format = 0x45564157;
  793.     whdr.wFormatTag = 0x01;
  794.     whdr.nSamplesPerSec = freqs[fr.sampling_frequency];
  795.     whdr.nChannels = 2;
  796.     whdr.wBitsPerSample = 16;
  797.    
  798.     return test_wav(&whdr);
  799. };
  800.  
  801.  
  802. void play_mp3()
  803. {  char *outPtr;
  804.     int totalout;
  805.     int outcount;
  806.  
  807.  //   memset(&fr,0,sizeof(fr));
  808.     fr.down_sample_sblimit = 32;
  809.     fr.single = -1;
  810.     reset_mpg();
  811.  
  812.     outPtr = outbuf;
  813.     totalout=0;
  814.     done = 0;
  815.     outremain=0x40000;
  816.  
  817.     memset(outbuf,0,0x40000);
  818.     set_reader(&rd, 0);    //;first_sync);
  819.  
  820.     while(1)
  821.     { if(status!=ST_PLAY)
  822.              break;
  823.  
  824.      for(;;)
  825.      {   outcount = 0;                          
  826.           if( !read_frame(&rd, &fr))
  827.           {  done = 1;
  828.               break;
  829.           };
  830.           fr.do_layer(&fr, outPtr,&outcount);
  831.           outPtr+= outcount;
  832.           totalout+=outcount;
  833.           outremain-=outcount;
  834.           if(outremain < outcount*2)
  835.             break;  
  836.     };
  837.  
  838.     if(done)
  839.     { if(totalout < 4096)
  840.       {  memset(outPtr,0,4096-totalout);
  841.                 totalout = 4096;
  842.       };
  843.     }
  844.     else
  845.       if(totalout < 8192)
  846.         continue;
  847.  
  848.     outPtr = outbuf;      
  849.     while (totalout >= 4096)
  850.     {
  851.    
  852.       WaveOut(hBuff,outPtr,4096);
  853.       if(status!=ST_PLAY)
  854.       { if ((status != ST_EXIT) && (status != ST_STOP))
  855.          status = ST_TRACK;
  856.         return;
  857.       };
  858.       totalout-=4096;
  859.       outPtr+=4096;
  860.       outremain+=4096;
  861.     };
  862.     if(done)
  863.       break;
  864.      
  865.     memmove(outbuf,outPtr, totalout);
  866.     outPtr = outbuf+totalout;
  867.    }
  868.  
  869.     if ((status != ST_EXIT) && (status != ST_STOP))
  870.       status =  ST_TRACK;
  871. };
  872.  
  873. void play_wave()
  874. {  int count;
  875.  
  876.    set_reader(&rd,44);
  877.    while(1)
  878.    {
  879.       if(status!=ST_PLAY)
  880.         break;
  881.  
  882.       if( count=stream_read_raw(&rd,outbuf,32768))
  883.       {
  884.         WaveOut(hBuff,outbuf,count);
  885.         continue;
  886.       }
  887.       done = 1;
  888.       break;
  889.    };
  890.  
  891.    if ((status != ST_EXIT) && (status != ST_STOP))
  892.      status =  ST_TRACK;
  893. };
  894.  
  895. void play_xm(){                             /* uFMOD integration */
  896.         while(status == ST_PLAY){                 /* uFMOD integration */
  897.                 uFMOD_WaveOut(hBuff);                   /* uFMOD integration */
  898.                 delay(8);                               /* uFMOD integration */
  899.         }                                         /* uFMOD integration */
  900.         if ((status != ST_EXIT) && (status != ST_STOP)) status = ST_TRACK;   /* uFMOD integration */
  901. }                                           /* uFMOD integration */
  902.  
  903. void snd_stop()
  904. {
  905.   StopBuffer(hBuff);
  906. };
  907.  
  908. void _stdcall thread_proc(void *param)
  909. {  int evnt;
  910.    int pos;
  911.    int key;
  912.    DWORD offset;
  913.    char ipc_buff[16];
  914.  
  915.    set_event_mask(0x47); //Asper + IPC event
  916.    ipc_init(ipc_buff, 16);
  917.  
  918.   _asm
  919.   {
  920.     mov eax, 66
  921.     mov ebx, 1
  922.     mov ecx, 1
  923.     int 0x40
  924.   };
  925.    
  926.   draw_window();
  927.  
  928.   while(1)
  929.   {
  930.          if(status==ST_PLAY)
  931.      {  draw_progress_bar();
  932.         evnt = wait_for_event(80);             
  933.      }
  934.      else
  935.                  evnt = wait_for_event_infinite();
  936.  
  937. #ifdef DOCKABLE_WINDOW
  938.      GetThreadInfo(thread_info, -1);
  939. #endif
  940.  
  941.     switch(evnt)
  942.     {
  943.       case EV_REDRAW:
  944.                   draw_window();
  945.                   break;
  946.  
  947.       case EV_KEY:
  948.         if(!get_key(&key))
  949.         {
  950.        
  951.           switch(key)
  952.           {  case 0xE0:
  953.              case 0xE1:
  954.                break;
  955.              default:
  956.                switch (key)
  957.                {
  958.                  case 0x01:  //Esc
  959.                    status = ST_EXIT;
  960.                    exit();
  961.                    break;
  962.                
  963.                                  case 0x13: //R - repeat on/off
  964.                                    replay=!replay;
  965.                                    redraw_R_button();
  966.                                    break;
  967.  
  968.                                  case 0x19: //P - playlist
  969.                                    switch (PLStatus)
  970.                                    {    case 0x00: //PL not started.
  971.                                                         pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  972.                                                         PLStatus=0x12;
  973.                                                 break;
  974.                                                 case 0x01: //PL started, but hidden.
  975.                                                         PLStatus=0x12;
  976.                                                 break;
  977.                                                 case 0x02: //PL started and showed.
  978.                                                         PLStatus=0x11;
  979.                                                 break;
  980.                                    }
  981.                                    break;
  982.  
  983.                                  case 0x2C: //Z - previous
  984.                                    currActive-=2;
  985.                                    status = ST_TRACK;
  986.                                    break;
  987.                                  
  988.                                  case 0x2D: //X - play
  989.                                    status = ST_PLAY;
  990.                                    break;
  991.                                  
  992.                                  case 0x2E: //C - pause
  993.                                    break;
  994.                                  
  995.                                  case 0x2F: //V - stop
  996.                    status = ST_STOP;
  997.                                    break;
  998.                                  
  999.                                  case 0x30: //B - next
  1000.                                    status = ST_TRACK;
  1001.                                    break;
  1002.  
  1003.                                  case 0x47:  //Home
  1004.                    if(l_vol < 0)
  1005.                    { l_vol+=100;
  1006.                      r_vol+=100;  
  1007.                      SetVolume(hBuff,l_vol,r_vol);
  1008.                    };
  1009.                    break;
  1010.                  case 0x4F:  //End                
  1011.                    if(l_vol > -10000)
  1012.                    { l_vol-=100;
  1013.                      r_vol-=100;  
  1014.                      SetVolume(hBuff,l_vol,r_vol);
  1015.                    };
  1016.                    break;
  1017.                  case 0x53:
  1018.                    if(pan > -10000)
  1019.                    { pan -=100;
  1020.                      SetPan(hBuff,pan);
  1021.                    };
  1022.                    break;  
  1023.                  case 0x51:
  1024.                    if(pan < 10000)
  1025.                    { pan +=100;
  1026.                      SetPan(hBuff,pan);
  1027.                    };
  1028.                    break;  
  1029.                }
  1030.           };    
  1031.         };  
  1032.         break;
  1033.  
  1034.       case EV_BUTTON:
  1035.         switch(get_button_id())
  1036.         {  case 1:
  1037.              status = ST_EXIT;
  1038.              exit();
  1039.              break;
  1040.              
  1041.            case 0x10:
  1042.              status = ST_PLAY;
  1043.              break;//continue;
  1044.  
  1045.            case 0x11:
  1046.              status = ST_STOP;
  1047.              break;
  1048.            case 0x12:
  1049.                          currActive-=2;
  1050.                          status = ST_TRACK;
  1051.                          break;
  1052.            case 0x13:
  1053.                          status = ST_TRACK;
  1054.                          break;
  1055.            case 0x14:
  1056.             if(l_vol > -10000)
  1057.             {
  1058.               l_vol-=100;
  1059.               r_vol-=100;  
  1060.               SetVolume(hBuff,l_vol,r_vol);
  1061.             };
  1062.             break;
  1063.  
  1064.            case 0x15:
  1065.             if(l_vol < 0)
  1066.             { l_vol+=100;
  1067.               r_vol+=100;  
  1068.               SetVolume(hBuff,l_vol,r_vol);
  1069.             };
  1070.             break;
  1071.  
  1072.            case 0x16: //Asper+ PL button action
  1073.                            switch (PLStatus)
  1074.                            {    case 0x00: //PL not started.
  1075.                                                 pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  1076.                                                 PLStatus=0x12;
  1077.                                     break;
  1078.                                         case 0x01: //PL started, but hidden.
  1079.                                                 PLStatus=0x12;
  1080.                                     break;
  1081.                                         case 0x02: //PL started and showed.
  1082.                                                 PLStatus=0x11;
  1083.                                         break;
  1084.                            }
  1085.             break;
  1086.            case 0x17: //Asper+ PL button action
  1087.                            replay=!replay;
  1088.                            redraw_R_button();
  1089.             break;
  1090.  
  1091.            case 0x30:
  1092.             if(status==ST_DONE)
  1093.               break;
  1094.             pos = (GetMousePos(REL_WINDOW)>>16)-7;
  1095.             offset = ((fileinfo.size-44)/286*pos+44)&0xFFFFFFFC;
  1096.             set_reader(&rd, offset);
  1097.             draw_progress_bar();
  1098.             break;
  1099.         };
  1100.                 break;
  1101.  
  1102.           case EV_IPC:
  1103.                   *ipc_buff='\0';
  1104.                   update_dynamic_content();
  1105.                   break;
  1106.  
  1107.     };
  1108.   };
  1109. };
  1110.  
  1111. void ExitInHiddenMode()
  1112. {
  1113.         uFMOD_StopSong();
  1114.         StopBuffer(hBuff);
  1115.         DestroyBuffer(hBuff);
  1116.         exit();
  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.