鱼C论坛

 找回密码
 立即注册
查看: 3320|回复: 0

[技术交流] js 处理cue格式的字符串

[复制链接]
发表于 2021-1-16 19:21:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
非常拉跨的一段代码,见笑了。

  1. function DEF_CUEOBJ(){
  2.     this.performer="";
  3.     this.songwriter="";
  4.     this.title="";
  5.     this.file="";
  6.     this.fileType="";
  7.     this.rem=[];
  8.     this.track=[];
  9. }
  10. DEF_CUEOBJ.prototype={
  11.     setCommand:{
  12.         /**
  13.          * @param {Array<String>} _cl 指令的字符串数组
  14.          */
  15.         rem:function(_cl){
  16.             this.rem.push(_cl);
  17.         },
  18.         file:function(_cl){
  19.             this.file=_cl[1];
  20.             this.fileType=_cl[2];
  21.         },
  22.         title:function(_cl){
  23.             this.title=_cl[1];
  24.         },
  25.         performer:function(_cl){
  26.             this.performer=_cl[1];
  27.         },
  28.         songwriter:function(){
  29.             this.songwriter=_cl[1];
  30.         },

  31.         // track
  32.         // 因为js的继承反射内容是复制这个对象的引用,所以即使是在子类追加也会追加到基类上,非常拉跨。   所以我把子类的反射的内容写在基类上了...
  33.         
  34.         index:function(_cl){
  35.             var indexNub=parseInt(_cl[1]);
  36.             var time=cue_timeToSecond(_cl[2]);
  37.             var lastTrack;
  38.             if(this.root.track.length-2>=0){
  39.                 lastTrack=this.root.track[this.root.track.length-2];
  40.             }
  41.             switch(indexNub){
  42.                 case 1:
  43.                     this.op=time;
  44.                     if(lastTrack&&(lastTrack.ed==undefined)){
  45.                         lastTrack.ed=time;
  46.                     }
  47.                 break;
  48.                 case 0:
  49.                     lastTrack.ed=time;
  50.                 break;
  51.                 default:
  52.                     this.indexList.push(time);
  53.                 break;
  54.             }
  55.         }
  56.     }
  57. }
  58. function DEF_CUEOBJTrack(file,root,trackIndex){
  59.     this.performer="";
  60.     this.songwriter="";
  61.     this.title="";
  62.     this.ListIndex;
  63.     this.rem=[];
  64.     this.trackIndex=trackIndex;
  65.     this.root=root;
  66.     this.file=file;
  67.     this.op;    //秒
  68.     this.ed;
  69.     this.indexList=[];
  70. }
  71. inheritClass(DEF_CUEOBJ,DEF_CUEOBJTrack);

  72. DEF_CUEOBJTrack.prototype.getDuration=function(){
  73.     return this.ed-this.op;
  74. }

  75. /**
  76. * 把cue的表示时间的格式转换成秒
  77. * @param {String} timeStr mm:ss:ff
  78. * @returns {Number}
  79. */
  80. function cue_timeToSecond(timeStr){
  81.     var temp=timeStr.split(':');
  82.    
  83.     return parseInt(temp[0])*60+parseInt(temp[1])+parseInt(temp[2])/75;
  84. }

  85. function loadCue(str){
  86.     var p=0,q=0,isQuotes=false;
  87.     var tempStr;
  88.     var rtn=new DEF_CUEOBJ();
  89.     var then=rtn;
  90.     var CommandList=[];
  91.     for(;p<str.length;++p){
  92.         if(str[p]!=' '){
  93.             for(q=p;(p<str.length);++p){
  94.                 if(str[p]=='"'){
  95.                     isQuotes=!isQuotes;
  96.                     if(isQuotes){
  97.                         q=p+1;
  98.                     }
  99.                 }
  100.                 if((str[p]==' ')&&(!isQuotes)){
  101.                     // 记录指令
  102.                     if(str[p-1]=='"'){
  103.                         tempStr=str.slice(q,p-1);
  104.                     }else{
  105.                         tempStr=str.slice(q,p);
  106.                     }
  107.                     CommandList.push(tempStr);
  108.                     q=p+1;
  109.                 }
  110.                 else if((str[p]=="\n")||(str[p]=="\r")){
  111.                     // 换行 进入下一条指令
  112.                     if(str[p-1]=='"'){
  113.                         tempStr=str.slice(q,p-1);
  114.                     }else{
  115.                         tempStr=str.slice(q,p);
  116.                     }
  117.                     CommandList.push(tempStr);

  118.                     if(CommandList[0].toLowerCase()=="track"){
  119.                         then=new DEF_CUEOBJTrack(rtn.file,rtn,rtn.track.length);
  120.                         rtn.track.push(then);
  121.                     }
  122.                     else{
  123.                         if(then.setCommand[CommandList[0].toLowerCase()]){
  124.                             then.setCommand[CommandList[0].toLowerCase()].call(then,CommandList);
  125.                         }
  126.                         else{
  127.                             // 不支持这个指令
  128.                         }
  129.                     }

  130.                     do{ ++p; } while((str[p+1]=="\n")||(str[p+1]=="\r"));
  131.                     CommandList=[];
  132.                     break;
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     return rtn;
  138. }
复制代码

就是把cue的指令的格式转换成json而已。
挺菜的。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 05:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表