forked from tambien/Piano
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.html
More file actions
247 lines (217 loc) · 8.78 KB
/
demo.html
File metadata and controls
247 lines (217 loc) · 8.78 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>d-piano — Progressive Loading Demo</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 720px;
margin: 48px auto;
padding: 0 24px;
background: #f8f8f8;
color: #1a1a1a;
}
h1 { margin: 0 0 4px; font-size: 24px; }
.subtitle { color: #666; margin: 0 0 32px; font-size: 14px; }
.card {
background: white;
border-radius: 10px;
padding: 20px 24px;
margin-bottom: 16px;
box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.card h2 { margin: 0 0 16px; font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: #555; }
/* Config */
.config-row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.config-row label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: #555; }
.config-row label span { font-weight: 500; }
.config-row input { padding: 8px 10px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; width: 280px; outline: none; }
.config-row input:focus { border-color: #2563eb; }
.config-row select { padding: 8px 10px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; outline: none; }
.btn { padding: 9px 18px; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; font-weight: 500; transition: background .15s; }
.btn-primary { background: #2563eb; color: white; }
.btn-primary:hover { background: #1d4ed8; }
.btn-primary:disabled { background: #93c5fd; cursor: default; }
/* Phases */
.phases { display: flex; flex-direction: column; gap: 0; }
.phase { display: flex; align-items: center; gap: 14px; padding: 12px 0; border-bottom: 1px solid #f2f2f2; }
.phase:last-child { border-bottom: none; }
.dot { width: 11px; height: 11px; border-radius: 50%; background: #e0e0e0; flex-shrink: 0; transition: background .3s; }
.dot.waiting { background: #e0e0e0; }
.dot.active { background: #f59e0b; box-shadow: 0 0 0 3px rgba(245,158,11,.2); }
.dot.done { background: #22c55e; }
.dot.skipped { background: #a3e635; }
.phase-text { flex: 1; font-size: 14px; color: #444; }
.phase-time { font-size: 12px; color: #999; font-family: monospace; min-width: 60px; text-align: right; }
/* Keys */
.keys { display: flex; gap: 6px; flex-wrap: wrap; }
.key {
padding: 11px 14px;
background: #f5f5f5;
border: 1px solid #e0e0e0;
border-radius: 7px;
font-size: 13px;
cursor: pointer;
user-select: none;
transition: background .1s, transform .08s;
}
.key:hover:not(:disabled) { background: #ebebeb; }
.key:active:not(:disabled) { transform: translateY(1px); }
.key:disabled { opacity: .35; cursor: default; }
.key.playing { background: #dbeafe; border-color: #93c5fd; color: #1d4ed8; }
/* Log */
.log {
margin-top: 16px;
font-family: monospace;
font-size: 12px;
line-height: 1.6;
color: #555;
max-height: 140px;
overflow-y: auto;
background: #fafafa;
border: 1px solid #eee;
border-radius: 6px;
padding: 10px 12px;
}
.log:empty::before { content: 'Events will appear here…'; color: #bbb; }
.log-line { padding: 1px 0; }
.log-line.hl { color: #2563eb; font-weight: 600; }
</style>
</head>
<body>
<h1>d-piano</h1>
<p class="subtitle">Plays immediately at 1 velocity, upgrades to full quality in the background during browser idle time.</p>
<div class="card">
<h2>Configuration</h2>
<div class="config-row">
<label>
<span>Sample URL</span>
<input id="sampleUrl" value="https://tambien.github.io/Piano/Salamander/" />
</label>
<label>
<span>Target velocities</span>
<select id="velocities">
<option value="1">1 — no upgrade</option>
<option value="4">4</option>
<option value="8" selected>8</option>
<option value="16">16</option>
</select>
</label>
<button class="btn btn-primary" id="loadBtn" onclick="window.initPiano()">Load Piano</button>
</div>
</div>
<div class="card">
<h2>Loading progress</h2>
<div class="phases">
<div class="phase">
<div class="dot waiting" id="dot1"></div>
<div class="phase-text" id="txt1">Waiting to start</div>
<div class="phase-time" id="t1"></div>
</div>
<div class="phase">
<div class="dot waiting" id="dot2"></div>
<div class="phase-text" id="txt2">—</div>
<div class="phase-time" id="t2"></div>
</div>
</div>
</div>
<div class="card">
<h2>Play</h2>
<div class="keys" id="keys">
<button class="key" disabled data-notes="C3">C3</button>
<button class="key" disabled data-notes="E3">E3</button>
<button class="key" disabled data-notes="G3">G3</button>
<button class="key" disabled data-notes="C4">C4</button>
<button class="key" disabled data-notes="E4">E4</button>
<button class="key" disabled data-notes="G4">G4</button>
<button class="key" disabled data-notes="C5">C5</button>
<button class="key" disabled data-notes="E5">E5</button>
<button class="key" disabled data-notes="G5">G5</button>
<button class="key" disabled data-notes="C4,E4,G4">C major</button>
<button class="key" disabled data-notes="G3,B3,D4">G major</button>
<button class="key" disabled data-notes="A3,C4,E4">A minor</button>
<button class="key" disabled data-notes="F3,A3,C4">F major</button>
</div>
<div class="log" id="log"></div>
</div>
<script src="https://unpkg.com/tone@latest/build/Tone.js"></script>
<script type="module">
import { Piano } from './src/index.ts';
let piano = null;
let t0 = null;
function elapsed() {
return t0 ? `+${Date.now() - t0}ms` : '';
}
function log(msg, highlight = false) {
const el = document.getElementById('log');
const line = document.createElement('div');
line.className = 'log-line' + (highlight ? ' hl' : '');
line.textContent = `${elapsed().padEnd(9)} ${msg}`;
el.appendChild(line);
el.scrollTop = el.scrollHeight;
}
function setPhase(n, state, text, time) {
document.getElementById(`dot${n}`).className = `dot ${state}`;
document.getElementById(`txt${n}`).textContent = text;
if (time != null) document.getElementById(`t${n}`).textContent = `${time}ms`;
}
// Wire up play keys
document.getElementById('keys').addEventListener('click', async e => {
const key = e.target.closest('.key');
if (!key || key.disabled || !piano) return;
await Tone.start();
const notes = key.dataset.notes.split(',');
notes.forEach(note => piano.keyDown({ note, velocity: 0.8 }));
key.classList.add('playing');
setTimeout(() => {
notes.forEach(note => piano.keyUp({ note }));
key.classList.remove('playing');
}, 1500);
});
window.initPiano = async function () {
const url = document.getElementById('sampleUrl').value.trim();
const velocities = parseInt(document.getElementById('velocities').value);
if (!url) { alert('Enter a sample URL first.'); return; }
document.getElementById('loadBtn').disabled = true;
document.getElementById('log').innerHTML = '';
document.querySelectorAll('.key').forEach(k => k.disabled = true);
t0 = Date.now();
const phase2Label = velocities > 1
? `Upgrade to ${velocities} velocities (background)`
: 'No upgrade needed (velocities = 1)';
setPhase(1, 'active', 'Loading first pass (1 velocity)…', null);
setPhase(2, 'waiting', phase2Label, null);
log('Piano created');
piano = new Piano({
url,
velocities,
onLoadProgress: (progress) => {
const current = Math.round(progress * velocities);
if (progress < 1) {
setPhase(2, 'active', `Loading velocities… (${current}/${velocities})`, null);
log(`Velocity progress: ${current}/${velocities}`);
return;
}
setPhase(2, 'done', `${velocities} velocities ready`, Date.now() - t0);
log(`Background upgrade complete — now at full quality (${velocities} vel)`, true);
},
}).toDestination();
await piano.load();
const ms = Date.now() - t0;
setPhase(1, 'done', 'First pass ready — playable now (1 velocity)', ms);
log(`First pass ready — piano is playable`, true);
if (velocities > 1) {
setPhase(2, 'active', `Upgrading to ${velocities} velocities in background…`, null);
log('Upgrade scheduled via requestIdleCallback');
} else {
setPhase(2, 'skipped', 'No upgrade (velocities = 1)', null);
}
document.querySelectorAll('.key').forEach(k => k.disabled = false);
document.getElementById('loadBtn').disabled = false;
};
</script>
</body>
</html>