beatsheet 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +63 -2
- package/bin/beatsheet.js +14 -0
- package/bin/register-fallback.mjs +23 -0
- package/bin/resolve-fallback.mjs +83 -0
- package/package.json +45 -4
- package/player/env.d.ts +5 -0
- package/player/gallery-audio.ts +147 -0
- package/player/gallery-css.ts +144 -0
- package/player/gallery-detail.ts +385 -0
- package/player/gallery-music.ts +257 -0
- package/player/gallery-pick-state.ts +167 -0
- package/player/gallery-pick.ts +749 -0
- package/player/gallery-sounds.ts +137 -0
- package/player/gallery-state.ts +457 -0
- package/player/gallery.ts +612 -0
- package/player/index.html +49 -0
- package/player/main.ts +83 -0
- package/skill/SKILL.md +658 -0
- package/skill/examples/library/assets/tour/avatar-mk.svg +1 -0
- package/skill/examples/library/assets/tour/dashboard-insights.svg +1 -0
- package/skill/examples/library/assets/tour/dashboard.svg +1 -0
- package/skill/examples/library/assets/tour/settings.svg +1 -0
- package/skill/examples/library/library-tour.plan.md +32 -0
- package/skill/examples/library/library-tour.video.json +268 -0
- package/skill/reference/scene-techniques.md +306 -0
- package/skill/scripts/beats.ts +34 -0
- package/skill/scripts/catalogue.ts +120 -0
- package/skill/scripts/frames.ts +105 -0
- package/src/args.ts +344 -0
- package/src/cli.ts +95 -0
- package/src/commands.ts +812 -0
- package/src/default-project.config.ts +9 -0
- package/src/index.ts +47 -0
- package/src/packages.ts +17 -0
- package/src/paths.ts +32 -0
- package/src/project.ts +200 -0
- package/src/render/browser.ts +185 -0
- package/src/render/capture.ts +320 -0
- package/src/render/ffmpeg.ts +229 -0
- package/src/render/gif-info.ts +61 -0
- package/src/render/render.ts +220 -0
- package/src/samples.ts +557 -0
- package/src/server/api.ts +494 -0
- package/src/server/jobs.ts +210 -0
- package/src/server/security.ts +47 -0
- package/src/server/server.ts +191 -0
- package/src/server/sse.ts +76 -0
- package/src/server/static.ts +127 -0
- package/src/server/vite.ts +100 -0
- package/src/skill.ts +111 -0
- package/src/snap.ts +212 -0
- package/src/speech.ts +689 -0
- package/src/svg-text.ts +136 -0
- package/src/voicefx.ts +311 -0
- package/studio/3rdpartylicenses.txt +287 -0
- package/studio/favicon.ico +0 -0
- package/studio/icons/apple-touch-icon.png +0 -0
- package/studio/icons/icon-192.png +0 -0
- package/studio/icons/icon-512.png +0 -0
- package/studio/icons/icon.svg +1 -0
- package/studio/icons/maskable-512.png +0 -0
- package/studio/index.html +18 -0
- package/studio/main-KMYDBWZH.js +15 -0
- package/studio/manifest.webmanifest +17 -0
- package/studio/offline.html +92 -0
- package/studio/styles-NJ2CULOW.css +1 -0
- package/studio/sw.js +71 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gallery's large view of one sample: the scene in the gallery's format(s) (fit to the window),
|
|
3
|
+
* a scrubber with beat marks, play with sound (renderAudio of the one-scene doc), and the sample
|
|
4
|
+
* as a ready-to-paste scene entry.
|
|
5
|
+
*/
|
|
6
|
+
import { createPlayer, h, type FormatId, type Player, type ProjectConfig, type ResolvedSample, type SceneType } from '@beatsheet/core';
|
|
7
|
+
import { liveContext, startPlayback, type Playback } from './gallery-audio';
|
|
8
|
+
import { fitRow, formatSeconds, sampleKey, sceneEntry, singleSceneDoc } from './gallery-state';
|
|
9
|
+
|
|
10
|
+
const DETAIL_T_KEY = 'beatsheet-gallery-detail-t';
|
|
11
|
+
const GAP = 14;
|
|
12
|
+
|
|
13
|
+
export interface DetailOptions {
|
|
14
|
+
project: ProjectConfig;
|
|
15
|
+
assetBase: string;
|
|
16
|
+
type: SceneType;
|
|
17
|
+
sample: ResolvedSample;
|
|
18
|
+
formats: FormatId[];
|
|
19
|
+
/** In the studio's iframe: offers "Add to video" (posted to the studio). */
|
|
20
|
+
embedded: boolean;
|
|
21
|
+
/** Position in the filtered list, e.g. '3 / 41'. */
|
|
22
|
+
position: string;
|
|
23
|
+
onClose(): void;
|
|
24
|
+
/** -1 previous sample, +1 next sample (in the filtered list). */
|
|
25
|
+
onStep(delta: number): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function storageGet(key: string): string | null {
|
|
29
|
+
try {
|
|
30
|
+
return sessionStorage.getItem(key);
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function storageSet(key: string, value: string): void {
|
|
37
|
+
try {
|
|
38
|
+
sessionStorage.setItem(key, value);
|
|
39
|
+
} catch {
|
|
40
|
+
// private mode: state just isn't kept
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function storageRemove(key: string): void {
|
|
45
|
+
try {
|
|
46
|
+
sessionStorage.removeItem(key);
|
|
47
|
+
} catch {
|
|
48
|
+
// private mode
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function copyText(text: string): Promise<void> {
|
|
53
|
+
try {
|
|
54
|
+
await navigator.clipboard.writeText(text);
|
|
55
|
+
return;
|
|
56
|
+
} catch {
|
|
57
|
+
// no clipboard API (or no permission): the old way
|
|
58
|
+
}
|
|
59
|
+
const area = document.createElement('textarea');
|
|
60
|
+
area.value = text;
|
|
61
|
+
area.style.cssText = 'position:fixed;left:-9999px;top:0';
|
|
62
|
+
document.body.appendChild(area);
|
|
63
|
+
area.select();
|
|
64
|
+
const ok = document.execCommand('copy');
|
|
65
|
+
area.remove();
|
|
66
|
+
if (!ok) throw new Error('copy refused');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class DetailView {
|
|
70
|
+
readonly root: HTMLElement;
|
|
71
|
+
private readonly o: DetailOptions;
|
|
72
|
+
private readonly key: string;
|
|
73
|
+
private readonly stage: HTMLElement;
|
|
74
|
+
private readonly previews: { box: HTMLElement; holder: HTMLElement; format: FormatId }[] = [];
|
|
75
|
+
private players: Player[] = [];
|
|
76
|
+
private readonly duration: number;
|
|
77
|
+
private t = 0;
|
|
78
|
+
private playback: Playback | null = null;
|
|
79
|
+
private buffer: Promise<AudioBuffer | null> | null = null;
|
|
80
|
+
/** The resolved mix (null: silent). */
|
|
81
|
+
private mixed: AudioBuffer | null = null;
|
|
82
|
+
private raf = 0;
|
|
83
|
+
private destroyed = false;
|
|
84
|
+
/** Showing the opening still: Play starts at 0 instead of from there. */
|
|
85
|
+
private fromStart = false;
|
|
86
|
+
private readonly playBtn: HTMLButtonElement;
|
|
87
|
+
private readonly loopBox: HTMLInputElement;
|
|
88
|
+
private readonly scrub: HTMLInputElement;
|
|
89
|
+
private readonly timeEl: HTMLElement;
|
|
90
|
+
private readonly status: HTMLElement;
|
|
91
|
+
private readonly ro: ResizeObserver | null;
|
|
92
|
+
private readonly onKey = (e: KeyboardEvent): void => this.keydown(e);
|
|
93
|
+
private readonly onHide = (): void => this.saveTime();
|
|
94
|
+
|
|
95
|
+
constructor(o: DetailOptions) {
|
|
96
|
+
this.o = o;
|
|
97
|
+
this.key = sampleKey(o.type.id, o.sample.id);
|
|
98
|
+
const entry = sceneEntry(o.type.id, o.sample);
|
|
99
|
+
const json = JSON.stringify(entry, null, 2);
|
|
100
|
+
|
|
101
|
+
this.stage = h('div', { class: 'g-detail-stage' });
|
|
102
|
+
for (const format of o.formats) {
|
|
103
|
+
const holder = h('div', { class: 'g-holder' });
|
|
104
|
+
const box = h('div', { class: 'g-preview' }, holder, o.formats.length > 1 ? h('span', { class: 'g-preview-label' }, format) : null);
|
|
105
|
+
this.previews.push({ box, holder, format });
|
|
106
|
+
this.stage.appendChild(box);
|
|
107
|
+
try {
|
|
108
|
+
const player = createPlayer({ container: holder, project: o.project, doc: singleSceneDoc(entry, [format]), format, assetBase: o.assetBase });
|
|
109
|
+
this.players.push(player);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
box.appendChild(h('div', { class: 'g-error' }, `Could not mount: ${err instanceof Error ? err.message : String(err)}`));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const timeline = this.players[0]?.timeline;
|
|
115
|
+
this.duration = timeline?.duration ?? o.sample.duration;
|
|
116
|
+
const fps = timeline?.fps ?? 30;
|
|
117
|
+
|
|
118
|
+
this.playBtn = h('button', { type: 'button', class: 'g-primary', title: 'Play with sound (Space)' }, '▶ Play');
|
|
119
|
+
this.loopBox = h('input', { type: 'checkbox' });
|
|
120
|
+
this.scrub = h('input', { type: 'range', min: 0, max: this.duration, step: 1 / fps, value: 0, 'aria-label': 'Scene time' });
|
|
121
|
+
this.timeEl = h('span', { class: 'g-time' });
|
|
122
|
+
const marks = h('div', { class: 'g-beat-marks', 'aria-hidden': 'true' });
|
|
123
|
+
const beats = timeline?.scenes[0]?.beats ?? [];
|
|
124
|
+
for (const b of beats) {
|
|
125
|
+
const mark = h('i', { title: `${b.label} · ${formatSeconds(b.offset)}` });
|
|
126
|
+
mark.style.left = `${(b.offset / this.duration) * 100}%`;
|
|
127
|
+
marks.appendChild(mark);
|
|
128
|
+
}
|
|
129
|
+
const transport = h(
|
|
130
|
+
'div',
|
|
131
|
+
{ class: 'g-transport' },
|
|
132
|
+
this.playBtn,
|
|
133
|
+
h('label', { class: 'g-check', title: 'Start over at the end' }, this.loopBox, 'Loop'),
|
|
134
|
+
this.timeEl,
|
|
135
|
+
h('div', { class: 'g-scrub' }, marks, this.scrub),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
this.status = h('span', { class: 'g-copied', role: 'status' });
|
|
139
|
+
const copyBtn = h('button', { type: 'button', title: 'Copy { id, type, duration, props } for a *.video.json' }, 'Copy');
|
|
140
|
+
const addBtn = o.embedded ? h('button', { type: 'button', class: 'g-primary', title: 'Add this scene after the selected scene of the open video' }, 'Add to video') : null;
|
|
141
|
+
const beatList = h('ol', { class: 'g-list' });
|
|
142
|
+
for (const b of beats) {
|
|
143
|
+
const btn = h('button', { type: 'button', title: 'Go to this beat' }, b.label);
|
|
144
|
+
btn.addEventListener('click', () => {
|
|
145
|
+
this.fromStart = false;
|
|
146
|
+
this.pause();
|
|
147
|
+
this.seekTo(b.offset);
|
|
148
|
+
});
|
|
149
|
+
beatList.appendChild(h('li', null, h('span', null, formatSeconds(b.offset)), btn, h('code', null, b.id)));
|
|
150
|
+
}
|
|
151
|
+
const sfxList = h('ul', { class: 'g-list' });
|
|
152
|
+
for (const s of this.players[0]?.timeline.sfx ?? []) {
|
|
153
|
+
sfxList.appendChild(h('li', null, h('span', null, formatSeconds(s.at)), h('code', null, s.sound), s.gain !== 1 ? h('span', { class: 'g-dur' }, `gain ${s.gain}`) : null));
|
|
154
|
+
}
|
|
155
|
+
const side = h(
|
|
156
|
+
'aside',
|
|
157
|
+
{ class: 'g-detail-side' },
|
|
158
|
+
h('h4', null, 'Scene entry', h('span', { class: 'g-spacer' }), this.status, copyBtn, addBtn),
|
|
159
|
+
h('pre', { class: 'g-json' }, json),
|
|
160
|
+
h('h4', null, `Beats (${beats.length})`),
|
|
161
|
+
beats.length ? beatList : h('p', null, 'No beats.'),
|
|
162
|
+
h('h4', null, `Sounds (${this.players[0]?.timeline.sfx.length ?? 0})`),
|
|
163
|
+
sfxList,
|
|
164
|
+
h('h4', null, 'Scene type'),
|
|
165
|
+
h('p', null, o.type.description ?? ''),
|
|
166
|
+
h('p', null, h('code', null, `${o.type.id} · ${o.type.category ?? 'other'} · default ${formatSeconds(o.type.defaultDuration)}`)),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const prev = h('button', { type: 'button', title: 'Previous sample (,)' }, '‹ Prev');
|
|
170
|
+
const next = h('button', { type: 'button', title: 'Next sample (.)' }, 'Next ›');
|
|
171
|
+
const close = h('button', { type: 'button', title: 'Close (Esc)' }, 'Close');
|
|
172
|
+
const head = h(
|
|
173
|
+
'div',
|
|
174
|
+
{ class: 'g-detail-head' },
|
|
175
|
+
h('h2', { id: 'g-detail-title' }, `${o.type.title} · ${o.sample.title}`),
|
|
176
|
+
h('code', null, this.key),
|
|
177
|
+
o.sample.stress ? h('span', { class: 'g-badge stress' }, 'stress') : null,
|
|
178
|
+
h('span', { class: 'g-dur' }, formatSeconds(this.duration)),
|
|
179
|
+
h('span', { class: 'g-spacer' }),
|
|
180
|
+
h('span', { class: 'g-dur' }, o.position),
|
|
181
|
+
prev,
|
|
182
|
+
next,
|
|
183
|
+
close,
|
|
184
|
+
);
|
|
185
|
+
this.root = h(
|
|
186
|
+
'div',
|
|
187
|
+
{ class: 'g-detail', role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': 'g-detail-title' },
|
|
188
|
+
h('div', { class: 'g-detail-panel' }, head, h('div', { class: 'g-detail-body' }, h('div', { class: 'g-detail-main' }, this.stage, transport), side)),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
this.root.addEventListener('click', (e) => {
|
|
192
|
+
if (e.target === this.root) o.onClose();
|
|
193
|
+
});
|
|
194
|
+
close.addEventListener('click', () => o.onClose());
|
|
195
|
+
prev.addEventListener('click', () => o.onStep(-1));
|
|
196
|
+
next.addEventListener('click', () => o.onStep(1));
|
|
197
|
+
this.playBtn.addEventListener('click', () => void this.togglePlay());
|
|
198
|
+
this.scrub.addEventListener('input', () => {
|
|
199
|
+
this.fromStart = false;
|
|
200
|
+
this.pause();
|
|
201
|
+
this.seekTo(Number(this.scrub.value));
|
|
202
|
+
});
|
|
203
|
+
copyBtn.addEventListener('click', () => {
|
|
204
|
+
copyText(json).then(
|
|
205
|
+
() => this.flash('Copied'),
|
|
206
|
+
() => this.flash('Copy failed: select the JSON'),
|
|
207
|
+
);
|
|
208
|
+
});
|
|
209
|
+
addBtn?.addEventListener('click', () => {
|
|
210
|
+
window.parent.postMessage({ source: 'beatsheet-gallery', type: 'add-scene', scene: entry, sampleId: o.sample.id }, location.origin);
|
|
211
|
+
this.flash('Sent to the studio');
|
|
212
|
+
});
|
|
213
|
+
document.addEventListener('keydown', this.onKey);
|
|
214
|
+
window.addEventListener('pagehide', this.onHide);
|
|
215
|
+
|
|
216
|
+
this.ro = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => this.layout());
|
|
217
|
+
this.ro?.observe(this.stage);
|
|
218
|
+
|
|
219
|
+
// Kept across a reload (hot reload of the scene's code): same sample, same time. Otherwise the
|
|
220
|
+
// view opens on a still with everything in (after the last beat); Play then starts at 0.
|
|
221
|
+
const saved = storageGet(DETAIL_T_KEY);
|
|
222
|
+
storageRemove(DETAIL_T_KEY);
|
|
223
|
+
let start: number | null = null;
|
|
224
|
+
if (saved) {
|
|
225
|
+
try {
|
|
226
|
+
const v = JSON.parse(saved) as { key?: string; t?: number };
|
|
227
|
+
if (v.key === this.key && typeof v.t === 'number') start = v.t;
|
|
228
|
+
} catch {
|
|
229
|
+
// ignore
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
this.fromStart = start === null;
|
|
233
|
+
const last = beats.at(-1);
|
|
234
|
+
this.seekTo(start ?? (last ? last.offset + 0.4 : 0));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Call once the root is in the document. */
|
|
238
|
+
attached(): void {
|
|
239
|
+
this.layout();
|
|
240
|
+
this.playBtn.focus();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private layout(): void {
|
|
244
|
+
const style = getComputedStyle(this.stage);
|
|
245
|
+
const w = this.stage.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight);
|
|
246
|
+
const hgt = this.stage.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom);
|
|
247
|
+
const boxes = fitRow(this.previews.map((p) => p.format), w, hgt, GAP);
|
|
248
|
+
this.previews.forEach((p, i) => {
|
|
249
|
+
const b = boxes[i];
|
|
250
|
+
if (!b) return;
|
|
251
|
+
p.box.style.width = `${b.width}px`;
|
|
252
|
+
p.box.style.height = `${b.height}px`;
|
|
253
|
+
p.holder.style.transform = `scale(${b.scale})`;
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private flash(text: string): void {
|
|
258
|
+
this.status.textContent = text;
|
|
259
|
+
setTimeout(() => {
|
|
260
|
+
if (this.status.textContent === text) this.status.textContent = '';
|
|
261
|
+
}, 1800);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private lastFrame(): number {
|
|
265
|
+
return Math.max(0, this.duration - 1 / 30);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private seekTo(t: number): void {
|
|
269
|
+
this.t = Math.min(Math.max(0, t), this.lastFrame());
|
|
270
|
+
for (const p of this.players) p.seek(this.t);
|
|
271
|
+
this.scrub.value = String(this.t);
|
|
272
|
+
this.timeEl.textContent = `${this.t.toFixed(2)} / ${this.duration.toFixed(2)} s`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private audio(): Promise<AudioBuffer | null> {
|
|
276
|
+
const player = this.players[0];
|
|
277
|
+
if (!player) return Promise.resolve(null);
|
|
278
|
+
this.buffer ??= player.renderAudio({ sampleRate: 22050 }).catch((err: unknown) => {
|
|
279
|
+
console.warn(`[beatsheet gallery] audio of ${this.key}: ${err instanceof Error ? err.message : String(err)}`);
|
|
280
|
+
return null;
|
|
281
|
+
});
|
|
282
|
+
return this.buffer;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private async togglePlay(): Promise<void> {
|
|
286
|
+
if (this.playback || this.playBtn.dataset.loading) {
|
|
287
|
+
this.pause();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
// Created inside the click: browsers start an AudioContext only from a user gesture.
|
|
291
|
+
liveContext();
|
|
292
|
+
if (this.fromStart || this.t >= this.lastFrame()) this.seekTo(0);
|
|
293
|
+
this.fromStart = false;
|
|
294
|
+
this.playBtn.dataset.loading = '1';
|
|
295
|
+
this.playBtn.textContent = '… Mixing';
|
|
296
|
+
this.mixed = await this.audio();
|
|
297
|
+
if (this.destroyed || !this.playBtn.dataset.loading) return;
|
|
298
|
+
delete this.playBtn.dataset.loading;
|
|
299
|
+
this.playback = startPlayback(this.mixed, this.t);
|
|
300
|
+
this.playBtn.textContent = this.playback.audible ? '❚❚ Pause' : '❚❚ Pause (silent)';
|
|
301
|
+
if (!this.raf) this.raf = requestAnimationFrame(() => this.frame());
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private frame(): void {
|
|
305
|
+
this.raf = 0;
|
|
306
|
+
if (!this.playback || this.destroyed) return;
|
|
307
|
+
let t = this.playback.time();
|
|
308
|
+
if (t >= this.duration) {
|
|
309
|
+
if (this.loopBox.checked) {
|
|
310
|
+
this.playback.stop();
|
|
311
|
+
this.playback = startPlayback(this.mixed, 0);
|
|
312
|
+
t = 0;
|
|
313
|
+
} else {
|
|
314
|
+
this.pause();
|
|
315
|
+
this.seekTo(this.lastFrame());
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
this.seekTo(t);
|
|
320
|
+
this.raf = requestAnimationFrame(() => this.frame());
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Stops playback (and a mix still loading from starting); the playhead stays. */
|
|
324
|
+
pause(): void {
|
|
325
|
+
delete this.playBtn.dataset.loading;
|
|
326
|
+
this.playback?.stop();
|
|
327
|
+
this.playback = null;
|
|
328
|
+
if (this.raf) cancelAnimationFrame(this.raf);
|
|
329
|
+
this.raf = 0;
|
|
330
|
+
this.playBtn.textContent = '▶ Play';
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private keydown(e: KeyboardEvent): void {
|
|
334
|
+
if (e.defaultPrevented || e.metaKey || e.ctrlKey || e.altKey) return;
|
|
335
|
+
const target = e.target as HTMLElement | null;
|
|
336
|
+
const inField = target instanceof HTMLInputElement && target.type !== 'range' && target.type !== 'checkbox';
|
|
337
|
+
if (inField) return;
|
|
338
|
+
const step = (e.shiftKey ? 1 : 1 / 30) * (e.key === 'ArrowLeft' ? -1 : 1);
|
|
339
|
+
switch (e.key) {
|
|
340
|
+
case 'Escape':
|
|
341
|
+
e.preventDefault();
|
|
342
|
+
this.o.onClose();
|
|
343
|
+
break;
|
|
344
|
+
case ' ':
|
|
345
|
+
if (target instanceof HTMLButtonElement) return;
|
|
346
|
+
e.preventDefault();
|
|
347
|
+
void this.togglePlay();
|
|
348
|
+
break;
|
|
349
|
+
case 'ArrowLeft':
|
|
350
|
+
case 'ArrowRight':
|
|
351
|
+
if (target === this.scrub) return;
|
|
352
|
+
e.preventDefault();
|
|
353
|
+
this.fromStart = false;
|
|
354
|
+
this.pause();
|
|
355
|
+
this.seekTo(this.t + step);
|
|
356
|
+
break;
|
|
357
|
+
case 'Home':
|
|
358
|
+
e.preventDefault();
|
|
359
|
+
this.pause();
|
|
360
|
+
this.seekTo(0);
|
|
361
|
+
break;
|
|
362
|
+
case ',':
|
|
363
|
+
this.o.onStep(-1);
|
|
364
|
+
break;
|
|
365
|
+
case '.':
|
|
366
|
+
this.o.onStep(1);
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
private saveTime(): void {
|
|
372
|
+
storageSet(DETAIL_T_KEY, JSON.stringify({ key: this.key, t: this.t }));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
destroy(): void {
|
|
376
|
+
this.destroyed = true;
|
|
377
|
+
this.pause();
|
|
378
|
+
this.ro?.disconnect();
|
|
379
|
+
document.removeEventListener('keydown', this.onKey);
|
|
380
|
+
window.removeEventListener('pagehide', this.onHide);
|
|
381
|
+
for (const p of this.players) p.destroy();
|
|
382
|
+
this.players = [];
|
|
383
|
+
this.root.remove();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gallery's music browser: every theme track by mood, each played over a short timeline of
|
|
3
|
+
* sample scenes (renderMix), with a playhead over the scene blocks and a small live preview.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
MUSIC_CATEGORIES,
|
|
7
|
+
createPlayer,
|
|
8
|
+
h,
|
|
9
|
+
isFileTrack,
|
|
10
|
+
renderMix,
|
|
11
|
+
resolveTimeline,
|
|
12
|
+
type Player,
|
|
13
|
+
type ProjectConfig,
|
|
14
|
+
type ResolvedTimeline,
|
|
15
|
+
type VideoDoc,
|
|
16
|
+
} from '@beatsheet/core';
|
|
17
|
+
import { liveContext, startPlayback, type Playback } from './gallery-audio';
|
|
18
|
+
import type { Panel } from './gallery-sounds';
|
|
19
|
+
import {
|
|
20
|
+
formatSeconds,
|
|
21
|
+
groupByCategory,
|
|
22
|
+
musicDoc,
|
|
23
|
+
musicMixKey,
|
|
24
|
+
mutedMix,
|
|
25
|
+
trackButtonLabel,
|
|
26
|
+
trackEntries,
|
|
27
|
+
type TrackEntry,
|
|
28
|
+
} from './gallery-state';
|
|
29
|
+
|
|
30
|
+
const PREVIEW_W = 352;
|
|
31
|
+
|
|
32
|
+
export function buildMusicBrowser(project: ProjectConfig, assetBase: string): Panel {
|
|
33
|
+
const theme = project.theme;
|
|
34
|
+
const tracks = trackEntries(theme.music);
|
|
35
|
+
const base = musicDoc(project, null);
|
|
36
|
+
const titles = new Map(project.scenes.map((t) => [t.id, t.title]));
|
|
37
|
+
const timeline: ResolvedTimeline = resolveTimeline(base.doc, project);
|
|
38
|
+
const duration = timeline.duration;
|
|
39
|
+
|
|
40
|
+
const holder = h('div', { class: 'g-holder' });
|
|
41
|
+
const preview = h('div', { class: 'g-preview g-music-preview' }, holder);
|
|
42
|
+
const scale = PREVIEW_W / 1920;
|
|
43
|
+
preview.style.width = `${PREVIEW_W}px`;
|
|
44
|
+
preview.style.height = `${Math.round(1080 * scale)}px`;
|
|
45
|
+
holder.style.transform = `scale(${scale})`;
|
|
46
|
+
|
|
47
|
+
const head = h('div', { class: 'g-tl-head' });
|
|
48
|
+
const strip = h('div', { class: 'g-tl', title: 'Click to move the playhead' });
|
|
49
|
+
for (const s of timeline.scenes) {
|
|
50
|
+
const block = h('div', { class: 'g-tl-block' }, titles.get(s.type) ?? s.type, h('span', null, formatSeconds(s.duration)));
|
|
51
|
+
block.style.left = `calc(${(s.start / duration) * 100}% + 1px)`;
|
|
52
|
+
block.style.width = `calc(${(s.duration / duration) * 100}% - 2px)`;
|
|
53
|
+
strip.appendChild(block);
|
|
54
|
+
}
|
|
55
|
+
strip.appendChild(head);
|
|
56
|
+
|
|
57
|
+
const stopBtn = h('button', { type: 'button', disabled: true }, '■ Stop');
|
|
58
|
+
const now = h('span', { class: 'g-music-now' }, 'Nothing playing.');
|
|
59
|
+
const timeEl = h('span', { class: 'g-time' }, `0.0 / ${formatSeconds(duration)}`);
|
|
60
|
+
const soundsBox = h('input', { type: 'checkbox', checked: true });
|
|
61
|
+
const bar = h(
|
|
62
|
+
'div',
|
|
63
|
+
{ class: 'g-music-bar' },
|
|
64
|
+
stopBtn,
|
|
65
|
+
timeEl,
|
|
66
|
+
now,
|
|
67
|
+
h('span', { class: 'g-spacer' }),
|
|
68
|
+
h('label', { class: 'g-check', title: "Mix in the scene types' own sounds (typing ticks, accents)" }, soundsBox, 'Scene sounds'),
|
|
69
|
+
);
|
|
70
|
+
const el = h(
|
|
71
|
+
'div',
|
|
72
|
+
null,
|
|
73
|
+
h(
|
|
74
|
+
'div',
|
|
75
|
+
{ class: 'g-music-head' },
|
|
76
|
+
preview,
|
|
77
|
+
h('div', null, h('p', { class: 'g-sub', style: 'margin:0 0 6px' }, `Each track plays over ${timeline.scenes.length} sample scenes (${formatSeconds(duration)}), one per category.`), strip),
|
|
78
|
+
bar,
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const rows = new Map<string, { row: HTMLElement; btn: HTMLButtonElement; state: HTMLElement; title: string }>();
|
|
83
|
+
/** Muted parts per generated track (its toggles). */
|
|
84
|
+
const mutes = new Map<string, Set<string>>();
|
|
85
|
+
if (!tracks.length) el.appendChild(h('p', { class: 'g-empty' }, `Theme "${theme.title}" has no music tracks.`));
|
|
86
|
+
for (const group of groupByCategory(tracks, (t) => t.track.category, MUSIC_CATEGORIES)) {
|
|
87
|
+
el.appendChild(h('h2', { class: 'g-cat' }, group.title, h('span', { class: 'g-count' }, String(group.items.length))));
|
|
88
|
+
const list = h('ul', { class: 'g-tracks' });
|
|
89
|
+
for (const entry of group.items) {
|
|
90
|
+
const title = entry.track.title || entry.id;
|
|
91
|
+
const btn = h('button', { type: 'button', class: 'g-play', 'data-track': entry.id, 'aria-label': trackButtonLabel(title, false) }, '▶ Play');
|
|
92
|
+
const state = h('span', { class: 'g-state', role: 'status' });
|
|
93
|
+
const row = h(
|
|
94
|
+
'li',
|
|
95
|
+
{ class: 'g-track' },
|
|
96
|
+
btn,
|
|
97
|
+
h(
|
|
98
|
+
'span',
|
|
99
|
+
{ class: 'g-track-name' },
|
|
100
|
+
h('strong', null, entry.track.title || entry.id),
|
|
101
|
+
h('code', null, entry.id),
|
|
102
|
+
h('span', { class: entry.kind === 'file' ? 'g-badge file' : 'g-badge' }, entry.kind),
|
|
103
|
+
),
|
|
104
|
+
state,
|
|
105
|
+
h('p', null, entry.track.description ?? ''),
|
|
106
|
+
);
|
|
107
|
+
btn.addEventListener('click', () => {
|
|
108
|
+
if (current?.id === entry.id) stop();
|
|
109
|
+
else void play(entry, 0);
|
|
110
|
+
});
|
|
111
|
+
const parts = isFileTrack(entry.track) ? [] : Object.entries(entry.track.parts ?? {});
|
|
112
|
+
if (parts.length) {
|
|
113
|
+
// A toggle per part: muted parts play at 0 (music.mix), re-mixed on the next play or right away.
|
|
114
|
+
const muted = new Set<string>();
|
|
115
|
+
mutes.set(entry.id, muted);
|
|
116
|
+
const toggles = parts.map(([pid, part]) => {
|
|
117
|
+
const t = h('button', { type: 'button', class: 'g-part', 'data-part': pid, 'aria-pressed': 'false', title: part.description ?? `Mute ${part.title}` }, part.title);
|
|
118
|
+
t.addEventListener('click', () => {
|
|
119
|
+
if (muted.has(pid)) muted.delete(pid);
|
|
120
|
+
else muted.add(pid);
|
|
121
|
+
t.setAttribute('aria-pressed', String(muted.has(pid)));
|
|
122
|
+
if (current?.id === entry.id) void play(entry, pos);
|
|
123
|
+
});
|
|
124
|
+
return t;
|
|
125
|
+
});
|
|
126
|
+
row.appendChild(h('div', { class: 'g-parts', role: 'group', 'aria-label': `Parts of ${title}: press to mute` }, ...toggles));
|
|
127
|
+
}
|
|
128
|
+
rows.set(entry.id, { row, btn, state, title });
|
|
129
|
+
list.appendChild(row);
|
|
130
|
+
}
|
|
131
|
+
el.appendChild(list);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let player: Player | null = null;
|
|
135
|
+
let current: { id: string; playback: Playback | null; buffer: AudioBuffer | null } | null = null;
|
|
136
|
+
let raf = 0;
|
|
137
|
+
let playId = 0;
|
|
138
|
+
let pos = 0;
|
|
139
|
+
const mixes = new Map<string, Promise<AudioBuffer>>();
|
|
140
|
+
|
|
141
|
+
function mix(id: string): Promise<AudioBuffer> {
|
|
142
|
+
const sceneSounds = soundsBox.checked;
|
|
143
|
+
const levels = mutedMix(mutes.get(id) ?? []);
|
|
144
|
+
const key = musicMixKey(id, sceneSounds, levels);
|
|
145
|
+
let p = mixes.get(key);
|
|
146
|
+
if (!p) {
|
|
147
|
+
const doc: VideoDoc = musicDoc(project, id, { sceneSounds, ...(levels ? { mix: levels } : {}) }).doc;
|
|
148
|
+
p = renderMix(doc, resolveTimeline(doc, project), theme, { sampleRate: 22050 });
|
|
149
|
+
mixes.set(key, p);
|
|
150
|
+
p.catch(() => mixes.delete(key));
|
|
151
|
+
}
|
|
152
|
+
return p;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function show(t: number): void {
|
|
156
|
+
pos = Math.min(Math.max(0, t), duration);
|
|
157
|
+
player?.seek(Math.min(pos, Math.max(0, duration - 1 / 30)));
|
|
158
|
+
head.style.left = `${(pos / duration) * 100}%`;
|
|
159
|
+
timeEl.textContent = `${formatSeconds(pos)} / ${formatSeconds(duration)}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function setRow(id: string | undefined, text: string, on: boolean): void {
|
|
163
|
+
const r = id ? rows.get(id) : undefined;
|
|
164
|
+
if (!r) return;
|
|
165
|
+
r.state.textContent = text;
|
|
166
|
+
r.row.classList.toggle('playing', on);
|
|
167
|
+
r.btn.textContent = on ? '■ Stop' : '▶ Play';
|
|
168
|
+
r.btn.setAttribute('aria-label', trackButtonLabel(r.title, on));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function frame(): void {
|
|
172
|
+
raf = 0;
|
|
173
|
+
if (!current?.playback) return;
|
|
174
|
+
const t = current.playback.time();
|
|
175
|
+
if (t >= duration) {
|
|
176
|
+
stop();
|
|
177
|
+
show(0);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
show(t);
|
|
181
|
+
raf = requestAnimationFrame(frame);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function play(entry: TrackEntry, from: number): Promise<void> {
|
|
185
|
+
stop();
|
|
186
|
+
// Created inside the click: browsers start an AudioContext only from a user gesture.
|
|
187
|
+
liveContext();
|
|
188
|
+
const id = ++playId;
|
|
189
|
+
current = { id: entry.id, playback: null, buffer: null };
|
|
190
|
+
setRow(entry.id, 'Mixing…', true);
|
|
191
|
+
now.textContent = '';
|
|
192
|
+
now.append('Mixing ', h('strong', null, entry.track.title || entry.id), '…');
|
|
193
|
+
stopBtn.disabled = false;
|
|
194
|
+
let buffer: AudioBuffer;
|
|
195
|
+
try {
|
|
196
|
+
buffer = await mix(entry.id);
|
|
197
|
+
} catch (err) {
|
|
198
|
+
if (id !== playId) return;
|
|
199
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
200
|
+
current = null;
|
|
201
|
+
setRow(entry.id, `Mix failed: ${message}`, false);
|
|
202
|
+
now.textContent = 'Nothing playing.';
|
|
203
|
+
stopBtn.disabled = true;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (id !== playId || !current) return;
|
|
207
|
+
current.buffer = buffer;
|
|
208
|
+
current.playback = startPlayback(buffer, from);
|
|
209
|
+
setRow(entry.id, current.playback.audible ? '' : 'no audio output', true);
|
|
210
|
+
now.textContent = '';
|
|
211
|
+
now.append('Playing ', h('strong', null, entry.track.title || entry.id), ` (${entry.kind})`);
|
|
212
|
+
if (!raf) raf = requestAnimationFrame(frame);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function stop(): void {
|
|
216
|
+
playId++;
|
|
217
|
+
if (current) setRow(current.id, '', false);
|
|
218
|
+
current?.playback?.stop();
|
|
219
|
+
current = null;
|
|
220
|
+
if (raf) cancelAnimationFrame(raf);
|
|
221
|
+
raf = 0;
|
|
222
|
+
stopBtn.disabled = true;
|
|
223
|
+
now.textContent = 'Nothing playing.';
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
stopBtn.addEventListener('click', () => stop());
|
|
227
|
+
strip.addEventListener('click', (e) => {
|
|
228
|
+
const r = strip.getBoundingClientRect();
|
|
229
|
+
const t = ((e.clientX - r.left) / r.width) * duration;
|
|
230
|
+
if (current?.playback && current.buffer) {
|
|
231
|
+
current.playback.stop();
|
|
232
|
+
current.playback = startPlayback(current.buffer, t);
|
|
233
|
+
}
|
|
234
|
+
show(t);
|
|
235
|
+
});
|
|
236
|
+
soundsBox.addEventListener('change', () => {
|
|
237
|
+
// A different mix: restart the playing track with it, from where it is.
|
|
238
|
+
const entry = current ? tracks.find((t) => t.id === current?.id) : undefined;
|
|
239
|
+
if (entry) void play(entry, pos);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
el,
|
|
244
|
+
activate(): void {
|
|
245
|
+
if (player) return;
|
|
246
|
+
try {
|
|
247
|
+
player = createPlayer({ container: holder, project, doc: base.doc, format: '16:9', assetBase });
|
|
248
|
+
show(pos);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
preview.appendChild(h('div', { class: 'g-error' }, `Preview failed: ${err instanceof Error ? err.message : String(err)}`));
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
deactivate(): void {
|
|
254
|
+
stop();
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
}
|