lrc-audio-player 0.1.0 → 0.1.1

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.
package/dist/index.cjs CHANGED
@@ -95,7 +95,9 @@ function parseLRC(lrc) {
95
95
  let rest = trimmed;
96
96
  let leadingMatch;
97
97
  while (leadingMatch = rest.match(/^\[(\d{1,2}):(\d{2})(?:[.:](\d{1,3}))?\]/)) {
98
- times.push(timeToSeconds(leadingMatch[1], leadingMatch[2], leadingMatch[3]));
98
+ times.push(
99
+ timeToSeconds(leadingMatch[1], leadingMatch[2], leadingMatch[3])
100
+ );
99
101
  rest = rest.slice(leadingMatch[0].length);
100
102
  }
101
103
  if (times.length === 0) {
@@ -106,17 +108,6 @@ function parseLRC(lrc) {
106
108
  lines.push({ time, text, tokens });
107
109
  }
108
110
  }
109
- const offsetSeconds = (metadata.offset ?? 0) / 1e3;
110
- if (offsetSeconds) {
111
- for (const line of lines) {
112
- line.time -= offsetSeconds;
113
- if (line.tokens) {
114
- for (const token of line.tokens) {
115
- token.time -= offsetSeconds;
116
- }
117
- }
118
- }
119
- }
120
111
  lines.sort((a, b) => a.time - b.time);
121
112
  return { metadata, lines };
122
113
  }
@@ -144,7 +135,8 @@ var LyricPlayer = class {
144
135
  const parsed = this.resolveLyrics(options.lyrics);
145
136
  this.metadata = parsed.metadata;
146
137
  this.lines = parsed.lines;
147
- this.offsetSeconds = (options.offsetMs ?? 0) / 1e3;
138
+ const tagOffsetMs = parsed.metadata.offset ?? 0;
139
+ this.offsetSeconds = (tagOffsetMs + (options.offsetMs ?? 0)) / 1e3;
148
140
  this.audio.addEventListener("timeupdate", this.handleTimeUpdate);
149
141
  this.audio.addEventListener("play", () => this.emit("play"));
150
142
  this.audio.addEventListener("pause", () => this.emit("pause"));
package/dist/index.d.cts CHANGED
@@ -61,13 +61,13 @@ interface LyricPlayerEvents {
61
61
  type LyricPlayerEventName = keyof LyricPlayerEvents;
62
62
 
63
63
  type LyricSource = {
64
- type: 'lrc';
64
+ type: "lrc";
65
65
  data: string;
66
66
  } | {
67
- type: 'json';
67
+ type: "json";
68
68
  data: string | LyricLine[];
69
69
  } | {
70
- type: 'parsed';
70
+ type: "parsed";
71
71
  data: ParsedLyrics;
72
72
  };
73
73
  interface LyricPlayerOptions {
@@ -101,7 +101,7 @@ declare class LyricPlayer {
101
101
  constructor(options: LyricPlayerOptions);
102
102
  private resolveLyrics;
103
103
  /** Replace the loaded lyrics at any time (e.g. after fetching a file). */
104
- setLyrics(lyrics: LyricPlayerOptions['lyrics']): void;
104
+ setLyrics(lyrics: LyricPlayerOptions["lyrics"]): void;
105
105
  /** Adjust the global lyric offset (in milliseconds) at runtime. */
106
106
  setOffset(offsetMs: number): void;
107
107
  play(): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -61,13 +61,13 @@ interface LyricPlayerEvents {
61
61
  type LyricPlayerEventName = keyof LyricPlayerEvents;
62
62
 
63
63
  type LyricSource = {
64
- type: 'lrc';
64
+ type: "lrc";
65
65
  data: string;
66
66
  } | {
67
- type: 'json';
67
+ type: "json";
68
68
  data: string | LyricLine[];
69
69
  } | {
70
- type: 'parsed';
70
+ type: "parsed";
71
71
  data: ParsedLyrics;
72
72
  };
73
73
  interface LyricPlayerOptions {
@@ -101,7 +101,7 @@ declare class LyricPlayer {
101
101
  constructor(options: LyricPlayerOptions);
102
102
  private resolveLyrics;
103
103
  /** Replace the loaded lyrics at any time (e.g. after fetching a file). */
104
- setLyrics(lyrics: LyricPlayerOptions['lyrics']): void;
104
+ setLyrics(lyrics: LyricPlayerOptions["lyrics"]): void;
105
105
  /** Adjust the global lyric offset (in milliseconds) at runtime. */
106
106
  setOffset(offsetMs: number): void;
107
107
  play(): Promise<void>;
package/dist/index.js CHANGED
@@ -67,7 +67,9 @@ function parseLRC(lrc) {
67
67
  let rest = trimmed;
68
68
  let leadingMatch;
69
69
  while (leadingMatch = rest.match(/^\[(\d{1,2}):(\d{2})(?:[.:](\d{1,3}))?\]/)) {
70
- times.push(timeToSeconds(leadingMatch[1], leadingMatch[2], leadingMatch[3]));
70
+ times.push(
71
+ timeToSeconds(leadingMatch[1], leadingMatch[2], leadingMatch[3])
72
+ );
71
73
  rest = rest.slice(leadingMatch[0].length);
72
74
  }
73
75
  if (times.length === 0) {
@@ -78,17 +80,6 @@ function parseLRC(lrc) {
78
80
  lines.push({ time, text, tokens });
79
81
  }
80
82
  }
81
- const offsetSeconds = (metadata.offset ?? 0) / 1e3;
82
- if (offsetSeconds) {
83
- for (const line of lines) {
84
- line.time -= offsetSeconds;
85
- if (line.tokens) {
86
- for (const token of line.tokens) {
87
- token.time -= offsetSeconds;
88
- }
89
- }
90
- }
91
- }
92
83
  lines.sort((a, b) => a.time - b.time);
93
84
  return { metadata, lines };
94
85
  }
@@ -116,7 +107,8 @@ var LyricPlayer = class {
116
107
  const parsed = this.resolveLyrics(options.lyrics);
117
108
  this.metadata = parsed.metadata;
118
109
  this.lines = parsed.lines;
119
- this.offsetSeconds = (options.offsetMs ?? 0) / 1e3;
110
+ const tagOffsetMs = parsed.metadata.offset ?? 0;
111
+ this.offsetSeconds = (tagOffsetMs + (options.offsetMs ?? 0)) / 1e3;
120
112
  this.audio.addEventListener("timeupdate", this.handleTimeUpdate);
121
113
  this.audio.addEventListener("play", () => this.emit("play"));
122
114
  this.audio.addEventListener("pause", () => this.emit("pause"));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "lrc-audio-player",
3
- "version": "0.1.0",
4
- "description": "Sync LRC/word-level lyrics to an HTML audio element with one constructor.",
3
+ "version": "0.1.1",
4
+ "description": "Sync LRC/word-level lyrics to an HTML audio element with one constructor. Includes an optional React hook at lrc-audio-player/react.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/hangerthem/lrc-audio-player.git"
@@ -22,6 +22,11 @@
22
22
  "types": "./dist/index.d.ts",
23
23
  "import": "./dist/index.js",
24
24
  "require": "./dist/index.cjs"
25
+ },
26
+ "./react": {
27
+ "types": "./dist/react.d.ts",
28
+ "import": "./dist/react.js",
29
+ "require": "./dist/react.cjs"
25
30
  }
26
31
  },
27
32
  "files": [
@@ -42,8 +47,18 @@
42
47
  "karaoke",
43
48
  "sync"
44
49
  ],
50
+ "peerDependencies": {
51
+ "react": ">=19"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "react": {
55
+ "optional": true
56
+ }
57
+ },
45
58
  "license": "MIT",
46
59
  "devDependencies": {
60
+ "@types/react": "^19.2.17",
61
+ "react": "^19.2.7",
47
62
  "tsup": "^8.5.1",
48
63
  "typescript": "^5.9.3",
49
64
  "vitest": "^4.1.8"