@hanzo/studio 0.1.8 → 0.1.11
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/lib/app/director.d.ts +137 -0
- package/lib/app/director.js +1263 -0
- package/lib/app/flow.d.ts +58 -0
- package/lib/app/flow.js +948 -0
- package/lib/chat/turn.js +19 -11
- package/lib/home.d.ts +3 -1
- package/lib/home.js +3 -3
- package/lib/index.d.ts +6 -0
- package/lib/index.js +5 -0
- package/lib/kinds/app.d.ts +40 -0
- package/lib/kinds/app.js +907 -0
- package/lib/kinds/document.js +256 -19
- package/lib/kinds/image.d.ts +3 -0
- package/lib/kinds/image.js +82 -10
- package/lib/kinds/index.js +2 -1
- package/lib/kinds/sound.js +109 -10
- package/lib/kinds/video.js +146 -17
- package/lib/live.d.ts +11 -0
- package/lib/live.js +10 -0
- package/lib/made.d.ts +2 -1
- package/lib/made.js +1 -0
- package/lib/pdf.js +1 -1
- package/lib/places.d.ts +0 -3
- package/lib/places.js +1 -11
- package/lib/route.d.ts +12 -0
- package/lib/route.js +17 -0
- package/lib/starters.d.ts +21 -0
- package/lib/starters.js +33 -2
- package/lib/studio.js +1 -1
- package/lib/talk.js +61 -23
- package/lib/wire.d.ts +2 -6
- package/lib/wire.js +16 -3
- package/package.json +1 -1
package/lib/kinds/app.js
ADDED
|
@@ -0,0 +1,907 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Interactive visual & audio applications (`app` kind): a complete, self-contained
|
|
4
|
+
* HTML5 / Canvas / WebAudio / SVG experience rendered live in a sandboxed iframe tab.
|
|
5
|
+
*
|
|
6
|
+
* When a person uploads a music or audio file (or creates music in a tab) and asks
|
|
7
|
+
* to "make a full-blown music app with it", or asks for an interactive visualizer,
|
|
8
|
+
* synth pad, micro-site, or interactive visual experience, `make_app` embeds any
|
|
9
|
+
* referenced audio (`window.__STUDIO_AUDIO__`) and image (`window.__STUDIO_IMAGE__`)
|
|
10
|
+
* as live data: URLs and generates a studio-grade interactive application.
|
|
11
|
+
*/
|
|
12
|
+
import { SizableText, XStack, YStack } from '@hanzo/gui';
|
|
13
|
+
import { Sparkles } from '@hanzogui/lucide-icons-2';
|
|
14
|
+
import { useEffect, useState } from 'react';
|
|
15
|
+
import { current, filename } from '../made.js';
|
|
16
|
+
import { data } from '../raster.js';
|
|
17
|
+
import { Row, Text1, TextN } from '../ui.js';
|
|
18
|
+
import { read, structured } from '../wire.js';
|
|
19
|
+
import { num, str } from './kind.js';
|
|
20
|
+
import { PRESETS, settle } from './type/system.js';
|
|
21
|
+
const PRESET_LIST = ['music-app', 'visualizer', 'interactive-visual', 'synth-studio'];
|
|
22
|
+
function parseSpec(v) {
|
|
23
|
+
const s = v;
|
|
24
|
+
return {
|
|
25
|
+
preset: PRESET_LIST.includes(s.preset ?? '') ? s.preset : 'music-app',
|
|
26
|
+
title: str(s.title, 'Sonic Studio'),
|
|
27
|
+
subtitle: str(s.subtitle, 'Interactive Audio & Visual Experience'),
|
|
28
|
+
artist: str(s.artist, 'Hanzo Studio'),
|
|
29
|
+
prompt: str(s.prompt, ''),
|
|
30
|
+
accent: str(s.accent, '#f43f5e'),
|
|
31
|
+
secondary: str(s.secondary, '#6366f1'),
|
|
32
|
+
audio: str(s.audio, ''),
|
|
33
|
+
audioName: str(s.audioName, 'Featured Track'),
|
|
34
|
+
image: str(s.image, ''),
|
|
35
|
+
tracks: Array.isArray(s.tracks) && s.tracks.length
|
|
36
|
+
? s.tracks.map((t) => ({
|
|
37
|
+
title: str(t?.title, 'Track 01'),
|
|
38
|
+
duration: str(t?.duration, '3:24'),
|
|
39
|
+
bpm: num(t?.bpm, 124, 60, 200),
|
|
40
|
+
key: str(t?.key, 'A minor'),
|
|
41
|
+
}))
|
|
42
|
+
: [
|
|
43
|
+
{ title: str(s.audioName, 'Main Mix'), duration: '3:24', bpm: 124, key: 'A minor' },
|
|
44
|
+
{ title: 'Midnight Stem (Bass + Drums)', duration: '2:48', bpm: 124, key: 'A minor' },
|
|
45
|
+
{ title: 'Atmospheric Pad (Ambient)', duration: '4:12', bpm: 96, key: 'C major' },
|
|
46
|
+
],
|
|
47
|
+
customHtml: typeof s.customHtml === 'string' && s.customHtml.trim() ? s.customHtml : undefined,
|
|
48
|
+
system: settle((s.system ?? {}), PRESETS[0]),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]);
|
|
52
|
+
/**
|
|
53
|
+
* Build the self-contained HTML5 application with embedded audio/image data URLs.
|
|
54
|
+
* Supports:
|
|
55
|
+
* - Real uploaded audio playback via HTML5 `<audio>` + WebAudio `AudioContext` + `AnalyserNode`
|
|
56
|
+
* - Built-in procedural WebAudio polyphonic synth + drum engine when no external audio file is playing
|
|
57
|
+
* - 3-Band EQ (Low / Mid / High BiquadFilterNodes)
|
|
58
|
+
* - Playback speed / pitch control (0.5x – 1.5x)
|
|
59
|
+
* - Real-time 60fps Canvas visualizer (4 modes: Spectrum Bars, Radial Orb, Oscilloscope Wave, Particle Nebula)
|
|
60
|
+
* - Interactive waveform scrubber + playlist + downloadable audio/HTML
|
|
61
|
+
*/
|
|
62
|
+
export function buildAppHtml(s, audioUrl, imageUrl) {
|
|
63
|
+
const safeSpec = JSON.stringify({
|
|
64
|
+
preset: s.preset,
|
|
65
|
+
title: s.title,
|
|
66
|
+
subtitle: s.subtitle,
|
|
67
|
+
artist: s.artist,
|
|
68
|
+
prompt: s.prompt,
|
|
69
|
+
accent: s.accent,
|
|
70
|
+
secondary: s.secondary,
|
|
71
|
+
audioName: s.audioName,
|
|
72
|
+
tracks: s.tracks,
|
|
73
|
+
}).replace(/</g, '\\u003c');
|
|
74
|
+
return `<!doctype html>
|
|
75
|
+
<html lang="en">
|
|
76
|
+
<head>
|
|
77
|
+
<meta charset="utf-8" />
|
|
78
|
+
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
79
|
+
<title>${esc(s.title)} — ${esc(s.artist)}</title>
|
|
80
|
+
<style>
|
|
81
|
+
:root {
|
|
82
|
+
--accent: ${esc(s.accent)};
|
|
83
|
+
--secondary: ${esc(s.secondary)};
|
|
84
|
+
--bg: #07070b;
|
|
85
|
+
--panel: rgba(18, 18, 26, 0.78);
|
|
86
|
+
--border: rgba(255, 255, 255, 0.1);
|
|
87
|
+
--text: #f8fafc;
|
|
88
|
+
--muted: #94a3b8;
|
|
89
|
+
}
|
|
90
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
91
|
+
html, body {
|
|
92
|
+
width: 100%; height: 100%; overflow: hidden;
|
|
93
|
+
background: var(--bg); color: var(--text);
|
|
94
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', sans-serif;
|
|
95
|
+
user-select: none;
|
|
96
|
+
}
|
|
97
|
+
#stage {
|
|
98
|
+
position: fixed; inset: 0; width: 100%; height: 100%;
|
|
99
|
+
z-index: 0; cursor: crosshair;
|
|
100
|
+
}
|
|
101
|
+
.shell {
|
|
102
|
+
position: relative; z-index: 1;
|
|
103
|
+
display: grid;
|
|
104
|
+
grid-template-columns: 320px 1fr 300px;
|
|
105
|
+
grid-template-rows: auto 1fr auto;
|
|
106
|
+
height: 100vh; gap: 14px; padding: 16px;
|
|
107
|
+
pointer-events: none;
|
|
108
|
+
}
|
|
109
|
+
@media (max-width: 980px) {
|
|
110
|
+
.shell { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; overflow-y: auto; }
|
|
111
|
+
.side-right { display: none; }
|
|
112
|
+
}
|
|
113
|
+
.card {
|
|
114
|
+
background: var(--panel);
|
|
115
|
+
backdrop-filter: blur(20px);
|
|
116
|
+
-webkit-backdrop-filter: blur(20px);
|
|
117
|
+
border: 1px solid var(--border);
|
|
118
|
+
border-radius: 16px;
|
|
119
|
+
padding: 16px;
|
|
120
|
+
pointer-events: auto;
|
|
121
|
+
box-shadow: 0 20px 50px rgba(0,0,0,0.45);
|
|
122
|
+
}
|
|
123
|
+
.top-bar {
|
|
124
|
+
grid-column: 1 / -1;
|
|
125
|
+
display: flex; align-items: center; justify-content: space-between;
|
|
126
|
+
gap: 12px; padding: 12px 18px;
|
|
127
|
+
}
|
|
128
|
+
.brand { display: flex; align-items: center; gap: 12px; }
|
|
129
|
+
.pulse-dot {
|
|
130
|
+
width: 10px; height: 10px; border-radius: 999px;
|
|
131
|
+
background: var(--accent);
|
|
132
|
+
box-shadow: 0 0 14px var(--accent);
|
|
133
|
+
}
|
|
134
|
+
.title-main { font-size: 16px; font-weight: 700; letter-spacing: -0.02em; }
|
|
135
|
+
.subtitle-main { font-size: 12px; color: var(--muted); }
|
|
136
|
+
.mode-pills { display: flex; gap: 6px; flex-wrap: wrap; }
|
|
137
|
+
.pill {
|
|
138
|
+
background: rgba(255,255,255,0.06); color: var(--muted);
|
|
139
|
+
border: 1px solid rgba(255,255,255,0.08);
|
|
140
|
+
padding: 6px 12px; border-radius: 999px; font-size: 12px; font-weight: 500;
|
|
141
|
+
cursor: pointer; transition: all 0.15s ease;
|
|
142
|
+
}
|
|
143
|
+
.pill:hover { color: var(--text); background: rgba(255,255,255,0.12); }
|
|
144
|
+
.pill.active {
|
|
145
|
+
background: var(--accent); color: #fff; border-color: var(--accent);
|
|
146
|
+
box-shadow: 0 0 18px rgba(244, 63, 94, 0.4);
|
|
147
|
+
}
|
|
148
|
+
.side-left { display: flex; flex-direction: column; gap: 14px; overflow-y: auto; }
|
|
149
|
+
.side-right { display: flex; flex-direction: column; gap: 14px; overflow-y: auto; }
|
|
150
|
+
.center-space { display: flex; align-items: flex-end; justify-content: center; pointer-events: none; }
|
|
151
|
+
.cover-box {
|
|
152
|
+
width: 100%; aspect-ratio: 1 / 1; border-radius: 12px; overflow: hidden;
|
|
153
|
+
position: relative; background: linear-gradient(135deg, var(--accent), var(--secondary));
|
|
154
|
+
display: flex; align-items: center; justify-content: center;
|
|
155
|
+
border: 1px solid rgba(255,255,255,0.12);
|
|
156
|
+
}
|
|
157
|
+
.cover-box img { width: 100%; height: 100%; object-fit: cover; }
|
|
158
|
+
.cover-overlay {
|
|
159
|
+
position: absolute; inset: 0;
|
|
160
|
+
background: linear-gradient(to top, rgba(7,7,11,0.9) 0%, transparent 65%);
|
|
161
|
+
display: flex; flex-direction: column; justify-content: flex-end; padding: 14px;
|
|
162
|
+
}
|
|
163
|
+
.section-label {
|
|
164
|
+
font-size: 10px; font-weight: 700; text-transform: uppercase;
|
|
165
|
+
letter-spacing: 0.1em; color: var(--muted); margin-bottom: 8px;
|
|
166
|
+
}
|
|
167
|
+
.track-list { display: flex; flex-direction: column; gap: 6px; }
|
|
168
|
+
.track-item {
|
|
169
|
+
display: flex; align-items: center; justify-content: space-between;
|
|
170
|
+
padding: 9px 11px; border-radius: 10px; cursor: pointer;
|
|
171
|
+
background: rgba(255,255,255,0.03); border: 1px solid transparent;
|
|
172
|
+
transition: all 0.15s ease;
|
|
173
|
+
}
|
|
174
|
+
.track-item:hover { background: rgba(255,255,255,0.08); }
|
|
175
|
+
.track-item.active {
|
|
176
|
+
background: rgba(244,63,94,0.14); border-color: var(--accent);
|
|
177
|
+
}
|
|
178
|
+
.track-name { font-size: 13px; font-weight: 600; }
|
|
179
|
+
.track-meta { font-size: 11px; color: var(--muted); }
|
|
180
|
+
.bottom-bar {
|
|
181
|
+
grid-column: 1 / -1;
|
|
182
|
+
display: flex; flex-direction: column; gap: 10px;
|
|
183
|
+
}
|
|
184
|
+
.scrubber-wrap {
|
|
185
|
+
display: flex; align-items: center; gap: 12px;
|
|
186
|
+
}
|
|
187
|
+
.time-readout { font-size: 11px; font-variant-numeric: tabular-nums; color: var(--muted); width: 42px; }
|
|
188
|
+
#waveCanvas {
|
|
189
|
+
flex: 1; height: 42px; border-radius: 8px;
|
|
190
|
+
background: rgba(255,255,255,0.04); cursor: pointer;
|
|
191
|
+
}
|
|
192
|
+
.transport {
|
|
193
|
+
display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
|
|
194
|
+
}
|
|
195
|
+
.transport-buttons { display: flex; align-items: center; gap: 10px; }
|
|
196
|
+
.btn-play {
|
|
197
|
+
width: 44px; height: 44px; border-radius: 999px; border: none;
|
|
198
|
+
background: var(--accent); color: #fff; font-size: 16px; font-weight: 700;
|
|
199
|
+
cursor: pointer; display: flex; align-items: center; justify-content: center;
|
|
200
|
+
box-shadow: 0 0 24px rgba(244,63,94,0.45);
|
|
201
|
+
transition: transform 0.12s ease;
|
|
202
|
+
}
|
|
203
|
+
.btn-play:hover { transform: scale(1.06); }
|
|
204
|
+
.btn-icon {
|
|
205
|
+
height: 34px; padding: 0 12px; border-radius: 8px;
|
|
206
|
+
border: 1px solid var(--border); background: rgba(255,255,255,0.05);
|
|
207
|
+
color: var(--text); font-size: 12px; cursor: pointer;
|
|
208
|
+
}
|
|
209
|
+
.btn-icon:hover { background: rgba(255,255,255,0.12); }
|
|
210
|
+
.slider-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
|
|
211
|
+
.slider-head { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }
|
|
212
|
+
input[type=range] {
|
|
213
|
+
width: 100%; accent-color: var(--accent); cursor: pointer;
|
|
214
|
+
}
|
|
215
|
+
.pad-grid {
|
|
216
|
+
display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
|
|
217
|
+
}
|
|
218
|
+
.synth-pad {
|
|
219
|
+
padding: 12px 8px; border-radius: 10px; border: 1px solid var(--border);
|
|
220
|
+
background: rgba(255,255,255,0.04); color: var(--text);
|
|
221
|
+
font-size: 12px; font-weight: 600; text-align: center; cursor: pointer;
|
|
222
|
+
transition: all 0.1s ease;
|
|
223
|
+
}
|
|
224
|
+
.synth-pad:active, .synth-pad.hit {
|
|
225
|
+
background: var(--accent); color: #fff; border-color: var(--accent);
|
|
226
|
+
transform: scale(0.96);
|
|
227
|
+
}
|
|
228
|
+
.file-drop {
|
|
229
|
+
border: 1px dashed rgba(255,255,255,0.2); border-radius: 10px;
|
|
230
|
+
padding: 10px; text-align: center; font-size: 11px; color: var(--muted);
|
|
231
|
+
cursor: pointer;
|
|
232
|
+
}
|
|
233
|
+
.file-drop:hover { border-color: var(--accent); color: var(--text); }
|
|
234
|
+
</style>
|
|
235
|
+
</head>
|
|
236
|
+
<body>
|
|
237
|
+
<canvas id="stage"></canvas>
|
|
238
|
+
|
|
239
|
+
<div class="shell">
|
|
240
|
+
<div class="card top-bar">
|
|
241
|
+
<div class="brand">
|
|
242
|
+
<div class="pulse-dot" id="liveDot"></div>
|
|
243
|
+
<div>
|
|
244
|
+
<div class="title-main" id="appTitle">${esc(s.title)}</div>
|
|
245
|
+
<div class="subtitle-main" id="appSub">${esc(s.artist)} · ${esc(s.subtitle)}</div>
|
|
246
|
+
</div>
|
|
247
|
+
</div>
|
|
248
|
+
<div class="mode-pills" id="modeBar">
|
|
249
|
+
<button class="pill active" data-mode="bars">Spectrum</button>
|
|
250
|
+
<button class="pill" data-mode="orb">Radial Orb</button>
|
|
251
|
+
<button class="pill" data-mode="wave">Oscilloscope</button>
|
|
252
|
+
<button class="pill" data-mode="particles">Nebula</button>
|
|
253
|
+
</div>
|
|
254
|
+
</div>
|
|
255
|
+
|
|
256
|
+
<div class="side-left">
|
|
257
|
+
<div class="card">
|
|
258
|
+
<div class="cover-box" id="coverBox">
|
|
259
|
+
${imageUrl ? `<img id="coverImg" src="${imageUrl}" alt="Cover" />` : `<div style="font-size:48px;font-weight:800;opacity:0.35">♫</div>`}
|
|
260
|
+
<div class="cover-overlay">
|
|
261
|
+
<div style="font-size:15px;font-weight:700" id="nowTitle">${esc(s.tracks[0]?.title ?? s.audioName)}</div>
|
|
262
|
+
<div style="font-size:12px;color:var(--muted)" id="nowMeta">${esc(s.artist)} · ${s.tracks[0]?.bpm ?? 124} BPM · ${esc(s.tracks[0]?.key ?? 'A minor')}</div>
|
|
263
|
+
</div>
|
|
264
|
+
</div>
|
|
265
|
+
</div>
|
|
266
|
+
|
|
267
|
+
<div class="card" style="flex:1">
|
|
268
|
+
<div class="section-label">Tracks & Stems</div>
|
|
269
|
+
<div class="track-list" id="trackList"></div>
|
|
270
|
+
<label class="file-drop" style="margin-top:12px;display:block">
|
|
271
|
+
+ Load another audio file (.mp3, .wav, .ogg)
|
|
272
|
+
<input type="file" id="audioInput" accept="audio/*" style="display:none" />
|
|
273
|
+
</label>
|
|
274
|
+
</div>
|
|
275
|
+
</div>
|
|
276
|
+
|
|
277
|
+
<div class="center-space"></div>
|
|
278
|
+
|
|
279
|
+
<div class="side-right">
|
|
280
|
+
<div class="card">
|
|
281
|
+
<div class="section-label">3-Band Mastering EQ & Speed</div>
|
|
282
|
+
<div class="slider-row">
|
|
283
|
+
<div class="slider-head"><span>Sub / Bass (100 Hz)</span><span id="eqLowVal">0 dB</span></div>
|
|
284
|
+
<input type="range" id="eqLow" min="-12" max="12" step="0.5" value="0" />
|
|
285
|
+
</div>
|
|
286
|
+
<div class="slider-row">
|
|
287
|
+
<div class="slider-head"><span>Presence / Mid (1 kHz)</span><span id="eqMidVal">0 dB</span></div>
|
|
288
|
+
<input type="range" id="eqMid" min="-12" max="12" step="0.5" value="0" />
|
|
289
|
+
</div>
|
|
290
|
+
<div class="slider-row">
|
|
291
|
+
<div class="slider-head"><span>Air / High (8 kHz)</span><span id="eqHighVal">0 dB</span></div>
|
|
292
|
+
<input type="range" id="eqHigh" min="-12" max="12" step="0.5" value="0" />
|
|
293
|
+
</div>
|
|
294
|
+
<div class="slider-row">
|
|
295
|
+
<div class="slider-head"><span>Tempo / Rate</span><span id="rateVal">1.00×</span></div>
|
|
296
|
+
<input type="range" id="rateSlider" min="0.5" max="1.5" step="0.02" value="1" />
|
|
297
|
+
</div>
|
|
298
|
+
</div>
|
|
299
|
+
|
|
300
|
+
<div class="card" style="flex:1">
|
|
301
|
+
<div class="section-label">Live Synth & FX Pads (Keys 1–6)</div>
|
|
302
|
+
<div class="pad-grid" id="padGrid">
|
|
303
|
+
<button class="synth-pad" data-note="220" data-type="chord">Am9 Chord</button>
|
|
304
|
+
<button class="synth-pad" data-note="174.61" data-type="chord">Fmaj7 Pad</button>
|
|
305
|
+
<button class="synth-pad" data-note="261.63" data-type="chord">Cmaj9 Swell</button>
|
|
306
|
+
<button class="synth-pad" data-note="196.00" data-type="chord">G6 Arp</button>
|
|
307
|
+
<button class="synth-pad" data-note="55" data-type="sub">808 Sub Drop</button>
|
|
308
|
+
<button class="synth-pad" data-note="880" data-type="riser">Shimmer Bell</button>
|
|
309
|
+
</div>
|
|
310
|
+
</div>
|
|
311
|
+
</div>
|
|
312
|
+
|
|
313
|
+
<div class="card bottom-bar">
|
|
314
|
+
<div class="scrubber-wrap">
|
|
315
|
+
<span class="time-readout" id="curTime">0:00</span>
|
|
316
|
+
<canvas id="waveCanvas" width="900" height="42"></canvas>
|
|
317
|
+
<span class="time-readout" id="durTime">3:24</span>
|
|
318
|
+
</div>
|
|
319
|
+
<div class="transport">
|
|
320
|
+
<div class="transport-buttons">
|
|
321
|
+
<button class="btn-icon" id="prevBtn">⏮</button>
|
|
322
|
+
<button class="btn-play" id="playBtn" aria-label="Play or Pause">▶</button>
|
|
323
|
+
<button class="btn-icon" id="nextBtn">⏭</button>
|
|
324
|
+
<button class="btn-icon" id="synthToggle">Synth Groove: Auto</button>
|
|
325
|
+
</div>
|
|
326
|
+
<div style="display:flex;align-items:center;gap:12px">
|
|
327
|
+
<span style="font-size:12px;color:var(--muted)">Volume</span>
|
|
328
|
+
<input type="range" id="volSlider" min="0" max="1" step="0.01" value="0.88" style="width:110px" />
|
|
329
|
+
</div>
|
|
330
|
+
</div>
|
|
331
|
+
</div>
|
|
332
|
+
</div>
|
|
333
|
+
|
|
334
|
+
<audio id="audioEl" crossorigin="anonymous" preload="auto"></audio>
|
|
335
|
+
|
|
336
|
+
<script>
|
|
337
|
+
(function() {
|
|
338
|
+
const SPEC = ${safeSpec};
|
|
339
|
+
const INITIAL_AUDIO = ${JSON.stringify(audioUrl || '')};
|
|
340
|
+
const audioEl = document.getElementById('audioEl');
|
|
341
|
+
const playBtn = document.getElementById('playBtn');
|
|
342
|
+
const stage = document.getElementById('stage');
|
|
343
|
+
const ctx2d = stage.getContext('2d');
|
|
344
|
+
const waveCanvas = document.getElementById('waveCanvas');
|
|
345
|
+
const waveCtx = waveCanvas.getContext('2d');
|
|
346
|
+
const curTimeEl = document.getElementById('curTime');
|
|
347
|
+
const durTimeEl = document.getElementById('durTime');
|
|
348
|
+
|
|
349
|
+
let mode = 'bars';
|
|
350
|
+
let activeTrack = 0;
|
|
351
|
+
let hasExternalAudio = Boolean(INITIAL_AUDIO);
|
|
352
|
+
if (INITIAL_AUDIO) {
|
|
353
|
+
audioEl.src = INITIAL_AUDIO;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
let actx = null;
|
|
357
|
+
let analyser = null;
|
|
358
|
+
let gainNode = null;
|
|
359
|
+
let lowEq = null, midEq = null, highEq = null;
|
|
360
|
+
let mediaSource = null;
|
|
361
|
+
let isPlaying = false;
|
|
362
|
+
let synthTimer = null;
|
|
363
|
+
let synthClock = 0;
|
|
364
|
+
let synthElapsed = 0;
|
|
365
|
+
|
|
366
|
+
function ensureAudio() {
|
|
367
|
+
if (actx) return;
|
|
368
|
+
const AC = window.AudioContext || window.webkitAudioContext;
|
|
369
|
+
actx = new AC();
|
|
370
|
+
analyser = actx.createAnalyser();
|
|
371
|
+
analyser.fftSize = 512;
|
|
372
|
+
analyser.smoothingTimeConstant = 0.82;
|
|
373
|
+
|
|
374
|
+
lowEq = actx.createBiquadFilter();
|
|
375
|
+
lowEq.type = 'lowshelf';
|
|
376
|
+
lowEq.frequency.value = 100;
|
|
377
|
+
|
|
378
|
+
midEq = actx.createBiquadFilter();
|
|
379
|
+
midEq.type = 'peaking';
|
|
380
|
+
midEq.frequency.value = 1000;
|
|
381
|
+
midEq.Q.value = 1;
|
|
382
|
+
|
|
383
|
+
highEq = actx.createBiquadFilter();
|
|
384
|
+
highEq.type = 'highshelf';
|
|
385
|
+
highEq.frequency.value = 8000;
|
|
386
|
+
|
|
387
|
+
gainNode = actx.createGain();
|
|
388
|
+
gainNode.gain.value = 0.88;
|
|
389
|
+
|
|
390
|
+
lowEq.connect(midEq);
|
|
391
|
+
midEq.connect(highEq);
|
|
392
|
+
highEq.connect(gainNode);
|
|
393
|
+
gainNode.connect(analyser);
|
|
394
|
+
analyser.connect(actx.destination);
|
|
395
|
+
|
|
396
|
+
try {
|
|
397
|
+
mediaSource = actx.createMediaElementSource(audioEl);
|
|
398
|
+
mediaSource.connect(lowEq);
|
|
399
|
+
} catch (e) {}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function triggerPad(freq, type) {
|
|
403
|
+
ensureAudio();
|
|
404
|
+
if (actx.state === 'suspended') actx.resume();
|
|
405
|
+
const now = actx.currentTime;
|
|
406
|
+
const ratios = type === 'chord' ? [1, 1.2, 1.5, 1.8] : type === 'sub' ? [1, 0.5] : [1, 1.5, 2];
|
|
407
|
+
ratios.forEach((r, idx) => {
|
|
408
|
+
const osc = actx.createOscillator();
|
|
409
|
+
const g = actx.createGain();
|
|
410
|
+
osc.type = type === 'sub' ? 'sine' : idx % 2 === 0 ? 'triangle' : 'sawtooth';
|
|
411
|
+
osc.frequency.setValueAtTime(freq * r, now);
|
|
412
|
+
if (type === 'sub') osc.frequency.exponentialRampToValueAtTime(32, now + 0.55);
|
|
413
|
+
g.gain.setValueAtTime(0.001, now);
|
|
414
|
+
g.gain.exponentialRampToValueAtTime(type === 'sub' ? 0.35 : 0.12, now + 0.03);
|
|
415
|
+
g.gain.exponentialRampToValueAtTime(0.0008, now + (type === 'chord' ? 1.4 : 0.65));
|
|
416
|
+
osc.connect(g);
|
|
417
|
+
g.connect(lowEq);
|
|
418
|
+
osc.start(now);
|
|
419
|
+
osc.stop(now + 1.5);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function startProceduralGroove() {
|
|
424
|
+
stopProceduralGroove();
|
|
425
|
+
const bpm = (SPEC.tracks[activeTrack] && SPEC.tracks[activeTrack].bpm) || 124;
|
|
426
|
+
const stepMs = (60 / bpm / 2) * 1000;
|
|
427
|
+
const roots = [220, 174.61, 261.63, 196.0];
|
|
428
|
+
synthTimer = setInterval(() => {
|
|
429
|
+
if (!isPlaying || hasExternalAudio) return;
|
|
430
|
+
ensureAudio();
|
|
431
|
+
const step = synthClock % 16;
|
|
432
|
+
const bar = Math.floor(synthClock / 16) % roots.length;
|
|
433
|
+
const root = roots[bar];
|
|
434
|
+
if (step === 0 || step === 6 || step === 10) triggerPad(root * 0.5, 'sub');
|
|
435
|
+
if (step === 0 || step === 8) triggerPad(root, 'chord');
|
|
436
|
+
if (step % 2 === 0) triggerPad(root * (step % 4 === 0 ? 2 : 1.5), 'riser');
|
|
437
|
+
synthClock++;
|
|
438
|
+
synthElapsed += stepMs / 1000;
|
|
439
|
+
}, stepMs);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function stopProceduralGroove() {
|
|
443
|
+
if (synthTimer) {
|
|
444
|
+
clearInterval(synthTimer);
|
|
445
|
+
synthTimer = null;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function togglePlay() {
|
|
450
|
+
ensureAudio();
|
|
451
|
+
if (actx.state === 'suspended') actx.resume();
|
|
452
|
+
if (isPlaying) {
|
|
453
|
+
isPlaying = false;
|
|
454
|
+
audioEl.pause();
|
|
455
|
+
stopProceduralGroove();
|
|
456
|
+
playBtn.textContent = '▶';
|
|
457
|
+
} else {
|
|
458
|
+
isPlaying = true;
|
|
459
|
+
playBtn.textContent = '⏸';
|
|
460
|
+
if (hasExternalAudio) {
|
|
461
|
+
audioEl.play().catch(() => {
|
|
462
|
+
hasExternalAudio = false;
|
|
463
|
+
startProceduralGroove();
|
|
464
|
+
});
|
|
465
|
+
} else {
|
|
466
|
+
startProceduralGroove();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
playBtn.addEventListener('click', togglePlay);
|
|
472
|
+
|
|
473
|
+
function renderTracks() {
|
|
474
|
+
const list = document.getElementById('trackList');
|
|
475
|
+
list.innerHTML = '';
|
|
476
|
+
SPEC.tracks.forEach((t, i) => {
|
|
477
|
+
const row = document.createElement('div');
|
|
478
|
+
row.className = 'track-item' + (i === activeTrack ? ' active' : '');
|
|
479
|
+
row.innerHTML = '<div><div class="track-name">' + t.title + '</div><div class="track-meta">' + t.bpm + ' BPM · ' + t.key + '</div></div><div class="track-meta">' + t.duration + '</div>';
|
|
480
|
+
row.addEventListener('click', () => {
|
|
481
|
+
activeTrack = i;
|
|
482
|
+
document.getElementById('nowTitle').textContent = t.title;
|
|
483
|
+
document.getElementById('nowMeta').textContent = SPEC.artist + ' · ' + t.bpm + ' BPM · ' + t.key;
|
|
484
|
+
renderTracks();
|
|
485
|
+
if (!isPlaying) togglePlay();
|
|
486
|
+
else if (!hasExternalAudio) startProceduralGroove();
|
|
487
|
+
});
|
|
488
|
+
list.appendChild(row);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
renderTracks();
|
|
492
|
+
|
|
493
|
+
document.getElementById('prevBtn').addEventListener('click', () => {
|
|
494
|
+
activeTrack = (activeTrack - 1 + SPEC.tracks.length) % SPEC.tracks.length;
|
|
495
|
+
renderTracks();
|
|
496
|
+
});
|
|
497
|
+
document.getElementById('nextBtn').addEventListener('click', () => {
|
|
498
|
+
activeTrack = (activeTrack + 1) % SPEC.tracks.length;
|
|
499
|
+
renderTracks();
|
|
500
|
+
});
|
|
501
|
+
document.getElementById('synthToggle').addEventListener('click', (e) => {
|
|
502
|
+
hasExternalAudio = !hasExternalAudio && Boolean(audioEl.src);
|
|
503
|
+
e.currentTarget.textContent = hasExternalAudio ? 'Source: Uploaded Audio' : 'Source: Live WebAudio Synth';
|
|
504
|
+
if (isPlaying) {
|
|
505
|
+
if (hasExternalAudio) { stopProceduralGroove(); audioEl.play(); }
|
|
506
|
+
else { audioEl.pause(); startProceduralGroove(); }
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
document.getElementById('audioInput').addEventListener('change', (e) => {
|
|
511
|
+
const file = e.target.files && e.target.files[0];
|
|
512
|
+
if (!file) return;
|
|
513
|
+
const u = URL.createObjectURL(file);
|
|
514
|
+
audioEl.src = u;
|
|
515
|
+
hasExternalAudio = true;
|
|
516
|
+
const cleanName = file.name.replace(/\\.[a-z0-9]+$/i, '');
|
|
517
|
+
SPEC.tracks.unshift({ title: cleanName, duration: 'Live', bpm: 124, key: 'Custom' });
|
|
518
|
+
activeTrack = 0;
|
|
519
|
+
document.getElementById('nowTitle').textContent = cleanName;
|
|
520
|
+
renderTracks();
|
|
521
|
+
if (!isPlaying) togglePlay();
|
|
522
|
+
else { stopProceduralGroove(); audioEl.play(); }
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
document.querySelectorAll('#modeBar .pill').forEach((btn) => {
|
|
526
|
+
btn.addEventListener('click', () => {
|
|
527
|
+
document.querySelectorAll('#modeBar .pill').forEach((b) => b.classList.remove('active'));
|
|
528
|
+
btn.classList.add('active');
|
|
529
|
+
mode = btn.getAttribute('data-mode') || 'bars';
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
document.querySelectorAll('.synth-pad').forEach((pad) => {
|
|
534
|
+
pad.addEventListener('click', () => {
|
|
535
|
+
pad.classList.add('hit');
|
|
536
|
+
setTimeout(() => pad.classList.remove('hit'), 160);
|
|
537
|
+
triggerPad(Number(pad.getAttribute('data-note') || 220), pad.getAttribute('data-type') || 'chord');
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
window.addEventListener('keydown', (e) => {
|
|
542
|
+
if (e.code === 'Space' && document.activeElement === document.body) {
|
|
543
|
+
e.preventDefault();
|
|
544
|
+
togglePlay();
|
|
545
|
+
}
|
|
546
|
+
const idx = ['Digit1','Digit2','Digit3','Digit4','Digit5','Digit6'].indexOf(e.code);
|
|
547
|
+
if (idx >= 0) {
|
|
548
|
+
const pads = document.querySelectorAll('.synth-pad');
|
|
549
|
+
if (pads[idx]) pads[idx].click();
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
const bindEq = (id, valId, getNode, unit) => {
|
|
554
|
+
const el = document.getElementById(id);
|
|
555
|
+
const vEl = document.getElementById(valId);
|
|
556
|
+
el.addEventListener('input', () => {
|
|
557
|
+
ensureAudio();
|
|
558
|
+
const v = Number(el.value);
|
|
559
|
+
vEl.textContent = (v > 0 ? '+' : '') + v + unit;
|
|
560
|
+
const n = getNode();
|
|
561
|
+
if (n) n.gain.value = v;
|
|
562
|
+
});
|
|
563
|
+
};
|
|
564
|
+
bindEq('eqLow', 'eqLowVal', () => lowEq, ' dB');
|
|
565
|
+
bindEq('eqMid', 'eqMidVal', () => midEq, ' dB');
|
|
566
|
+
bindEq('eqHigh', 'eqHighVal', () => highEq, ' dB');
|
|
567
|
+
|
|
568
|
+
document.getElementById('rateSlider').addEventListener('input', (e) => {
|
|
569
|
+
const r = Number(e.target.value);
|
|
570
|
+
document.getElementById('rateVal').textContent = r.toFixed(2) + '×';
|
|
571
|
+
audioEl.playbackRate = r;
|
|
572
|
+
});
|
|
573
|
+
document.getElementById('volSlider').addEventListener('input', (e) => {
|
|
574
|
+
ensureAudio();
|
|
575
|
+
if (gainNode) gainNode.gain.value = Number(e.target.value);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
waveCanvas.addEventListener('click', (e) => {
|
|
579
|
+
const rect = waveCanvas.getBoundingClientRect();
|
|
580
|
+
const ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
|
|
581
|
+
if (hasExternalAudio && Number.isFinite(audioEl.duration) && audioEl.duration > 0) {
|
|
582
|
+
audioEl.currentTime = ratio * audioEl.duration;
|
|
583
|
+
} else {
|
|
584
|
+
synthElapsed = ratio * 204;
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
function fmt(sec) {
|
|
589
|
+
if (!Number.isFinite(sec) || sec < 0) return '0:00';
|
|
590
|
+
const m = Math.floor(sec / 60);
|
|
591
|
+
const s = Math.floor(sec % 60);
|
|
592
|
+
return m + ':' + (s < 10 ? '0' : '') + s;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function resize() {
|
|
596
|
+
stage.width = window.innerWidth * window.devicePixelRatio;
|
|
597
|
+
stage.height = window.innerHeight * window.devicePixelRatio;
|
|
598
|
+
}
|
|
599
|
+
window.addEventListener('resize', resize);
|
|
600
|
+
resize();
|
|
601
|
+
|
|
602
|
+
const freqData = new Uint8Array(256);
|
|
603
|
+
const timeData = new Uint8Array(256);
|
|
604
|
+
const particles = Array.from({ length: 90 }, (_, i) => ({
|
|
605
|
+
x: Math.random(),
|
|
606
|
+
y: Math.random(),
|
|
607
|
+
r: 1.5 + Math.random() * 3.5,
|
|
608
|
+
vx: (Math.random() - 0.5) * 0.0012,
|
|
609
|
+
vy: (Math.random() - 0.5) * 0.0012,
|
|
610
|
+
hue: i % 2 === 0 ? SPEC.accent : SPEC.secondary
|
|
611
|
+
}));
|
|
612
|
+
|
|
613
|
+
let pointer = { x: 0.5, y: 0.5 };
|
|
614
|
+
stage.addEventListener('pointermove', (e) => {
|
|
615
|
+
pointer.x = e.clientX / window.innerWidth;
|
|
616
|
+
pointer.y = e.clientY / window.innerHeight;
|
|
617
|
+
});
|
|
618
|
+
stage.addEventListener('pointerdown', (e) => {
|
|
619
|
+
const freq = 110 + (1 - e.clientY / window.innerHeight) * 550;
|
|
620
|
+
triggerPad(freq, 'chord');
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
function drawFrame(ts) {
|
|
624
|
+
const w = stage.width;
|
|
625
|
+
const h = stage.height;
|
|
626
|
+
ctx2d.fillStyle = 'rgba(7, 7, 11, 0.24)';
|
|
627
|
+
ctx2d.fillRect(0, 0, w, h);
|
|
628
|
+
|
|
629
|
+
if (analyser && isPlaying) {
|
|
630
|
+
analyser.getByteFrequencyData(freqData);
|
|
631
|
+
analyser.getByteTimeDomainData(timeData);
|
|
632
|
+
} else {
|
|
633
|
+
for (let i = 0; i < 256; i++) {
|
|
634
|
+
freqData[i] = isPlaying ? Math.floor(80 + 60 * Math.sin(ts * 0.006 + i * 0.15)) : Math.floor(14 + 8 * Math.sin(ts * 0.002 + i * 0.1));
|
|
635
|
+
timeData[i] = 128 + Math.floor(24 * Math.sin(ts * 0.004 + i * 0.08));
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
let bass = 0;
|
|
640
|
+
for (let i = 0; i < 16; i++) bass += freqData[i];
|
|
641
|
+
bass = bass / (16 * 255);
|
|
642
|
+
|
|
643
|
+
const grad = ctx2d.createRadialGradient(w * pointer.x, h * pointer.y, 10, w * 0.5, h * 0.5, Math.max(w, h) * 0.65);
|
|
644
|
+
grad.addColorStop(0, SPEC.accent + '33');
|
|
645
|
+
grad.addColorStop(0.5, SPEC.secondary + '18');
|
|
646
|
+
grad.addColorStop(1, 'transparent');
|
|
647
|
+
ctx2d.fillStyle = grad;
|
|
648
|
+
ctx2d.fillRect(0, 0, w, h);
|
|
649
|
+
|
|
650
|
+
if (mode === 'bars') {
|
|
651
|
+
const count = 72;
|
|
652
|
+
const bw = (w * 0.52) / count;
|
|
653
|
+
const startX = w * 0.24;
|
|
654
|
+
const baseY = h * 0.72;
|
|
655
|
+
for (let i = 0; i < count; i++) {
|
|
656
|
+
const v = freqData[i * 2] / 255;
|
|
657
|
+
const bh = Math.max(8, v * h * 0.42);
|
|
658
|
+
ctx2d.fillStyle = i % 2 === 0 ? SPEC.accent : SPEC.secondary;
|
|
659
|
+
ctx2d.beginPath();
|
|
660
|
+
ctx2d.roundRect(startX + i * bw, baseY - bh, bw * 0.72, bh, 6);
|
|
661
|
+
ctx2d.fill();
|
|
662
|
+
}
|
|
663
|
+
} else if (mode === 'orb') {
|
|
664
|
+
const cx = w * 0.5;
|
|
665
|
+
const cy = h * 0.5;
|
|
666
|
+
const baseR = Math.min(w, h) * (0.14 + bass * 0.08);
|
|
667
|
+
const rays = 96;
|
|
668
|
+
for (let i = 0; i < rays; i++) {
|
|
669
|
+
const angle = (i / rays) * Math.PI * 2 + ts * 0.0004;
|
|
670
|
+
const v = freqData[i % 128] / 255;
|
|
671
|
+
const r2 = baseR + v * Math.min(w, h) * 0.22;
|
|
672
|
+
ctx2d.strokeStyle = i % 2 === 0 ? SPEC.accent : SPEC.secondary;
|
|
673
|
+
ctx2d.lineWidth = 3 * window.devicePixelRatio;
|
|
674
|
+
ctx2d.beginPath();
|
|
675
|
+
ctx2d.moveTo(cx + Math.cos(angle) * baseR, cy + Math.sin(angle) * baseR);
|
|
676
|
+
ctx2d.lineTo(cx + Math.cos(angle) * r2, cy + Math.sin(angle) * r2);
|
|
677
|
+
ctx2d.stroke();
|
|
678
|
+
}
|
|
679
|
+
} else if (mode === 'wave') {
|
|
680
|
+
ctx2d.strokeStyle = SPEC.accent;
|
|
681
|
+
ctx2d.lineWidth = 4 * window.devicePixelRatio;
|
|
682
|
+
ctx2d.beginPath();
|
|
683
|
+
for (let i = 0; i < 256; i++) {
|
|
684
|
+
const x = (i / 255) * w;
|
|
685
|
+
const y = h * 0.5 + ((timeData[i] - 128) / 128) * (h * 0.28);
|
|
686
|
+
if (i === 0) ctx2d.moveTo(x, y);
|
|
687
|
+
else ctx2d.lineTo(x, y);
|
|
688
|
+
}
|
|
689
|
+
ctx2d.stroke();
|
|
690
|
+
} else {
|
|
691
|
+
particles.forEach((p, idx) => {
|
|
692
|
+
p.x = (p.x + p.vx * (1 + bass * 4) + 1) % 1;
|
|
693
|
+
p.y = (p.y + p.vy * (1 + bass * 4) + 1) % 1;
|
|
694
|
+
const v = freqData[idx % 128] / 255;
|
|
695
|
+
ctx2d.fillStyle = p.hue;
|
|
696
|
+
ctx2d.beginPath();
|
|
697
|
+
ctx2d.arc(p.x * w, p.y * h, (p.r + v * 9) * window.devicePixelRatio, 0, Math.PI * 2);
|
|
698
|
+
ctx2d.fill();
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// Scrubber waveform
|
|
703
|
+
const ww = waveCanvas.width;
|
|
704
|
+
const wh = waveCanvas.height;
|
|
705
|
+
waveCtx.clearRect(0, 0, ww, wh);
|
|
706
|
+
const dur = hasExternalAudio && Number.isFinite(audioEl.duration) && audioEl.duration > 0 ? audioEl.duration : 204;
|
|
707
|
+
const cur = hasExternalAudio ? audioEl.currentTime : (synthElapsed % dur);
|
|
708
|
+
curTimeEl.textContent = fmt(cur);
|
|
709
|
+
durTimeEl.textContent = fmt(dur);
|
|
710
|
+
const prog = Math.max(0, Math.min(1, cur / dur));
|
|
711
|
+
|
|
712
|
+
const bars = 90;
|
|
713
|
+
const stepW = ww / bars;
|
|
714
|
+
for (let i = 0; i < bars; i++) {
|
|
715
|
+
const ratio = i / bars;
|
|
716
|
+
const amp = 0.25 + 0.65 * Math.abs(Math.sin(i * 0.43 + 1.7) * Math.cos(i * 0.19));
|
|
717
|
+
const bh = amp * (wh - 8);
|
|
718
|
+
waveCtx.fillStyle = ratio <= prog ? SPEC.accent : 'rgba(255,255,255,0.22)';
|
|
719
|
+
waveCtx.fillRect(i * stepW + 1, (wh - bh) / 2, stepW - 3, bh);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
requestAnimationFrame(drawFrame);
|
|
723
|
+
}
|
|
724
|
+
requestAnimationFrame(drawFrame);
|
|
725
|
+
})();
|
|
726
|
+
</script>
|
|
727
|
+
</body>
|
|
728
|
+
</html>`;
|
|
729
|
+
}
|
|
730
|
+
const SCHEMA = {
|
|
731
|
+
type: 'object',
|
|
732
|
+
additionalProperties: false,
|
|
733
|
+
required: ['preset', 'title', 'subtitle', 'artist', 'accent', 'secondary', 'tracks'],
|
|
734
|
+
properties: {
|
|
735
|
+
preset: { type: 'string', enum: PRESET_LIST },
|
|
736
|
+
title: { type: 'string' },
|
|
737
|
+
subtitle: { type: 'string' },
|
|
738
|
+
artist: { type: 'string' },
|
|
739
|
+
accent: { type: 'string' },
|
|
740
|
+
secondary: { type: 'string' },
|
|
741
|
+
tracks: {
|
|
742
|
+
type: 'array',
|
|
743
|
+
items: {
|
|
744
|
+
type: 'object',
|
|
745
|
+
additionalProperties: false,
|
|
746
|
+
required: ['title', 'duration', 'bpm', 'key'],
|
|
747
|
+
properties: {
|
|
748
|
+
title: { type: 'string' },
|
|
749
|
+
duration: { type: 'string' },
|
|
750
|
+
bpm: { type: 'number' },
|
|
751
|
+
key: { type: 'string' },
|
|
752
|
+
},
|
|
753
|
+
},
|
|
754
|
+
},
|
|
755
|
+
},
|
|
756
|
+
};
|
|
757
|
+
/** Resolve an audio or image Stored file from a tab number or from any tab in the conversation. */
|
|
758
|
+
function findMedia(w, tabNum, mimePrefix) {
|
|
759
|
+
if (tabNum > 0) {
|
|
760
|
+
const m = w.find(tabNum);
|
|
761
|
+
const hit = m ? current(m).files.find((f) => f.mime.startsWith(mimePrefix)) : null;
|
|
762
|
+
if (hit)
|
|
763
|
+
return hit;
|
|
764
|
+
}
|
|
765
|
+
// Scan recent tabs (1..12) for matching media if none explicitly passed
|
|
766
|
+
for (let i = 1; i <= 12; i++) {
|
|
767
|
+
const m = w.find(i);
|
|
768
|
+
if (!m)
|
|
769
|
+
continue;
|
|
770
|
+
const hit = current(m).files.find((f) => f.mime.startsWith(mimePrefix));
|
|
771
|
+
if (hit)
|
|
772
|
+
return hit;
|
|
773
|
+
}
|
|
774
|
+
return null;
|
|
775
|
+
}
|
|
776
|
+
async function renderApp(w, s) {
|
|
777
|
+
w.doing('Building the interactive app');
|
|
778
|
+
const audioUrl = s.audio ? await data(await read(w.wire, s.audio)).catch(() => '') : '';
|
|
779
|
+
const imageUrl = s.image ? await data(await read(w.wire, s.image)).catch(() => '') : '';
|
|
780
|
+
const html = buildAppHtml(s, audioUrl, imageUrl);
|
|
781
|
+
const stored = await w.keep('app.html', new Blob([html], { type: 'text/html' }));
|
|
782
|
+
return [stored];
|
|
783
|
+
}
|
|
784
|
+
export const app = {
|
|
785
|
+
id: 'app',
|
|
786
|
+
label: 'Interactive app',
|
|
787
|
+
blurb: 'Music player & visualizer apps, synth studios, and interactive visual experiences from your files or a prompt.',
|
|
788
|
+
icon: Sparkles,
|
|
789
|
+
tool: {
|
|
790
|
+
name: 'make_app',
|
|
791
|
+
description: 'Create a full-blown interactive HTML5/Canvas/WebAudio app in a tab — such as a music player & visualizer app built around an uploaded music/audio file or generated track, an interactive synth studio, or an interactive visual experience.',
|
|
792
|
+
parameters: {
|
|
793
|
+
type: 'object',
|
|
794
|
+
properties: {
|
|
795
|
+
title: { type: 'string', description: 'Title of the music app or interactive experience.' },
|
|
796
|
+
subtitle: { type: 'string', description: 'Tagline or subtitle.' },
|
|
797
|
+
artist: { type: 'string', description: 'Artist, creator, or brand name.' },
|
|
798
|
+
preset: {
|
|
799
|
+
type: 'string',
|
|
800
|
+
enum: PRESET_LIST,
|
|
801
|
+
description: 'music-app for a full music player + EQ + visualizer + stem deck; visualizer for full-screen audio/visual art; synth-studio for an interactive synthesizer.',
|
|
802
|
+
},
|
|
803
|
+
prompt: { type: 'string', description: 'Visual and musical direction, mood, colours, and features.' },
|
|
804
|
+
audio_tab: { type: 'integer', description: '1-based tab number of an uploaded or generated audio/music file to wire into the app.' },
|
|
805
|
+
image_tab: { type: 'integer', description: '1-based tab number of an uploaded or generated image to use as cover art / visual backdrop.' },
|
|
806
|
+
accent: { type: 'string', description: 'Primary hex colour, e.g. #f43f5e.' },
|
|
807
|
+
secondary: { type: 'string', description: 'Secondary hex colour, e.g. #6366f1.' },
|
|
808
|
+
},
|
|
809
|
+
required: ['title', 'prompt'],
|
|
810
|
+
},
|
|
811
|
+
},
|
|
812
|
+
needs: () => null,
|
|
813
|
+
make: async (w, args) => {
|
|
814
|
+
const audioFile = findMedia(w, num(args.audio_tab, 0), 'audio/');
|
|
815
|
+
const imageFile = findMedia(w, num(args.image_tab, 0), 'image/');
|
|
816
|
+
const prompt = str(args.prompt, str(args.title, 'Interactive music & visualizer app'));
|
|
817
|
+
let enriched = {};
|
|
818
|
+
try {
|
|
819
|
+
w.doing('Designing the app layout & tracklist');
|
|
820
|
+
enriched = await structured(w.wire, w.writer, 'You design interactive music and visual web apps. Given the user prompt and any attached audio/image filename, return a cohesive title, subtitle, artist name, vibrant hex accent & secondary colours, and a 3-4 item track/stem list.', `Prompt: ${prompt}\nAudio file: ${audioFile?.name ?? 'Live WebAudio Synth'}\nTitle hint: ${str(args.title)}`, SCHEMA, w.signal);
|
|
821
|
+
}
|
|
822
|
+
catch {
|
|
823
|
+
// Fallback cleanly to deterministic defaults
|
|
824
|
+
}
|
|
825
|
+
const cleanAudioName = audioFile?.name ? audioFile.name.replace(/\.[a-z0-9]+$/i, '') : str(args.title, 'Featured Track');
|
|
826
|
+
const s = parseSpec({
|
|
827
|
+
...enriched,
|
|
828
|
+
preset: str(args.preset, enriched.preset ?? 'music-app'),
|
|
829
|
+
title: str(args.title, enriched.title || cleanAudioName || 'Sonic Studio'),
|
|
830
|
+
subtitle: str(args.subtitle, enriched.subtitle || 'Interactive Music Player, 3-Band EQ & Visualizer'),
|
|
831
|
+
artist: str(args.artist, enriched.artist || 'Hanzo Studio'),
|
|
832
|
+
prompt,
|
|
833
|
+
accent: str(args.accent, enriched.accent || (w.brand?.colors.accent ?? '#f43f5e')),
|
|
834
|
+
secondary: str(args.secondary, enriched.secondary || '#6366f1'),
|
|
835
|
+
audio: audioFile?.key ?? '',
|
|
836
|
+
audioName: cleanAudioName,
|
|
837
|
+
image: imageFile?.key ?? '',
|
|
838
|
+
tracks: enriched.tracks?.length
|
|
839
|
+
? [{ title: cleanAudioName, duration: '3:24', bpm: enriched.tracks[0]?.bpm ?? 124, key: enriched.tracks[0]?.key ?? 'A minor' }, ...enriched.tracks.slice(1)]
|
|
840
|
+
: undefined,
|
|
841
|
+
system: w.brand ?? PRESETS[0],
|
|
842
|
+
});
|
|
843
|
+
const files = await renderApp(w, s);
|
|
844
|
+
return {
|
|
845
|
+
title: s.title,
|
|
846
|
+
draft: {
|
|
847
|
+
spec: s,
|
|
848
|
+
files,
|
|
849
|
+
note: audioFile ? `${s.title} (wired to ${audioFile.name})` : s.title,
|
|
850
|
+
by: 'Studio interactive engine',
|
|
851
|
+
},
|
|
852
|
+
};
|
|
853
|
+
},
|
|
854
|
+
revise: async (w, made, change) => {
|
|
855
|
+
const now = parseSpec(current(made).spec);
|
|
856
|
+
let next = { ...now, ...(change.patch ?? {}) };
|
|
857
|
+
if (change.instruction) {
|
|
858
|
+
w.doing('Updating the interactive app');
|
|
859
|
+
try {
|
|
860
|
+
const ask = await structured(w.wire, w.writer, 'You revise an interactive music & visual app spec. Apply the instruction and return the updated JSON spec.', `Current spec: ${JSON.stringify({ preset: now.preset, title: now.title, subtitle: now.subtitle, artist: now.artist, accent: now.accent, secondary: now.secondary, tracks: now.tracks })}\nInstruction: ${change.instruction}`, SCHEMA, w.signal);
|
|
861
|
+
next = parseSpec({ ...now, ...ask, audio: now.audio, audioName: now.audioName, image: now.image });
|
|
862
|
+
}
|
|
863
|
+
catch {
|
|
864
|
+
next = parseSpec({ ...now, subtitle: change.instruction });
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
const files = await renderApp(w, next);
|
|
868
|
+
return { spec: next, files, note: change.instruction ?? 'Updated app', by: 'Studio interactive engine' };
|
|
869
|
+
},
|
|
870
|
+
View: AppFrame,
|
|
871
|
+
exports: [
|
|
872
|
+
{
|
|
873
|
+
label: 'Interactive App (HTML)',
|
|
874
|
+
run: async (w, m, v) => ({ name: filename(m.title, 'html'), blob: await read(w, v.files[0].key) }),
|
|
875
|
+
},
|
|
876
|
+
],
|
|
877
|
+
};
|
|
878
|
+
function AppFrame({ made, version, wire, edit, busy }) {
|
|
879
|
+
const s = parseSpec(version.spec);
|
|
880
|
+
const [draft, setDraft] = useState(s);
|
|
881
|
+
const [srcDoc, setSrcDoc] = useState('');
|
|
882
|
+
useEffect(() => setDraft(parseSpec(version.spec)), [version.id]);
|
|
883
|
+
useEffect(() => {
|
|
884
|
+
let live = true;
|
|
885
|
+
const f = version.files[0];
|
|
886
|
+
if (!f)
|
|
887
|
+
return;
|
|
888
|
+
read(wire, f.key)
|
|
889
|
+
.then((b) => b.text())
|
|
890
|
+
.then((txt) => {
|
|
891
|
+
if (live)
|
|
892
|
+
setSrcDoc(txt);
|
|
893
|
+
})
|
|
894
|
+
.catch(() => undefined);
|
|
895
|
+
return () => {
|
|
896
|
+
live = false;
|
|
897
|
+
};
|
|
898
|
+
}, [wire, version.id]);
|
|
899
|
+
const changed = draft.title !== s.title ||
|
|
900
|
+
draft.subtitle !== s.subtitle ||
|
|
901
|
+
draft.artist !== s.artist ||
|
|
902
|
+
draft.accent !== s.accent ||
|
|
903
|
+
draft.secondary !== s.secondary ||
|
|
904
|
+
draft.prompt !== s.prompt;
|
|
905
|
+
const set = (patch) => setDraft({ ...draft, ...patch });
|
|
906
|
+
return (_jsxs(YStack, { flex: 1, minH: 0, $md: { flexDirection: 'row' }, children: [_jsx(YStack, { flex: 1, minH: 360, bg: "#07070b", position: "relative", children: srcDoc ? (_jsx("iframe", { title: made.title, srcDoc: srcDoc, sandbox: "allow-scripts allow-downloads", style: { width: '100%', height: '100%', border: 0, display: 'block' } })) : (_jsx(YStack, { flex: 1, items: "center", justify: "center", children: _jsx(SizableText, { color: "$soft", children: "Loading interactive app\u2026" }) })) }), _jsxs(YStack, { width: "100%", $md: { width: 290 }, gap: "$3", p: "$3", overflow: "scroll", borderLeftWidth: 1, borderColor: "$borderColor", children: [_jsx(Row, { label: "App title", children: _jsx(Text1, { label: "App title", value: draft.title, onChange: (title) => set({ title }) }) }), _jsx(Row, { label: "Artist / Creator", children: _jsx(Text1, { label: "Artist", value: draft.artist, onChange: (artist) => set({ artist }) }) }), _jsx(Row, { label: "Subtitle", children: _jsx(Text1, { label: "Subtitle", value: draft.subtitle, onChange: (subtitle) => set({ subtitle }) }) }), _jsx(Row, { label: "Primary accent", children: _jsx(Text1, { label: "Primary accent", value: draft.accent, onChange: (accent) => set({ accent }) }) }), _jsx(Row, { label: "Secondary colour", children: _jsx(Text1, { label: "Secondary colour", value: draft.secondary, onChange: (secondary) => set({ secondary }) }) }), _jsx(Row, { label: "Creative direction", children: _jsx(TextN, { label: "Creative direction", rows: 3, value: draft.prompt, onChange: (prompt) => set({ prompt }) }) }), _jsx(XStack, { render: "button", "aria-label": "Save changes to app", disabled: !changed || busy, onPress: () => changed && !busy && edit(draft, 'Updated app settings'), height: 36, items: "center", justify: "center", rounded: "$3", bg: changed ? '$ink' : '$hover', cursor: changed ? 'pointer' : 'default', children: _jsx(SizableText, { size: "$2", color: changed ? '$background' : '$soft', children: busy ? 'Rebuilding…' : 'Rebuild app' }) })] })] }));
|
|
907
|
+
}
|