@music-lyric-player/base 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("lodash-es");const a=require("@music-lyric-player/utils"),l={},o="0.5.1";class h{get version(){return o}type="Incorrect";metas=[];lines=[];agents=[];toJSON(){return{version:this.version,type:this.type,metas:this.metas,lines:this.lines,agents:this.agents}}}class f{config=new a.ConfigManager(l);event=new a.Event;state;time;info;constructor(){this.state={playing:!1,frameId:null,index:0,lines:[]},this.time={start:0,seek:0},this.info=new h}handleGetCurrentTime(){return this.state.playing?this.time.seek+(performance.now()-this.time.start):this.time.seek}handleGetLineTime(e){if(e<0||e>=this.info.lines.length)return 0;if(e===this.info.lines.length-1)return 1/0;const i=this.info.lines[e],s=this.info.lines[e+1];return Math.max(i.time.end,s.time.start)}handleSyncTime(e){const i=[];let s=this.info.lines.length;for(let t=0;t<this.info.lines.length;t++){const n=this.info.lines[t];if(n.time.start>e){s=t;break}this.handleGetLineTime(t)>e&&i.push(n)}this.state.lines=i,this.state.index=s,this.event.emit("linesUpdate",[...this.state.lines])}handleUpdateActiveLines(e){let i=!1;const s=this.state.lines.filter(t=>{const n=this.info.lines.indexOf(t);return e>=this.handleGetLineTime(n)?(i=!0,!1):!0});for(;this.state.index<this.info.lines.length;){const t=this.info.lines[this.state.index];if(e>=t.time.start)e<this.handleGetLineTime(this.state.index)&&(s.push(t),i=!0),this.state.index++;else break}i&&(this.state.lines=s,this.event.emit("linesUpdate",[...this.state.lines]))}onTick=()=>{if(!this.state.playing)return;const e=this.handleGetCurrentTime();this.handleUpdateActiveLines(e),this.state.frameId=requestAnimationFrame(this.onTick)};updateLyric(e){this.pause(),this.info=e,this.state.index=0,this.state.lines=[],this.time.seek=0,this.event.emit("lyricUpdate",e),this.event.emit("linesUpdate",[])}play(e){this.pause(),typeof e=="number"&&!Number.isNaN(e)&&(this.time.seek=e,this.handleSyncTime(e)),this.time.start=performance.now(),this.state.playing=!0,this.onTick(),this.event.emit("play",this.handleGetCurrentTime())}pause(){this.state.playing&&(this.time.seek=this.handleGetCurrentTime(),this.state.playing=!1),this.state.frameId!==null&&(cancelAnimationFrame(this.state.frameId),this.state.frameId=null),this.event.emit("pause",this.handleGetCurrentTime())}dispose(){this.pause(),this.event.clear(),this.state.lines=[],this.info=new h}get currentLines(){return[...this.state.lines]}get currentInfo(){return this.info}get currentTime(){return this.handleGetCurrentTime()}}exports.BaseLyricPlayer=f;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("lodash-es");const r=require("@music-lyric-player/utils"),d={},u="0.5.1";class l{get version(){return u}type="Incorrect";metas=[];lines=[];agents=[];toJSON(){return{version:this.version,type:this.type,metas:this.metas,lines:this.lines,agents:this.agents}}}class o{config=new r.ConfigManager(d);event=new r.Event;state;active;time;info;constructor(){this.state={playing:!1,frameId:null,scanIndex:0},this.active={lines:[],index:[]},this.time={start:0,seek:0},this.info=new l}handleGetCurrentTime(){return this.state.playing?this.time.seek+(performance.now()-this.time.start):this.time.seek}handleGetLineTime(e){if(e<0||e>=this.info.lines.length)return 0;if(e===this.info.lines.length-1)return 1/0;const i=this.info.lines[e],s=this.info.lines[e+1];return Math.max(i.time.end,s.time.start)}handleGetActiveIndex(){return this.active.index.length>0?this.active.index[0]:-1}handleSyncTime(e){const i=[],s=[];let n=this.info.lines.length;for(let t=0;t<this.info.lines.length;t++){const a=this.info.lines[t];if(a.time.start>e){n=t;break}this.handleGetLineTime(t)>e&&(i.push(a),s.push(t))}this.state.scanIndex=n,this.active.lines=i,this.active.index=s,this.event.emit("linesUpdate",[...this.active.lines],[...this.active.index],this.handleGetActiveIndex())}handleUpdateActiveLines(e){let i=!1;const s=[],n=[];for(let t=0;t<this.active.lines.length;t++){const a=this.active.lines[t],h=this.active.index[t];e>=this.handleGetLineTime(h)?i=!0:(s.push(a),n.push(h))}for(;this.state.scanIndex<this.info.lines.length;){const t=this.info.lines[this.state.scanIndex];if(e>=t.time.start)e<this.handleGetLineTime(this.state.scanIndex)&&(s.push(t),n.push(this.state.scanIndex),i=!0),this.state.scanIndex++;else break}i&&(this.active.lines=s,this.active.index=n,this.event.emit("linesUpdate",[...this.active.lines],[...this.active.index],this.handleGetActiveIndex()))}onTick=()=>{if(!this.state.playing)return;const e=this.handleGetCurrentTime();this.handleUpdateActiveLines(e),this.state.frameId=requestAnimationFrame(this.onTick)};updateLyric(e){e&&(this.pause(),this.info=e,this.active.lines=[],this.active.index=[],this.state.scanIndex=0,this.time.seek=0,this.event.emit("lyricUpdate",e),this.event.emit("linesUpdate",[],[],-1))}play(e){this.pause(),typeof e=="number"&&!Number.isNaN(e)&&(this.time.seek=e,this.handleSyncTime(e)),this.time.start=performance.now(),this.state.playing=!0,this.onTick(),this.event.emit("play",this.handleGetCurrentTime())}pause(){this.state.playing&&(this.time.seek=this.handleGetCurrentTime(),this.state.playing=!1),this.state.frameId!==null&&(cancelAnimationFrame(this.state.frameId),this.state.frameId=null),this.event.emit("pause",this.handleGetCurrentTime())}dispose(){this.pause(),this.event.clear(),this.active.lines=[],this.active.index=[],this.info=new l}get currentLines(){return[...this.active.lines]}get currentIndex(){return[...this.active.index]}get currentActive(){return this.handleGetActiveIndex()}get currentInfo(){return this.info}get currentTime(){return this.handleGetCurrentTime()}}exports.BaseLyricPlayer=o;
2
2
  //# sourceMappingURL=index.comm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.comm.js","sources":["../src/options.ts","../../../node_modules/.pnpm/@music-lyric-kit+lyric@0.4.0/node_modules/@music-lyric-kit/lyric/dist/index.ecma.js","../src/index.ts"],"sourcesContent":["export interface Options {}\n\nexport const DEFAULT_OPTIONS: Options = {}\n","import { createRandomHex as r } from \"@music-lyric-kit/utils\";\nclass s {\n start = 0;\n end = 0;\n get duration() {\n return this.end - this.start;\n }\n toJSON() {\n return {\n start: this.start,\n end: this.end,\n duration: this.duration\n };\n }\n}\nvar o = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Space = \"Space\", t))(o || {});\nclass i {\n stress = !1;\n toJSON() {\n return {\n stress: this.stress\n };\n }\n}\nclass p {\n get type() {\n return \"Normal\";\n }\n time = new s();\n content = \"\";\n extended = [];\n config = new i();\n toJSON() {\n return {\n type: this.type,\n time: this.time,\n content: this.content,\n extended: this.extended,\n config: this.config\n };\n }\n}\nclass N {\n get type() {\n return \"Space\";\n }\n count = 1;\n toJSON() {\n return {\n type: this.type,\n count: this.count\n };\n }\n}\nvar a = /* @__PURE__ */ ((t) => (t.UnKnown = \"UnKnown\", t.Translate = \"Translate\", t.Roman = \"Roman\", t))(a || {});\nclass S {\n type = \"UnKnown\";\n content = \"\";\n toJSON() {\n return {\n type: this.type,\n content: this.content\n };\n }\n}\nvar l = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Interlude = \"Interlude\", t))(l || {});\nclass x {\n get type() {\n return \"Interlude\";\n }\n id = r(4).toUpperCase();\n time = new s();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time\n };\n }\n}\nclass c {\n words = [];\n extended = [];\n get original() {\n return this.words.map((n) => n.type === o.Normal ? n.content : new Array(n.count).fill(\" \").join(\"\")).join(\"\");\n }\n toJSON() {\n return {\n words: this.words,\n extended: this.extended,\n original: this.original\n };\n }\n}\nclass y {\n get type() {\n return \"Normal\";\n }\n id = r(4).toUpperCase();\n time = new s();\n content = new c();\n agent;\n background;\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time,\n content: this.content,\n agent: this.agent,\n background: this.background\n };\n }\n}\nvar u = /* @__PURE__ */ ((t) => (t.Offset = \"Offset\", t.Duration = \"Duration\", t.Title = \"Title\", t.Singer = \"Singer\", t.Album = \"Album\", t.Creator = \"Creator\", t.UnKnown = \"UnKnown\", t))(u || {});\nclass e {\n id = r(4).toUpperCase();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n content: this.content\n };\n }\n}\nclass f extends e {\n get type() {\n return \"Offset\";\n }\n content = 0;\n}\nclass w extends e {\n get type() {\n return \"Duration\";\n }\n content = 0;\n}\nclass O extends e {\n get type() {\n return \"Title\";\n }\n content = \"\";\n}\nclass b extends e {\n get type() {\n return \"Singer\";\n }\n content = \"\";\n}\nclass J extends e {\n get type() {\n return \"Album\";\n }\n content = \"\";\n}\nclass U extends e {\n get type() {\n return \"Creator\";\n }\n content = {\n role: \"\",\n name: []\n };\n}\nclass C extends e {\n get type() {\n return \"UnKnown\";\n }\n content = \"\";\n}\nclass I {\n id = \"\";\n name = \"\";\n count = 0;\n}\nclass d {\n // index in global\n global = 0;\n // index in block\n block = 0;\n toJSON() {\n return {\n global: this.global,\n block: this.block\n };\n }\n}\nclass v {\n id = \"\";\n index = new d();\n toJSON() {\n return {\n id: this.id,\n index: this.index\n };\n }\n}\nconst m = \"0.5.1\";\nvar g = /* @__PURE__ */ ((t) => (t.Incorrect = \"Incorrect\", t.Normal = \"Normal\", t.Syllable = \"Syllable\", t.Pure = \"Pure\", t.Empty = \"Empty\", t.NoTime = \"NoTime\", t))(g || {});\nclass A {\n get version() {\n return m;\n }\n type = \"Incorrect\";\n metas = [];\n lines = [];\n agents = [];\n toJSON() {\n return {\n version: this.version,\n type: this.type,\n metas: this.metas,\n lines: this.lines,\n agents: this.agents\n };\n }\n}\nexport {\n I as Agent,\n v as AgentLine,\n d as AgentLineIndex,\n S as Extended,\n a as ExtendedType,\n A as Info,\n x as LineInterlude,\n y as LineNormal,\n c as LineNormalContent,\n l as LineType,\n J as MetaAlbum,\n U as MetaCreator,\n w as MetaDuration,\n f as MetaOffset,\n b as MetaSinger,\n O as MetaTitle,\n u as MetaType,\n C as MetaUnKnown,\n s as Time,\n g as Type,\n m as Version,\n p as WordNormal,\n N as WordSpace,\n o as WordType\n};\n//# sourceMappingURL=index.ecma.js.map\n","import type { Line } from '@music-lyric-kit/lyric'\nimport type { DeepPartial } from '@music-lyric-player/utils'\nimport type { Options } from './options'\n\nimport { DEFAULT_OPTIONS } from './options'\n\nimport { Info } from '@music-lyric-kit/lyric'\nimport { ConfigManager, Event } from '@music-lyric-player/utils'\n\nexport interface BaseLyricPlayerEventMap {\n /**\n * When the player starts or resumes playback.\n * @param currentTime The current playback time.\n */\n play: (currentTime: number) => void\n\n /**\n * When the player pauses playback.\n * @param currentTime The current playback time.\n */\n pause: (currentTime: number) => void\n\n /**\n * When the entire lyric information is updated (e.g., loading a new lyric/song).\n * @param info The newly loaded lyric information object.\n */\n lyricUpdate: (info: Info) => void\n\n /**\n * When the currently active lyric lines change during playback.\n * @param lines An array of the currently active lyric lines.\n */\n linesUpdate: (lines: Line[]) => void\n}\n\nexport class BaseLyricPlayer {\n readonly config = new ConfigManager<Options, DeepPartial<Options>>(DEFAULT_OPTIONS)\n\n readonly event: Event<BaseLyricPlayerEventMap> = new Event()\n\n private state: {\n playing: boolean\n frameId: number | null\n index: number\n lines: Line[]\n }\n private time: {\n start: number\n seek: number\n }\n private info: Info\n\n constructor() {\n this.state = {\n playing: false,\n frameId: null,\n index: 0,\n lines: [],\n }\n this.time = {\n start: 0,\n seek: 0,\n }\n this.info = new Info()\n }\n\n private handleGetCurrentTime() {\n if (!this.state.playing) {\n return this.time.seek\n }\n return this.time.seek + (performance.now() - this.time.start)\n }\n\n private handleGetLineTime(index: number): number {\n if (index < 0 || index >= this.info.lines.length) {\n return 0\n }\n\n if (index === this.info.lines.length - 1) {\n return Infinity\n }\n\n const line = this.info.lines[index]\n const nextLine = this.info.lines[index + 1]\n return Math.max(line.time.end, nextLine.time.start)\n }\n\n private handleSyncTime(time: number) {\n const result: Line[] = []\n\n let firstIndex = this.info.lines.length\n for (let i = 0; i < this.info.lines.length; i++) {\n const line = this.info.lines[i]\n if (line.time.start > time) {\n firstIndex = i\n break\n }\n\n if (this.handleGetLineTime(i) > time) {\n result.push(line)\n }\n }\n\n this.state.lines = result\n this.state.index = firstIndex\n\n this.event.emit('linesUpdate', [...this.state.lines])\n }\n\n private handleUpdateActiveLines(now: number) {\n let hasChanged = false\n\n const activeLines = this.state.lines.filter((line) => {\n const infoIndex = this.info.lines.indexOf(line)\n if (now >= this.handleGetLineTime(infoIndex)) {\n hasChanged = true\n return false\n }\n return true\n })\n\n while (this.state.index < this.info.lines.length) {\n const nextLine = this.info.lines[this.state.index]\n\n if (now >= nextLine.time.start) {\n if (now < this.handleGetLineTime(this.state.index)) {\n activeLines.push(nextLine)\n hasChanged = true\n }\n this.state.index++\n } else {\n break\n }\n }\n\n if (hasChanged) {\n this.state.lines = activeLines\n this.event.emit('linesUpdate', [...this.state.lines])\n }\n }\n\n private onTick = () => {\n if (!this.state.playing) {\n return\n }\n\n const now = this.handleGetCurrentTime()\n this.handleUpdateActiveLines(now)\n\n this.state.frameId = requestAnimationFrame(this.onTick)\n }\n\n updateLyric(info: Info) {\n this.pause()\n this.info = info\n\n this.state.index = 0\n this.state.lines = []\n this.time.seek = 0\n\n this.event.emit('lyricUpdate', info)\n this.event.emit('linesUpdate', [])\n }\n\n play(time?: number) {\n this.pause()\n\n if (typeof time === 'number' && !Number.isNaN(time)) {\n this.time.seek = time\n this.handleSyncTime(time)\n }\n\n this.time.start = performance.now()\n this.state.playing = true\n this.onTick()\n\n this.event.emit('play', this.handleGetCurrentTime())\n }\n\n pause() {\n if (this.state.playing) {\n this.time.seek = this.handleGetCurrentTime()\n this.state.playing = false\n }\n if (this.state.frameId !== null) {\n cancelAnimationFrame(this.state.frameId)\n this.state.frameId = null\n }\n\n this.event.emit('pause', this.handleGetCurrentTime())\n }\n\n dispose() {\n this.pause()\n this.event.clear()\n this.state.lines = []\n this.info = new Info()\n }\n\n get currentLines() {\n return [...this.state.lines]\n }\n\n get currentInfo() {\n return this.info\n }\n\n get currentTime() {\n return this.handleGetCurrentTime()\n }\n}\n"],"names":["DEFAULT_OPTIONS","m","A","BaseLyricPlayer","ConfigManager","Event","Info","index","line","nextLine","time","result","firstIndex","i","now","hasChanged","activeLines","infoIndex","info"],"mappings":"k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Y,CAAC,OAAO,MAAMA,CAAI,IAChD,KAAK,KAAK,KAAOA,EACjB,KAAK,eAAeA,CAAI,GAG1B,KAAK,KAAK,MAAQ,YAAY,IAAA,EAC9B,KAAK,MAAM,QAAU,GACrB,KAAK,OAAA,EAEL,KAAK,MAAM,KAAK,OAAQ,KAAK,sBAAsB,CACrD,CAEA,OAAQ,CACF,KAAK,MAAM,UACb,KAAK,KAAK,KAAO,KAAK,qBAAA,EACtB,KAAK,MAAM,QAAU,IAEnB,KAAK,MAAM,UAAY,OACzB,qBAAqB,KAAK,MAAM,OAAO,EACvC,KAAK,MAAM,QAAU,MAGvB,KAAK,MAAM,KAAK,QAAS,KAAK,sBAAsB,CACtD,CAEA,SAAU,CACR,KAAK,MAAA,EACL,KAAK,MAAM,MAAA,EACX,KAAK,MAAM,MAAQ,CAAA,EACnB,KAAK,KAAO,IAAIJ,CAClB,CAEA,IAAI,cAAe,CACjB,MAAO,CAAC,GAAG,KAAK,MAAM,KAAK,CAC7B,CAEA,IAAI,aAAc,CAChB,OAAO,KAAK,IACd,CAEA,IAAI,aAAc,CAChB,OAAO,KAAK,qBAAA,CACd,CACF","x_google_ignoreList":[1]}
1
+ {"version":3,"file":"index.comm.js","sources":["../src/options.ts","../../../node_modules/.pnpm/@music-lyric-kit+lyric@0.4.0/node_modules/@music-lyric-kit/lyric/dist/index.ecma.js","../src/index.ts"],"sourcesContent":["export interface Options {}\n\nexport const DEFAULT_OPTIONS: Options = {}\n","import { createRandomHex as r } from \"@music-lyric-kit/utils\";\nclass s {\n start = 0;\n end = 0;\n get duration() {\n return this.end - this.start;\n }\n toJSON() {\n return {\n start: this.start,\n end: this.end,\n duration: this.duration\n };\n }\n}\nvar o = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Space = \"Space\", t))(o || {});\nclass i {\n stress = !1;\n toJSON() {\n return {\n stress: this.stress\n };\n }\n}\nclass p {\n get type() {\n return \"Normal\";\n }\n time = new s();\n content = \"\";\n extended = [];\n config = new i();\n toJSON() {\n return {\n type: this.type,\n time: this.time,\n content: this.content,\n extended: this.extended,\n config: this.config\n };\n }\n}\nclass N {\n get type() {\n return \"Space\";\n }\n count = 1;\n toJSON() {\n return {\n type: this.type,\n count: this.count\n };\n }\n}\nvar a = /* @__PURE__ */ ((t) => (t.UnKnown = \"UnKnown\", t.Translate = \"Translate\", t.Roman = \"Roman\", t))(a || {});\nclass S {\n type = \"UnKnown\";\n content = \"\";\n toJSON() {\n return {\n type: this.type,\n content: this.content\n };\n }\n}\nvar l = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Interlude = \"Interlude\", t))(l || {});\nclass x {\n get type() {\n return \"Interlude\";\n }\n id = r(4).toUpperCase();\n time = new s();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time\n };\n }\n}\nclass c {\n words = [];\n extended = [];\n get original() {\n return this.words.map((n) => n.type === o.Normal ? n.content : new Array(n.count).fill(\" \").join(\"\")).join(\"\");\n }\n toJSON() {\n return {\n words: this.words,\n extended: this.extended,\n original: this.original\n };\n }\n}\nclass y {\n get type() {\n return \"Normal\";\n }\n id = r(4).toUpperCase();\n time = new s();\n content = new c();\n agent;\n background;\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time,\n content: this.content,\n agent: this.agent,\n background: this.background\n };\n }\n}\nvar u = /* @__PURE__ */ ((t) => (t.Offset = \"Offset\", t.Duration = \"Duration\", t.Title = \"Title\", t.Singer = \"Singer\", t.Album = \"Album\", t.Creator = \"Creator\", t.UnKnown = \"UnKnown\", t))(u || {});\nclass e {\n id = r(4).toUpperCase();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n content: this.content\n };\n }\n}\nclass f extends e {\n get type() {\n return \"Offset\";\n }\n content = 0;\n}\nclass w extends e {\n get type() {\n return \"Duration\";\n }\n content = 0;\n}\nclass O extends e {\n get type() {\n return \"Title\";\n }\n content = \"\";\n}\nclass b extends e {\n get type() {\n return \"Singer\";\n }\n content = \"\";\n}\nclass J extends e {\n get type() {\n return \"Album\";\n }\n content = \"\";\n}\nclass U extends e {\n get type() {\n return \"Creator\";\n }\n content = {\n role: \"\",\n name: []\n };\n}\nclass C extends e {\n get type() {\n return \"UnKnown\";\n }\n content = \"\";\n}\nclass I {\n id = \"\";\n name = \"\";\n count = 0;\n}\nclass d {\n // index in global\n global = 0;\n // index in block\n block = 0;\n toJSON() {\n return {\n global: this.global,\n block: this.block\n };\n }\n}\nclass v {\n id = \"\";\n index = new d();\n toJSON() {\n return {\n id: this.id,\n index: this.index\n };\n }\n}\nconst m = \"0.5.1\";\nvar g = /* @__PURE__ */ ((t) => (t.Incorrect = \"Incorrect\", t.Normal = \"Normal\", t.Syllable = \"Syllable\", t.Pure = \"Pure\", t.Empty = \"Empty\", t.NoTime = \"NoTime\", t))(g || {});\nclass A {\n get version() {\n return m;\n }\n type = \"Incorrect\";\n metas = [];\n lines = [];\n agents = [];\n toJSON() {\n return {\n version: this.version,\n type: this.type,\n metas: this.metas,\n lines: this.lines,\n agents: this.agents\n };\n }\n}\nexport {\n I as Agent,\n v as AgentLine,\n d as AgentLineIndex,\n S as Extended,\n a as ExtendedType,\n A as Info,\n x as LineInterlude,\n y as LineNormal,\n c as LineNormalContent,\n l as LineType,\n J as MetaAlbum,\n U as MetaCreator,\n w as MetaDuration,\n f as MetaOffset,\n b as MetaSinger,\n O as MetaTitle,\n u as MetaType,\n C as MetaUnKnown,\n s as Time,\n g as Type,\n m as Version,\n p as WordNormal,\n N as WordSpace,\n o as WordType\n};\n//# sourceMappingURL=index.ecma.js.map\n","import type { Line } from '@music-lyric-kit/lyric'\nimport type { DeepPartial } from '@music-lyric-player/utils'\nimport type { Options } from './options'\n\nimport { DEFAULT_OPTIONS } from './options'\n\nimport { Info } from '@music-lyric-kit/lyric'\nimport { ConfigManager, Event } from '@music-lyric-player/utils'\n\nexport interface BaseLyricPlayerEventMap {\n /**\n * When the player starts or resumes playback.\n * @param currentTime The current playback time.\n */\n play: (currentTime: number) => void\n\n /**\n * When the player pauses playback.\n * @param currentTime The current playback time.\n */\n pause: (currentTime: number) => void\n\n /**\n * When the entire lyric information is updated (e.g., loading a new lyric/song).\n * @param info The newly loaded lyric information object.\n */\n lyricUpdate: (info: Info) => void\n\n /**\n * When the currently active lyric lines change during playback.\n * @param lines An array of the currently active lyric lines.\n * @param indexs An array of the currently active lyric lines' indexes.\n * @param firstIndex The index of the first currently active lyric line (-1 if none).\n */\n linesUpdate: (lines: Line[], indexs: number[], index: number) => void\n}\n\nexport class BaseLyricPlayer {\n readonly config = new ConfigManager<Options, DeepPartial<Options>>(DEFAULT_OPTIONS)\n\n readonly event: Event<BaseLyricPlayerEventMap> = new Event()\n\n private state: {\n playing: boolean\n frameId: number | null\n scanIndex: number\n }\n private active: {\n lines: Line[]\n index: number[]\n }\n private time: {\n start: number\n seek: number\n }\n private info: Info\n\n constructor() {\n this.state = {\n playing: false,\n frameId: null,\n scanIndex: 0,\n }\n this.active = {\n lines: [],\n index: [],\n }\n this.time = {\n start: 0,\n seek: 0,\n }\n this.info = new Info()\n }\n\n private handleGetCurrentTime() {\n if (!this.state.playing) {\n return this.time.seek\n }\n return this.time.seek + (performance.now() - this.time.start)\n }\n\n private handleGetLineTime(index: number): number {\n if (index < 0 || index >= this.info.lines.length) {\n return 0\n }\n\n if (index === this.info.lines.length - 1) {\n return Infinity\n }\n\n const line = this.info.lines[index]\n const nextLine = this.info.lines[index + 1]\n return Math.max(line.time.end, nextLine.time.start)\n }\n\n private handleGetActiveIndex() {\n return this.active.index.length > 0 ? this.active.index[0] : -1\n }\n\n private handleSyncTime(time: number) {\n const lines: Line[] = []\n const index: number[] = []\n\n let firstIndex = this.info.lines.length\n for (let i = 0; i < this.info.lines.length; i++) {\n const line = this.info.lines[i]\n if (line.time.start > time) {\n firstIndex = i\n break\n }\n\n if (this.handleGetLineTime(i) > time) {\n lines.push(line)\n index.push(i) // 保证 lines 和 index 同步推入\n }\n }\n\n this.state.scanIndex = firstIndex\n\n this.active.lines = lines\n this.active.index = index\n\n this.event.emit('linesUpdate', [...this.active.lines], [...this.active.index], this.handleGetActiveIndex())\n }\n\n private handleUpdateActiveLines(now: number) {\n let hasChanged = false\n\n const newActiveLines: Line[] = []\n const newActiveIndex: number[] = []\n\n for (let i = 0; i < this.active.lines.length; i++) {\n const line = this.active.lines[i]\n const infoIndex = this.active.index[i]\n\n if (now >= this.handleGetLineTime(infoIndex)) {\n hasChanged = true\n } else {\n newActiveLines.push(line)\n newActiveIndex.push(infoIndex)\n }\n }\n\n while (this.state.scanIndex < this.info.lines.length) {\n const nextLine = this.info.lines[this.state.scanIndex]\n if (now >= nextLine.time.start) {\n if (now < this.handleGetLineTime(this.state.scanIndex)) {\n newActiveLines.push(nextLine)\n newActiveIndex.push(this.state.scanIndex)\n hasChanged = true\n }\n this.state.scanIndex++\n } else {\n break\n }\n }\n\n if (!hasChanged) {\n return\n }\n\n this.active.lines = newActiveLines\n this.active.index = newActiveIndex\n this.event.emit('linesUpdate', [...this.active.lines], [...this.active.index], this.handleGetActiveIndex())\n }\n\n private onTick = () => {\n if (!this.state.playing) {\n return\n }\n\n const now = this.handleGetCurrentTime()\n this.handleUpdateActiveLines(now)\n\n this.state.frameId = requestAnimationFrame(this.onTick)\n }\n\n updateLyric(info: Info) {\n if (!info) {\n return\n }\n\n this.pause()\n this.info = info\n\n this.active.lines = []\n this.active.index = []\n\n this.state.scanIndex = 0\n this.time.seek = 0\n\n this.event.emit('lyricUpdate', info)\n this.event.emit('linesUpdate', [], [], -1)\n }\n\n play(time?: number) {\n this.pause()\n\n if (typeof time === 'number' && !Number.isNaN(time)) {\n this.time.seek = time\n this.handleSyncTime(time)\n }\n\n this.time.start = performance.now()\n this.state.playing = true\n this.onTick()\n\n this.event.emit('play', this.handleGetCurrentTime())\n }\n\n pause() {\n if (this.state.playing) {\n this.time.seek = this.handleGetCurrentTime()\n this.state.playing = false\n }\n if (this.state.frameId !== null) {\n cancelAnimationFrame(this.state.frameId)\n this.state.frameId = null\n }\n\n this.event.emit('pause', this.handleGetCurrentTime())\n }\n\n dispose() {\n this.pause()\n this.event.clear()\n\n this.active.lines = []\n this.active.index = []\n\n this.info = new Info()\n }\n\n get currentLines() {\n return [...this.active.lines]\n }\n\n get currentIndex() {\n return [...this.active.index]\n }\n\n get currentActive() {\n return this.handleGetActiveIndex()\n }\n\n get currentInfo() {\n return this.info\n }\n\n get currentTime() {\n return this.handleGetCurrentTime()\n }\n}\n"],"names":["DEFAULT_OPTIONS","m","A","BaseLyricPlayer","ConfigManager","Event","Info","index","line","nextLine","time","lines","firstIndex","i","now","hasChanged","newActiveLines","newActiveIndex","infoIndex","info"],"mappings":"kJAEaA,EAA2B,CAAA,ECmMlCC,EAAI,QAEV,MAAMC,CAAE,CACN,IAAI,SAAU,CACZ,OAAOD,CACT,CACA,KAAO,YACP,MAAQ,CAAA,EACR,MAAQ,CAAA,EACR,OAAS,CAAA,EACT,QAAS,CACP,MAAO,CACL,QAAS,KAAK,QACd,KAAM,KAAK,KACX,MAAO,KAAK,MACZ,MAAO,KAAK,MACZ,OAAQ,KAAK,MACnB,CACE,CACF,CCnLO,MAAME,CAAgB,CAClB,OAAS,IAAIC,EAAAA,cAA6CJ,CAAe,EAEzE,MAAwC,IAAIK,EAAAA,MAE7C,MAKA,OAIA,KAIA,KAER,aAAc,CACZ,KAAK,MAAQ,CACX,QAAS,GACT,QAAS,KACT,UAAW,CAAA,EAEb,KAAK,OAAS,CACZ,MAAO,CAAA,EACP,MAAO,CAAA,CAAC,EAEV,KAAK,KAAO,CACV,MAAO,EACP,KAAM,CAAA,EAER,KAAK,KAAO,IAAIC,CAClB,CAEQ,sBAAuB,CAC7B,OAAK,KAAK,MAAM,QAGT,KAAK,KAAK,MAAQ,YAAY,MAAQ,KAAK,KAAK,OAF9C,KAAK,KAAK,IAGrB,CAEQ,kBAAkBC,EAAuB,CAC/C,GAAIA,EAAQ,GAAKA,GAAS,KAAK,KAAK,MAAM,OACxC,MAAO,GAGT,GAAIA,IAAU,KAAK,KAAK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AK,qBAAA,EACtB,KAAK,MAAM,QAAU,IAEnB,KAAK,MAAM,UAAY,OACzB,qBAAqB,KAAK,MAAM,OAAO,EACvC,KAAK,MAAM,QAAU,MAGvB,KAAK,MAAM,KAAK,QAAS,KAAK,sBAAsB,CACtD,CAEA,SAAU,CACR,KAAK,MAAA,EACL,KAAK,MAAM,MAAA,EAEX,KAAK,OAAO,MAAQ,CAAA,EACpB,KAAK,OAAO,MAAQ,CAAA,EAEpB,KAAK,KAAO,IAAIJ,CAClB,CAEA,IAAI,cAAe,CACjB,MAAO,CAAC,GAAG,KAAK,OAAO,KAAK,CAC9B,CAEA,IAAI,cAAe,CACjB,MAAO,CAAC,GAAG,KAAK,OAAO,KAAK,CAC9B,CAEA,IAAI,eAAgB,CAClB,OAAO,KAAK,qBAAA,CACd,CAEA,IAAI,aAAc,CAChB,OAAO,KAAK,IACd,CAEA,IAAI,aAAc,CAChB,OAAO,KAAK,qBAAA,CACd,CACF","x_google_ignoreList":[1]}
@@ -7,11 +7,13 @@ export declare class BaseLyricPlayer {
7
7
  readonly config: ConfigManager<Options, {}, never>;
8
8
  readonly event: Event_2<BaseLyricPlayerEventMap>;
9
9
  private state;
10
+ private active;
10
11
  private time;
11
12
  private info;
12
13
  constructor();
13
14
  private handleGetCurrentTime;
14
15
  private handleGetLineTime;
16
+ private handleGetActiveIndex;
15
17
  private handleSyncTime;
16
18
  private handleUpdateActiveLines;
17
19
  private onTick;
@@ -20,6 +22,8 @@ export declare class BaseLyricPlayer {
20
22
  pause(): void;
21
23
  dispose(): void;
22
24
  get currentLines(): Line[];
25
+ get currentIndex(): number[];
26
+ get currentActive(): number;
23
27
  get currentInfo(): Info;
24
28
  get currentTime(): number;
25
29
  }
@@ -43,8 +47,10 @@ export declare interface BaseLyricPlayerEventMap {
43
47
  /**
44
48
  * When the currently active lyric lines change during playback.
45
49
  * @param lines An array of the currently active lyric lines.
50
+ * @param indexs An array of the currently active lyric lines' indexes.
51
+ * @param firstIndex The index of the first currently active lyric line (-1 if none).
46
52
  */
47
- linesUpdate: (lines: Line[]) => void;
53
+ linesUpdate: (lines: Line[], indexs: number[], index: number) => void;
48
54
  }
49
55
 
50
56
  declare interface Options {
@@ -1,9 +1,9 @@
1
1
  import "lodash-es";
2
- import { ConfigManager as r, Event as l } from "@music-lyric-player/utils";
3
- const o = {}, f = "0.5.1";
4
- class a {
2
+ import { ConfigManager as c, Event as d } from "@music-lyric-player/utils";
3
+ const o = {}, m = "0.5.1";
4
+ class r {
5
5
  get version() {
6
- return f;
6
+ return m;
7
7
  }
8
8
  type = "Incorrect";
9
9
  metas = [];
@@ -19,22 +19,25 @@ class a {
19
19
  };
20
20
  }
21
21
  }
22
- class c {
23
- config = new r(o);
24
- event = new l();
22
+ class v {
23
+ config = new c(o);
24
+ event = new d();
25
25
  state;
26
+ active;
26
27
  time;
27
28
  info;
28
29
  constructor() {
29
30
  this.state = {
30
31
  playing: !1,
31
32
  frameId: null,
32
- index: 0,
33
- lines: []
33
+ scanIndex: 0
34
+ }, this.active = {
35
+ lines: [],
36
+ index: []
34
37
  }, this.time = {
35
38
  start: 0,
36
39
  seek: 0
37
- }, this.info = new a();
40
+ }, this.info = new r();
38
41
  }
39
42
  handleGetCurrentTime() {
40
43
  return this.state.playing ? this.time.seek + (performance.now() - this.time.start) : this.time.seek;
@@ -47,33 +50,37 @@ class c {
47
50
  const i = this.info.lines[e], s = this.info.lines[e + 1];
48
51
  return Math.max(i.time.end, s.time.start);
49
52
  }
53
+ handleGetActiveIndex() {
54
+ return this.active.index.length > 0 ? this.active.index[0] : -1;
55
+ }
50
56
  handleSyncTime(e) {
51
- const i = [];
52
- let s = this.info.lines.length;
57
+ const i = [], s = [];
58
+ let n = this.info.lines.length;
53
59
  for (let t = 0; t < this.info.lines.length; t++) {
54
- const n = this.info.lines[t];
55
- if (n.time.start > e) {
56
- s = t;
60
+ const a = this.info.lines[t];
61
+ if (a.time.start > e) {
62
+ n = t;
57
63
  break;
58
64
  }
59
- this.handleGetLineTime(t) > e && i.push(n);
65
+ this.handleGetLineTime(t) > e && (i.push(a), s.push(t));
60
66
  }
61
- this.state.lines = i, this.state.index = s, this.event.emit("linesUpdate", [...this.state.lines]);
67
+ this.state.scanIndex = n, this.active.lines = i, this.active.index = s, this.event.emit("linesUpdate", [...this.active.lines], [...this.active.index], this.handleGetActiveIndex());
62
68
  }
63
69
  handleUpdateActiveLines(e) {
64
70
  let i = !1;
65
- const s = this.state.lines.filter((t) => {
66
- const n = this.info.lines.indexOf(t);
67
- return e >= this.handleGetLineTime(n) ? (i = !0, !1) : !0;
68
- });
69
- for (; this.state.index < this.info.lines.length; ) {
70
- const t = this.info.lines[this.state.index];
71
+ const s = [], n = [];
72
+ for (let t = 0; t < this.active.lines.length; t++) {
73
+ const a = this.active.lines[t], h = this.active.index[t];
74
+ e >= this.handleGetLineTime(h) ? i = !0 : (s.push(a), n.push(h));
75
+ }
76
+ for (; this.state.scanIndex < this.info.lines.length; ) {
77
+ const t = this.info.lines[this.state.scanIndex];
71
78
  if (e >= t.time.start)
72
- e < this.handleGetLineTime(this.state.index) && (s.push(t), i = !0), this.state.index++;
79
+ e < this.handleGetLineTime(this.state.scanIndex) && (s.push(t), n.push(this.state.scanIndex), i = !0), this.state.scanIndex++;
73
80
  else
74
81
  break;
75
82
  }
76
- i && (this.state.lines = s, this.event.emit("linesUpdate", [...this.state.lines]));
83
+ i && (this.active.lines = s, this.active.index = n, this.event.emit("linesUpdate", [...this.active.lines], [...this.active.index], this.handleGetActiveIndex()));
77
84
  }
78
85
  onTick = () => {
79
86
  if (!this.state.playing)
@@ -82,7 +89,7 @@ class c {
82
89
  this.handleUpdateActiveLines(e), this.state.frameId = requestAnimationFrame(this.onTick);
83
90
  };
84
91
  updateLyric(e) {
85
- this.pause(), this.info = e, this.state.index = 0, this.state.lines = [], this.time.seek = 0, this.event.emit("lyricUpdate", e), this.event.emit("linesUpdate", []);
92
+ e && (this.pause(), this.info = e, this.active.lines = [], this.active.index = [], this.state.scanIndex = 0, this.time.seek = 0, this.event.emit("lyricUpdate", e), this.event.emit("linesUpdate", [], [], -1));
86
93
  }
87
94
  play(e) {
88
95
  this.pause(), typeof e == "number" && !Number.isNaN(e) && (this.time.seek = e, this.handleSyncTime(e)), this.time.start = performance.now(), this.state.playing = !0, this.onTick(), this.event.emit("play", this.handleGetCurrentTime());
@@ -91,10 +98,16 @@ class c {
91
98
  this.state.playing && (this.time.seek = this.handleGetCurrentTime(), this.state.playing = !1), this.state.frameId !== null && (cancelAnimationFrame(this.state.frameId), this.state.frameId = null), this.event.emit("pause", this.handleGetCurrentTime());
92
99
  }
93
100
  dispose() {
94
- this.pause(), this.event.clear(), this.state.lines = [], this.info = new a();
101
+ this.pause(), this.event.clear(), this.active.lines = [], this.active.index = [], this.info = new r();
95
102
  }
96
103
  get currentLines() {
97
- return [...this.state.lines];
104
+ return [...this.active.lines];
105
+ }
106
+ get currentIndex() {
107
+ return [...this.active.index];
108
+ }
109
+ get currentActive() {
110
+ return this.handleGetActiveIndex();
98
111
  }
99
112
  get currentInfo() {
100
113
  return this.info;
@@ -104,6 +117,6 @@ class c {
104
117
  }
105
118
  }
106
119
  export {
107
- c as BaseLyricPlayer
120
+ v as BaseLyricPlayer
108
121
  };
109
122
  //# sourceMappingURL=index.ecma.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.ecma.js","sources":["../src/options.ts","../../../node_modules/.pnpm/@music-lyric-kit+lyric@0.4.0/node_modules/@music-lyric-kit/lyric/dist/index.ecma.js","../src/index.ts"],"sourcesContent":["export interface Options {}\n\nexport const DEFAULT_OPTIONS: Options = {}\n","import { createRandomHex as r } from \"@music-lyric-kit/utils\";\nclass s {\n start = 0;\n end = 0;\n get duration() {\n return this.end - this.start;\n }\n toJSON() {\n return {\n start: this.start,\n end: this.end,\n duration: this.duration\n };\n }\n}\nvar o = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Space = \"Space\", t))(o || {});\nclass i {\n stress = !1;\n toJSON() {\n return {\n stress: this.stress\n };\n }\n}\nclass p {\n get type() {\n return \"Normal\";\n }\n time = new s();\n content = \"\";\n extended = [];\n config = new i();\n toJSON() {\n return {\n type: this.type,\n time: this.time,\n content: this.content,\n extended: this.extended,\n config: this.config\n };\n }\n}\nclass N {\n get type() {\n return \"Space\";\n }\n count = 1;\n toJSON() {\n return {\n type: this.type,\n count: this.count\n };\n }\n}\nvar a = /* @__PURE__ */ ((t) => (t.UnKnown = \"UnKnown\", t.Translate = \"Translate\", t.Roman = \"Roman\", t))(a || {});\nclass S {\n type = \"UnKnown\";\n content = \"\";\n toJSON() {\n return {\n type: this.type,\n content: this.content\n };\n }\n}\nvar l = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Interlude = \"Interlude\", t))(l || {});\nclass x {\n get type() {\n return \"Interlude\";\n }\n id = r(4).toUpperCase();\n time = new s();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time\n };\n }\n}\nclass c {\n words = [];\n extended = [];\n get original() {\n return this.words.map((n) => n.type === o.Normal ? n.content : new Array(n.count).fill(\" \").join(\"\")).join(\"\");\n }\n toJSON() {\n return {\n words: this.words,\n extended: this.extended,\n original: this.original\n };\n }\n}\nclass y {\n get type() {\n return \"Normal\";\n }\n id = r(4).toUpperCase();\n time = new s();\n content = new c();\n agent;\n background;\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time,\n content: this.content,\n agent: this.agent,\n background: this.background\n };\n }\n}\nvar u = /* @__PURE__ */ ((t) => (t.Offset = \"Offset\", t.Duration = \"Duration\", t.Title = \"Title\", t.Singer = \"Singer\", t.Album = \"Album\", t.Creator = \"Creator\", t.UnKnown = \"UnKnown\", t))(u || {});\nclass e {\n id = r(4).toUpperCase();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n content: this.content\n };\n }\n}\nclass f extends e {\n get type() {\n return \"Offset\";\n }\n content = 0;\n}\nclass w extends e {\n get type() {\n return \"Duration\";\n }\n content = 0;\n}\nclass O extends e {\n get type() {\n return \"Title\";\n }\n content = \"\";\n}\nclass b extends e {\n get type() {\n return \"Singer\";\n }\n content = \"\";\n}\nclass J extends e {\n get type() {\n return \"Album\";\n }\n content = \"\";\n}\nclass U extends e {\n get type() {\n return \"Creator\";\n }\n content = {\n role: \"\",\n name: []\n };\n}\nclass C extends e {\n get type() {\n return \"UnKnown\";\n }\n content = \"\";\n}\nclass I {\n id = \"\";\n name = \"\";\n count = 0;\n}\nclass d {\n // index in global\n global = 0;\n // index in block\n block = 0;\n toJSON() {\n return {\n global: this.global,\n block: this.block\n };\n }\n}\nclass v {\n id = \"\";\n index = new d();\n toJSON() {\n return {\n id: this.id,\n index: this.index\n };\n }\n}\nconst m = \"0.5.1\";\nvar g = /* @__PURE__ */ ((t) => (t.Incorrect = \"Incorrect\", t.Normal = \"Normal\", t.Syllable = \"Syllable\", t.Pure = \"Pure\", t.Empty = \"Empty\", t.NoTime = \"NoTime\", t))(g || {});\nclass A {\n get version() {\n return m;\n }\n type = \"Incorrect\";\n metas = [];\n lines = [];\n agents = [];\n toJSON() {\n return {\n version: this.version,\n type: this.type,\n metas: this.metas,\n lines: this.lines,\n agents: this.agents\n };\n }\n}\nexport {\n I as Agent,\n v as AgentLine,\n d as AgentLineIndex,\n S as Extended,\n a as ExtendedType,\n A as Info,\n x as LineInterlude,\n y as LineNormal,\n c as LineNormalContent,\n l as LineType,\n J as MetaAlbum,\n U as MetaCreator,\n w as MetaDuration,\n f as MetaOffset,\n b as MetaSinger,\n O as MetaTitle,\n u as MetaType,\n C as MetaUnKnown,\n s as Time,\n g as Type,\n m as Version,\n p as WordNormal,\n N as WordSpace,\n o as WordType\n};\n//# sourceMappingURL=index.ecma.js.map\n","import type { Line } from '@music-lyric-kit/lyric'\nimport type { DeepPartial } from '@music-lyric-player/utils'\nimport type { Options } from './options'\n\nimport { DEFAULT_OPTIONS } from './options'\n\nimport { Info } from '@music-lyric-kit/lyric'\nimport { ConfigManager, Event } from '@music-lyric-player/utils'\n\nexport interface BaseLyricPlayerEventMap {\n /**\n * When the player starts or resumes playback.\n * @param currentTime The current playback time.\n */\n play: (currentTime: number) => void\n\n /**\n * When the player pauses playback.\n * @param currentTime The current playback time.\n */\n pause: (currentTime: number) => void\n\n /**\n * When the entire lyric information is updated (e.g., loading a new lyric/song).\n * @param info The newly loaded lyric information object.\n */\n lyricUpdate: (info: Info) => void\n\n /**\n * When the currently active lyric lines change during playback.\n * @param lines An array of the currently active lyric lines.\n */\n linesUpdate: (lines: Line[]) => void\n}\n\nexport class BaseLyricPlayer {\n readonly config = new ConfigManager<Options, DeepPartial<Options>>(DEFAULT_OPTIONS)\n\n readonly event: Event<BaseLyricPlayerEventMap> = new Event()\n\n private state: {\n playing: boolean\n frameId: number | null\n index: number\n lines: Line[]\n }\n private time: {\n start: number\n seek: number\n }\n private info: Info\n\n constructor() {\n this.state = {\n playing: false,\n frameId: null,\n index: 0,\n lines: [],\n }\n this.time = {\n start: 0,\n seek: 0,\n }\n this.info = new Info()\n }\n\n private handleGetCurrentTime() {\n if (!this.state.playing) {\n return this.time.seek\n }\n return this.time.seek + (performance.now() - this.time.start)\n }\n\n private handleGetLineTime(index: number): number {\n if (index < 0 || index >= this.info.lines.length) {\n return 0\n }\n\n if (index === this.info.lines.length - 1) {\n return Infinity\n }\n\n const line = this.info.lines[index]\n const nextLine = this.info.lines[index + 1]\n return Math.max(line.time.end, nextLine.time.start)\n }\n\n private handleSyncTime(time: number) {\n const result: Line[] = []\n\n let firstIndex = this.info.lines.length\n for (let i = 0; i < this.info.lines.length; i++) {\n const line = this.info.lines[i]\n if (line.time.start > time) {\n firstIndex = i\n break\n }\n\n if (this.handleGetLineTime(i) > time) {\n result.push(line)\n }\n }\n\n this.state.lines = result\n this.state.index = firstIndex\n\n this.event.emit('linesUpdate', [...this.state.lines])\n }\n\n private handleUpdateActiveLines(now: number) {\n let hasChanged = false\n\n const activeLines = this.state.lines.filter((line) => {\n const infoIndex = this.info.lines.indexOf(line)\n if (now >= this.handleGetLineTime(infoIndex)) {\n hasChanged = true\n return false\n }\n return true\n })\n\n while (this.state.index < this.info.lines.length) {\n const nextLine = this.info.lines[this.state.index]\n\n if (now >= nextLine.time.start) {\n if (now < this.handleGetLineTime(this.state.index)) {\n activeLines.push(nextLine)\n hasChanged = true\n }\n this.state.index++\n } else {\n break\n }\n }\n\n if (hasChanged) {\n this.state.lines = activeLines\n this.event.emit('linesUpdate', [...this.state.lines])\n }\n }\n\n private onTick = () => {\n if (!this.state.playing) {\n return\n }\n\n const now = this.handleGetCurrentTime()\n this.handleUpdateActiveLines(now)\n\n this.state.frameId = requestAnimationFrame(this.onTick)\n }\n\n updateLyric(info: Info) {\n this.pause()\n this.info = info\n\n this.state.index = 0\n this.state.lines = []\n this.time.seek = 0\n\n this.event.emit('lyricUpdate', info)\n this.event.emit('linesUpdate', [])\n }\n\n play(time?: number) {\n this.pause()\n\n if (typeof time === 'number' && !Number.isNaN(time)) {\n this.time.seek = time\n this.handleSyncTime(time)\n }\n\n this.time.start = performance.now()\n this.state.playing = true\n this.onTick()\n\n this.event.emit('play', this.handleGetCurrentTime())\n }\n\n pause() {\n if (this.state.playing) {\n this.time.seek = this.handleGetCurrentTime()\n this.state.playing = false\n }\n if (this.state.frameId !== null) {\n cancelAnimationFrame(this.state.frameId)\n this.state.frameId = null\n }\n\n this.event.emit('pause', this.handleGetCurrentTime())\n }\n\n dispose() {\n this.pause()\n this.event.clear()\n this.state.lines = []\n this.info = new Info()\n }\n\n get currentLines() {\n return [...this.state.lines]\n }\n\n get currentInfo() {\n return this.info\n }\n\n get currentTime() {\n return this.handleGetCurrentTime()\n }\n}\n"],"names":["DEFAULT_OPTIONS","m","A","BaseLyricPlayer","ConfigManager","Event","Info","index","line","nextLine","time","result","firstIndex","i","now","hasChanged","activeLines","infoIndex","info"],"mappings":";;AAEO,MAAMA,IAA2B,CAAA,GCmMlCC,IAAI;AAEV,MAAMC,EAAE;AAAA,EACN,IAAI,UAAU;AACZ,WAAOD;AAAA,EACT;AAAA,EACA,OAAO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Q,CAAA,GACnB,KAAK,KAAK,OAAO,GAEjB,KAAK,MAAM,KAAK,eAAeA,CAAI,GACnC,KAAK,MAAM,KAAK,eAAe,CAAA,CAAE;AAAA,EACnC;AAAA,EAEA,KAAKR,GAAe;AAClB,SAAK,MAAA,GAED,OAAOA,KAAS,YAAY,CAAC,OAAO,MAAMA,CAAI,MAChD,KAAK,KAAK,OAAOA,GACjB,KAAK,eAAeA,CAAI,IAG1B,KAAK,KAAK,QAAQ,YAAY,IAAA,GAC9B,KAAK,MAAM,UAAU,IACrB,KAAK,OAAA,GAEL,KAAK,MAAM,KAAK,QAAQ,KAAK,sBAAsB;AAAA,EACrD;AAAA,EAEA,QAAQ;AACN,IAAI,KAAK,MAAM,YACb,KAAK,KAAK,OAAO,KAAK,qBAAA,GACtB,KAAK,MAAM,UAAU,KAEnB,KAAK,MAAM,YAAY,SACzB,qBAAqB,KAAK,MAAM,OAAO,GACvC,KAAK,MAAM,UAAU,OAGvB,KAAK,MAAM,KAAK,SAAS,KAAK,sBAAsB;AAAA,EACtD;AAAA,EAEA,UAAU;AACR,SAAK,MAAA,GACL,KAAK,MAAM,MAAA,GACX,KAAK,MAAM,QAAQ,CAAA,GACnB,KAAK,OAAO,IAAIJ,EAAA;AAAA,EAClB;AAAA,EAEA,IAAI,eAAe;AACjB,WAAO,CAAC,GAAG,KAAK,MAAM,KAAK;AAAA,EAC7B;AAAA,EAEA,IAAI,cAAc;AAChB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,cAAc;AAChB,WAAO,KAAK,qBAAA;AAAA,EACd;AACF;","x_google_ignoreList":[1]}
1
+ {"version":3,"file":"index.ecma.js","sources":["../src/options.ts","../../../node_modules/.pnpm/@music-lyric-kit+lyric@0.4.0/node_modules/@music-lyric-kit/lyric/dist/index.ecma.js","../src/index.ts"],"sourcesContent":["export interface Options {}\n\nexport const DEFAULT_OPTIONS: Options = {}\n","import { createRandomHex as r } from \"@music-lyric-kit/utils\";\nclass s {\n start = 0;\n end = 0;\n get duration() {\n return this.end - this.start;\n }\n toJSON() {\n return {\n start: this.start,\n end: this.end,\n duration: this.duration\n };\n }\n}\nvar o = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Space = \"Space\", t))(o || {});\nclass i {\n stress = !1;\n toJSON() {\n return {\n stress: this.stress\n };\n }\n}\nclass p {\n get type() {\n return \"Normal\";\n }\n time = new s();\n content = \"\";\n extended = [];\n config = new i();\n toJSON() {\n return {\n type: this.type,\n time: this.time,\n content: this.content,\n extended: this.extended,\n config: this.config\n };\n }\n}\nclass N {\n get type() {\n return \"Space\";\n }\n count = 1;\n toJSON() {\n return {\n type: this.type,\n count: this.count\n };\n }\n}\nvar a = /* @__PURE__ */ ((t) => (t.UnKnown = \"UnKnown\", t.Translate = \"Translate\", t.Roman = \"Roman\", t))(a || {});\nclass S {\n type = \"UnKnown\";\n content = \"\";\n toJSON() {\n return {\n type: this.type,\n content: this.content\n };\n }\n}\nvar l = /* @__PURE__ */ ((t) => (t.Normal = \"Normal\", t.Interlude = \"Interlude\", t))(l || {});\nclass x {\n get type() {\n return \"Interlude\";\n }\n id = r(4).toUpperCase();\n time = new s();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time\n };\n }\n}\nclass c {\n words = [];\n extended = [];\n get original() {\n return this.words.map((n) => n.type === o.Normal ? n.content : new Array(n.count).fill(\" \").join(\"\")).join(\"\");\n }\n toJSON() {\n return {\n words: this.words,\n extended: this.extended,\n original: this.original\n };\n }\n}\nclass y {\n get type() {\n return \"Normal\";\n }\n id = r(4).toUpperCase();\n time = new s();\n content = new c();\n agent;\n background;\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n time: this.time,\n content: this.content,\n agent: this.agent,\n background: this.background\n };\n }\n}\nvar u = /* @__PURE__ */ ((t) => (t.Offset = \"Offset\", t.Duration = \"Duration\", t.Title = \"Title\", t.Singer = \"Singer\", t.Album = \"Album\", t.Creator = \"Creator\", t.UnKnown = \"UnKnown\", t))(u || {});\nclass e {\n id = r(4).toUpperCase();\n toJSON() {\n return {\n id: this.id,\n type: this.type,\n content: this.content\n };\n }\n}\nclass f extends e {\n get type() {\n return \"Offset\";\n }\n content = 0;\n}\nclass w extends e {\n get type() {\n return \"Duration\";\n }\n content = 0;\n}\nclass O extends e {\n get type() {\n return \"Title\";\n }\n content = \"\";\n}\nclass b extends e {\n get type() {\n return \"Singer\";\n }\n content = \"\";\n}\nclass J extends e {\n get type() {\n return \"Album\";\n }\n content = \"\";\n}\nclass U extends e {\n get type() {\n return \"Creator\";\n }\n content = {\n role: \"\",\n name: []\n };\n}\nclass C extends e {\n get type() {\n return \"UnKnown\";\n }\n content = \"\";\n}\nclass I {\n id = \"\";\n name = \"\";\n count = 0;\n}\nclass d {\n // index in global\n global = 0;\n // index in block\n block = 0;\n toJSON() {\n return {\n global: this.global,\n block: this.block\n };\n }\n}\nclass v {\n id = \"\";\n index = new d();\n toJSON() {\n return {\n id: this.id,\n index: this.index\n };\n }\n}\nconst m = \"0.5.1\";\nvar g = /* @__PURE__ */ ((t) => (t.Incorrect = \"Incorrect\", t.Normal = \"Normal\", t.Syllable = \"Syllable\", t.Pure = \"Pure\", t.Empty = \"Empty\", t.NoTime = \"NoTime\", t))(g || {});\nclass A {\n get version() {\n return m;\n }\n type = \"Incorrect\";\n metas = [];\n lines = [];\n agents = [];\n toJSON() {\n return {\n version: this.version,\n type: this.type,\n metas: this.metas,\n lines: this.lines,\n agents: this.agents\n };\n }\n}\nexport {\n I as Agent,\n v as AgentLine,\n d as AgentLineIndex,\n S as Extended,\n a as ExtendedType,\n A as Info,\n x as LineInterlude,\n y as LineNormal,\n c as LineNormalContent,\n l as LineType,\n J as MetaAlbum,\n U as MetaCreator,\n w as MetaDuration,\n f as MetaOffset,\n b as MetaSinger,\n O as MetaTitle,\n u as MetaType,\n C as MetaUnKnown,\n s as Time,\n g as Type,\n m as Version,\n p as WordNormal,\n N as WordSpace,\n o as WordType\n};\n//# sourceMappingURL=index.ecma.js.map\n","import type { Line } from '@music-lyric-kit/lyric'\nimport type { DeepPartial } from '@music-lyric-player/utils'\nimport type { Options } from './options'\n\nimport { DEFAULT_OPTIONS } from './options'\n\nimport { Info } from '@music-lyric-kit/lyric'\nimport { ConfigManager, Event } from '@music-lyric-player/utils'\n\nexport interface BaseLyricPlayerEventMap {\n /**\n * When the player starts or resumes playback.\n * @param currentTime The current playback time.\n */\n play: (currentTime: number) => void\n\n /**\n * When the player pauses playback.\n * @param currentTime The current playback time.\n */\n pause: (currentTime: number) => void\n\n /**\n * When the entire lyric information is updated (e.g., loading a new lyric/song).\n * @param info The newly loaded lyric information object.\n */\n lyricUpdate: (info: Info) => void\n\n /**\n * When the currently active lyric lines change during playback.\n * @param lines An array of the currently active lyric lines.\n * @param indexs An array of the currently active lyric lines' indexes.\n * @param firstIndex The index of the first currently active lyric line (-1 if none).\n */\n linesUpdate: (lines: Line[], indexs: number[], index: number) => void\n}\n\nexport class BaseLyricPlayer {\n readonly config = new ConfigManager<Options, DeepPartial<Options>>(DEFAULT_OPTIONS)\n\n readonly event: Event<BaseLyricPlayerEventMap> = new Event()\n\n private state: {\n playing: boolean\n frameId: number | null\n scanIndex: number\n }\n private active: {\n lines: Line[]\n index: number[]\n }\n private time: {\n start: number\n seek: number\n }\n private info: Info\n\n constructor() {\n this.state = {\n playing: false,\n frameId: null,\n scanIndex: 0,\n }\n this.active = {\n lines: [],\n index: [],\n }\n this.time = {\n start: 0,\n seek: 0,\n }\n this.info = new Info()\n }\n\n private handleGetCurrentTime() {\n if (!this.state.playing) {\n return this.time.seek\n }\n return this.time.seek + (performance.now() - this.time.start)\n }\n\n private handleGetLineTime(index: number): number {\n if (index < 0 || index >= this.info.lines.length) {\n return 0\n }\n\n if (index === this.info.lines.length - 1) {\n return Infinity\n }\n\n const line = this.info.lines[index]\n const nextLine = this.info.lines[index + 1]\n return Math.max(line.time.end, nextLine.time.start)\n }\n\n private handleGetActiveIndex() {\n return this.active.index.length > 0 ? this.active.index[0] : -1\n }\n\n private handleSyncTime(time: number) {\n const lines: Line[] = []\n const index: number[] = []\n\n let firstIndex = this.info.lines.length\n for (let i = 0; i < this.info.lines.length; i++) {\n const line = this.info.lines[i]\n if (line.time.start > time) {\n firstIndex = i\n break\n }\n\n if (this.handleGetLineTime(i) > time) {\n lines.push(line)\n index.push(i) // 保证 lines 和 index 同步推入\n }\n }\n\n this.state.scanIndex = firstIndex\n\n this.active.lines = lines\n this.active.index = index\n\n this.event.emit('linesUpdate', [...this.active.lines], [...this.active.index], this.handleGetActiveIndex())\n }\n\n private handleUpdateActiveLines(now: number) {\n let hasChanged = false\n\n const newActiveLines: Line[] = []\n const newActiveIndex: number[] = []\n\n for (let i = 0; i < this.active.lines.length; i++) {\n const line = this.active.lines[i]\n const infoIndex = this.active.index[i]\n\n if (now >= this.handleGetLineTime(infoIndex)) {\n hasChanged = true\n } else {\n newActiveLines.push(line)\n newActiveIndex.push(infoIndex)\n }\n }\n\n while (this.state.scanIndex < this.info.lines.length) {\n const nextLine = this.info.lines[this.state.scanIndex]\n if (now >= nextLine.time.start) {\n if (now < this.handleGetLineTime(this.state.scanIndex)) {\n newActiveLines.push(nextLine)\n newActiveIndex.push(this.state.scanIndex)\n hasChanged = true\n }\n this.state.scanIndex++\n } else {\n break\n }\n }\n\n if (!hasChanged) {\n return\n }\n\n this.active.lines = newActiveLines\n this.active.index = newActiveIndex\n this.event.emit('linesUpdate', [...this.active.lines], [...this.active.index], this.handleGetActiveIndex())\n }\n\n private onTick = () => {\n if (!this.state.playing) {\n return\n }\n\n const now = this.handleGetCurrentTime()\n this.handleUpdateActiveLines(now)\n\n this.state.frameId = requestAnimationFrame(this.onTick)\n }\n\n updateLyric(info: Info) {\n if (!info) {\n return\n }\n\n this.pause()\n this.info = info\n\n this.active.lines = []\n this.active.index = []\n\n this.state.scanIndex = 0\n this.time.seek = 0\n\n this.event.emit('lyricUpdate', info)\n this.event.emit('linesUpdate', [], [], -1)\n }\n\n play(time?: number) {\n this.pause()\n\n if (typeof time === 'number' && !Number.isNaN(time)) {\n this.time.seek = time\n this.handleSyncTime(time)\n }\n\n this.time.start = performance.now()\n this.state.playing = true\n this.onTick()\n\n this.event.emit('play', this.handleGetCurrentTime())\n }\n\n pause() {\n if (this.state.playing) {\n this.time.seek = this.handleGetCurrentTime()\n this.state.playing = false\n }\n if (this.state.frameId !== null) {\n cancelAnimationFrame(this.state.frameId)\n this.state.frameId = null\n }\n\n this.event.emit('pause', this.handleGetCurrentTime())\n }\n\n dispose() {\n this.pause()\n this.event.clear()\n\n this.active.lines = []\n this.active.index = []\n\n this.info = new Info()\n }\n\n get currentLines() {\n return [...this.active.lines]\n }\n\n get currentIndex() {\n return [...this.active.index]\n }\n\n get currentActive() {\n return this.handleGetActiveIndex()\n }\n\n get currentInfo() {\n return this.info\n }\n\n get currentTime() {\n return this.handleGetCurrentTime()\n }\n}\n"],"names":["DEFAULT_OPTIONS","A","BaseLyricPlayer","ConfigManager","Event","Info","index","line","nextLine","time","lines","firstIndex","i","now","hasChanged","newActiveLines","newActiveIndex","infoIndex","info"],"mappings":";;AAEO,MAAMA,IAA2B,CAAA,GCmMlC,IAAI;AAEV,MAAMC,EAAE;AAAA,EACN,IAAI,UAAU;AACZ,WAAO;AAAA,EACT;AAAA,EACA,OAAO;AAAA,EACP,QAAQ,CAAA;AAAA,EACR,QAAQ,CAAA;AAAA,EACR,SAAS,CAAA;AAAA,EACT,SAAS;AACP,WAAO;AAAA,MACL,SAAS,KAAK;AAAA,MACd,MAAM,KAAK;AAAA,MACX,OAAO,KAAK;AAAA,MACZ,OAAO,KAAK;AAAA,MACZ,QAAQ,KAAK;AAAA,IACnB;AAAA,EACE;AACF;ACnLO,MAAMC,EAAgB;AAAA,EAClB,SAAS,IAAIC,EAA6CH,CAAe;AAAA,EAEzE,QAAwC,IAAII,EAAA;AAAA,EAE7C;AAAA,EAKA;AAAA,EAIA;AAAA,EAIA;AAAA,EAER,cAAc;AACZ,SAAK,QAAQ;AAAA,MACX,SAAS;AAA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KAAK;AAAA,EACd;AAAA,EAEA,IAAI,cAAc;AAChB,WAAO,KAAK,qBAAA;AAAA,EACd;AACF;","x_google_ignoreList":[1]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@music-lyric-player/base",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "author": "folltoshe",
5
5
  "type": "module",
6
6
  "description": "Music Lyric Player - Base Player",
@@ -32,7 +32,7 @@
32
32
  "@music-lyric-kit/lyric": "^0.4.0"
33
33
  },
34
34
  "dependencies": {
35
- "@music-lyric-player/utils": "^0.1.0"
35
+ "@music-lyric-player/utils": "^0.2.0"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "vite build"