//LRC歌词解析
import win.ui;
/*DSG{{*/
var winform = win.form(text="解析LRC歌词";right=698;bottom=351;bgcolor=16777215)
winform.add(
edit={cls="richedit";left=18;top=71;right=405;bottom=330;db=1;dl=1;dr=1;dt=1;edge=1;hscroll=1;multiline=1;readonly=1;vscroll=1;z=3};
lbBrowser={cls="static";left=425;top=70;right=680;bottom=329;db=1;dr=1;dt=1;notify=1;z=2};
lbWmplayer={cls="static";left=15;top=12;right=682;bottom=56;dl=1;dr=1;dt=1;notify=1;z=1}
)
/*}}*/

winform.edit.text  = /*
下面的歌词考虑到了各种不规范写法,但string.lrc都可以解析出来
 [ti:给我一首歌的时间]
 [ar:周杰伦]
 [al:魔杰座]
  [offset:200]
 歌词时间补偿，在显示歌词的timer事件中加入计算，目的是方便后期实现用户自定义歌词补偿时间
 [00:02.00]周杰伦 - 给我一首歌的时间
 支持00:02、00:02.10、00:02.100
 [00:04.00]词：周杰伦 曲：周杰伦
 [00:06.00]
 [00:08.00]放飞心情
 [00:10.00]
 无关字符串~~~~无关字符串~~~~
 [00:18.020][01:31.057]雨淋湿了天空（缺少换行）[01:33.60][00:20.35]毁得很讲究 [00:22.30][01:35.63]你说你不懂
 [00:23.086][01:37.013]为何在这时牵手
 [01:39.66][00:26]我晒干了沉默
 [00:28.47][01:41]悔得很冲动
 [00:30.52][01:43]就算这是做错
 [01:45.31][00:32]也只是怕错过
 [00:34.24]
 无关字符串~~~~无关字符串~~~~
 [00:34][01:48]在一起叫梦（缺少毫秒）
 [01:50][00:37]分开了叫痛（缺少毫秒）
 [00:39][01:52]是不是说（缺少毫秒）
 [01:53.41][00:40.22]没有做完的梦最痛
 [00:43.11][01:56.28]迷路的后果
 [01:58.23][00:45.06]我能承受
 [00:46.31][01:59.54]这最后的出口
 [00:48.62][02:01.80]在爱过了才有
 [00:52.49]
 [03:31.81][00:53.19][01:09.44][03:15.55][02:06.47][02:22.64]能不能给我一首歌的时间
 [02:10.47][00:57.25][03:19.61]紧紧的把那拥抱变成永远
 [01:01.33][02:14.51][03:23.63]在我的怀里你不用害怕失眠
 [02:18.57][03:27.75][01:05.32]哦 如果你想忘记我也能失忆
 [01:13.52][02:26.74][03:35.91]把故事听到最后才说再见
 [02:30.76][01:17.52][03:40.67]你送我的眼泪
 [02:32.65][01:19.38][03:42.24]让它留在雨天
 [01:21.64][02:34.92][03:44.56]哦 越过你划的线
 [02:37.21][01:23.97][03:46.37]我定了勇气的终点
 [01:29.01]
 [01:47.39]
 [02:43.11][03:57.82][03:49.71]你说我不该不该
 [03:51.46][02:45.83][03:59.58]不该在这时候说了我爱你
 [02:52.41][04:01.64][03:53.45]要怎么证明我没有说谎力气
 [02:59.45]请告诉我暂停算不算放弃
 [03:08.67][04:03.98]我只有一天的回忆
 [03:14.85]
 [03:49.01]
 [03:55.78]可是我只有一天的回忆
 [03:57.12]
 [04:06.81]~~End~~
*/
 
import string.lrc;
var lyric = string.lrc( winform.edit.text )
winform.edit.text = lyric.stringify();//重新格式化lrc歌词


import web.form;
var wb = web.form( winform.lbBrowser ); 
var html = `<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<style>
li{ list-style-type:square; font-size:10.5pt;color:#000000 }
li.lrcCur {
	color:#00CCFF
}
</style></head>
<body><ul>`
 
for( line,ms in lyric.each() ){
	html = html + "<li>" + line + "</li>"
} 

wb.html = html + `</ul>
<script type="text/javascript">
var lrcList=document.getElementsByTagName('li');
var lrcCur;
window.lrcNext = function(idx){  
    if( lrcCur ) lrcCur.className = '';
    lrcCur = lrcList[idx]; 
    lrcCur.className = 'lrcCur';
    window.scrollTo(0,lrcCur.offsetTop-100);  
}
</script>`;

//创建播放器控件 
//https://docs.microsoft.com/zh-cn/windows/win32/wmp/object-model-reference-for-scripting
var wmp = winform.lbWmplayer.createEmbed( "WMPlayer.OCX" )._object;
wmp.url = "http://download.aardio.com/v10.files/demo/mp3/lrc.mp3"; 

//创建定时器用于同步歌词
winform.setInterval(  
    function(){ 
        var ms = wmp.controls.currentPosition * 1000;
        if(ms){
         	var i,j,delay = lyric.find(ms)
       		if(i) {
       			try{ wb.script.lrcNext(i-1); } 
       			return delay;//修改定时器的延时为当前歌词显示时长
            }
        } 
    },100 
);  

winform.onClose = function(hwnd,message,wParam,lParam){  
      wb.write("");
}  

winform.show();
win.loopMessage();