-
-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathprogrammatic.html
More file actions
363 lines (335 loc) · 12.7 KB
/
Copy pathprogrammatic.html
File metadata and controls
363 lines (335 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>dawcore — programmatic tracks & clips</title>
<style>
body {
font-family: system-ui, sans-serif;
background: #0f0f1a;
color: #e0d4c8;
padding: 24px;
}
h1 {
font-size: 1.2rem;
margin-bottom: 8px;
}
p.intro {
color: #aaa;
font-size: 0.85rem;
max-width: 760px;
line-height: 1.5;
}
daw-editor {
--daw-wave-color: #c49a6c;
--daw-playhead-color: #d08070;
--daw-background: #1a1a2e;
--daw-track-background: #16213e;
--daw-ruler-color: #c49a6c;
--daw-ruler-background: #0f0f1a;
--daw-controls-width: 200px;
--daw-min-height: 200px;
margin-bottom: 12px;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin: 12px 0;
align-items: center;
}
.toolbar fieldset {
display: flex;
gap: 6px;
align-items: center;
border: 1px solid rgba(255,255,255,0.15);
padding: 6px 8px;
margin: 0;
}
.toolbar legend {
color: #888;
font-size: 0.7rem;
padding: 0 4px;
text-transform: uppercase;
letter-spacing: 0.05em;
}
button {
cursor: pointer;
background: #1a1a2e;
color: #e0d4c8;
border: 1px solid currentColor;
padding: 4px 10px;
font: inherit;
font-size: 0.8rem;
line-height: 1.4;
}
button.good {
color: #63C75F;
}
button:disabled {
opacity: 0.4;
cursor: not-allowed;
}
daw-transport {
display: flex;
gap: 8px;
}
#log {
margin-top: 12px;
font-family: monospace;
font-size: 0.75rem;
color: #888;
max-height: 220px;
overflow-y: auto;
border: 1px solid rgba(255,255,255,0.08);
padding: 8px;
background: #0a0a14;
}
#log .err { color: #d08070; }
#log .ok { color: #63C75F; }
#log .warn { color: #c49a6c; }
</style>
</head>
<body>
<h1>dawcore — programmatic tracks & clips</h1>
<p class="intro">
Imperative API on <code><daw-editor></code> for tracks and clips.
<code>editor.ready()</code> bootstraps the engine before any track exists.
<code>addTrack</code> / <code>updateTrack</code> / <code>removeTrack</code>
operate on whole tracks; <code>addClip</code> / <code>updateClip</code> /
<code>removeClip</code> operate on individual clips. The same flow works
declaratively — appending or mutating <code><daw-track></code> /
<code><daw-clip></code> in the DOM goes through the exact same pipeline.
</p>
<script type="module">import '@dawcore/components';</script>
<!-- Intentionally empty: no <daw-track> children. -->
<daw-editor id="editor" samples-per-pixel="1024" wave-height="80" timescale file-drop interactive-clips indefinite-playback>
<daw-keyboard-shortcuts playback splitting undo></daw-keyboard-shortcuts>
</daw-editor>
<div class="toolbar">
<daw-transport for="editor">
<daw-play-button></daw-play-button>
<daw-pause-button></daw-pause-button>
<daw-stop-button></daw-stop-button>
</daw-transport>
</div>
<div class="toolbar">
<fieldset>
<legend>Tracks (imperative)</legend>
<button class="good" id="add-track-src">addTrack({ src })</button>
<button class="good" id="add-track-multiclip">addTrack({ clips: [..] })</button>
<button class="good" id="add-track-empty">addTrack({}) — empty</button>
<button id="update-track-vol">updateTrack(volume=0.4)</button>
<button id="remove-track-last">removeTrack(last)</button>
</fieldset>
<fieldset>
<legend>Clips (imperative)</legend>
<button class="good" id="add-clip">addClip(last track)</button>
<button id="update-clip-shift">updateClip(shift +1s)</button>
<button id="remove-clip-first">removeClip(first)</button>
</fieldset>
</div>
<div class="toolbar">
<fieldset>
<legend>Tracks (declarative DOM)</legend>
<button id="dom-append-track">append <daw-track src></button>
<button id="dom-mutate-track-vol">mutate volume attr</button>
</fieldset>
<fieldset>
<legend>Clips (declarative DOM)</legend>
<button id="dom-append-clip">append <daw-clip></button>
<button id="dom-mutate-clip-start">mutate start attr</button>
</fieldset>
<fieldset>
<legend>State</legend>
<button id="dump">dump editor.tracks</button>
<button id="clear">clear all</button>
</fieldset>
</div>
<div id="log"></div>
<script type="module">
import { createToneAdapter } from '@waveform-playlist/playout';
const editor = document.getElementById('editor');
const adapter = createToneAdapter({ ppqn: 960 });
editor.adapter = adapter;
const log = document.getElementById('log');
function addLog(msg, cls = '') {
const line = document.createElement('div');
if (cls) line.className = cls;
line.textContent = new Date().toLocaleTimeString() + ' ' + msg;
log.prepend(line);
while (log.children.length > 50) log.lastChild.remove();
}
// --- Editor events -----------------------------------------------------
editor.addEventListener('daw-track-ready', (e) =>
addLog('track-ready: ' + e.detail.trackId.slice(0, 8), 'ok')
);
editor.addEventListener('daw-track-error', (e) =>
addLog('track-error: ' + String(e.detail.error), 'err')
);
editor.addEventListener('daw-clip-ready', (e) =>
addLog('clip-ready: trackId=' + e.detail.trackId.slice(0, 8) + ' clipId=' + e.detail.clipId.slice(0, 8), 'ok')
);
editor.addEventListener('daw-clip-error', (e) =>
addLog('clip-error: ' + String(e.detail.error), 'err')
);
// --- Bootstrap ---------------------------------------------------------
(async () => {
const engine = await editor.ready();
addLog('editor.ready() → engine = ' + (engine ? 'built' : 'null'), 'ok');
})();
// Helper: id of the most recently added track
function lastTrackId() {
const ids = editor.engine?.getState().tracks.map((t) => t.id) ?? [];
return ids[ids.length - 1] ?? null;
}
// --- Tracks (imperative) ----------------------------------------------
document.getElementById('add-track-src').addEventListener('click', async () => {
try {
const t = await editor.addTrack({
name: 'Kick (addTrack)',
volume: 0.8,
clips: [{ src: '/media/audio/AlbertKader_Whiptails/03_Kick.opus' }],
});
addLog('addTrack({src}) → ' + t.trackId.slice(0, 8), 'ok');
} catch (err) {
addLog('addTrack rejected: ' + String(err), 'err');
}
});
document.getElementById('add-track-multiclip').addEventListener('click', async () => {
try {
const t = await editor.addTrack({
name: 'Snare 2-clip',
clips: [
{ src: '/media/audio/AlbertKader_Whiptails/04_Snare.opus', start: 0, duration: 4, offset: 0 },
{ src: '/media/audio/AlbertKader_Whiptails/04_Snare.opus', start: 6, duration: 4, offset: 4 },
],
});
addLog('addTrack(multi-clip) → ' + t.trackId.slice(0, 8), 'ok');
} catch (err) {
addLog('addTrack rejected: ' + String(err), 'err');
}
});
document.getElementById('add-track-empty').addEventListener('click', async () => {
try {
const t = await editor.addTrack({ name: 'Empty track' });
addLog('addTrack({}) → ' + t.trackId.slice(0, 8), 'ok');
} catch (err) {
addLog('addTrack rejected: ' + String(err), 'err');
}
});
document.getElementById('update-track-vol').addEventListener('click', () => {
const id = lastTrackId();
if (!id) {
addLog('no track to update', 'err');
return;
}
editor.updateTrack(id, { volume: 0.4 });
addLog('updateTrack(' + id.slice(0, 8) + ', {volume: 0.4})', 'ok');
});
document.getElementById('remove-track-last').addEventListener('click', () => {
const id = lastTrackId();
if (!id) {
addLog('no track to remove', 'err');
return;
}
editor.removeTrack(id);
addLog('removeTrack(' + id.slice(0, 8) + ')', 'ok');
});
// --- Clips (imperative) -----------------------------------------------
document.getElementById('add-clip').addEventListener('click', async () => {
const id = lastTrackId();
if (!id) {
addLog('no track for addClip — add a track first', 'err');
return;
}
try {
const clipId = await editor.addClip(id, {
src: '/media/audio/AlbertKader_Whiptails/06_HiHat.opus',
start: 8,
duration: 4,
offset: 0,
name: 'HiHat (addClip)',
});
addLog('addClip → trackId=' + id.slice(0, 8) + ' clipId=' + clipId.slice(0, 8), 'ok');
} catch (err) {
addLog('addClip rejected: ' + String(err), 'err');
}
});
document.getElementById('update-clip-shift').addEventListener('click', () => {
const id = lastTrackId();
if (!id) return addLog('no track', 'err');
const t = editor.engine.getState().tracks.find((tr) => tr.id === id);
const clip = t?.clips[t.clips.length - 1];
if (!clip) return addLog('no clip', 'err');
const sr = clip.sampleRate ?? 48000;
const newStart = clip.startSample / sr + 1;
editor.updateClip(id, clip.id, { start: newStart });
addLog(
'updateClip(' + id.slice(0, 8) + ', ' + clip.id.slice(0, 8) + ', {start: ' + newStart.toFixed(2) + '})',
'ok'
);
});
document.getElementById('remove-clip-first').addEventListener('click', () => {
const id = lastTrackId();
if (!id) return addLog('no track', 'err');
const t = editor.engine.getState().tracks.find((tr) => tr.id === id);
const clip = t?.clips[0];
if (!clip) return addLog('no clip', 'err');
editor.removeClip(id, clip.id);
addLog('removeClip(' + id.slice(0, 8) + ', ' + clip.id.slice(0, 8) + ')', 'ok');
});
// --- Tracks (declarative DOM) -----------------------------------------
document.getElementById('dom-append-track').addEventListener('click', () => {
const t = document.createElement('daw-track');
t.setAttribute('src', '/media/audio/AlbertKader_Whiptails/05_Claps.opus');
t.setAttribute('name', 'Claps (DOM)');
editor.appendChild(t);
addLog('appended <daw-track src=...> id=' + t.trackId.slice(0, 8));
});
document.getElementById('dom-mutate-track-vol').addEventListener('click', () => {
const trackEl = editor.querySelector('daw-track');
if (!trackEl) return addLog('no <daw-track> in DOM', 'err');
trackEl.volume = 0.2;
addLog('mutated <daw-track>.volume = 0.2 (fires daw-track-update)');
});
// --- Clips (declarative DOM) ------------------------------------------
document.getElementById('dom-append-clip').addEventListener('click', () => {
const trackEl = editor.querySelector('daw-track');
if (!trackEl) return addLog('no <daw-track> in DOM — append a track first', 'err');
const c = document.createElement('daw-clip');
c.setAttribute('src', '/media/audio/AlbertKader_Whiptails/04_Snare.opus');
c.setAttribute('start', '12');
c.setAttribute('duration', '4');
c.setAttribute('offset', '0');
trackEl.appendChild(c);
addLog('appended <daw-clip> to first track — clipId=' + c.clipId.slice(0, 8));
});
document.getElementById('dom-mutate-clip-start').addEventListener('click', () => {
const clipEl = editor.querySelector('daw-clip');
if (!clipEl) return addLog('no <daw-clip> in DOM', 'err');
const newStart = (Number(clipEl.start || 0) + 1).toFixed(2);
clipEl.setAttribute('start', newStart);
addLog('mutated <daw-clip>.start = ' + newStart + ' (fires daw-clip-update)');
});
// --- State helpers -----------------------------------------------------
document.getElementById('dump').addEventListener('click', () => {
const desc = editor.tracks;
const eng = editor.engine?.getState().tracks ?? [];
addLog(
'descriptors=' + desc.length +
' engineTracks=' + eng.length +
' clips=' + eng.reduce((sum, t) => sum + t.clips.length, 0) +
(desc.length !== eng.length ? ' MISMATCH' : ''),
desc.length !== eng.length ? 'err' : ''
);
});
document.getElementById('clear').addEventListener('click', () => {
const ids = editor.engine?.getState().tracks.map((t) => t.id) ?? [];
ids.forEach((id) => editor.removeTrack(id));
addLog('cleared ' + ids.length + ' track(s)', 'ok');
});
</script>
</body>
</html>