Advertisement
Guest User

Br3tt

a guest
Oct 20th, 2010
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ===========================================================================================================
  3. Lyriks 1.1 (build 20101020.1225) - a WSH lyrics module by Br3tt
  4. - This WSH script get lyrics from TAG first then from local file in Music folder then in foobar2000\lyrics\ and finally it displays them (scrolling+sync)
  5. - It handles TXT and LRC format (sync handled), but not the LRC Enhanced format (translated to simple LRC format)
  6. - exotics timestamps are now handled (like [9:99], [99:99.999], [9:99.999], [99:99:999] ...)
  7. - filenames supported are : <%artist%> -<%title%>.lrc (or .txt)  AND <%artist%> - <%title%>.lrc (or .txt)
  8. - Comments, Blank lines (no timestamp) and Info lines (ar:, ti, ...) are ignored.
  9. - Manual Scrolling possible with the mousewheel (double click on the panel to re-center on the active line)
  10. - Requirements :
  11.   1) foobar2000
  12.   2) WSH Panel Mod component
  13.   3) this script!
  14.   4) Optional: a lyrics grabber (i.e: Lyric Show Panel by hidding the panel, foo_lyricsDB, ...)
  15. - Some settings are available by using the panel Properties (right click on WSH panel -> Properties)
  16. - This script don't make any changes to your files and write nothing on your system, except the creation of the folder foobar2000/lyrics if not found
  17. ===========================================================================================================
  18. */
  19.  
  20. // Text formatting function
  21. // {{
  22. function StringFormat() {
  23.     var h_align = 0, v_align = 0, trimming = 0, flags = 0;
  24.     switch (arguments.length)
  25.     {
  26.         // fall-thru
  27.         case 4:
  28.             flags = arguments[3];
  29.         case 3:
  30.             trimming = arguments[2];
  31.         case 2:
  32.             v_align = arguments[1];
  33.         case 1:
  34.             h_align = arguments[0];
  35.             break;
  36.         default:
  37.             return 0;
  38.     }
  39.     return ((h_align << 28) | (v_align << 24) | (trimming << 20) | flags);
  40. }
  41. StringAlignment = {
  42.     Near: 0,
  43.     Centre: 1,
  44.     Far: 2
  45. };
  46. var lt_stringformat = StringFormat(StringAlignment.Near, StringAlignment.Near);
  47. var ct_stringformat = StringFormat(StringAlignment.Centre, StringAlignment.Near);
  48. var rt_stringformat = StringFormat(StringAlignment.Far, StringAlignment.Near);
  49. var lc_stringformat = StringFormat(StringAlignment.Near, StringAlignment.Centre);
  50. var cc_stringformat = StringFormat(StringAlignment.Centre, StringAlignment.Centre);
  51. var rc_stringformat = StringFormat(StringAlignment.Far, StringAlignment.Centre);
  52. var lb_stringformat = StringFormat(StringAlignment.Near, StringAlignment.Far);
  53. var cb_stringformat = StringFormat(StringAlignment.Centre, StringAlignment.Far);
  54. var rb_stringformat = StringFormat(StringAlignment.Far, StringAlignment.Far);
  55. // }}
  56.  
  57. // Use with GdiDrawText()
  58. // {{
  59. var DT_LEFT = 0x00000000;
  60. var DT_RIGHT = 0x00000002;
  61. var DT_TOP = 0x00000000;
  62. var DT_CENTER = 0x00000001;
  63. var DT_VCENTER = 0x00000004;
  64. var DT_WORDBREAK = 0x00000010;
  65. var DT_CALCRECT = 0x00000400;
  66. var DT_NOPREFIX = 0x00000800;
  67. var DT_END_ELLIPSIS = 0x00008000;
  68. // }}
  69.  
  70. //{{
  71. // IO Mode
  72. var ForReading = 1;
  73. var ForWriting = 2;
  74. var ForAppending = 8;
  75. //}}
  76.  
  77. //{{
  78. // Paths
  79. var lyrics_dir1 =  fb.FoobarPath + "lyrics\\";
  80. var lyrics_dir2 = fb.Titleformat("$replace($replace(%path%,%filename_ext%,),\,\\)");
  81. //}}
  82.  
  83. //{{
  84. // new objects
  85. sentence = function () {
  86.     this.type = null;
  87.     this.timer = 0;
  88.     this.text = "";
  89.     this.total_lines = 0;
  90.     this.ante_lines = 0;
  91. }
  92. //}}
  93.  
  94. function RGBA(r, g, b, a) {
  95.     return ((a << 24) | (r << 16) | (g << 8) | (b));
  96. }
  97.  
  98. // ================================================/ TF
  99. var len = fb.Titleformat("%length%");
  100. var elap = fb.TitleFormat("%playback_time%");
  101. var remain = fb.TitleFormat("%playback_time_remaining%");
  102. var len_seconds = fb.Titleformat("%length_seconds%");
  103. var elap_seconds = fb.TitleFormat("%playback_time_seconds%");
  104. var remain_seconds = fb.Titleformat("%playback_time_remaining_seconds%");
  105. var artist = fb.Titleformat("$replace(%artist%,'/','_')");
  106. var title = fb.Titleformat("%title%");
  107. var lyrics = fb.TitleFormat("[$if2(%LYRICS%,$if2(%LYRIC%,$if2(%UNSYNCED LYRICS%,%UNSYNCED LYRIC%)))]");
  108.  
  109. // ================================================/ Declarations
  110. var g_font = gdi.Font("segoe ui", 12, 0);
  111. var g_playtimer;
  112. var g_metadb;
  113. var ww = 0, wh = 0;
  114.  
  115. var PLAYTIMER_VALUE = window.GetProperty("Timer value in ms [10;100]", 10);
  116. var LINE_HEIGHT = window.GetProperty("Line height [20;30]", 24);
  117. var TEXT_SHADOW = window.GetProperty("Text shadow (true/false)", true);
  118. var PLAIN_COLOR_BG = window.GetProperty("Plain colour background (true/false)", false);
  119. var DITHER = window.GetProperty("Top & Botom dither (true/false)", true);
  120. var H_PADDING = window.GetProperty("Horizontal padding [0;20]", 20);
  121. var TXT_ALIGN = window.GetProperty("Text alignment (left/center/right)", "center");
  122. var LIGHT_BG = window.GetProperty("Light background", true);
  123. var DEBUG = false;
  124. var DEFAULT_OFFSET = 29;
  125.  
  126. var g_fso = new ActiveXObject("Scripting.FileSystemObject");
  127. var g_file = null;
  128. var g_tab = Array();
  129. var g_scroll=0;
  130. var g_lyrics_path;
  131. var g_lyrics_filename;
  132. var g_lyrics_status;
  133. var focus=0;
  134. var focus_next=0;
  135. var g_plainbg_colour;
  136. var g_ovbg_normalcolour;
  137. var g_ovbg_lineseparatorcolour;
  138. var g_ovbg_highlightcolour;
  139. var g_txt_normalcolour;
  140. var g_txt_highlightcolour;
  141. var g_txt_shadowcolour;
  142. var centiemes = 0;
  143. var g_is_scrolling = false;
  144. var g_multi_balise = false;
  145. var g_balise_total;
  146. var g_balise_char_offset = 10;
  147. var g_tab_length;
  148. var g_txt_align;
  149.  
  150. var midpoint;
  151. var pos;
  152.  
  153. // START
  154. function on_size() {
  155.     var k;
  156.     ww = window.Width;
  157.     wh = window.Height;
  158.    
  159.     g_metadb = fb.IsPlaying ? fb.GetNowPlaying() : false;
  160.      
  161.     // exit test on useless calls made (wsh tweak)
  162.     if(!ww && !wh) return true;
  163.    
  164.     midpoint = wh / 2 - LINE_HEIGHT / 2;
  165.  
  166.     // colour definitions
  167.     g_plainbg_colour = RGBA(140, 200, 250, 255);
  168.     g_ovbg_normalcolour = RGBA(000, 000, 000, 130);
  169.     g_ovbg_lineseparatorcolour = RGBA(000, 000, 000, 100);
  170.     g_ovbg_highlightcolour = RGBA(020, 100, 240, 130);
  171.     g_txt_normalcolour = RGBA(200, 200, 210, 255);
  172.     g_txt_highlightcolour = RGBA(255, 255, 255, 255);
  173.     g_txt_shadowcolour = RGBA(000, 000, 000, 150);
  174.    
  175.     g_playtimer && window.KillTimer(g_playtimer);
  176.  
  177.     if(fb.IsPlaying||fb.IsPaused) {
  178.         g_scroll=0;
  179.         g_is_scrolling = false;
  180.         get_lyrics();
  181.         change_focus();
  182.         k = g_tab[focus].ante_lines * LINE_HEIGHT;
  183.         if(g_lyrics_status>0) {
  184.             pos = midpoint - k;
  185.             g_playtimer = window.CreateTimerInterval(PLAYTIMER_VALUE);
  186.         } else {
  187.             delta = (g_tab[g_tab.length-1].ante_lines + g_tab[g_tab.length-1].total_lines);
  188.             pos = (wh / 2) - (delta * LINE_HEIGHT / 2);
  189.         }
  190.     }
  191. }
  192.  
  193. function on_paint(gr) {
  194.     PLAIN_COLOR_BG && gr.FillSolidRect(0, 0, ww, wh, g_plainbg_colour);
  195.     if(fb.IsPlaying||fb.IsPaused) {
  196.         show_lyrics(gr, g_tab, pos);
  197.         DEBUG && gr.GdiDrawText(g_lyrics_status.toString(), g_font, g_normalcolour, 5, 5, ww, LINE_HEIGHT, DT_TOP | DT_LEFT | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
  198.     }
  199.     DITHER && gr.FillGradRect(0, 0, ww, 11, 90, g_txt_shadowcolour, RGBA(0,0,0,0));
  200.     DITHER && gr.FillGradRect(0, wh-11, ww, 11, 270, g_txt_shadowcolour, RGBA(0,0,0,0));
  201.    
  202.     DITHER && gr.FillGradRect(0, 0, ww, 1, 90, g_txt_shadowcolour, RGBA(0,0,0,0));
  203.     DITHER && gr.FillGradRect(0, wh-2, ww, 1, 90, RGBA(0,0,0,0), g_txt_shadowcolour);
  204. }
  205.  
  206. function on_mouse_lbtn_dblclk(x, y, mask) {
  207.     on_size();
  208. }
  209.  
  210. function on_mouse_leave() {
  211.     window.Repaint();
  212. }
  213.  
  214. function on_playback_time(time) {
  215.     // at each new seconde, centiemes is reset to 0 (Increment on timer every 100ms)
  216.     centiemes = 0;
  217.     if(g_lyrics_status==0) {
  218.         if(elap_seconds.Eval()==3) {
  219.             on_size();
  220.         }
  221.         window.Repaint();
  222.     }
  223. }
  224.  
  225. function on_playback_new_track(info) {
  226.     g_metadb = fb.IsPlaying ? fb.GetNowPlaying() : false;
  227.     g_playtimer && window.KillTimer(g_playtimer);
  228.     pos=0;
  229.     g_scroll = 0;
  230.     g_is_scrolling = false;
  231.     get_lyrics();
  232.     change_focus();
  233.     if(g_lyrics_status>0) {
  234.         g_playtimer = window.CreateTimerInterval(PLAYTIMER_VALUE);
  235.     }
  236. }
  237.  
  238. function on_playback_seek(time) {
  239.     var k;
  240.     if(g_lyrics_status>0) {
  241.         g_scroll = 0;
  242.         g_is_scrolling = false;
  243.         change_focus();
  244.         k = g_tab[focus].ante_lines * LINE_HEIGHT;
  245.         pos = midpoint - k;
  246.         window.Repaint();
  247.     }
  248. }
  249.  
  250. function on_playback_stop(reason) {
  251.     g_metadb = fb.IsPlaying ? fb.GetNowPlaying() : false;
  252.     g_playtimer && window.KillTimer(g_playtimer);
  253.     if(reason==0) {
  254.         // Stop
  255.         window.Repaint();
  256.     }
  257. }
  258.  
  259. function on_mouse_wheel(delta) {
  260.     if(g_lyrics_status>0) {
  261.         if(delta>0) {
  262.             pos = (pos>=midpoint)?pos:pos + LINE_HEIGHT;
  263.         } else {
  264.             pos = (pos<=(wh/2 - g_tab_length*LINE_HEIGHT))?pos:pos - LINE_HEIGHT;
  265.         }
  266.         window.Repaint();
  267.     }
  268. }
  269.  
  270. function on_timer(id) {
  271.    
  272.     var t1 = elap_seconds.Eval() * 100 + centiemes;
  273.     var t2 = len_seconds.Eval() * 100;
  274.     var p1, p2;
  275.    
  276.     if(t1>t2-100) {
  277.         g_playtimer && window.KillTimer(g_playtimer);
  278.     }
  279.  
  280.     if(g_playtimer) {
  281.         if(g_playtimer.ID == id) {
  282.             if(!g_is_scrolling && t1>=g_tab[focus_next].timer) {
  283.                 p1 = g_tab[focus].ante_lines*LINE_HEIGHT;
  284.                 p2 = g_tab[focus_next].ante_lines*LINE_HEIGHT;
  285.                 g_scroll = p2 - p1;
  286.                 change_focus();
  287.                 g_is_scrolling = true;
  288.             }
  289.             if (g_scroll>0) {
  290.                 pos -= 1;
  291.                 g_scroll -= 1;
  292.                 window.Repaint();
  293.             } else {
  294.                 g_is_scrolling = false;
  295.             }
  296.             centiemes = (centiemes>98)?0:(centiemes+1);
  297.         }
  298.     }
  299.  
  300. }
  301.  
  302. function show_lyrics(gr, tab, posy) {
  303.     var i, k;
  304.     var text_colour;
  305.    
  306.     gr.SetTextRenderingHint(5);
  307.    
  308.     // decoding text alignment (from Properties)
  309.     switch(TXT_ALIGN.toUpperCase()) {
  310.         case "LEFT":
  311.             g_txt_align = lc_stringformat;
  312.             break;
  313.         case "CENTER":
  314.             g_txt_align = cc_stringformat;
  315.             break;
  316.         case "RIGHT":
  317.             g_txt_align = rc_stringformat;
  318.             break;
  319.         default:
  320.             g_txt_align = cc_stringformat;
  321.     }
  322.  
  323.     if(g_lyrics_status>0) {
  324.         if(posy>=0) {
  325.             LIGHT_BG && gr.FillSolidRect(0, Math.floor(posy - wh/2), ww, Math.floor(wh/2), g_ovbg_normalcolour);
  326.         }
  327.     } else {
  328.         LIGHT_BG && gr.FillSolidRect(0, 0, ww, wh, g_ovbg_normalcolour);
  329.     }
  330.  
  331.     for(i=0;i<tab.length;i++) {
  332.         if(Math.round(posy)>=(LINE_HEIGHT*-4) && Math.round(posy)<wh)
  333.         {
  334.             if(i==focus && g_lyrics_status==1) {
  335.                 text_colour = g_txt_highlightcolour;
  336.             } else {
  337.                 if(g_lyrics_status==0) {
  338.                     text_colour = g_txt_highlightcolour;
  339.                 } else {
  340.                     text_colour = g_txt_normalcolour;
  341.                 }
  342.             }
  343.             if(g_lyrics_status>0) {
  344.                 if(!LIGHT_BG && i==focus && tab[i].text.length>1 && g_lyrics_status==1) {
  345.                     gr.FillSolidRect(0, Math.floor(posy), ww, (tab[i].total_lines*LINE_HEIGHT), g_ovbg_highlightcolour);
  346.                 } else {
  347.                     LIGHT_BG && gr.FillSolidRect(0, Math.floor(posy), ww, (tab[i].total_lines*LINE_HEIGHT-1), (i==focus && tab[i].text.length>1 && g_lyrics_status==1)?g_ovbg_highlightcolour:g_ovbg_normalcolour);
  348.                     //LIGHT_BG && gr.FillSolidRect(0, Math.floor(posy+(tab[i].total_lines*LINE_HEIGHT-1)), ww, 1, g_ovbg_lineseparatorcolour);
  349.                 }
  350.             }
  351.             TEXT_SHADOW && gr.DrawString(tab[i].text, g_font, g_txt_shadowcolour, 1+H_PADDING, Math.floor(posy)-1, ww-H_PADDING*2, (tab[i].total_lines*LINE_HEIGHT), g_txt_align);
  352.             TEXT_SHADOW && gr.DrawString(tab[i].text, g_font, g_txt_shadowcolour, 1+H_PADDING, Math.floor(posy), ww-H_PADDING*2, (tab[i].total_lines*LINE_HEIGHT), g_txt_align);
  353.             gr.DrawString(tab[i].text, g_font, text_colour, 0+H_PADDING, Math.floor(posy)-1, ww-H_PADDING*2, (tab[i].total_lines*LINE_HEIGHT), g_txt_align);
  354.         }
  355.         posy = Math.floor(posy+LINE_HEIGHT+((tab[i].total_lines-1)*LINE_HEIGHT));
  356.     }
  357.  
  358.     if(g_lyrics_status>0 && posy<=wh) {
  359.         LIGHT_BG && gr.FillSolidRect(0, Math.floor(posy), ww, Math.floor(wh), g_ovbg_normalcolour);
  360.     }
  361.  
  362. }
  363.  
  364. function grab_timer(tab, str, count, timer_prec, balise) {
  365.     var tminutes, tsecondes, tcentiemes;
  366.     var i;
  367.     var f_sentence = new sentence;
  368.     var b = balise - 1;
  369.     if(g_lyrics_status==1 && balise>0) {
  370.         if (str.substring(0,1)=="[") {
  371.             tminutes = str.substring(1+(b*g_balise_char_offset), 3+(b*g_balise_char_offset));
  372.             tsecondes = str.substring(4+(b*g_balise_char_offset), 6+(b*g_balise_char_offset));
  373.             tcentiemes = str.substring(7+(b*g_balise_char_offset), 9+(b*g_balise_char_offset));
  374.             if(IsNumeric(str.substring(1,2))==true) {
  375.                 f_sentence.type = "ly";
  376.                 f_sentence.timer = Math.round(tminutes)*60*100 + Math.round(tsecondes)*100 + Math.round(tcentiemes) - DEFAULT_OFFSET;
  377.                 f_sentence.text = remove_enhanced_balises(str.substring(g_balise_char_offset*g_balise_total, str.length));
  378.                 f_sentence.text = (f_sentence.text.length==0)?f_sentence.text+" ":f_sentence.text;
  379.             } else {
  380.                 f_sentence.type = null;
  381.                 f_sentence.timer = 0;
  382.                 f_sentence.text = "";
  383.             }
  384.         } else {
  385.             // no balise ==> line is set as a comment (as a blank line)
  386.             if(count==0) g_lyrics_status = 2;
  387.             f_sentence.type = null;
  388.             f_sentence.timer = timer_prec;
  389.             f_sentence.text = "";
  390.         }
  391.     } else {
  392.         f_sentence.type = "tx";
  393.         f_sentence.timer = 0;
  394.         // check if it's not finally a synch lyrics (lrc) with some first lines with no timestamp!
  395.         if (str.substring(0,1)=="[") {
  396.             // test balise (=ts) type is ok ( [99:99.999] )
  397.             if (ts_analyzer(str).substring(9,10)=="]") {
  398.                 g_lyrics_status = 1;
  399.                 str = ts_analyzer(str);
  400.                 tminutes = str.substring(1+(b*g_balise_char_offset), 3+(b*g_balise_char_offset));
  401.                 tsecondes = str.substring(4+(b*g_balise_char_offset), 6+(b*g_balise_char_offset));
  402.                 tcentiemes = str.substring(7+(b*g_balise_char_offset), 9+(b*g_balise_char_offset));
  403.                 f_sentence.type = "ly";
  404.                 f_sentence.timer = Math.round(tminutes)*60*100 + Math.round(tsecondes)*100 + Math.round(tcentiemes) - DEFAULT_OFFSET;
  405.                 f_sentence.text = remove_enhanced_balises(str.substring(g_balise_char_offset*g_balise_total, str.length));
  406.                 f_sentence.text = (f_sentence.text.length==0)?f_sentence.text+" ":f_sentence.text;
  407.             } else {
  408.                 f_sentence.text = str;
  409.             }
  410.         } else {
  411.             f_sentence.text = str;
  412.         }
  413.     }
  414.     return f_sentence;
  415. }
  416.  
  417. function load_file(filePath, fileName) {
  418.     var tab = Array();
  419.     var fline = new sentence;
  420.     var count = 0;
  421.     var enr = 0;
  422.     var str="";
  423.     var balise;
  424.     var delta;
  425.     g_file = open_file(filePath, fileName, ForReading);
  426.     g_balise_shift = 0;
  427.     while(!g_file.AtEndOfStream) {
  428.         str = g_file.ReadLine();
  429.         if(g_lyrics_status==1) {
  430.             if (str.substring(0,1)=="[") {
  431.                 str = ts_analyzer(str);
  432.                 g_balise_total = 0;
  433.                 balise = 0;
  434.                 while(str.substring(balise*10,balise*10+1)=="[") {
  435.                     balise += 1;
  436.                 }
  437.                 g_balise_total = balise;
  438.             } else if(enr==0) {
  439.                 g_lyrics_status = 2;
  440.                 g_balise_total = 1;
  441.             }
  442.         } else {
  443.             g_balise_total = 1;
  444.         }
  445.                
  446.         if(g_balise_total>1) g_multi_balise = true; else g_multi_balise = false;
  447.  
  448.         // str loop for each timestamp found for it ==> occurs created in the array
  449.         for (balise=1;balise<=g_balise_total;balise++) {
  450.             fline=grab_timer(tab, str, count, (count>0)?tab[count-1].timer:0, balise);
  451.             if(fline.type!=null) {
  452.                 tab.push(fline);
  453.                 count++;
  454.             }
  455.         }
  456.         enr += 1;
  457.     }
  458.     close_file(g_file);
  459.    
  460.     // if file read is empty, set lyrics as not found to avoid script errors
  461.     if(count==0) {
  462.         g_lyrics_status = 0;
  463.         g_tab = load_track_info();
  464.         delta = (g_tab[g_tab.length-1].ante_lines + g_tab[g_tab.length-1].total_lines);
  465.         pos = (wh / 2) - (delta * LINE_HEIGHT / 2);
  466.         return g_tab;
  467.     } else {
  468.         // add a default last sentence with a timer maximum (for ease of use focus line search method)
  469.         tab.push(new sentence);
  470.         tab[count].type = "ly";
  471.         tab[count].timer = 9999999;
  472.         tab[count].text = "---";
  473.         tab[count].total_lines = 1;
  474.         tab[count].ante_lines = tab[count-1].ante_lines + tab[count-1].total_lines;
  475.         return sort_tab(tab);
  476.     }
  477. }
  478.  
  479. function parse_tag(tag) {
  480.     var t_tab = Array();
  481.     var i, j, count;
  482.     j = 0;
  483.     for(i=0;i<tag.length;i++) {
  484.         if(tag.charCodeAt(i)==10) {
  485.             t_tab.push(tag.substring(j, i));
  486.             j = i+1;
  487.         }
  488.     }
  489.     return parse_line(t_tab);
  490. }
  491.  
  492. function parse_line(tagtab) {
  493.     var tab = Array();
  494.     var fline = new sentence;
  495.     var count = 0;
  496.     var enr = 0;
  497.     var str="";
  498.     var balise;
  499.     var delta;
  500.     g_balise_shift = 0;
  501.     for(enr=0;enr<tagtab.length;enr++) {
  502.         str = tagtab[enr];
  503.         if(g_lyrics_status==1) {
  504.             if (str.substring(0,1)=="[") {
  505.                 str = ts_analyzer(str);
  506.                 g_balise_total = 0;
  507.                 balise = 0;
  508.                 while(str.substring(balise*10,balise*10+1)=="[") {
  509.                     balise += 1;
  510.                 }
  511.                 g_balise_total = balise;
  512.             } else if(enr==0) {
  513.                 g_lyrics_status = 2;
  514.                 g_balise_total = 1;
  515.             }
  516.         } else {
  517.             g_balise_total = 1;
  518.         }
  519.        
  520.         if(g_balise_total>1) g_multi_balise = true; else g_multi_balise = false;
  521.  
  522.         // str loop for each timestamp found for it ==> occurs created in the array
  523.         for (balise=1;balise<=g_balise_total;balise++) {
  524.             fline=grab_timer(tab, str, count, (count>0)?tab[count-1].timer:0, balise);
  525.             if(fline.type!=null) {
  526.                 tab.push(fline);
  527.                 count++;
  528.             }
  529.         }
  530.     }
  531.    
  532.     // if file read is empty, set lyrics as not found to avoid script errors
  533.     if(count==0) {
  534.         g_lyrics_status = 0;
  535.         g_tab = load_track_info();
  536.         delta = (g_tab[g_tab.length-1].ante_lines + g_tab[g_tab.length-1].total_lines);
  537.         pos = (wh / 2) - (delta * LINE_HEIGHT / 2);
  538.         return g_tab;
  539.     } else {
  540.         // add a default last sentence with a timer maximum (for ease of use focus line search method)
  541.         tab.push(new sentence);
  542.         tab[count].type = "ly";
  543.         tab[count].timer = 9999999;
  544.         tab[count].text = "---";
  545.         tab[count].total_lines = 1;
  546.         tab[count].ante_lines = tab[count-1].ante_lines + tab[count-1].total_lines;
  547.         return sort_tab(tab);
  548.     }
  549. }
  550.  
  551. function load_track_info() {
  552.     var tab = Array(new sentence, new sentence, new sentence, new sentence);
  553.     var count = 0;
  554.  
  555.     tab[count].type = "tx";
  556.     tab[count].text = "---";
  557.     count++;
  558.    
  559.     tab[count].type = "tx";
  560.     tab[count].text = artist.Eval();
  561.     count++;
  562.    
  563.     tab[count].type = "tx";
  564.     tab[count].text = title.Eval();
  565.     count++;
  566.  
  567.     tab[count].type = "tx";
  568.     tab[count].text = "---";
  569.     count++;
  570.    
  571.     return calc_lines(tab);
  572. }
  573.  
  574. function open_file(filePath, fileName, ioMode) {
  575.     var fileInst = null;
  576.     var bool = file_exists(filePath + fileName);
  577.     if(bool) {
  578.         fileInst = g_fso.OpenTextFile(filePath + fileName, ioMode);
  579.     }
  580.     return fileInst;
  581. }
  582.  
  583. function close_file(fileInst) {
  584.         fileInst.Close();
  585. }
  586.  
  587. function read_file(fileInst) {
  588.     var line = fileInst.ReadLine();
  589.     return line;
  590. }
  591.  
  592.  
  593. function folder_exists(folderPath) {
  594.     var fso, bool;
  595.     fso = new ActiveXObject("Scripting.FileSystemObject");
  596.     bool = fso.FolderExists(folderPath);
  597.     return bool;
  598. }
  599.  
  600.  
  601. function file_exists(filePath) {
  602.     var fso, bool;
  603.     fso = new ActiveXObject("Scripting.FileSystemObject");
  604.     bool = fso.Fileexists(filePath);
  605.     return bool;
  606. }
  607.  
  608. function lyrics_folder_exists(folder_name) {
  609.     var fso;
  610.     var bool=1;
  611.     fso = new ActiveXObject("Scripting.FileSystemObject");
  612.     if (folder_exists(fb.ProfilePath + folder_name) == false) {
  613.         fso.CreateFolder(fb.ProfilePath + folder_name);
  614.         bool=0;
  615.     }
  616.     return bool;
  617. }
  618.  
  619. function get_lyrics() {
  620.     var i=0;
  621.     var nbl=0;
  622.     var delta;
  623.     var tag;
  624.     var bool_tag = false;
  625.        
  626.     // reset lyrics tab
  627.     g_lyrics_status = 0;
  628.     if(g_tab.length>0) g_tab.splice(0, g_tab.length);
  629.     pos = midpoint;
  630.     focus = 0;
  631.     window.Repaint();
  632.        
  633.     // check TAGs
  634.     tag = lyrics.Eval();
  635.     if(tag.length>0) {
  636.         bool_tag = true;
  637.         if(tag.substring(0,1)=="[") g_lyrics_status = 1; else g_lyrics_status = 2;
  638.     } else {
  639.         // check files in music folder (filename syntax #1)
  640.         g_lyrics_status=null;
  641.         g_lyrics_filename = artist.Eval() + " -" + title.Eval();
  642.         if(file_exists(lyrics_dir2.EvalWithMetadb(g_metadb)+g_lyrics_filename+".lrc")) {
  643.             g_lyrics_filename  = g_lyrics_filename+".lrc";
  644.             g_lyrics_status = 1;
  645.             g_lyrics_path = lyrics_dir2.EvalWithMetadb(g_metadb);
  646.         } else {
  647.             if(file_exists(lyrics_dir2.EvalWithMetadb(g_metadb)+g_lyrics_filename+".txt")) {
  648.                 g_lyrics_filename  = g_lyrics_filename+".txt";
  649.                 g_lyrics_status = 2;
  650.                 g_lyrics_path = lyrics_dir2.EvalWithMetadb(g_metadb);
  651.             }
  652.         }
  653.         // check files in music folder (filename syntax #2)
  654.         if(g_lyrics_status==null) {
  655.             g_lyrics_filename = artist.Eval() + " - " + title.Eval();
  656.             if(file_exists(lyrics_dir2.EvalWithMetadb(g_metadb)+g_lyrics_filename+".lrc")) {
  657.                 g_lyrics_filename  = g_lyrics_filename+".lrc";
  658.                 g_lyrics_status = 1;
  659.                 g_lyrics_path = lyrics_dir2.EvalWithMetadb(g_metadb);
  660.             } else {
  661.                 if(file_exists(lyrics_dir2.EvalWithMetadb(g_metadb)+g_lyrics_filename+".txt")) {
  662.                     g_lyrics_filename  = g_lyrics_filename+".txt";
  663.                     g_lyrics_status = 2;
  664.                     g_lyrics_path = lyrics_dir2.EvalWithMetadb(g_metadb);
  665.                 }
  666.             }
  667.         }
  668.         // if not found, check in lyrics folder (filename syntax #1)
  669.         if(g_lyrics_status==null) {
  670.             g_lyrics_filename = artist.Eval() + " -" + title.Eval();
  671.             if(file_exists(lyrics_dir1+g_lyrics_filename+".lrc")) {
  672.                 g_lyrics_filename  = g_lyrics_filename+".lrc";
  673.                 g_lyrics_status = 1;
  674.                 g_lyrics_path = lyrics_dir1;
  675.             } else {
  676.                 if(file_exists(lyrics_dir1+g_lyrics_filename+".txt")) {
  677.                     g_lyrics_filename  = g_lyrics_filename+".txt";
  678.                     g_lyrics_status = 2;
  679.                     g_lyrics_path = lyrics_dir1;
  680.                 }
  681.             }
  682.         }
  683.         // if not found, check in lyrics folder (filename syntax #2)
  684.         if(g_lyrics_status==null) {
  685.             g_lyrics_filename = artist.Eval() + " - " + title.Eval();
  686.             if(file_exists(lyrics_dir1+g_lyrics_filename+".lrc")) {
  687.                 g_lyrics_filename  = g_lyrics_filename+".lrc";
  688.                 g_lyrics_status = 1;
  689.                 g_lyrics_path = lyrics_dir1;
  690.             } else {
  691.                 if(file_exists(lyrics_dir1+g_lyrics_filename+".txt")) {
  692.                     g_lyrics_filename  = g_lyrics_filename+".txt";
  693.                     g_lyrics_status = 2;
  694.                     g_lyrics_path = lyrics_dir1;
  695.                 } else {
  696.                     g_lyrics_status = 0;
  697.                     g_lyrics_path = null;
  698.                 }
  699.             }          
  700.         }
  701.     }
  702.     // if lyrics found
  703.     if(g_lyrics_status>0) {
  704.         if(bool_tag) {
  705.             g_tab = parse_tag(tag);
  706.         } else {
  707.             g_tab = load_file(g_lyrics_path, g_lyrics_filename);
  708.         }
  709.     }  else {
  710.         g_tab = load_track_info();
  711.         delta = (g_tab[g_tab.length-1].ante_lines + g_tab[g_tab.length-1].total_lines);
  712.         pos = (wh / 2) - (delta * LINE_HEIGHT / 2);
  713.     }
  714.  
  715.     if(g_lyrics_status==2) {
  716.         for(i=0;i<g_tab.length-1;i++) {
  717.             g_tab[i].timer = i * Math.floor(len_seconds.Eval() * 100 / g_tab.length);
  718.         }
  719.     }
  720. }
  721.  
  722. function change_focus() {
  723.     var i, j;
  724.     var t1 = elap_seconds.Eval()*100+centiemes;
  725.    
  726.     if(g_lyrics_status>0) {
  727.         // search line index just after actual timer
  728.         for(i=0;i<g_tab.length;i++) {
  729.             if(g_tab[i].timer>t1) break;
  730.         }
  731.         focus_next=i;
  732.         focus = (i>0)?i-1:0;
  733.         // now i check if there are more than one line with the same timer as focus one & if found, i take it as new focus 'cause it's the first (not a blank line)
  734.         if(focus>0) {
  735.             for(i=0;i<focus;i++) {
  736.                 if(g_tab[i].timer==focus) {
  737.                     focus = i;
  738.                     break;
  739.                 }
  740.             }
  741.         }
  742.     }
  743. }
  744.  
  745. function calc_lines(ctab) {
  746.     var i, j;
  747.     var padx = 0;
  748.     var tmp_img;
  749.     var gp;
  750.     var lineh;
  751.     g_tab_length = 0;
  752.     if(ww==0) return ctab; // test to avoid errors if panel is hidden (ww=0)
  753.     tmp_img = gdi.CreateImage(ww-(H_PADDING*2)-padx, 100);
  754.     gp = tmp_img.GetGraphics();
  755.     for(i=0;i<ctab.length;i++) {
  756.         // calc sentence #lines to display / window.width
  757.         if(ctab[i].type!=null) {
  758.             lineh = gp.MeasureString(ctab[i].text, g_font, 0, 0, ww-(H_PADDING*2)-padx, wh).Height;
  759.             ctab[i].total_lines = (lineh/LINE_HEIGHT) > Math.floor(lineh/LINE_HEIGHT) ? Math.floor(lineh/LINE_HEIGHT) + 1 : Math.floor(lineh/LINE_HEIGHT)  ;
  760.             ctab[i].ante_lines = 0;
  761.             for(j=0;j<i;j++) {
  762.                 ctab[i].ante_lines += ctab[j].total_lines;
  763.             }
  764.         }
  765.         g_tab_length += ctab[i].total_lines;
  766.     }
  767.     CollectGarbage();
  768.     return ctab;
  769. }
  770.  
  771. function sort_tab(tab2sort) {
  772.     var tab = Array();
  773.     var i, j;
  774.     var tmp = new sentence;
  775.     var smallest = 0;
  776.     for(i=0;i<tab2sort.length;i++) {
  777.         for(j=i;j<tab2sort.length;j++) {
  778.             if(tab2sort[i].timer > tab2sort[j].timer) {
  779.                 tmp = tab2sort[i];
  780.                 tab2sort[i] = tab2sort[j];
  781.                 tab2sort[j] = tmp;
  782.             }
  783.         }
  784.         tab.push(tab2sort[i]);
  785.     }
  786.     tab = calc_lines(tab);
  787.     return tab;
  788. }
  789.  
  790. function remove_enhanced_balises(str) {
  791.     var i;
  792.     var chr1, chr2;
  793.     var new_str="";
  794.     for(i=0;i<str.length;i++) {
  795.         chr1 = str.substring(i, i+1);
  796.         if(i+10<=str.length) chr2 = str.substring(i+9, i+10); else chr2=null;
  797.         if(chr1=="<" && chr2==">") {
  798.             i = i + 10 - 1;
  799.         } else {
  800.             new_str = new_str + chr1;
  801.         }
  802.     }
  803.     return new_str;
  804. }
  805.  
  806. function ts_analyzer(str) {
  807.     var i, j;
  808.     var ch;
  809.     var str2 = "";
  810.     var state=0;
  811.     var deb = -1;
  812.     var fin = -1;
  813.     var sep1 = -1;
  814.     var sep2 = -1;
  815.     var ts_len;
  816.     var suite=0;
  817.     for(i=0;i<str.length;i++) {
  818.         ch = str.substring(i, i+1);
  819.         switch(state) {
  820.         case 0:            
  821.             if(ch=="[") {
  822.                 state=1;
  823.                 deb=i;
  824.                 fin=-1;
  825.             }
  826.             break;
  827.         case 1:
  828.             if(deb>=0 && ch==":") {
  829.                 state=2;
  830.                 sep1=i;
  831.             }
  832.             if(i>deb+3) {
  833.                 deb=-1;
  834.                 fin=-1;
  835.                 sep1=-1;
  836.                 sep2=-1;
  837.                 state=0;
  838.             }
  839.             break;
  840.         case 2:
  841.             if(sep1>=0 && (ch==":" || ch==".")) {
  842.                 state=3;
  843.                 sep2=i;
  844.             }
  845.             if(sep1>=0 && sep2==-1 && ch=="]") {
  846.                 state=4;
  847.                 fin=i;
  848.             }
  849.             if(i>sep1+3) {
  850.                 deb=-1;
  851.                 fin=-1;
  852.                 sep1=-1;
  853.                 sep2=-1;
  854.                 state=0;
  855.             }
  856.             break;
  857.         case 3:
  858.             if(deb>=0 && ch=="]") {
  859.                 state=4;
  860.                 if(i==str.length-1) {
  861.                     str=str+" ";
  862.                 }
  863.                 fin=i;
  864.             }
  865.             if(i>deb+10) {
  866.                 deb=-1;
  867.                 fin=-1;
  868.                 sep1=-1;
  869.                 sep2=-1;
  870.                 state=0;
  871.             }
  872.             break;
  873.         case 4:
  874.             ts_len=fin-deb+1;
  875.             switch(ts_len) {
  876.                 case 6:
  877.                     str2 = str2 + "[0" + str.substring(deb+1,deb+2) + ":" + str.substring(deb+3, deb+5) + ".00]";
  878.                     break;
  879.                 case 7:
  880.                     str2 = str2 + str.substring(deb,deb+6) + ".00]";
  881.                     break;
  882.                 case 9:
  883.                     str2 = str2 + "[0" + str.substring(deb+1,deb+2) + ":" + str.substring(deb+3, deb+5) + "." + str.substring(deb+6, deb+8) + "]";
  884.                     break;
  885.                 case 10:
  886.                     if(sep1==deb+2) {
  887.                         str2 = str2 + "[0" + str.substring(deb+1,deb+2) + ":" + str.substring(deb+3, deb+5) + "." + str.substring(deb+6, deb+8) + "]";
  888.                     } else {
  889.                         str2 = str2 + str.substring(deb,deb+10);
  890.                     }
  891.                     break;
  892.                 case 11:
  893.                     str2 = str2 + str.substring(deb,deb+9) + "]";
  894.                     break;
  895.             }
  896.             j += 10;
  897.             if(str.substring(fin+1,fin+2)=="[") {
  898.                 suite = 1;
  899.                 i = fin;
  900.                 deb = -1;
  901.                 fin = -1;
  902.                 sep1 = -1;
  903.                 sep2 = -1;
  904.                 state = 0;
  905.             } else {
  906.                 suite = -1;
  907.                 i = str.length - 1;
  908.                 str2 = str2 + str.substring(fin+1, str.length);
  909.             }
  910.             break;      
  911.         }
  912.     }
  913.     return str2;
  914. }
  915.  
  916. function IsNumeric(str) {
  917.     var ValidChars = "0123456789.";
  918.     for (i = 0; i < str.length; i++) {
  919.         if (ValidChars.indexOf(str.charAt(i)) == -1) {
  920.             return false;
  921.         }
  922.     }
  923.     return true;
  924. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement