UNPKG

lrc-file-parser

Version:
7 lines (6 loc) 8.92 kB
/**! * lrc-file-parser.js v2.4.1 * Homepage: https://github.com/lyswhut/lrc-file-parser#readme * License: MIT */ var Lyric=function(){"use strict";function t(t,e){var i=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),i.push.apply(i,n)}return i}function e(e){for(var i=1;i<arguments.length;i++){var n=null!=arguments[i]?arguments[i]:{};i%2?t(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):t(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}function n(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,l(n.key),n)}}function r(t,e,i){return(e=l(e))in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function s(t){return function(t){if(Array.isArray(t))return o(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||a(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,e){if(t){if("string"==typeof t)return o(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?o(t,e):void 0}}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=new Array(e);i<e;i++)n[i]=t[i];return n}function u(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!i){if(Array.isArray(t)||(i=a(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var n=0,r=function(){};return{s:r,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,o=!0,u=!1;return{s:function(){i=i.call(t)},n:function(){var t=i.next();return o=t.done,t},e:function(t){u=!0,s=t},f:function(){try{o||null==i.return||i.return()}finally{if(u)throw s}}}}function l(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var n=i.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var c=/^(?:\[[\d:.]+\])+/g,h=/\d{1,3}(:\d{1,3}){0,2}(?:\.\d{1,3})/g,f={title:"ti",artist:"ar",album:"al",offset:"offset",by:"by"},m="object"==("undefined"==typeof performance?"undefined":i(performance))&&performance.now?performance.now.bind(performance):Date.now.bind(Date),y=function(){},d={invokeTime:0,animationFrameId:null,timeoutId:null,callback:null,thresholdTime:200,run:function(){var t=this;this.animationFrameId=window.requestAnimationFrame((function(){t.animationFrameId=null;var e=t.invokeTime-m();if(e>0)return e<t.thresholdTime?void t.run():t.timeoutId=setTimeout((function(){t.timeoutId=null,t.run()}),e-t.thresholdTime);t.callback(e)}))},start:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:y,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;this.callback=t,this.invokeTime=m()+e,this.run()},clear:function(){this.animationFrameId&&(window.cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.timeoutId&&(window.clearTimeout(this.timeoutId),this.timeoutId=null),this.callback=null}},v=/^0+(\d+)/,p=/:0+(\d+)/g,b=/\.0+(\d+)/,L=function(t){return t.replace(v,"$1").replace(p,":$1").replace(b,".$1")},g=function(t,e){for(var i=e.split(/\r\n|\n|\r/),n=0;n<i.length;n++){var r=i[n].trim(),s=c.exec(r);if(s){var a=s[0],o=r.replace(c,"").trim();if(o){var l=a.match(h);if(null==l)continue;var f,m=u(l);try{for(m.s();!(f=m.n()).done;){var y=f.value,d=t[L(y)];d&&d.extendedLyrics.push(o)}}catch(t){m.e(t)}finally{m.f()}}}}},_={title:"",artist:"",album:"",offset:0,by:""},k=function(){function t(i){var n=i.lyric,s=void 0===n?"":n,a=i.extendedLyrics,o=void 0===a?[]:a,u=i.offset,l=void 0===u?150:u,c=i.playbackRate,h=void 0===c?1:c,f=i.onPlay,m=void 0===f?y:f,d=i.onSetLyric,v=void 0===d?y:d,p=i.isRemoveBlankLine,b=void 0===p||p;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),r(this,"lyric",void 0),r(this,"extendedLyrics",void 0),r(this,"tags",void 0),r(this,"lines",void 0),r(this,"onPlay",void 0),r(this,"onSetLyric",void 0),r(this,"isPlay",void 0),r(this,"curLineNum",void 0),r(this,"maxLine",void 0),r(this,"offset",void 0),r(this,"isRemoveBlankLine",void 0),r(this,"_playbackRate",void 0),r(this,"_performanceTime",void 0),r(this,"_startTime",void 0),this.lyric=s,this.extendedLyrics=o,this.tags=e({},_),this.lines=[],this.onPlay=m,this.onSetLyric=v,this.isPlay=!1,this.curLineNum=0,this.maxLine=0,this.offset=l,this.isRemoveBlankLine=b,this._playbackRate=h,this._performanceTime=0,this._startTime=0,this._init()}var i,a,o;return i=t,a=[{key:"_init",value:function(){null==this.lyric&&(this.lyric=""),null==this.extendedLyrics&&(this.extendedLyrics=[]),this._initTag(),this._initLines(),this.onSetLyric(this.lines)}},{key:"_initTag",value:function(){this.tags=e({},_);for(var t=0,i=Object.keys(_);t<i.length;t++){var n=i[t],r=this.lyric.match(new RegExp("\\[".concat(f[n],":([^\\]]*)]"),"i"));r&&(this.tags[n]=r[1])}if(this.tags.offset){var s=parseInt(this.tags.offset);this.tags.offset=Number.isNaN(s)?0:s}else this.tags.offset=0}},{key:"_initLines",value:function(){this.lines=[];for(var t=this.lyric.split(/\r\n|\n|\r/),e={},i=t.length,n=0;n<i;n++){var r=t[n].trim(),a=c.exec(r);if(a){var o=a[0],l=r.replace(c,"").trim();if(l||!this.isRemoveBlankLine){var f=o.match(h);if(null==f)continue;var m,y=u(f);try{for(y.s();!(m=y.n()).done;){var d=m.value,v=L(d);if(e[v])e[v].extendedLyrics.push(l);else{var p=v.split(":");if(!(p.length>3)){if(p.length<3)for(var b=3-p.length;b--;)p.unshift("0");p[2].includes(".")&&p.splice.apply(p,[2,1].concat(s(p[2].split(".")))),e[v]={time:60*parseInt(p[0])*60*1e3+60*parseInt(p[1])*1e3+1e3*parseInt(p[2])+parseInt(p[3]||"0"),text:l,extendedLyrics:[]}}}}}catch(t){y.e(t)}finally{y.f()}}}}var _,k=u(this.extendedLyrics);try{for(k.s();!(_=k.n()).done;){var w=_.value;g(e,w)}}catch(t){k.e(t)}finally{k.f()}this.lines=Object.values(e),this.lines.sort((function(t,e){return t.time-e.time})),this.maxLine=this.lines.length-1}},{key:"_currentTime",value:function(){return(m()-this._performanceTime)*this._playbackRate+this._startTime}},{key:"_findCurLineNum",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(t<=0)return 0;for(var i=this.lines.length,n=e;n<i;n++)if(t<=this.lines[n].time)return 0===n?0:n-1;return i-1}},{key:"_handleMaxLine",value:function(){this.onPlay(this.curLineNum,this.lines[this.curLineNum].text),this.pause()}},{key:"_refresh",value:function(){var t=this;if(this.curLineNum++,this.curLineNum>=this.maxLine)this._handleMaxLine();else{var e=this.lines[this.curLineNum],i=this._currentTime(),n=i-e.time;if(n>=0||0===this.curLineNum){var r=(this.lines[this.curLineNum+1].time-e.time-n)/this._playbackRate;if(r>0)this.isPlay&&d.start((function(){t.isPlay&&t._refresh()}),r),this.onPlay(this.curLineNum,e.text);else{var s=this._findCurLineNum(i,this.curLineNum+1);s>this.curLineNum&&(this.curLineNum=s-1),this._refresh()}}else this.curLineNum=this._findCurLineNum(i,this.curLineNum)-1,this._refresh()}}},{key:"play",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.lines.length&&(this.pause(),this.isPlay=!0,this._performanceTime=m()-Math.trunc(this.tags.offset+this.offset),this._startTime=t,this.curLineNum=this._findCurLineNum(this._currentTime())-1,this._refresh())}},{key:"pause",value:function(){if(this.isPlay&&(this.isPlay=!1,d.clear(),this.curLineNum!==this.maxLine)){var t=this._findCurLineNum(this._currentTime());this.curLineNum!==t&&(this.curLineNum=t,this.onPlay(t,this.lines[t].text))}}},{key:"setPlaybackRate",value:function(t){this._playbackRate=t,this.lines.length&&this.isPlay&&this.play(this._currentTime())}},{key:"setLyric",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];this.isPlay&&this.pause(),this.lyric=t,this.extendedLyrics=e,this._init()}}],a&&n(i.prototype,a),o&&n(i,o),Object.defineProperty(i,"prototype",{writable:!1}),t}();return k}();