Subversion Repositories Kolibri OS

Rev

Rev 3533 | Rev 5485 | 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.    BeginDraw();
  453.    if (!hidden)
  454.    {
  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.    }
  478.    EndDraw();
  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 (LoadTrack(++currActive))
  709.                         {
  710.                                 if (LoadFile(full_filename))
  711.                                         status = ST_PLAY;
  712.                         }
  713.                         else
  714.                         {
  715.                                 currSelected=currFirstShowed=0;
  716.                                 currActive=-1;
  717.                                 if (!replay) status = ST_STOP;
  718.                                 continue;
  719.                         }
  720.  
  721.                         //Update ac97snd and PL windows
  722.                         i=currActive-currFirstShowed;
  723.                         if (i>PL_MAX_SHOWN_ITEMS-1)
  724.                                 currFirstShowed = currActive - PL_MAX_SHOWN_ITEMS/2;
  725.                         ipc_send_msg(tid, ipc_msg);
  726.                         ipc_send_msg(pl_tid, ipc_msg);
  727.                         continue;
  728.          
  729.           case ST_PLAY:
  730.                   snd_play();
  731.                   continue;
  732.  
  733.           case ST_STOP:
  734.                   StopBuffer(hBuff);
  735.                   status = ST_DONE;
  736.                   continue;
  737.  
  738.           case ST_EXIT:
  739.                   uFMOD_StopSong();          /* uFMOD integration */
  740.                   StopBuffer(hBuff);
  741.                   DestroyBuffer(hBuff);
  742.                   return 0;
  743.           };
  744.    };
  745.    return 0;
  746. };
  747.  
  748. void touch(char *buf, int size)
  749. { int i;
  750.    char a;
  751.     for ( i = 0;i < size; i+=4096)     //alloc all pages
  752.       a = buf[i];
  753. };
  754.  
  755. DWORD test_m3u(char *buf) //Asper+
  756. {
  757.         char  *sign="#EXTM3U";
  758.         return _strncmp(buf, sign, 7);
  759. }
  760.  
  761. DWORD test_mp3(char *buf)
  762. {  unsigned long hdr;
  763.     WAVEHEADER whdr;
  764.      
  765.     while (1)
  766.     {  if(rd.filepos > 102400)
  767.           return 0;
  768.         if(!rd.head_read(&rd,&hdr))
  769.                         return 0;
  770.         if(!decode_header(&fr,hdr))
  771.         {
  772.          if((hdr & 0xffffff00) == 0x49443300)
  773.               {
  774.                     int id3length = 0;
  775.                     id3length = parse_new_id3(&rd, hdr);
  776.                     continue;
  777.               };
  778.           rd.strpos-=3;
  779.           rd.stream-=3;
  780.           rd.strremain+=3;
  781.           continue;
  782.         };
  783.         break;
  784.     };
  785.          
  786.     first_sync = rd.filepos-rd.strremain-4;
  787.          
  788.     whdr.riff_id = 0x46464952;
  789.     whdr.riff_format = 0x45564157;
  790.     whdr.wFormatTag = 0x01;
  791.     whdr.nSamplesPerSec = freqs[fr.sampling_frequency];
  792.     whdr.nChannels = 2;
  793.     whdr.wBitsPerSample = 16;
  794.    
  795.     return test_wav(&whdr);
  796. };
  797.  
  798.  
  799. void play_mp3()
  800. {  char *outPtr;
  801.     int totalout;
  802.     int outcount;
  803.  
  804.  //   memset(&fr,0,sizeof(fr));
  805.     fr.down_sample_sblimit = 32;
  806.     fr.single = -1;
  807.     reset_mpg();
  808.  
  809.     outPtr = outbuf;
  810.     totalout=0;
  811.     done = 0;
  812.     outremain=0x40000;
  813.  
  814.     memset(outbuf,0,0x40000);
  815.     set_reader(&rd, 0);    //;first_sync);
  816.  
  817.     while(1)
  818.     { if(status!=ST_PLAY)
  819.              break;
  820.  
  821.      for(;;)
  822.      {   outcount = 0;                          
  823.           if( !read_frame(&rd, &fr))
  824.           {  done = 1;
  825.               break;
  826.           };
  827.           fr.do_layer(&fr, outPtr,&outcount);
  828.           outPtr+= outcount;
  829.           totalout+=outcount;
  830.           outremain-=outcount;
  831.           if(outremain < outcount*2)
  832.             break;  
  833.     };
  834.  
  835.     if(done)
  836.     { if(totalout < 4096)
  837.       {  memset(outPtr,0,4096-totalout);
  838.                 totalout = 4096;
  839.       };
  840.     }
  841.     else
  842.       if(totalout < 8192)
  843.         continue;
  844.  
  845.     outPtr = outbuf;      
  846.     while (totalout >= 4096)
  847.     {
  848.    
  849.       WaveOut(hBuff,outPtr,4096);
  850.       if(status!=ST_PLAY)
  851.       { if ((status != ST_EXIT) && (status != ST_STOP))
  852.          status = ST_TRACK;
  853.         return;
  854.       };
  855.       totalout-=4096;
  856.       outPtr+=4096;
  857.       outremain+=4096;
  858.     };
  859.     if(done)
  860.       break;
  861.      
  862.     memmove(outbuf,outPtr, totalout);
  863.     outPtr = outbuf+totalout;
  864.    }
  865.  
  866.     if ((status != ST_EXIT) && (status != ST_STOP))
  867.       status =  ST_TRACK;
  868. };
  869.  
  870. void play_wave()
  871. {  int count;
  872.  
  873.    set_reader(&rd,44);
  874.    while(1)
  875.    {
  876.       if(status!=ST_PLAY)
  877.         break;
  878.  
  879.       if( count=stream_read_raw(&rd,outbuf,32768))
  880.       {
  881.         WaveOut(hBuff,outbuf,count);
  882.         continue;
  883.       }
  884.       done = 1;
  885.       break;
  886.    };
  887.  
  888.    if ((status != ST_EXIT) && (status != ST_STOP))
  889.      status =  ST_TRACK;
  890. };
  891.  
  892. void play_xm(){                             /* uFMOD integration */
  893.         while(status == ST_PLAY){                 /* uFMOD integration */
  894.                 uFMOD_WaveOut(hBuff);                   /* uFMOD integration */
  895.                 delay(8);                               /* uFMOD integration */
  896.         }                                         /* uFMOD integration */
  897.         if ((status != ST_EXIT) && (status != ST_STOP)) status = ST_TRACK;   /* uFMOD integration */
  898. }                                           /* uFMOD integration */
  899.  
  900. void snd_stop()
  901. {
  902.   StopBuffer(hBuff);
  903. };
  904.  
  905. void _stdcall thread_proc(void *param)
  906. {  int evnt;
  907.    int pos;
  908.    int key;
  909.    DWORD offset;
  910.    char ipc_buff[16];
  911.  
  912.    set_event_mask(0x47); //Asper + IPC event
  913.    ipc_init(ipc_buff, 16);
  914.  
  915.   _asm
  916.   {
  917.     mov eax, 66
  918.     mov ebx, 1
  919.     mov ecx, 1
  920.     int 0x40
  921.   };
  922.    
  923.   draw_window();
  924.  
  925.   while(1)
  926.   {
  927.          if(status==ST_PLAY)
  928.      {  draw_progress_bar();
  929.         evnt = wait_for_event(80);             
  930.      }
  931.      else
  932.                  evnt = wait_for_event_infinite();
  933.  
  934. #ifdef DOCKABLE_WINDOW
  935.      GetThreadInfo(thread_info, -1);
  936. #endif
  937.  
  938.     switch(evnt)
  939.     {
  940.       case EV_REDRAW:
  941.                   draw_window();
  942.                   break;
  943.  
  944.       case EV_KEY:
  945.         if(!get_key(&key))
  946.         {
  947.        
  948.           switch(key)
  949.           {  case 0xE0:
  950.              case 0xE1:
  951.                break;
  952.              default:
  953.                switch (key)
  954.                {
  955.                  case 0x01:  //Esc
  956.                    status = ST_EXIT;
  957.                    exit();
  958.                    break;
  959.                
  960.                                  case 0x13: //R - repeat on/off
  961.                                    replay=!replay;
  962.                                    redraw_R_button();
  963.                                    break;
  964.  
  965.                                  case 0x19: //P - playlist
  966.                                    switch (PLStatus)
  967.                                    {    case 0x00: //PL not started.
  968.                                                         pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  969.                                                         PLStatus=0x12;
  970.                                                 break;
  971.                                                 case 0x01: //PL started, but hidden.
  972.                                                         PLStatus=0x12;
  973.                                                 break;
  974.                                                 case 0x02: //PL started and showed.
  975.                                                         PLStatus=0x11;
  976.                                                 break;
  977.                                    }
  978.                                    break;
  979.  
  980.                                  case 0x2C: //Z - previous
  981.                                    currActive-=2;
  982.                                    status = ST_TRACK;
  983.                                    break;
  984.                                  
  985.                                  case 0x2D: //X - play
  986.                                    status = ST_PLAY;
  987.                                    break;
  988.                                  
  989.                                  case 0x2E: //C - pause
  990.                                    break;
  991.                                  
  992.                                  case 0x2F: //V - stop
  993.                    status = ST_STOP;
  994.                                    break;
  995.                                  
  996.                                  case 0x30: //B - next
  997.                                    status = ST_TRACK;
  998.                                    break;
  999.  
  1000.                                  case 0x47:  //Home
  1001.                    if(l_vol < 0)
  1002.                    { l_vol+=100;
  1003.                      r_vol+=100;  
  1004.                      SetVolume(hBuff,l_vol,r_vol);
  1005.                    };
  1006.                    break;
  1007.                  case 0x4F:  //End                
  1008.                    if(l_vol > -10000)
  1009.                    { l_vol-=100;
  1010.                      r_vol-=100;  
  1011.                      SetVolume(hBuff,l_vol,r_vol);
  1012.                    };
  1013.                    break;
  1014.                  case 0x53:
  1015.                    if(pan > -10000)
  1016.                    { pan -=100;
  1017.                      SetPan(hBuff,pan);
  1018.                    };
  1019.                    break;  
  1020.                  case 0x51:
  1021.                    if(pan < 10000)
  1022.                    { pan +=100;
  1023.                      SetPan(hBuff,pan);
  1024.                    };
  1025.                    break;  
  1026.                }
  1027.           };    
  1028.         };  
  1029.         break;
  1030.  
  1031.       case EV_BUTTON:
  1032.         switch(get_button_id())
  1033.         {  case 1:
  1034.              status = ST_EXIT;
  1035.              exit();
  1036.              break;
  1037.              
  1038.            case 0x10:
  1039.              status = ST_PLAY;
  1040.              break;//continue;
  1041.  
  1042.            case 0x11:
  1043.              status = ST_STOP;
  1044.              break;
  1045.            case 0x12:
  1046.                          currActive-=2;
  1047.                          status = ST_TRACK;
  1048.                          break;
  1049.            case 0x13:
  1050.                          status = ST_TRACK;
  1051.                          break;
  1052.            case 0x14:
  1053.             if(l_vol > -10000)
  1054.             {
  1055.               l_vol-=100;
  1056.               r_vol-=100;  
  1057.               SetVolume(hBuff,l_vol,r_vol);
  1058.             };
  1059.             break;
  1060.  
  1061.            case 0x15:
  1062.             if(l_vol < 0)
  1063.             { l_vol+=100;
  1064.               r_vol+=100;  
  1065.               SetVolume(hBuff,l_vol,r_vol);
  1066.             };
  1067.             break;
  1068.  
  1069.            case 0x16: //Asper+ PL button action
  1070.                            switch (PLStatus)
  1071.                            {    case 0x00: //PL not started.
  1072.                                                 pl_tid=create_thread(pl_thread_proc, 0, 4096);              
  1073.                                                 PLStatus=0x12;
  1074.                                     break;
  1075.                                         case 0x01: //PL started, but hidden.
  1076.                                                 PLStatus=0x12;
  1077.                                     break;
  1078.                                         case 0x02: //PL started and showed.
  1079.                                                 PLStatus=0x11;
  1080.                                         break;
  1081.                            }
  1082.             break;
  1083.            case 0x17: //Asper+ PL button action
  1084.                            replay=!replay;
  1085.                            redraw_R_button();
  1086.             break;
  1087.  
  1088.            case 0x30:
  1089.             if(status==ST_DONE)
  1090.               break;
  1091.             pos = (GetMousePos(REL_WINDOW)>>16)-7;
  1092.             offset = ((fileinfo.size-44)/286*pos+44)&0xFFFFFFFC;
  1093.             set_reader(&rd, offset);
  1094.             draw_progress_bar();
  1095.             break;
  1096.         };
  1097.                 break;
  1098.  
  1099.           case EV_IPC:
  1100.                   *ipc_buff='\0';
  1101.                   update_dynamic_content();
  1102.                   break;
  1103.  
  1104.     };
  1105.   };
  1106. };
  1107.  
  1108. void delay (int val)
  1109. {
  1110.   _asm
  1111.  {    
  1112.       mov   eax,5
  1113.       mov   ebx, [val]
  1114.       int   0x40
  1115.   };  
  1116. }
  1117.  
  1118. int wait_for_event(int time)
  1119. { int retval;
  1120.   _asm
  1121.  {  
  1122.      mov  eax,23
  1123.      mov  ebx,[time]
  1124.      int  0x40
  1125.      mov [retval], eax
  1126.  };
  1127.  return retval;
  1128. };
  1129.  
  1130. int wait_for_event_infinite()
  1131. { int retval;
  1132.   _asm
  1133.   {  
  1134.       mov  eax,10
  1135.       int  0x40
  1136.       mov [retval], eax
  1137.   };
  1138.   return retval;
  1139. };
  1140.  
  1141. void BeginDraw()
  1142. {_asm
  1143.  {  
  1144.     mov   eax,12
  1145.     mov   ebx, 1
  1146.     int   0x40
  1147.   };  
  1148. };
  1149.  
  1150. void EndDraw()
  1151. { _asm
  1152.  {  
  1153.     mov   eax,12
  1154.     mov   ebx, 2
  1155.     int   0x40
  1156.   };  
  1157. };
  1158.  
  1159. //Asper+_______start KolibriOS sys functions___________________
  1160. void ResizeReplaceWindow (DWORD x, DWORD y, DWORD w, DWORD h) //Asper+
  1161. {
  1162.   _asm
  1163.  {    
  1164.       mov   eax, 67
  1165.       mov   ebx, [x]
  1166.       mov   ecx, [y]
  1167.       mov   edx, [w]
  1168.       mov   esi, [h]
  1169.       int   0x40
  1170.   };  
  1171. }
  1172.  
  1173. #ifdef DOCKABLE_WINDOW
  1174. void GetThreadInfo (char *info, int slot) //Asper+
  1175. {
  1176.         _asm
  1177.         {    
  1178.                 mov   eax, 9
  1179.                 mov   ebx, [info]
  1180.                 mov   ecx, [slot]
  1181.                 int   0x40
  1182.         }  
  1183. }
  1184. #endif
  1185.  
  1186. void set_event_mask(int mask)
  1187. {
  1188.         _asm
  1189.         {  
  1190.                 mov  eax, 40
  1191.                 mov  ebx, [mask]
  1192.                 int  0x40    
  1193.         }
  1194. }
  1195.  
  1196. void ipc_init(char *buf, int bufsize)
  1197. {
  1198.         _asm
  1199.         {  
  1200.                 mov  eax, 60
  1201.                 mov  ebx, 1
  1202.                 mov  ecx, [buf]
  1203.                 mov  edx, [bufsize]
  1204.                 int  0x40    
  1205.         }
  1206. }
  1207.  
  1208. int ipc_send_msg(int PID, char *msg)
  1209. {
  1210.         int len = strlen(msg);
  1211.         int retval;
  1212.         _asm
  1213.         {  
  1214.                 mov  eax, 60
  1215.                 mov  ebx, 2
  1216.                 mov  ecx, [PID]
  1217.                 mov  edx, [msg]
  1218.                 mov  esi, [len]
  1219.                 int  0x40    
  1220.                 mov [retval], eax
  1221.         }
  1222. }
  1223. //Asper+_______end KolibriOS sys functions___________________
  1224.  
  1225. //Asper+_______start strings routines___________________
  1226. int _strncmp(char *src, char *dst, DWORD n)
  1227. {
  1228.         _asm{
  1229.                 mov             esi, src
  1230.                 mov             edi, dst
  1231.                 mov             ecx, n
  1232.         }
  1233.  l1:
  1234.         _asm{
  1235.                 cmpsb
  1236.                 jne     err
  1237.                 loop    l1     
  1238.         }
  1239.         return 1;
  1240.  err:
  1241.         return 0;
  1242. }
  1243.  
  1244. int _strncpy (char *dst, char *src, int n)
  1245. {
  1246.         int  i;
  1247.         for (i=0; i<n; i++)
  1248.         {
  1249.                 dst[i]=src[i];
  1250.                 if (src[i]=='\0') break;
  1251.         }
  1252.         return 0;
  1253. }
  1254.  
  1255. void uint2str(unsigned int value, char *string)
  1256. {
  1257.   char tmp[33];
  1258.   int i, j;
  1259.   unsigned v;
  1260.  
  1261.   v = (unsigned)value;
  1262.   j = 0;
  1263.   do{
  1264.     i = v % 10;
  1265.     v = v / 10;
  1266.     if (i < 10)
  1267.       tmp[j] = i+'0';
  1268.     else
  1269.       tmp[j] = i + 'a' - 10;
  1270.         j++;
  1271.   }while (v);
  1272.  
  1273.   for (i=0; i<j; i++)
  1274.     string[i] = tmp[j-i-1];
  1275.   string[i] = '\0';
  1276. }
  1277.  
  1278. //Asper+_______end strings routines___________________
  1279.  
  1280.  
  1281. ///*********
  1282. void * __cdecl memmove ( void * dst, const void * src, unsigned int count)  /* uFMOD integration */
  1283. { void *ret;
  1284.   ret = dst;
  1285.  
  1286.   if (dst <= src || (char *)dst >= ((char *)src + count))
  1287.   {
  1288.       while (count--)
  1289.       { *(char *)dst = *(char *)src;
  1290.           dst = (char *)dst + 1;
  1291.           src = (char *)src + 1;
  1292.       }
  1293.    }
  1294.    else
  1295.     {
  1296.         dst = (char *)dst + count - 1;
  1297.         src = (char *)src + count - 1;
  1298.          while (count--)
  1299.           {  *(char *)dst = *(char *)src;
  1300.               dst = (char *)dst - 1;
  1301.               src = (char *)src - 1;
  1302.           }
  1303.     }
  1304.     return ret;
  1305. };
  1306. //**********/
  1307.  
  1308. void * __cdecl mem_cpy(void * dst,const void * src,size_t count)
  1309. {    void * ret = dst;
  1310.       while (count--)
  1311.       {  *(char *)dst = *(char *)src;
  1312.           dst = (char *)dst + 1;
  1313.           src = (char *)src + 1;
  1314.       };
  1315.       return(ret);
  1316. }
  1317.  
  1318. char * __cdecl strrchr (const char * string,int ch)
  1319. {
  1320.         char *start = (char *)string;
  1321.  
  1322.         while (*string++)                       /* find end of string */
  1323.                 ;
  1324.                                                 /* search towards front */
  1325.         while (--string != start && *string != (char)ch)
  1326.                 ;
  1327.  
  1328.         if (*string == (char)ch)                /* char found ? */
  1329.                 return( (char *)string );
  1330.  
  1331.         return(NULL);
  1332. }
  1333.  
  1334. int __cdecl _stricmp (const char * dst, const char * src)
  1335. {
  1336.     int f, l;
  1337.  
  1338.     do
  1339.     {
  1340.         if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') )
  1341.             f -= 'A' - 'a';
  1342.         if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') )
  1343.             l -= 'A' - 'a';
  1344.     }
  1345.     while ( f && (f == l) );
  1346.  
  1347.     return(f - l);
  1348. }
  1349.  
  1350.  
  1351.