UNPKG

lyric-parser

Version:
586 lines (511 loc) 17.4 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["Lyric"] = factory(); else root["Lyric"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 20); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { // Thank's IE8 for his funny defineProperty module.exports = !__webpack_require__(3)(function(){ return Object.defineProperty({}, 'a', {get: function(){ return 7; }}).a != 7; }); /***/ }), /* 1 */ /***/ (function(module, exports) { module.exports = function(it){ return typeof it === 'object' ? it !== null : typeof it === 'function'; }; /***/ }), /* 2 */ /***/ (function(module, exports) { var core = module.exports = {version: '2.4.0'}; if(typeof __e == 'number')__e = core; // eslint-disable-line no-undef /***/ }), /* 3 */ /***/ (function(module, exports) { module.exports = function(exec){ try { return !!exec(); } catch(e){ return true; } }; /***/ }), /* 4 */ /***/ (function(module, exports) { // https://github.com/zloirock/core-js/issues/86#issuecomment-115759028 var global = module.exports = typeof window != 'undefined' && window.Math == Math ? window : typeof self != 'undefined' && self.Math == Math ? self : Function('return this')(); if(typeof __g == 'number')__g = global; // eslint-disable-line no-undef /***/ }), /* 5 */ /***/ (function(module, exports, __webpack_require__) { var anObject = __webpack_require__(11) , IE8_DOM_DEFINE = __webpack_require__(16) , toPrimitive = __webpack_require__(18) , dP = Object.defineProperty; exports.f = __webpack_require__(0) ? Object.defineProperty : function defineProperty(O, P, Attributes){ anObject(O); P = toPrimitive(P, true); anObject(Attributes); if(IE8_DOM_DEFINE)try { return dP(O, P, Attributes); } catch(e){ /* empty */ } if('get' in Attributes || 'set' in Attributes)throw TypeError('Accessors not supported!'); if('value' in Attributes)O[P] = Attributes.value; return O; }; /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; exports.__esModule = true; exports.default = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; /***/ }), /* 7 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; exports.__esModule = true; var _defineProperty = __webpack_require__(8); var _defineProperty2 = _interopRequireDefault(_defineProperty); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } exports.default = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; (0, _defineProperty2.default)(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /***/ }), /* 8 */ /***/ (function(module, exports, __webpack_require__) { module.exports = { "default": __webpack_require__(9), __esModule: true }; /***/ }), /* 9 */ /***/ (function(module, exports, __webpack_require__) { __webpack_require__(19); var $Object = __webpack_require__(2).Object; module.exports = function defineProperty(it, key, desc){ return $Object.defineProperty(it, key, desc); }; /***/ }), /* 10 */ /***/ (function(module, exports) { module.exports = function(it){ if(typeof it != 'function')throw TypeError(it + ' is not a function!'); return it; }; /***/ }), /* 11 */ /***/ (function(module, exports, __webpack_require__) { var isObject = __webpack_require__(1); module.exports = function(it){ if(!isObject(it))throw TypeError(it + ' is not an object!'); return it; }; /***/ }), /* 12 */ /***/ (function(module, exports, __webpack_require__) { // optional / simple context binding var aFunction = __webpack_require__(10); module.exports = function(fn, that, length){ aFunction(fn); if(that === undefined)return fn; switch(length){ case 1: return function(a){ return fn.call(that, a); }; case 2: return function(a, b){ return fn.call(that, a, b); }; case 3: return function(a, b, c){ return fn.call(that, a, b, c); }; } return function(/* ...args */){ return fn.apply(that, arguments); }; }; /***/ }), /* 13 */ /***/ (function(module, exports, __webpack_require__) { var isObject = __webpack_require__(1) , document = __webpack_require__(4).document // in old IE typeof document.createElement is 'object' , is = isObject(document) && isObject(document.createElement); module.exports = function(it){ return is ? document.createElement(it) : {}; }; /***/ }), /* 14 */ /***/ (function(module, exports, __webpack_require__) { var global = __webpack_require__(4) , core = __webpack_require__(2) , ctx = __webpack_require__(12) , hide = __webpack_require__(15) , PROTOTYPE = 'prototype'; var $export = function(type, name, source){ var IS_FORCED = type & $export.F , IS_GLOBAL = type & $export.G , IS_STATIC = type & $export.S , IS_PROTO = type & $export.P , IS_BIND = type & $export.B , IS_WRAP = type & $export.W , exports = IS_GLOBAL ? core : core[name] || (core[name] = {}) , expProto = exports[PROTOTYPE] , target = IS_GLOBAL ? global : IS_STATIC ? global[name] : (global[name] || {})[PROTOTYPE] , key, own, out; if(IS_GLOBAL)source = name; for(key in source){ // contains in native own = !IS_FORCED && target && target[key] !== undefined; if(own && key in exports)continue; // export native or passed out = own ? target[key] : source[key]; // prevent global pollution for namespaces exports[key] = IS_GLOBAL && typeof target[key] != 'function' ? source[key] // bind timers to global for call from export context : IS_BIND && own ? ctx(out, global) // wrap global constructors for prevent change them in library : IS_WRAP && target[key] == out ? (function(C){ var F = function(a, b, c){ if(this instanceof C){ switch(arguments.length){ case 0: return new C; case 1: return new C(a); case 2: return new C(a, b); } return new C(a, b, c); } return C.apply(this, arguments); }; F[PROTOTYPE] = C[PROTOTYPE]; return F; // make static versions for prototype methods })(out) : IS_PROTO && typeof out == 'function' ? ctx(Function.call, out) : out; // export proto methods to core.%CONSTRUCTOR%.methods.%NAME% if(IS_PROTO){ (exports.virtual || (exports.virtual = {}))[key] = out; // export proto methods to core.%CONSTRUCTOR%.prototype.%NAME% if(type & $export.R && expProto && !expProto[key])hide(expProto, key, out); } } }; // type bitmap $export.F = 1; // forced $export.G = 2; // global $export.S = 4; // static $export.P = 8; // proto $export.B = 16; // bind $export.W = 32; // wrap $export.U = 64; // safe $export.R = 128; // real proto method for `library` module.exports = $export; /***/ }), /* 15 */ /***/ (function(module, exports, __webpack_require__) { var dP = __webpack_require__(5) , createDesc = __webpack_require__(17); module.exports = __webpack_require__(0) ? function(object, key, value){ return dP.f(object, key, createDesc(1, value)); } : function(object, key, value){ object[key] = value; return object; }; /***/ }), /* 16 */ /***/ (function(module, exports, __webpack_require__) { module.exports = !__webpack_require__(0) && !__webpack_require__(3)(function(){ return Object.defineProperty(__webpack_require__(13)('div'), 'a', {get: function(){ return 7; }}).a != 7; }); /***/ }), /* 17 */ /***/ (function(module, exports) { module.exports = function(bitmap, value){ return { enumerable : !(bitmap & 1), configurable: !(bitmap & 2), writable : !(bitmap & 4), value : value }; }; /***/ }), /* 18 */ /***/ (function(module, exports, __webpack_require__) { // 7.1.1 ToPrimitive(input [, PreferredType]) var isObject = __webpack_require__(1); // instead of the ES6 spec version, we didn't implement @@toPrimitive case // and the second argument - flag - preferred type is a string module.exports = function(it, S){ if(!isObject(it))return it; var fn, val; if(S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val; if(typeof (fn = it.valueOf) == 'function' && !isObject(val = fn.call(it)))return val; if(!S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val; throw TypeError("Can't convert object to primitive value"); }; /***/ }), /* 19 */ /***/ (function(module, exports, __webpack_require__) { var $export = __webpack_require__(14); // 19.1.2.4 / 15.2.3.6 Object.defineProperty(O, P, Attributes) $export($export.S + $export.F * !__webpack_require__(0), 'Object', {defineProperty: __webpack_require__(5).f}); /***/ }), /* 20 */ /***/ (function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function (global, factory) { if (true) { !(__WEBPACK_AMD_DEFINE_ARRAY__ = [module, exports, __webpack_require__(6), __webpack_require__(7)], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); } else if (typeof exports !== "undefined") { factory(module, exports, require('babel-runtime/helpers/classCallCheck'), require('babel-runtime/helpers/createClass')); } else { var mod = { exports: {} }; factory(mod, mod.exports, global.classCallCheck, global.createClass); global.index = mod.exports; } })(this, function (module, exports, _classCallCheck2, _createClass2) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass3 = _interopRequireDefault(_createClass2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var timeExp = /\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g; var STATE_PAUSE = 0; var STATE_PLAYING = 1; var tagRegMap = { title: 'ti', artist: 'ar', album: 'al', offset: 'offset', by: 'by' }; function noop() {} var Lyric = function () { function Lyric(lrc) { var hanlder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop; (0, _classCallCheck3.default)(this, Lyric); this.lrc = lrc; this.tags = {}; this.lines = []; this.handler = hanlder; this.state = STATE_PAUSE; this.curLine = 0; this._init(); } (0, _createClass3.default)(Lyric, [{ key: '_init', value: function _init() { this._initTag(); this._initLines(); } }, { key: '_initTag', value: function _initTag() { for (var tag in tagRegMap) { var matches = this.lrc.match(new RegExp('\\[' + tagRegMap[tag] + ':([^\\]]*)]', 'i')); this.tags[tag] = matches && matches[1] || ''; } } }, { key: '_initLines', value: function _initLines() { var lines = this.lrc.split('\n'); for (var i = 0; i < lines.length; i++) { var line = lines[i]; var result = timeExp.exec(line); if (result) { var txt = line.replace(timeExp, '').trim(); if (txt) { this.lines.push({ time: result[1] * 60 * 1000 + result[2] * 1000 + (result[3] || 0) * 10, txt: txt }); } } } this.lines.sort(function (a, b) { return a.time - b.time; }); } }, { key: '_findCurNum', value: function _findCurNum(time) { for (var i = 0; i < this.lines.length; i++) { if (time <= this.lines[i].time) { return i; } } return this.lines.length - 1; } }, { key: '_callHandler', value: function _callHandler(i) { if (i < 0) { return; } this.handler({ txt: this.lines[i].txt, lineNum: i }); } }, { key: '_playRest', value: function _playRest() { var _this = this; var line = this.lines[this.curNum]; var delay = line.time - (+new Date() - this.startStamp); this.timer = setTimeout(function () { _this._callHandler(_this.curNum++); if (_this.curNum < _this.lines.length && _this.state === STATE_PLAYING) { _this._playRest(); } }, delay); } }, { key: 'play', value: function play() { var startTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var skipLast = arguments[1]; if (!this.lines.length) { return; } this.state = STATE_PLAYING; this.curNum = this._findCurNum(startTime); this.startStamp = +new Date() - startTime; if (!skipLast) { this._callHandler(this.curNum - 1); } if (this.curNum < this.lines.length) { clearTimeout(this.timer); this._playRest(); } } }, { key: 'togglePlay', value: function togglePlay() { var now = +new Date(); if (this.state === STATE_PLAYING) { this.stop(); this.pauseStamp = now; } else { this.state = STATE_PLAYING; this.play((this.pauseStamp || now) - (this.startStamp || now), true); this.pauseStamp = 0; } } }, { key: 'stop', value: function stop() { this.state = STATE_PAUSE; clearTimeout(this.timer); } }, { key: 'seek', value: function seek(offset) { this.play(offset); } }]); return Lyric; }(); exports.default = Lyric; module.exports = exports['default']; }); /***/ }) /******/ ]); });