/** * syllabes.js * @version 1.0.0 * @link https://github.com/Komanaki/syllabesjs * @license MIT */ var Syllabes=function(){function t(){this.default_config={offset:0,syllable_precision:!1,start_as_previous_end:!1}}return t.prototype.parse=function(t,e,r){void 0===r&&(r=null);var s=null!==r?r:{};for(var n in this.default_config)void 0===s[n]&&(s[n]=this.default_config[n]);null===s.offset||isNaN(parseInt(s.offset+""))?s.offset=0:s.offset=parseInt(s.offset+"");var a=null;if("ultrastar"==t.toLowerCase()?a=new UltrastarParser(s):["srt","subrip"].indexOf(t.toLowerCase())>-1?a=new SubRipParser(s):["vtt","webvtt"].indexOf(t.toLowerCase())>-1&&(a=new WebVTTParser(s)),null==a)return console.error("Couldn't file any parser for the file format \""+t+'"'),null;var i=a.parse(e);return a=null,i},t}(),SubRipParser=function(){function t(t){this.config=t,this.track=[]}return t.prototype.parse=function(t){for(var e=t.replace(/\r+/g,"").split("\n"),r=!1,s=1,n="",a=0,i=0,l=0,o=0,p=0,f=0,h=0;h0)r=!0,s=parseInt(c[0]);else{var d=u.match(/^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}/);if(null!=d&&d.length>0){r||(r=!0);var g=u.match(/X1:(\d+) X2:(\d+) Y1:(\d+) Y2:(\d+)$/);null!=g&&g.length>0&&(l=parseInt(g[1]),p=parseInt(g[2])-l,o=parseInt(g[3]),f=parseInt(g[4])-o),d=d[0].split(" --> "),a=this.parseTimecode(d[0])+this.config.offset,i=this.parseTimecode(d[1])+this.config.offset}else if(u.length>0&&r&&(n.length>0?n+="
"+this.parseText(u):n=this.parseText(u)),0!=u.length&&h!=e.length-1||!r);else{this.config.offset<0&&a<0&&(a=0);var v={id:s,start:a,end:i,duration:i-a,text:n};0!=l&&0!=o&&(v.position={left:l,top:o,width:p,height:f}),this.track.push(v),r=!1,s++,n="",a=0,i=0,l=0,o=0,p=0,f=0}}}return{track:this.track}},t.prototype.parseTimecode=function(t){var e=t.split(":");return e=e.concat(e.pop().split(",")),36e5*parseInt(e[0])+6e4*parseInt(e[1])+1e3*parseInt(e[2])+parseInt(e[3])},t.prototype.parseText=function(t){return t=t.replace(/{(\/?[biu]{1})}/g,"<$1>")},t}(),UltrastarParser=function(){function t(t){this.config=t,this.meta={title:null,artist:null,mp3:null,bpm:null,gap:0,duet:!1},this.track=[],this.track_duet=[]}return t.prototype.parse=function(t){for(var e=t.replace(/\r+/g,"").split("\n"),r=1,s=1,n=!1,a=0,i=null,l=null,o=null,p=[],f=0;f0){var u=this.makeSentence(r,p,l,null,o);null!=l&&(l=null),1==s?this.track.push(u):this.track_duet.push(u),r++,p=[]}if("P2"!=h&&"P 2"!=h){if([":","*","F"].indexOf(h[0])>-1){var c=h.match(/^[:*F] (\d+) (\d+) (-?\d+) (.+)/);if(null==c||0==c.length)continue;var d={type:"normal"};d.text=c[0].split(" ").splice(4).join(" "),c=c[0].split(" ").splice(1,3),n?d.start=Math.floor(this.meta.gap+(a+parseInt(c[0]))*i):d.start=Math.floor(this.meta.gap+parseInt(c[0])*i),d.duration=Math.floor(parseInt(c[1])*i),d.end=d.start+d.duration,d.pitch=parseInt(c[2]),this.meta.gap<0&&d.start<0&&(d.start=0,d.duration=d.end),"*"==h[0]?d.type="golden":"F"==h[0]&&(d.type="freestyle"),a+=parseInt(c[1]),p.push(d)}if("-"==h[0]){var c=h.match(/^- (\d+)\s?(\d+)/);if(null==c||0==c.length)continue;c=c[0].split(" ").splice(1);var g=null;g=n?Math.floor(this.meta.gap+(a+parseInt(c[0]))*i):Math.floor(this.meta.gap+parseInt(c[0])*i),this.meta.gap<0&&l<0&&(l=0);var u=this.makeSentence(r,p,l,g,o);null!=l&&(l=null),o=u.end,void 0!==c[1]&&(n?(l=Math.floor(this.config.offset+(a+parseInt(c[1]))*i),a+=parseInt(c[1])):(l=Math.floor(this.config.offset+parseInt(c[1])*i),a=parseInt(c[1]))),1==s?this.track.push(u):this.track_duet.push(u),r++,p=[]}if("E"==h[0])break}else s=2,this.meta.duet=!0}else{var c=h.match(/(\w+):(.+)/);if(null==c||0==c.length)continue;c=c[0].split(":");var v=c[0].toLowerCase(),m=c[1];"bpm"!=v&&"gap"!=v||(m=parseFloat(m.replace(",","."))),"bpm"==v&&(i=6e4/(4*m)),"gap"==v&&(null!==this.config.offset&&(m+=this.config.offset),l=m),"relative"==v&&"yes"==m.toLowerCase()&&(n=!0),""!=m&&(this.meta[v]=m)}}return{meta:this.meta,track:this.track,track_duet:this.track_duet.length>0?this.track_duet:null}},t.prototype.makeSentence=function(t,e,r,s,n){var a={id:t,start:e[0].start,end:e[e.length-1].end};if(this.config.syllable_precision)a.syllables=e;else{a.text="";for(var i=0;i \d{2}:\d{2}:\d{2}.\d{3}/);if(c.length>0&&null!=d&&d.length>0&&!s){s=!0,n=c;continue}}var g=c.match(/^\d{2}:\d{2}:\d{2}.\d{3} --> \d{2}:\d{2}:\d{2}.\d{3}/);if(null!=g&&g.length>0){s||(s=!0);var v=c.match(/X1:(\d+) X2:(\d+) Y1:(\d+) Y2:(\d+)$/);null!=v&&v.length>0&&(o=parseInt(v[1]),f=parseInt(v[2])-o,p=parseInt(v[3]),h=parseInt(v[4])-p),g=g[0].split(" --> "),i=this.parseTimecode(g[0])+this.config.offset,l=this.parseTimecode(g[1])+this.config.offset}else if(c.length>0&&s&&(a.length>0?a+="
"+this.parseText(c):a=this.parseText(c)),0!=c.length&&u!=e.length-1||!s);else{this.config.offset<0&&i<0&&(i=0);var m={id:n,start:i,end:l,duration:l-i,text:a};0!=o&&0!=p&&(m.position={left:o,top:p,width:f,height:h}),this.track.push(m),s=!1,n="number"==typeof n?n++:n,a="",i=0,l=0,o=0,p=0,f=0,h=0}}else this.style.push(this.parseStyle(c))}else r=!0}return{track:this.track,style:this.style}},t.prototype.parseTimecode=function(t){var e=t.split(":");return e=e.concat(e.pop().split(".")),36e5*parseInt(e[0])+6e4*parseInt(e[1])+1e3*parseInt(e[2])+parseInt(e[3])},t.prototype.parseText=function(t){return t=t.replace(/{(\/?[biu]{1})}/g,"<$1>")},t.prototype.parseStyle=function(t){return t},t}();