@braccato/core 1.22.0 → 2.0.0-beta.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.
Files changed (102) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +202 -607
  3. package/js/element.js +234 -0
  4. package/js/renderer.d.ts +157 -0
  5. package/js/renderer.js +535 -0
  6. package/package.json +22 -17
  7. package/pkg/braccato_core_rs.d.ts +236 -0
  8. package/pkg/braccato_core_rs.js +1735 -0
  9. package/pkg/braccato_core_rs_bg.wasm +0 -0
  10. package/pkg/braccato_core_rs_bg.wasm.d.ts +55 -0
  11. package/dist/LICENSE +0 -21
  12. package/dist/README.md +0 -639
  13. package/dist/element.d.ts +0 -168
  14. package/dist/element.js +0 -730
  15. package/dist/engine/animationConfig.d.ts +0 -84
  16. package/dist/engine/animationConfig.js +0 -95
  17. package/dist/engine/animationDrift.d.ts +0 -11
  18. package/dist/engine/animationDrift.js +0 -43
  19. package/dist/engine/animationTiming.d.ts +0 -27
  20. package/dist/engine/animationTiming.js +0 -145
  21. package/dist/engine/autoScroll.d.ts +0 -24
  22. package/dist/engine/autoScroll.js +0 -103
  23. package/dist/engine/clock.d.ts +0 -29
  24. package/dist/engine/clock.js +0 -70
  25. package/dist/engine/contextSweep.d.ts +0 -10
  26. package/dist/engine/contextSweep.js +0 -102
  27. package/dist/engine/cssValues.d.ts +0 -13
  28. package/dist/engine/cssValues.js +0 -47
  29. package/dist/engine/culling.d.ts +0 -6
  30. package/dist/engine/culling.js +0 -76
  31. package/dist/engine/debugOverlay.d.ts +0 -14
  32. package/dist/engine/debugOverlay.js +0 -58
  33. package/dist/engine/decorationSlides.d.ts +0 -6
  34. package/dist/engine/decorationSlides.js +0 -53
  35. package/dist/engine/elementMotion.d.ts +0 -18
  36. package/dist/engine/elementMotion.js +0 -98
  37. package/dist/engine/glow.d.ts +0 -7
  38. package/dist/engine/glow.js +0 -59
  39. package/dist/engine/instance.d.ts +0 -105
  40. package/dist/engine/instance.js +0 -89
  41. package/dist/engine/instrumentalAnimations.d.ts +0 -7
  42. package/dist/engine/instrumentalAnimations.js +0 -59
  43. package/dist/engine/letterPlans.d.ts +0 -31
  44. package/dist/engine/letterPlans.js +0 -62
  45. package/dist/engine/lineAnimationState.d.ts +0 -10
  46. package/dist/engine/lineAnimationState.js +0 -82
  47. package/dist/engine/lineAnimations.d.ts +0 -5
  48. package/dist/engine/lineAnimations.js +0 -63
  49. package/dist/engine/lineLifecycle.d.ts +0 -12
  50. package/dist/engine/lineLifecycle.js +0 -92
  51. package/dist/engine/lineScroll.d.ts +0 -47
  52. package/dist/engine/lineScroll.js +0 -184
  53. package/dist/engine/lineScrollStyle.d.ts +0 -32
  54. package/dist/engine/lineScrollStyle.js +0 -133
  55. package/dist/engine/liveViews.d.ts +0 -3
  56. package/dist/engine/liveViews.js +0 -5
  57. package/dist/engine/motionCurves.d.ts +0 -19
  58. package/dist/engine/motionCurves.js +0 -85
  59. package/dist/engine/motionPreference.d.ts +0 -1
  60. package/dist/engine/motionPreference.js +0 -1
  61. package/dist/engine/passiveScroll.d.ts +0 -10
  62. package/dist/engine/passiveScroll.js +0 -93
  63. package/dist/engine/renderedLines.d.ts +0 -16
  64. package/dist/engine/renderedLines.js +0 -130
  65. package/dist/engine/scrollViewport.d.ts +0 -36
  66. package/dist/engine/scrollViewport.js +0 -104
  67. package/dist/engine/stageMotion.d.ts +0 -7
  68. package/dist/engine/stageMotion.js +0 -319
  69. package/dist/engine/sweepPlans.d.ts +0 -38
  70. package/dist/engine/sweepPlans.js +0 -66
  71. package/dist/engine/tick.d.ts +0 -16
  72. package/dist/engine/tick.js +0 -270
  73. package/dist/engine/timingLogs.d.ts +0 -7
  74. package/dist/engine/timingLogs.js +0 -81
  75. package/dist/engine/userScroll.d.ts +0 -12
  76. package/dist/engine/userScroll.js +0 -62
  77. package/dist/engine/wavePool.d.ts +0 -7
  78. package/dist/engine/wavePool.js +0 -22
  79. package/dist/engine/wordAnimations.d.ts +0 -5
  80. package/dist/engine/wordAnimations.js +0 -206
  81. package/dist/imageHighlights.d.ts +0 -4
  82. package/dist/imageHighlights.js +0 -34
  83. package/dist/index.d.ts +0 -17
  84. package/dist/index.js +0 -12
  85. package/dist/inject.d.ts +0 -98
  86. package/dist/inject.js +0 -591
  87. package/dist/instrumental.d.ts +0 -11
  88. package/dist/instrumental.js +0 -121
  89. package/dist/language.d.ts +0 -9
  90. package/dist/language.js +0 -47
  91. package/dist/letters.d.ts +0 -2
  92. package/dist/letters.js +0 -25
  93. package/dist/renderer.d.ts +0 -29
  94. package/dist/renderer.js +0 -412
  95. package/dist/seek.d.ts +0 -1
  96. package/dist/seek.js +0 -27
  97. package/dist/stage.d.ts +0 -58
  98. package/dist/stage.js +0 -121
  99. package/dist/types.d.ts +0 -236
  100. package/dist/types.js +0 -1
  101. package/dist/view.d.ts +0 -51
  102. package/dist/view.js +0 -398
package/js/element.js ADDED
@@ -0,0 +1,234 @@
1
+ // The custom element around the Rust renderer. It only forwards: every property is handed to the
2
+ // renderer, which owns the DOM, the canvas and the clock. Properties written before the element
3
+ // connects (or before the wasm has loaded) are kept and applied once the renderer exists.
4
+
5
+ import { Renderer } from "../pkg/braccato_core_rs.js";
6
+ import { load } from "./renderer.js";
7
+
8
+ export { load };
9
+
10
+ const OBSERVED = ["source", "theme", "current-time", "playing", "layout"];
11
+
12
+ export class BraccatoLyricsElement extends HTMLElement {
13
+ static observedAttributes = OBSERVED;
14
+
15
+ #renderer = null;
16
+ #lyrics = null;
17
+ #lyricsOptions = {};
18
+ #source = null;
19
+ #media = null;
20
+ #currentTime = 0;
21
+ #playing = false;
22
+ #tickOptions = {};
23
+ #theme = "";
24
+ #host = {};
25
+ #decorations = { translations: true, romanizations: true };
26
+ #layout = "scroll";
27
+ #connecting = null;
28
+
29
+ connectedCallback() {
30
+ // A disconnect, or a later connect, while the wasm loads makes this connect stale.
31
+ const generation = Symbol();
32
+ this.#connecting = generation;
33
+ load().then(
34
+ () => {
35
+ if (this.#connecting !== generation || !this.isConnected || this.#renderer) return;
36
+ try {
37
+ this.#renderer = new Renderer(this);
38
+ } catch (error) {
39
+ this.#emitError("connect", error);
40
+ return;
41
+ }
42
+ this.#applyAll();
43
+ },
44
+ (error) => this.#emitError("connect", error),
45
+ );
46
+ }
47
+
48
+ disconnectedCallback() {
49
+ this.#connecting = null;
50
+ this.#renderer?.destroy();
51
+ this.#renderer?.free();
52
+ this.#renderer = null;
53
+ }
54
+
55
+ attributeChangedCallback(name, _old, value) {
56
+ switch (name) {
57
+ case "source":
58
+ this.source = value;
59
+ break;
60
+ case "theme":
61
+ this.theme = value ?? "";
62
+ break;
63
+ case "current-time": {
64
+ const seconds = Number.parseFloat(value ?? "");
65
+ if (Number.isFinite(seconds)) this.currentTime = seconds;
66
+ break;
67
+ }
68
+ case "playing":
69
+ this.playing = value !== null;
70
+ break;
71
+ case "layout":
72
+ this.layout = value;
73
+ break;
74
+ }
75
+ }
76
+
77
+ #applyAll() {
78
+ const renderer = this.#renderer;
79
+ renderer.setLayout(this.#layout);
80
+ renderer.setScrollElement(this.#host.getScrollElement?.() ?? null);
81
+ renderer.setTheme(this.#theme);
82
+ renderer.setTickOptions(this.#tickOptions);
83
+ renderer.setDecorations(this.#decorations.translations, this.#decorations.romanizations);
84
+ this.#bindSource();
85
+ renderer.setTime(this.#currentTime, this.#playing);
86
+ if (this.#lyrics !== null) this.#applyLyrics();
87
+ }
88
+
89
+ #applyLyrics() {
90
+ try {
91
+ this.#renderer?.setLyrics(this.#lyrics, this.#lyricsOptions);
92
+ } catch (error) {
93
+ this.#emitError("lyrics", error);
94
+ }
95
+ }
96
+
97
+ #bindSource() {
98
+ // The element's own document and window: in an iframe or a Picture-in-Picture window, a
99
+ // selector names that document's player, and its media elements are that window's class.
100
+ const view = this.ownerDocument.defaultView;
101
+ const isMedia = (node) => view !== null && node instanceof view.HTMLMediaElement;
102
+ let media = null;
103
+ if (typeof this.#source === "string") {
104
+ media = this.isConnected ? this.ownerDocument.querySelector(this.#source) : null;
105
+ if (this.#source && !media) this.#emitError("source", new Error(`No element matches ${this.#source}`));
106
+ } else if (isMedia(this.#source)) {
107
+ media = this.#source;
108
+ }
109
+ this.#media = isMedia(media) ? media : null;
110
+ this.#renderer?.setMedia(this.#media);
111
+ }
112
+
113
+ #emitError(phase, error) {
114
+ queueMicrotask(() =>
115
+ this.dispatchEvent(
116
+ new CustomEvent("braccato:error", { bubbles: true, composed: true, detail: { phase, error } }),
117
+ ),
118
+ );
119
+ }
120
+
121
+ /** The song: a `Lyric[]`. An empty array clears the view. */
122
+ get lyrics() {
123
+ return this.#lyrics;
124
+ }
125
+ set lyrics(value) {
126
+ this.#lyrics = value;
127
+ this.#applyLyrics();
128
+ }
129
+
130
+ /** `{ noLyrics?, language?, songwriters? }`, read by the next `lyrics` write. */
131
+ get lyricsOptions() {
132
+ return this.#lyricsOptions;
133
+ }
134
+ set lyricsOptions(value) {
135
+ this.#lyricsOptions = value ?? {};
136
+ }
137
+
138
+ /** A media element, or a selector for one, that drives the clock. */
139
+ get source() {
140
+ return this.#source;
141
+ }
142
+ set source(value) {
143
+ this.#source = value ?? null;
144
+ this.#bindSource();
145
+ }
146
+
147
+ get mediaElement() {
148
+ return this.#renderer ? this.#media : null;
149
+ }
150
+
151
+ /** Playback position in seconds, for hosts that drive the clock themselves. */
152
+ get currentTime() {
153
+ return this.#currentTime;
154
+ }
155
+ set currentTime(value) {
156
+ this.#currentTime = Number(value) || 0;
157
+ this.#renderer?.setTime(this.#currentTime, this.#playing);
158
+ }
159
+
160
+ get playing() {
161
+ return this.#playing;
162
+ }
163
+ set playing(value) {
164
+ this.#playing = Boolean(value);
165
+ this.#renderer?.setTime(this.#currentTime, this.#playing);
166
+ }
167
+
168
+ /** `{ lyricOffset?, globalLyricOffset?, playbackRate?, smoothScroll? }`. */
169
+ get tickOptions() {
170
+ return this.#tickOptions;
171
+ }
172
+ set tickOptions(value) {
173
+ this.#tickOptions = value ?? {};
174
+ this.#renderer?.setTickOptions(this.#tickOptions);
175
+ }
176
+
177
+ /** A stylesheet of `--blyrics-*` properties and `blyrics-*` comment settings. */
178
+ get theme() {
179
+ return this.#theme;
180
+ }
181
+ set theme(value) {
182
+ this.#theme = String(value ?? "");
183
+ this.#renderer?.setTheme(this.#theme);
184
+ }
185
+
186
+ /** `{ getScrollElement? }`: which element autoscroll moves. */
187
+ get host() {
188
+ return this.#host;
189
+ }
190
+ set host(value) {
191
+ this.#host = value ?? {};
192
+ this.#renderer?.setScrollElement(this.#host.getScrollElement?.() ?? null);
193
+ }
194
+
195
+ /** `"stage"` shows only the lines being sung, bottom-anchored; anything else scrolls. */
196
+ get layout() {
197
+ return this.#layout;
198
+ }
199
+ set layout(value) {
200
+ this.#layout = value === "stage" ? "stage" : "scroll";
201
+ this.#renderer?.setLayout(this.#layout);
202
+ }
203
+
204
+ /** Which of the lyrics' own translations and romanizations to show. */
205
+ get decorations() {
206
+ return { ...this.#decorations };
207
+ }
208
+ set decorations(value) {
209
+ this.#decorations = { translations: Boolean(value?.translations), romanizations: Boolean(value?.romanizations) };
210
+ this.#renderer?.setDecorations(this.#decorations.translations, this.#decorations.romanizations);
211
+ }
212
+
213
+ get renderer() {
214
+ return this.#renderer;
215
+ }
216
+
217
+ get status() {
218
+ return this.#renderer ? "rendering" : "idle";
219
+ }
220
+
221
+ resumeAutoscroll() {
222
+ this.#renderer?.resumeAutoscroll();
223
+ }
224
+
225
+ relayout() {
226
+ this.#renderer?.relayout();
227
+ }
228
+ }
229
+
230
+ /** Defines the element under `tag` (default `braccato-lyrics`) unless something already holds it. */
231
+ export function defineLyricsElement(tag = "braccato-lyrics") {
232
+ if (!customElements.get(tag)) customElements.define(tag, class extends BraccatoLyricsElement {});
233
+ return customElements.get(tag);
234
+ }
@@ -0,0 +1,157 @@
1
+ // Types for js/renderer.js: `@braccato/core`'s renderer interface over the Rust renderer.
2
+ import type { Lyric, LyricPart, LyricSyncType } from "@braccato/types";
3
+ import type { Renderer } from "../pkg/braccato_core_rs.js";
4
+
5
+ export type { Lyric, LyricPart, LyricSyncType };
6
+
7
+ /** What `load` and `loadSync` take: an address, a response, bytes, or a compiled module. */
8
+ export type WasmSource = string | URL | Request | Response | BufferSource | WebAssembly.Module;
9
+
10
+ /** Loads the wasm. Resolves once renderers can be created; later calls return the same promise. */
11
+ export function load(source?: WasmSource): Promise<void>;
12
+ /** Loads the wasm from bytes or a compiled module, without waiting. */
13
+ export function loadSync(module: BufferSource | WebAssembly.Module): void;
14
+ /** Whether `createLyricsRenderer` can be called. */
15
+ export function isLoaded(): boolean;
16
+
17
+ /** Forgets the last player snapshot, as a new song should. */
18
+ export function resetPlaybackClock(): void;
19
+ /** Resumes autoscroll in every view a reader scrolled away in. */
20
+ export function resumeAllAutoscroll(): void;
21
+ /**
22
+ * A 2D copy of what the renderer drawing on `canvas` shows now, styled like it, or null when no
23
+ * renderer here draws on it. A WebGL canvas clones blank, so a host cloning the page uses this.
24
+ */
25
+ export function snapshotCanvas(canvas: HTMLCanvasElement): HTMLCanvasElement | null;
26
+ /** The canvas the renderer that built `container` draws on, or null. */
27
+ export function canvasFor(container: HTMLElement): HTMLCanvasElement | null;
28
+ /** Whether `lyricElement` is a line one of this module's renderers built. */
29
+ export function ownsLine(lyricElement: Element): boolean;
30
+
31
+ export function injectTranslation(
32
+ doc: Document,
33
+ lyricElement: HTMLElement,
34
+ text: string,
35
+ language?: string | null,
36
+ ): void;
37
+ export function injectRomanization(
38
+ doc: Document,
39
+ lyricElement: HTMLElement,
40
+ lineData: unknown,
41
+ text: string,
42
+ timedRomanization?: LyricPart[] | null,
43
+ ): void;
44
+ export function removeTranslation(lyricElement: HTMLElement): boolean;
45
+ export function removeRomanization(lyricElement: HTMLElement, lineData?: unknown): boolean;
46
+
47
+ export interface TickOptions {
48
+ /** `Date.now()` when the time was read off the player; -1 (the default) when it was not. */
49
+ eventCreationTime?: number;
50
+ isPlaying: boolean;
51
+ smoothScroll?: boolean;
52
+ globalLyricOffset?: number;
53
+ lyricOffset?: number;
54
+ richsyncOffsetTrim?: number;
55
+ lineOffsetTrim?: number;
56
+ passiveScrollEnabled?: boolean;
57
+ playbackRate?: number;
58
+ /** Accepted for `@braccato/core` compatibility; the Rust renderer always follows paused seeks. */
59
+ followPausedSeeks?: boolean;
60
+ /** Accepted for `@braccato/core` compatibility. */
61
+ countEveryUserScroll?: boolean;
62
+ }
63
+
64
+ export type AnimationTickStatus = "ok" | "lyrics-missing";
65
+
66
+ export interface StageBox {
67
+ x: number;
68
+ y: number;
69
+ width: number;
70
+ height: number;
71
+ }
72
+
73
+ export interface LyricsRendererHost {
74
+ isViewVisible(): boolean;
75
+ isLoaderActive(): boolean;
76
+ syncAdState(): boolean;
77
+ getScrollElement(): HTMLElement | null;
78
+ setResumeAffordanceVisible(visible: boolean): void;
79
+ seek(timeS: number): void;
80
+ log(...args: unknown[]): void;
81
+ onStageLayout?(box: StageBox | null): void;
82
+ /** Accepted for `@braccato/core` compatibility; nothing is drawn to it. */
83
+ debug?: unknown;
84
+ }
85
+
86
+ export type LyricsLayout = "scroll" | "stage";
87
+
88
+ export interface LyricsRendererOptions {
89
+ document: Document;
90
+ window: Window;
91
+ mount?: HTMLElement;
92
+ host?: Partial<LyricsRendererHost>;
93
+ layout?: LyricsLayout;
94
+ }
95
+
96
+ export interface SetLyricsOptions {
97
+ language?: string | null;
98
+ loaderVisible?: boolean;
99
+ noLyrics?: boolean;
100
+ songwriters?: readonly string[];
101
+ /** Lyrics given with the key of the song on screen are swapped into it. */
102
+ songKey?: string;
103
+ mount?: HTMLElement;
104
+ }
105
+
106
+ /** A word of a line. `time` and `duration` are in seconds; writing `time` moves the word. */
107
+ export interface PartData {
108
+ lyricElement: HTMLElement;
109
+ time: number;
110
+ readonly duration: number;
111
+ animations: Animation[];
112
+ }
113
+
114
+ /** A line the renderer built. `time` and `duration` are in seconds; writing `time` moves the line. */
115
+ export interface LineData {
116
+ lyricElement: HTMLElement;
117
+ time: number;
118
+ readonly duration: number;
119
+ parts: PartData[];
120
+ isScrolled: boolean;
121
+ isAnimationPlayStatePlaying: boolean;
122
+ accumulatedOffsetMs: number;
123
+ isAnimating: boolean;
124
+ lastAnimSetupAt: number;
125
+ isSelected: boolean;
126
+ height: number;
127
+ position: number;
128
+ decorations: Map<HTMLElement, number>;
129
+ animations: Animation[];
130
+ }
131
+
132
+ export interface LyricsRenderer {
133
+ setLyrics(lyrics: Lyric[], options?: SetLyricsOptions): void;
134
+ setLanguage(language?: string | null): void;
135
+ /** Always false: the Rust renderer keeps the lyrics and rebuilds them itself. */
136
+ setTheme(css: string): boolean;
137
+ tick(currentTimeS: number, options: TickOptions): AnimationTickStatus;
138
+ relayout(measureLines?: boolean): void;
139
+ clear(): void;
140
+ destroy(): void;
141
+ noteUserScroll(): void;
142
+ noteVisibilityChange(): void;
143
+ resumeAutoscroll(): void;
144
+ clearOnScreenLyrics(): boolean;
145
+ scheduleLyricPositionUpdate(isTicking: () => boolean, retick: () => void): void;
146
+ retickFromPlaybackClock(
147
+ buildOptions: (eventCreationTime: number, isPlaying: boolean) => TickOptions,
148
+ ): AnimationTickStatus;
149
+ readonly container: HTMLElement | null;
150
+ readonly lines: readonly LineData[];
151
+ readonly syncType: LyricSyncType;
152
+ /** The Rust renderer underneath, for `setProfiling()`, `stats()` and `takeProfile()`; null before the first `setLyrics`. */
153
+ readonly renderer: Renderer | null;
154
+ }
155
+
156
+ /** A lyrics view with `@braccato/core`'s interface, drawn by the Rust renderer. `load()` first. */
157
+ export function createLyricsRenderer(options: LyricsRendererOptions): LyricsRenderer;