-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.html
More file actions
463 lines (416 loc) · 22 KB
/
player.html
File metadata and controls
463 lines (416 loc) · 22 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>VIBE: idle</title>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;700&display=swap" rel="stylesheet">
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{background:#0a0a0a;color:#ccc;font-family:'JetBrains Mono',monospace;font-size:13px;overflow-x:hidden}
::selection{background:#00ff88;color:#0a0a0a}
::-webkit-scrollbar{width:6px}
::-webkit-scrollbar-track{background:#0a0a0a}
::-webkit-scrollbar-thumb{background:#222}
#now-playing{border-bottom:1px solid #1a1a1a;padding:20px 24px;position:sticky;top:0;z-index:100;background:#0a0a0a}
#np-title{font-size:22px;font-weight:700;color:#fff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:70vw}
#np-artist{color:#666;font-size:12px;margin-top:2px}
#np-meta{color:#444;font-size:11px;margin-top:4px}
.waveform{display:flex;align-items:flex-end;gap:2px;height:24px;margin-top:8px}
.waveform .bar{width:3px;background:#00ff88;animation:wave 0.8s ease-in-out infinite}
.waveform.paused .bar{animation:none;height:2px!important;background:#333}
@keyframes wave{0%,100%{height:4px;opacity:0.4}50%{height:20px;opacity:1}}
#progress-wrap{height:3px;background:#1a1a1a;cursor:pointer;margin-top:10px}
#progress-bar{height:100%;background:#00ff88;width:0%;transition:none}
#time-display{font-size:10px;color:#444;margin-top:4px}
#controls{display:flex;align-items:center;gap:16px;padding:12px 24px;border-bottom:1px solid #1a1a1a;flex-wrap:wrap}
#controls button{background:none;border:1px solid #333;color:#ccc;padding:4px 12px;font-family:inherit;font-size:11px;cursor:pointer;letter-spacing:1px}
#controls button:hover{border-color:#00ff88;color:#00ff88}
#controls button.active{background:#00ff88;color:#0a0a0a;border-color:#00ff88}
#volume-wrap{display:flex;align-items:center;gap:6px;margin-left:auto;color:#444;font-size:11px}
#volume{-webkit-appearance:none;width:80px;height:2px;background:#333;outline:none}
#volume::-webkit-slider-thumb{-webkit-appearance:none;width:10px;height:10px;background:#00ff88;cursor:pointer}
/* DJ Controls */
#dj-controls{display:flex;align-items:center;gap:12px;padding:10px 24px;border-bottom:1px solid #1a1a1a;flex-wrap:wrap}
#dj-controls label{color:#555;font-size:10px;text-transform:uppercase;letter-spacing:1px}
#dj-controls input[type="range"]{-webkit-appearance:none;width:100px;height:2px;background:#333;outline:none}
#dj-controls input[type="range"]::-webkit-slider-thumb{-webkit-appearance:none;width:10px;height:10px;background:#ff6600;cursor:pointer}
.bpm-display{color:#ff6600;font-size:12px;font-weight:700;min-width:60px}
.crossfade-display{color:#cc00ff;font-size:11px;min-width:30px}
/* Context Mode Buttons */
#context-modes{display:flex;gap:0;padding:0 24px;border-bottom:1px solid #1a1a1a}
.ctx-btn{padding:8px 14px;cursor:pointer;font-size:10px;letter-spacing:1px;color:#555;text-transform:uppercase;border:none;background:none;font-family:inherit;transition:all 0.15s}
.ctx-btn:hover{color:#ccc}
.ctx-btn.active{color:#0a0a0a;font-weight:700}
.ctx-btn[data-ctx="coding"].active{background:#00ff88}
.ctx-btn[data-ctx="debug"].active{background:#ff0044;color:#fff}
.ctx-btn[data-ctx="ship"].active{background:#ffaa00}
.ctx-btn[data-ctx="chill"].active{background:#00ccff}
.ctx-btn[data-ctx="focus"].active{background:#cc00ff;color:#fff}
.ctx-btn[data-ctx="hype"].active{background:#ff6600}
/* DJ Status */
#dj-status{padding:6px 24px;font-size:10px;color:#333;border-bottom:1px solid #111;font-style:italic}
/* Queue Panel */
#queue-panel{border-bottom:1px solid #1a1a1a;max-height:0;overflow:hidden;transition:max-height 0.3s ease}
#queue-panel.open{max-height:300px;overflow-y:auto}
#queue-header{display:flex;justify-content:space-between;align-items:center;padding:8px 24px;cursor:pointer;color:#555;font-size:10px;letter-spacing:1px;border-bottom:1px solid #1a1a1a}
#queue-header:hover{color:#ccc}
#queue-list{padding:0}
.queue-item{display:flex;align-items:center;gap:12px;padding:4px 24px;font-size:11px;color:#555;border-bottom:1px solid #0d0d0d}
.queue-item .q-num{color:#333;min-width:20px}
.queue-item .q-title{color:#888;flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.queue-item .q-bpm{color:#444;font-size:10px}
.queue-item .q-remove{cursor:pointer;color:#333;font-size:14px}
.queue-item .q-remove:hover{color:#ff0044}
#cluster-tabs{display:flex;gap:0;padding:0 24px;border-bottom:1px solid #1a1a1a;overflow-x:auto}
.cluster-tab{padding:8px 16px;cursor:pointer;font-size:11px;letter-spacing:1px;border-bottom:2px solid transparent;white-space:nowrap;color:#666;transition:color 0.15s}
.cluster-tab:hover{color:#ccc}
.cluster-tab.active{color:#fff;border-bottom-color:var(--c)}
#sort-bar{display:flex;gap:0;padding:0 24px;border-bottom:1px solid #1a1a1a}
.sort-btn{padding:6px 12px;cursor:pointer;font-size:10px;letter-spacing:1px;color:#444;text-transform:uppercase}
.sort-btn:hover{color:#ccc}
.sort-btn.active{color:#00ff88}
#track-list{padding:0}
#track-count{padding:8px 24px;color:#333;font-size:10px;border-bottom:1px solid #111}
.track-row{display:grid;grid-template-columns:36px 1fr 180px 60px 48px 80px;align-items:center;padding:6px 24px;border-bottom:1px solid #111;cursor:pointer;transition:background 0.1s}
.track-row:hover{background:#111}
.track-row.playing{background:#0d1a0f;border-left:2px solid #00ff88}
.track-row .play-btn{color:#444;font-size:14px;text-align:center}
.track-row:hover .play-btn{color:#00ff88}
.track-row.playing .play-btn{color:#00ff88}
.track-row .title{color:#ddd;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size:12px}
.track-row.playing .title{color:#00ff88}
.track-row .artist{color:#555;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size:11px}
.track-row .bpm-val{color:#555;text-align:right;font-size:11px}
.track-row .key-val{color:#555;text-align:center;font-size:11px}
.energy-bar-wrap{height:6px;background:#1a1a1a;position:relative}
.energy-bar-fill{height:100%;position:absolute;left:0;top:0}
/* Queue add button on hover */
.track-row .add-queue{display:none;color:#333;font-size:10px;cursor:pointer;position:absolute;right:24px}
.track-row:hover .add-queue{display:inline}
.track-row .add-queue:hover{color:#00ff88}
#ai-state{display:none}
</style>
</head>
<body>
<div id="now-playing">
<div style="display:flex;justify-content:space-between;align-items:flex-start">
<div style="min-width:0;flex:1">
<div id="np-title">-- no track selected --</div>
<div id="np-artist"></div>
<div id="np-meta"></div>
</div>
<div class="waveform paused" id="waveform"></div>
</div>
<div id="progress-wrap"><div id="progress-bar"></div></div>
<div id="time-display">0:00 / 0:00</div>
</div>
<div id="controls">
<button onclick="vibeAPI.prev()">PREV</button>
<button id="play-pause-btn" onclick="togglePlayPause()">PLAY</button>
<button onclick="vibeAPI.next()">NEXT</button>
<button id="shuffle-btn" onclick="toggleShuffle()">SHUF</button>
<button onclick="vibeAPI.celebrate('victory')" style="border-color:#ffaa00;color:#ffaa00">CELEBRATE</button>
<div id="volume-wrap">
<span>VOL</span>
<input type="range" id="volume" min="0" max="1" step="0.01" value="0.7">
</div>
</div>
<div id="dj-controls">
<label>BPM</label>
<input type="range" id="bpm-slider" min="0.5" max="2.0" step="0.01" value="1.0">
<span class="bpm-display" id="bpm-display">1.00x</span>
<label style="margin-left:16px">XFADE</label>
<input type="range" id="crossfade-slider" min="1" max="8" step="0.5" value="3">
<span class="crossfade-display" id="crossfade-display">3s</span>
</div>
<div id="context-modes">
<button class="ctx-btn active" data-ctx="coding" onclick="vibeAPI.setContext('coding')">CODING</button>
<button class="ctx-btn" data-ctx="debug" onclick="vibeAPI.setContext('debug')">DEBUG</button>
<button class="ctx-btn" data-ctx="ship" onclick="vibeAPI.setContext('ship')">SHIP</button>
<button class="ctx-btn" data-ctx="chill" onclick="vibeAPI.setContext('chill')">CHILL</button>
<button class="ctx-btn" data-ctx="focus" onclick="vibeAPI.setContext('focus')">FOCUS</button>
<button class="ctx-btn" data-ctx="hype" onclick="vibeAPI.setContext('hype')">HYPE</button>
</div>
<div id="dj-status">DJ idle -- pick a context and hit play</div>
<div id="queue-header" onclick="toggleQueue()">
<span>QUEUE (<span id="queue-count">0</span>)</span>
<span id="queue-toggle">+</span>
</div>
<div id="queue-panel">
<div id="queue-list"></div>
</div>
<div id="cluster-tabs"></div>
<div id="sort-bar">
<span class="sort-btn active" data-sort="default">DEFAULT</span>
<span class="sort-btn" data-sort="title">TITLE</span>
<span class="sort-btn" data-sort="bpm">BPM</span>
<span class="sort-btn" data-sort="energy">ENERGY</span>
<span class="sort-btn" data-sort="brightness">BRIGHT</span>
</div>
<div id="track-count"></div>
<div id="track-list"></div>
<audio id="audio" preload="none"></audio>
<div id="ai-state"></div>
<script>
let TRACKS = [];
async function loadTracks() {
try {
const res = await fetch('data/tracks.json');
if (!res.ok) throw new Error('No tracks');
TRACKS = await res.json();
} catch {
document.body.innerHTML = '<div style="display:flex;align-items:center;justify-content:center;height:100vh;color:#555;font-family:JetBrains Mono,monospace;font-size:14px;text-align:center;padding:40px"><div><div style="font-size:48px;margin-bottom:20px">🎧</div><div style="color:#888;font-size:18px;margin-bottom:12px">No tracks loaded</div><div>Run the setup pipeline first, then start the server:<br><br><code style="color:#00ff88">node scripts/turntable-server.mjs</code></div></div></div>';
return;
}
TRACKS.forEach(t => { const c = getCluster(t); t.cluster = c.name; t.clusterColor = c.color; });
initUI();
}
const CLUSTER_RULES=[
{name:'dark-bass',color:'#ff0044',test:t=>t.b<1500&&t.e<0.3},
{name:'heavy-dark',color:'#cc00ff',test:t=>t.b<1500&&t.e>=0.3},
{name:'balanced',color:'#00ff88',test:t=>t.b>=1500&&t.b<2500},
{name:'bright-percussive',color:'#ffaa00',test:t=>t.b>=2500&&t.p>0.15},
{name:'bright-airy',color:'#00ccff',test:t=>t.b>=2500}
];
function getCluster(track){for(const r of CLUSTER_RULES)if(r.test(track))return r;return CLUSTER_RULES[2]}
let currentId=null,isPlaying=false,shuffleOn=false,activeCluster='all',activeSort='default';
let activeContext='coding',crossfadeDuration=3,bpmRate=1.0;
let displayList=[];
let queue=[];
const audio=document.getElementById('audio');
// BPM slider
const bpmSlider=document.getElementById('bpm-slider');
const bpmDisplay=document.getElementById('bpm-display');
bpmSlider.addEventListener('input',()=>{
bpmRate=parseFloat(bpmSlider.value);
audio.playbackRate=bpmRate;
bpmDisplay.textContent=bpmRate.toFixed(2)+'x';
if(currentId){
const track=TRACKS.find(t=>t.id===currentId);
if(track)bpmDisplay.textContent=Math.round(track.bpm*bpmRate)+' BPM ('+bpmRate.toFixed(2)+'x)';
}
});
// Crossfade slider
const xfSlider=document.getElementById('crossfade-slider');
const xfDisplay=document.getElementById('crossfade-display');
xfSlider.addEventListener('input',()=>{
crossfadeDuration=parseFloat(xfSlider.value);
xfDisplay.textContent=crossfadeDuration+'s';
});
// Context filtering rules
const CTX_RULES={
coding:{bpmMin:110,bpmMax:145,eMin:0.2,eMax:0.6},
debug:{bpmMin:75,bpmMax:115,eMin:0.05,eMax:0.35},
ship:{bpmMin:125,bpmMax:200,eMin:0.35,eMax:1.0},
chill:{bpmMin:60,bpmMax:105,eMin:0.0,eMax:0.25},
focus:{bpmMin:100,bpmMax:135,eMin:0.15,eMax:0.45},
hype:{bpmMin:130,bpmMax:200,eMin:0.4,eMax:1.0}
};
function getContextFiltered(ctx){
const rule=CTX_RULES[ctx];
if(!rule)return[...TRACKS];
return TRACKS.filter(t=>{
const bpmOk=t.bpm>=rule.bpmMin-15&&t.bpm<=rule.bpmMax+15;
const eOk=t.e>=rule.eMin-0.1&&t.e<=rule.eMax+0.1;
return bpmOk&&eOk;
}).sort((a,b)=>{
let sa=0,sb=0;
if(a.bpm>=rule.bpmMin&&a.bpm<=rule.bpmMax)sa+=3;
if(b.bpm>=rule.bpmMin&&b.bpm<=rule.bpmMax)sb+=3;
if(a.e>=rule.eMin&&a.e<=rule.eMax)sa+=3;
if(b.e>=rule.eMin&&b.e<=rule.eMax)sb+=3;
return sb-sa;
});
}
function getFiltered(){
let list;
if(activeCluster==='all'){
list=[...TRACKS];
} else {
list=TRACKS.filter(t=>t.cluster===activeCluster);
}
if(activeSort==='title')list.sort((a,b)=>a.t.localeCompare(b.t));
else if(activeSort==='bpm')list.sort((a,b)=>b.bpm-a.bpm);
else if(activeSort==='energy')list.sort((a,b)=>b.e-a.e);
else if(activeSort==='brightness')list.sort((a,b)=>b.b-a.b);
return list;
}
function esc(s){return s.replace(/&/g,'&').replace(/"/g,'"').replace(/</g,'<').replace(/>/g,'>')}
function renderList(){
displayList=getFiltered();
document.querySelectorAll('.sort-btn').forEach(b=>b.classList.toggle('active',b.dataset.sort===activeSort));
document.querySelectorAll('.cluster-tab').forEach(t=>{t.classList.toggle('active',(t.dataset.cluster||'all')===activeCluster)});
document.getElementById('track-count').textContent=displayList.length+' tracks';
const c=document.getElementById('track-list');c.innerHTML='';
displayList.forEach(track=>{
const row=document.createElement('div');
row.className='track-row'+(track.id===currentId?' playing':'');
row.dataset.id=track.id;
const ep=Math.min(100,Math.round(track.e*100/0.8));
row.innerHTML='<span class="play-btn">'+(track.id===currentId&&isPlaying?'\u25A0':'\u25B6')+'</span><span class="title" title="'+esc(track.t)+'">'+esc(track.t)+'</span><span class="artist" title="'+esc(track.a)+'">'+esc(track.a)+'</span><span class="bpm-val">'+track.bpm+'</span><span class="key-val">'+track.k+'</span><div class="energy-bar-wrap"><div class="energy-bar-fill" style="width:'+ep+'%;background:'+track.clusterColor+'"></div></div>';
row.onclick=(e)=>{
if(e.shiftKey){vibeAPI.addToQueue(track.id);return}
vibeAPI.play(track.id);
};
row.ondblclick=()=>vibeAPI.crossfade(track.id,crossfadeDuration);
c.appendChild(row);
});
}
function renderQueue(){
const ql=document.getElementById('queue-list');ql.innerHTML='';
document.getElementById('queue-count').textContent=queue.length;
queue.forEach((id,i)=>{
const track=TRACKS.find(t=>t.id===id);
if(!track)return;
const item=document.createElement('div');item.className='queue-item';
item.innerHTML='<span class="q-num">'+(i+1)+'</span><span class="q-title">'+esc(track.t)+'</span><span class="q-bpm">'+track.bpm+'</span><span class="q-remove" onclick="vibeAPI.removeFromQueue('+i+')">x</span>';
ql.appendChild(item);
});
}
function toggleQueue(){
document.getElementById('queue-panel').classList.toggle('open');
const toggle=document.getElementById('queue-toggle');
toggle.textContent=toggle.textContent==='+'?'-':'+';
}
function updateNowPlaying(){
const track=TRACKS.find(t=>t.id===currentId);
const npT=document.getElementById('np-title'),npA=document.getElementById('np-artist'),npM=document.getElementById('np-meta');
if(track){
npT.textContent=track.t;npA.textContent=track.a;
const effectiveBpm=Math.round(track.bpm*bpmRate);
npM.textContent=effectiveBpm+' BPM ('+bpmRate.toFixed(2)+'x) | '+track.k+' | '+track.cluster.toUpperCase()+' | E:'+track.e.toFixed(2);
document.title='VIBE: '+track.t;
} else {
npT.textContent='-- no track selected --';npA.textContent='';npM.textContent='';document.title='VIBE: idle';
}
wf.classList.toggle('paused',!isPlaying);
document.getElementById('play-pause-btn').textContent=isPlaying?'PAUSE':'PLAY';
updateDJStatus();
updateAIState();
}
function updateDJStatus(){
const el=document.getElementById('dj-status');
if(!isPlaying){el.textContent='DJ idle -- pick a context and hit play';return}
const track=TRACKS.find(t=>t.id===currentId);
if(!track)return;
const ctx=activeContext.toUpperCase();
el.textContent='DJ ['+ctx+'] playing '+track.t.substring(0,40)+' | queue: '+queue.length+' tracks';
}
function updateAIState(){
const pos=displayList.findIndex(t=>t.id===currentId);
document.getElementById('ai-state').textContent=JSON.stringify({
playing:isPlaying,current_track_id:currentId||null,current_cluster:activeCluster,
track_count:displayList.length,queue_position:pos>=0?pos:null,
context:activeContext,bpm_rate:bpmRate,crossfade_duration:crossfadeDuration,queue
});
}
function formatTime(s){if(!s||isNaN(s))return'0:00';const m=Math.floor(s/60),sec=Math.floor(s%60);return m+':'+(sec<10?'0':'')+sec}
const progressWrap=document.getElementById('progress-wrap'),progressBar=document.getElementById('progress-bar'),timeDisplay=document.getElementById('time-display');
audio.addEventListener('timeupdate',()=>{if(audio.duration){progressBar.style.width=(audio.currentTime/audio.duration*100)+'%';timeDisplay.textContent=formatTime(audio.currentTime)+' / '+formatTime(audio.duration)}});
audio.addEventListener('ended',()=>vibeAPI.next());
progressWrap.addEventListener('click',e=>{if(audio.duration){const r=progressWrap.getBoundingClientRect();audio.currentTime=((e.clientX-r.left)/r.width)*audio.duration}});
const vol=document.getElementById('volume');audio.volume=0.7;
vol.addEventListener('input',()=>{audio.volume=parseFloat(vol.value)});
const wf=document.getElementById('waveform');
function togglePlayPause(){
if(!currentId&&displayList.length){vibeAPI.play(displayList[0].id);return}
isPlaying?vibeAPI.pause():audio.play().then(()=>{isPlaying=true;updateNowPlaying();renderList()});
}
function toggleShuffle(){shuffleOn=!shuffleOn;document.getElementById('shuffle-btn').classList.toggle('active',shuffleOn)}
window.vibeAPI={
play(id){
const track=TRACKS.find(t=>t.id===id);if(!track)return;
currentId=id;audio.src='data/previews/'+encodeURIComponent(track.f);
audio.playbackRate=bpmRate;
audio.play().then(()=>{isPlaying=true;updateNowPlaying();renderList()}).catch(()=>{});
},
pause(){audio.pause();isPlaying=false;updateNowPlaying();renderList()},
next(){
if(queue.length>0){const nextId=queue.shift();renderQueue();vibeAPI.play(nextId);return}
if(!displayList.length)return;
if(shuffleOn){vibeAPI.play(displayList[Math.floor(Math.random()*displayList.length)].id);return}
const i=displayList.findIndex(t=>t.id===currentId);
vibeAPI.play(displayList[(i+1)%displayList.length].id);
},
prev(){if(!displayList.length)return;const i=displayList.findIndex(t=>t.id===currentId);vibeAPI.play(displayList[i<=0?displayList.length-1:i-1].id)},
shuffle(){toggleShuffle()},
setCluster(name){activeCluster=name;renderList();updateAIState()},
setContext(ctx){
activeContext=ctx;
document.querySelectorAll('.ctx-btn').forEach(b=>b.classList.toggle('active',b.dataset.ctx===ctx));
updateDJStatus();updateAIState();
},
setBPM(targetBPM){
if(!currentId)return;
const track=TRACKS.find(t=>t.id===currentId);
if(!track)return;
bpmRate=targetBPM/track.bpm;
bpmSlider.value=bpmRate;
audio.playbackRate=bpmRate;
bpmDisplay.textContent=Math.round(targetBPM)+' BPM ('+bpmRate.toFixed(2)+'x)';
},
crossfade(nextId,duration){
duration=duration||crossfadeDuration;
const nextTrack=TRACKS.find(t=>t.id===nextId);if(!nextTrack)return;
// Fade out current
const fadeSteps=20;const stepTime=duration*1000/fadeSteps;
const origVol=audio.volume;let step=0;
const fadeOut=setInterval(()=>{
step++;audio.volume=Math.max(0,origVol*(1-step/fadeSteps));
if(step>=fadeSteps){clearInterval(fadeOut);audio.volume=origVol;vibeAPI.play(nextId)}
},stepTime);
},
celebrate(style){
// Play a system notification in browser
const ctx=new(window.AudioContext||window.webkitAudioContext)();
const osc=ctx.createOscillator();const gain=ctx.createGain();
osc.connect(gain);gain.connect(ctx.destination);
if(style==='airhorn'){osc.frequency.value=440;gain.gain.value=0.3;osc.start();osc.stop(ctx.currentTime+0.15);
setTimeout(()=>{const o2=ctx.createOscillator();const g2=ctx.createGain();o2.connect(g2);g2.connect(ctx.destination);o2.frequency.value=440;g2.gain.value=0.3;o2.start();o2.stop(ctx.currentTime+0.15)},200);
setTimeout(()=>{const o3=ctx.createOscillator();const g3=ctx.createGain();o3.connect(g3);g3.connect(ctx.destination);o3.frequency.value=440;g3.gain.value=0.3;o3.start();o3.stop(ctx.currentTime+0.2)},400);
} else {
// Victory: ascending tones
osc.frequency.setValueAtTime(523,ctx.currentTime);
osc.frequency.setValueAtTime(659,ctx.currentTime+0.15);
osc.frequency.setValueAtTime(784,ctx.currentTime+0.3);
gain.gain.value=0.2;osc.start();osc.stop(ctx.currentTime+0.5);
}
},
addToQueue(id){if(!queue.includes(id)){queue.push(id);renderQueue()}},
removeFromQueue(idx){queue.splice(idx,1);renderQueue()},
getQueue(){return queue},
setQueue(ids){queue=ids;renderQueue()},
getDJState(){
const pos=displayList.findIndex(t=>t.id===currentId);
return{playing:isPlaying,current_track_id:currentId,current_cluster:activeCluster,
track_count:displayList.length,queue_position:pos>=0?pos:null,
context:activeContext,bpm_rate:bpmRate,crossfade_duration:crossfadeDuration,queue}
},
getState(){return this.getDJState()}
};
function initUI(){
// waveform bars
for(let i=0;i<12;i++){const b=document.createElement('div');b.className='bar';b.style.animationDelay=(i*0.07)+'s';wf.appendChild(b)}
// cluster tabs
const clusterCounts={};
TRACKS.forEach(t=>{clusterCounts[t.cluster]=(clusterCounts[t.cluster]||0)+1});
const tabsEl=document.getElementById('cluster-tabs');
const allTab=document.createElement('span');
allTab.className='cluster-tab active';allTab.style.setProperty('--c','#00ff88');
allTab.textContent='ALL ('+TRACKS.length+')';allTab.onclick=()=>vibeAPI.setCluster('all');
tabsEl.appendChild(allTab);
for(const r of CLUSTER_RULES){
if(!clusterCounts[r.name])continue;
const tab=document.createElement('span');tab.className='cluster-tab';tab.dataset.cluster=r.name;
tab.style.setProperty('--c',r.color);tab.textContent=r.name.toUpperCase()+' ('+clusterCounts[r.name]+')';
tab.onclick=()=>vibeAPI.setCluster(r.name);tabsEl.appendChild(tab);
}
// sort buttons
document.querySelectorAll('.sort-btn').forEach(btn=>{btn.onclick=()=>{activeSort=btn.dataset.sort;renderList()}});
renderList();
}
loadTracks();
</script>
</body>
</html>