@braccato/core 2.0.0-beta.7 → 2.0.0-beta.9

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/README.md CHANGED
@@ -69,18 +69,34 @@ The element takes the same `Lyric[]` as `@braccato/core` and keeps most of its s
69
69
  | Property | Notes |
70
70
  | --------------- | ----- |
71
71
  | `lyrics` | `Lyric[]`. Writing it builds the view. |
72
- | `lyricsOptions` | `{ noLyrics?, language?, songwriters? }`, read by the next `lyrics` write. |
72
+ | `lyricsOptions` | `{ noLyrics?, language?, songwriters?, songKey? }`, read by the next `lyrics` write. |
73
73
  | `source` | A media element or a selector for one. It drives the clock. |
74
74
  | `currentTime`, `playing` | Drive the clock by hand when there is no `source`. |
75
- | `tickOptions` | `{ lyricOffset?, globalLyricOffset?, playbackRate?, smoothScroll?, passiveScrollEnabled? }` |
75
+ | `tickOptions` | `{ lyricOffset?, globalLyricOffset?, richsyncOffsetTrim?, lineOffsetTrim?, playbackRate?, smoothScroll?, passiveScrollEnabled? }`. The trims, in seconds, come off the theme's lead (below). |
76
76
  | `theme` | A stylesheet: `--blyrics-*` custom properties, and `/* blyrics-key = value; */` settings in comments. |
77
- | `host` | `{ getScrollElement? }`, the element autoscroll moves. |
77
+ | `host` | `{ getScrollElement? }`, the element autoscroll moves. Without it, the nearest scrollable element, which can be the element itself (`overflow-y: auto` and a height on it). |
78
78
  | `layout` | `"scroll"` or `"stage"`. Also an attribute. |
79
79
  | `decorations` | `{ translations, romanizations }`: whether to show the lyrics' own `translations` and `romanization` / `timedRomanization`. Both on by default. |
80
- | `renderer` | The Rust `Renderer`, which has `resumeAutoscroll()`, `relayout()`, `setProfiling(on)`, `stats()` and `takeProfile()`. |
80
+ | `renderer` | The Rust `Renderer`, which has `resumeAutoscroll()`, `relayout()`, `timingOffsets()`, `setProfiling(on)`, `stats()` and `takeProfile()`. |
81
+
82
+ Properties written before the element is defined, as a page that imports it lazily may write them,
83
+ are applied once it connects. `@braccato/core/element` has types, which name each event's `detail`.
81
84
 
82
85
  It dispatches the same events: `braccato:lyrics-loaded`, `braccato:line-click`,
83
- `braccato:scroll-state`, `braccato:stage-layout` and `braccato:error`.
86
+ `braccato:scroll-state`, `braccato:stage-layout` and `braccato:error`. A line click's `timeS` is the
87
+ player time to seek to, to the microsecond.
88
+
89
+ Lines light ahead of the player by the theme's `--blyrics-richsync-timing-offset` (word-synced,
90
+ 0.15s by default) and `--blyrics-timing-offset` (line-synced, 0.115s), less the trims in
91
+ `tickOptions`. `renderer.timingOffsets()` answers `{ richsync, line }`, the leads as the lines resolve
92
+ them before the trims, so a host that wants none while paused can trim them all off without reading
93
+ the theme's CSS.
94
+
95
+ A scroll of the scroll element that autoscroll did not cause pauses autoscroll, as a reader's
96
+ does, with a `braccato:scroll-state` event and `blyrics-user-scrolling` on the container. Scrolls
97
+ in the 600 ms and two frames after the lines are built or measured again are taken for the page
98
+ settling (a position clamped to new content) and ignored, so a test that scrolls as a reader waits
99
+ that long first.
84
100
 
85
101
  Anything the host puts in the container stays ordinary, visible DOM (a footer, buttons), and so does
86
102
  anything it puts in the credits (a source card), which moves with the credits on the stage. The
@@ -0,0 +1,122 @@
1
+ // Types for js/element.js: `@braccato/core/element`, the `<braccato-lyrics>` custom element.
2
+ import type { Lyric, LyricSyncType } from "@braccato/types";
3
+ import type { Renderer } from "../pkg/braccato_core_rs.js";
4
+ import type { LyricsLayout, SetLyricsOptions, StageBox, WasmSource } from "./renderer.js";
5
+
6
+ export { load } from "./renderer.js";
7
+ export type { Lyric, LyricsLayout, StageBox, WasmSource };
8
+
9
+ /** What the next `lyrics` write reads. */
10
+ export type LyricsElementOptions = Pick<SetLyricsOptions, "noLyrics" | "language" | "songwriters" | "songKey">;
11
+
12
+ /** What the element passes to the renderer's clock. Offsets and trims are in seconds. */
13
+ export interface LyricsElementTickOptions {
14
+ lyricOffset?: number;
15
+ globalLyricOffset?: number;
16
+ /** Taken off the theme's `--blyrics-richsync-timing-offset` lead (see `Renderer.timingOffsets()`). */
17
+ richsyncOffsetTrim?: number;
18
+ /** Taken off the theme's `--blyrics-timing-offset` lead. */
19
+ lineOffsetTrim?: number;
20
+ playbackRate?: number;
21
+ smoothScroll?: boolean;
22
+ passiveScrollEnabled?: boolean;
23
+ }
24
+
25
+ /** Where autoscroll moves. Without it, the nearest scrollable element, the element itself included. */
26
+ export interface LyricsElementHost {
27
+ getScrollElement?(): HTMLElement | null;
28
+ }
29
+
30
+ export interface LyricsElementDecorations {
31
+ translations: boolean;
32
+ romanizations: boolean;
33
+ }
34
+
35
+ export interface LyricsLoadedDetail {
36
+ lineCount: number;
37
+ syncType: LyricSyncType;
38
+ }
39
+
40
+ export interface LineClickDetail {
41
+ /** The player time the click seeks to, in seconds, to the microsecond. */
42
+ timeS: number;
43
+ }
44
+
45
+ export interface ScrollStateDetail {
46
+ userScrolling: boolean;
47
+ }
48
+
49
+ export interface StageLayoutDetail {
50
+ box: StageBox | null;
51
+ }
52
+
53
+ export interface LyricsErrorDetail {
54
+ phase: "connect" | "lyrics" | "source";
55
+ error: unknown;
56
+ }
57
+
58
+ export interface LyricsElementEventMap extends HTMLElementEventMap {
59
+ "braccato:lyrics-loaded": CustomEvent<LyricsLoadedDetail>;
60
+ "braccato:line-click": CustomEvent<LineClickDetail>;
61
+ "braccato:scroll-state": CustomEvent<ScrollStateDetail>;
62
+ "braccato:stage-layout": CustomEvent<StageLayoutDetail>;
63
+ "braccato:error": CustomEvent<LyricsErrorDetail>;
64
+ }
65
+
66
+ export class BraccatoLyricsElement extends HTMLElement {
67
+ static observedAttributes: string[];
68
+ /** The song. An empty array clears the view. */
69
+ lyrics: Lyric[] | null;
70
+ lyricsOptions: LyricsElementOptions;
71
+ /** A media element, or a selector for one, that drives the clock. */
72
+ source: HTMLMediaElement | string | null;
73
+ /** The media element `source` resolved to, once the renderer exists. */
74
+ readonly mediaElement: HTMLMediaElement | null;
75
+ /** Playback position in seconds, for hosts that drive the clock themselves. */
76
+ currentTime: number;
77
+ playing: boolean;
78
+ tickOptions: LyricsElementTickOptions;
79
+ /** A stylesheet of `--blyrics-*` properties and `blyrics-*` comment settings. */
80
+ theme: string;
81
+ host: LyricsElementHost;
82
+ layout: LyricsLayout;
83
+ decorations: LyricsElementDecorations;
84
+ /** The Rust renderer, once the element is connected and the wasm has loaded. */
85
+ readonly renderer: Renderer | null;
86
+ readonly status: "idle" | "rendering";
87
+ resumeAutoscroll(): void;
88
+ relayout(): void;
89
+ connectedCallback(): void;
90
+ disconnectedCallback(): void;
91
+ attributeChangedCallback(name: string, old: string | null, value: string | null): void;
92
+
93
+ addEventListener<K extends keyof LyricsElementEventMap>(
94
+ type: K,
95
+ listener: (this: BraccatoLyricsElement, event: LyricsElementEventMap[K]) => unknown,
96
+ options?: boolean | AddEventListenerOptions,
97
+ ): void;
98
+ addEventListener(
99
+ type: string,
100
+ listener: EventListenerOrEventListenerObject,
101
+ options?: boolean | AddEventListenerOptions,
102
+ ): void;
103
+ removeEventListener<K extends keyof LyricsElementEventMap>(
104
+ type: K,
105
+ listener: (this: BraccatoLyricsElement, event: LyricsElementEventMap[K]) => unknown,
106
+ options?: boolean | EventListenerOptions,
107
+ ): void;
108
+ removeEventListener(
109
+ type: string,
110
+ listener: EventListenerOrEventListenerObject,
111
+ options?: boolean | EventListenerOptions,
112
+ ): void;
113
+ }
114
+
115
+ /** Defines the element under `tag` (default `braccato-lyrics`) unless something already holds it. */
116
+ export function defineLyricsElement(tag?: string): typeof BraccatoLyricsElement;
117
+
118
+ declare global {
119
+ interface HTMLElementTagNameMap {
120
+ "braccato-lyrics": BraccatoLyricsElement;
121
+ }
122
+ }
package/js/element.js CHANGED
@@ -8,6 +8,19 @@ import { load } from "./renderer.js";
8
8
  export { load };
9
9
 
10
10
  const OBSERVED = ["source", "theme", "current-time", "playing", "layout"];
11
+ /** What a page can write before the element is defined, in the order they are applied again. */
12
+ const UPGRADED = [
13
+ "lyricsOptions",
14
+ "lyrics",
15
+ "tickOptions",
16
+ "currentTime",
17
+ "playing",
18
+ "theme",
19
+ "host",
20
+ "layout",
21
+ "decorations",
22
+ "source",
23
+ ];
11
24
 
12
25
  export class BraccatoLyricsElement extends HTMLElement {
13
26
  static observedAttributes = OBSERVED;
@@ -27,6 +40,14 @@ export class BraccatoLyricsElement extends HTMLElement {
27
40
  #connecting = null;
28
41
 
29
42
  connectedCallback() {
43
+ // A page that writes these before `customElements.define` (a lazily imported element) leaves
44
+ // own properties that hide the accessors: they are handed to the accessors instead.
45
+ for (const name of UPGRADED) {
46
+ if (!Object.hasOwn(this, name)) continue;
47
+ const value = this[name];
48
+ delete this[name];
49
+ this[name] = value;
50
+ }
30
51
  // A disconnect, or a later connect, while the wasm loads makes this connect stale.
31
52
  const generation = Symbol();
32
53
  this.#connecting = generation;
@@ -127,7 +148,7 @@ export class BraccatoLyricsElement extends HTMLElement {
127
148
  this.#applyLyrics();
128
149
  }
129
150
 
130
- /** `{ noLyrics?, language?, songwriters? }`, read by the next `lyrics` write. */
151
+ /** `{ noLyrics?, language?, songwriters?, songKey? }`, read by the next `lyrics` write. */
131
152
  get lyricsOptions() {
132
153
  return this.#lyricsOptions;
133
154
  }
@@ -165,7 +186,10 @@ export class BraccatoLyricsElement extends HTMLElement {
165
186
  this.#renderer?.setTime(this.#currentTime, this.#playing);
166
187
  }
167
188
 
168
- /** `{ lyricOffset?, globalLyricOffset?, playbackRate?, smoothScroll? }`. */
189
+ /**
190
+ * `{ lyricOffset?, globalLyricOffset?, richsyncOffsetTrim?, lineOffsetTrim?, playbackRate?,
191
+ * smoothScroll?, passiveScrollEnabled? }`.
192
+ */
169
193
  get tickOptions() {
170
194
  return this.#tickOptions;
171
195
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@braccato/core",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.0-beta.9",
4
4
  "description": "Synchronized lyrics renderer with word-by-word animations, in Rust and WebGL2",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -27,7 +27,10 @@
27
27
  "types": "./js/renderer.d.ts",
28
28
  "import": "./js/renderer.js"
29
29
  },
30
- "./element": "./js/element.js",
30
+ "./element": {
31
+ "types": "./js/element.d.ts",
32
+ "import": "./js/element.js"
33
+ },
31
34
  "./wasm": "./pkg/braccato_core_rs_bg.wasm",
32
35
  "./constants": {
33
36
  "types": "./dist/constants.d.ts",
@@ -140,9 +140,17 @@ export class Renderer {
140
140
  */
141
141
  takeProfile(): any;
142
142
  /**
143
- * `[start, duration]` in seconds of line `index`, or with `part` one of its parts.
143
+ * `{ richsync, line }`: how far ahead of the player word-synced and line-synced lines light, in
144
+ * seconds. They are the theme's `--blyrics-richsync-timing-offset` and `--blyrics-timing-offset`
145
+ * as the lines resolve them, or the defaults (0.15 and 0.115) where it sets none, before the
146
+ * tick options' trims. Read once the lyrics are built.
144
147
  */
145
- timing(index: number, part?: number | null): Float32Array | undefined;
148
+ timingOffsets(): { richsync: number; line: number };
149
+ /**
150
+ * `[start, duration]` in seconds of line `index`, or with `part` one of its parts, to the
151
+ * microsecond.
152
+ */
153
+ timing(index: number, part?: number | null): Float64Array | undefined;
146
154
  /**
147
155
  * The canvas the view draws on.
148
156
  */
@@ -199,10 +207,11 @@ export interface InitOutput {
199
207
  readonly renderer_syncType: (a: number) => [number, number];
200
208
  readonly renderer_takeProfile: (a: number) => any;
201
209
  readonly renderer_timing: (a: number, b: number, c: number) => [number, number];
210
+ readonly renderer_timingOffsets: (a: number) => any;
202
211
  readonly wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___f64______true_: (a: number, b: number, c: number) => void;
203
212
  readonly wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true_: (a: number, b: number, c: any) => void;
204
- readonly wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__39: (a: number, b: number, c: any) => void;
205
213
  readonly wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__40: (a: number, b: number, c: any) => void;
214
+ readonly wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__41: (a: number, b: number, c: any) => void;
206
215
  readonly wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke_______true_: (a: number, b: number) => void;
207
216
  readonly __wbindgen_malloc: (a: number, b: number) => number;
208
217
  readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
@@ -337,17 +337,29 @@ export class Renderer {
337
337
  return ret;
338
338
  }
339
339
  /**
340
- * `[start, duration]` in seconds of line `index`, or with `part` one of its parts.
340
+ * `{ richsync, line }`: how far ahead of the player word-synced and line-synced lines light, in
341
+ * seconds. They are the theme's `--blyrics-richsync-timing-offset` and `--blyrics-timing-offset`
342
+ * as the lines resolve them, or the defaults (0.15 and 0.115) where it sets none, before the
343
+ * tick options' trims. Read once the lyrics are built.
344
+ * @returns {{ richsync: number; line: number }}
345
+ */
346
+ timingOffsets() {
347
+ const ret = wasm.renderer_timingOffsets(this.__wbg_ptr);
348
+ return ret;
349
+ }
350
+ /**
351
+ * `[start, duration]` in seconds of line `index`, or with `part` one of its parts, to the
352
+ * microsecond.
341
353
  * @param {number} index
342
354
  * @param {number | null} [part]
343
- * @returns {Float32Array | undefined}
355
+ * @returns {Float64Array | undefined}
344
356
  */
345
357
  timing(index, part) {
346
358
  const ret = wasm.renderer_timing(this.__wbg_ptr, index, isLikeNone(part) ? Number.MAX_SAFE_INTEGER : (part) >>> 0);
347
359
  let v1;
348
360
  if (ret[0] !== 0) {
349
- v1 = getArrayF32FromWasm0(ret[0], ret[1]).slice();
350
- wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
361
+ v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
362
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
351
363
  }
352
364
  return v1;
353
365
  }
@@ -1368,12 +1380,12 @@ function __wbg_get_imports() {
1368
1380
  },
1369
1381
  __wbindgen_generic_0000000000000003: function(arg0, arg1) {
1370
1382
  // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("Array<any>")], shim_idx: 6, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1371
- const ret = makeMutClosure(arg0, arg1, wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__39);
1383
+ const ret = makeMutClosure(arg0, arg1, wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__40);
1372
1384
  return ret;
1373
1385
  },
1374
1386
  __wbindgen_generic_0000000000000004: function(arg0, arg1) {
1375
1387
  // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("Event")], shim_idx: 6, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1376
- const ret = makeMutClosure(arg0, arg1, wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__40);
1388
+ const ret = makeMutClosure(arg0, arg1, wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__41);
1377
1389
  return ret;
1378
1390
  },
1379
1391
  __wbindgen_generic_0000000000000005: function(arg0, arg1) {
@@ -1419,14 +1431,14 @@ function wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bin
1419
1431
  wasm.wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true_(arg0, arg1, arg2);
1420
1432
  }
1421
1433
 
1422
- function wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__39(arg0, arg1, arg2) {
1423
- wasm.wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__39(arg0, arg1, arg2);
1424
- }
1425
-
1426
1434
  function wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__40(arg0, arg1, arg2) {
1427
1435
  wasm.wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__40(arg0, arg1, arg2);
1428
1436
  }
1429
1437
 
1438
+ function wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__41(arg0, arg1, arg2) {
1439
+ wasm.wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__41(arg0, arg1, arg2);
1440
+ }
1441
+
1430
1442
  function wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___f64______true_(arg0, arg1, arg2) {
1431
1443
  wasm.wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___f64______true_(arg0, arg1, arg2);
1432
1444
  }
@@ -1508,6 +1520,11 @@ function getArrayF32FromWasm0(ptr, len) {
1508
1520
  return getFloat32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
1509
1521
  }
1510
1522
 
1523
+ function getArrayF64FromWasm0(ptr, len) {
1524
+ ptr = ptr >>> 0;
1525
+ return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
1526
+ }
1527
+
1511
1528
  function getArrayU8FromWasm0(ptr, len) {
1512
1529
  ptr = ptr >>> 0;
1513
1530
  return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
@@ -1529,6 +1546,14 @@ function getFloat32ArrayMemory0() {
1529
1546
  return cachedFloat32ArrayMemory0;
1530
1547
  }
1531
1548
 
1549
+ let cachedFloat64ArrayMemory0 = null;
1550
+ function getFloat64ArrayMemory0() {
1551
+ if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
1552
+ cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
1553
+ }
1554
+ return cachedFloat64ArrayMemory0;
1555
+ }
1556
+
1532
1557
  function getStringFromWasm0(ptr, len) {
1533
1558
  ptr = ptr >>> 0;
1534
1559
  return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
@@ -1669,6 +1694,7 @@ function __wbg_finalize_init(instance, module) {
1669
1694
  wasmModule = module;
1670
1695
  cachedDataViewMemory0 = null;
1671
1696
  cachedFloat32ArrayMemory0 = null;
1697
+ cachedFloat64ArrayMemory0 = null;
1672
1698
  cachedUint8ArrayMemory0 = null;
1673
1699
  wasm.__wbindgen_start();
1674
1700
  return wasm;
Binary file
@@ -39,10 +39,11 @@ export const renderer_stats: (a: number) => any;
39
39
  export const renderer_syncType: (a: number) => [number, number];
40
40
  export const renderer_takeProfile: (a: number) => any;
41
41
  export const renderer_timing: (a: number, b: number, c: number) => [number, number];
42
+ export const renderer_timingOffsets: (a: number) => any;
42
43
  export const wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___f64______true_: (a: number, b: number, c: number) => void;
43
44
  export const wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true_: (a: number, b: number, c: any) => void;
44
- export const wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__39: (a: number, b: number, c: any) => void;
45
45
  export const wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__40: (a: number, b: number, c: any) => void;
46
+ export const wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke___wasm_bindgen_662efdbedeaa8f26___JsValue______true__41: (a: number, b: number, c: any) => void;
46
47
  export const wasm_bindgen_662efdbedeaa8f26___convert__closures_____invoke_______true_: (a: number, b: number) => void;
47
48
  export const __wbindgen_malloc: (a: number, b: number) => number;
48
49
  export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;