-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperimental.html
More file actions
274 lines (240 loc) · 8.73 KB
/
Copy pathexperimental.html
File metadata and controls
274 lines (240 loc) · 8.73 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Experimental | Sophia Marinova</title>
<!-- Reuse your main styles (Hydra layering, buttons, fonts, etc.) -->
<link rel="stylesheet" href="style.css">
<style>
/* Page-specific tweaks */
.container { max-width: 980px; margin: 0 auto; padding: 48px 20px; text-align: center; }
h1 { font-size: clamp(28px,4vw,40px); margin:18px 0 10px; }
p.lead { margin: 0 0 24px; opacity: .95; }
.controls { display:flex; gap:12px; justify-content:center; flex-wrap:wrap; margin-top: 16px; }
/* Waveform canvas sits above Hydra and below UI */
#wave-canvas{
display:block;
width:min(720px, 92vw);
height:160px; /* CSS height; JS scales for DPR */
margin:18px auto 8px;
background:#000; /* black background */
border-radius:14px;
/* soft pink glow & inner ring */
box-shadow: 0 12px 28px -16px rgba(236,72,153,.55), 0 0 0 1px rgba(236,72,153,.25) inset;
}
.hint { font-size: 13px; opacity: .8; margin-top: 4px; }
/* Pretty file input */
.file {
display: inline-block;
background: #fff;
border-radius: 999px;
padding: 10px 14px;
box-shadow: 0 10px 20px -12px rgba(0,0,0,.25);
border: 1px solid rgba(0,0,0,.06);
cursor: pointer;
}
.file input { display: none; }
</style>
</head>
<body>
<!-- Hydra background -->
<canvas id="hydra-bg"></canvas>
<div class="backdrop"></div>
<main class="container">
<h1>Experimental</h1>
<p class="lead">Toggle DJ Mode for spicier visuals, then play a track to see the live sound waveform.</p>
<!-- Waveform -->
<canvas id="wave-canvas"></canvas>
<div class="hint">Tip: click “Choose Song” to load a file, or put a file at <code>music/demo.mp3</code> and hit Play.</div>
<!-- Controls -->
<div class="controls">
<button id="djBtn" class="btn">DJ Mode</button>
<label class="file">
<span id="fileLabel">Choose Song</span>
<input id="filePicker" type="file" accept="audio/*">
</label>
<button id="playBtn" class="btn">Play</button>
<button id="pauseBtn" class="btn" style="display:none;">Pause</button>
</div>
<!-- Hidden audio element (we control it via JS) -->
<audio id="player" crossorigin="anonymous" preload="auto"></audio>
</main>
<!-- Hydra synth -->
<script src="https://unpkg.com/hydra-synth"></script>
<script>
/* ---------- Canvas sizing helpers ---------- */
function fitHydraCanvas(c){
const dpr = Math.min(window.devicePixelRatio || 1, 2);
c.width = Math.floor(innerWidth * dpr);
c.height = Math.floor(innerHeight * dpr);
c.style.width = innerWidth + 'px';
c.style.height = innerHeight + 'px';
}
const hydraCanvas = document.getElementById('hydra-bg');
fitHydraCanvas(hydraCanvas);
addEventListener('resize', () => fitHydraCanvas(hydraCanvas));
/* ---------- Hydra scenes ---------- */
function sceneCalm(){
const kCount = () => 4 + Math.floor(2 * Math.abs(Math.sin((window.time||0) * 0.35)));
osc(0.9, 0.45, 4.8)
.kaleid(kCount)
.diff(osc(0.9, 0.45, 4.8))
.modulateScale(osc(1.8, -0.22, 1))
.saturate(0.6)
.contrast(0.85)
.brightness(0.2)
.color(1.08, 1.08, 1.12)
.out();
}
function sceneDJ(){
const kCount = () => 5 + Math.floor(2 * Math.abs(Math.sin((window.time||0) * 0.8)));
osc(1.6, 0.6, 8)
.kaleid(kCount)
.diff(osc(2.2, 0.5, 9))
.modulate(osc(3.0, -0.15, 1.2))
.saturate(1.2)
.contrast(1.05)
.brightness(0.1)
.hue( () => Math.sin((window.time||0) * 0.12) * 0.3 )
.color(1.25, 1.05, 0.9)
.out();
}
(async () => {
try {
if (window.initHydra) {
await window.initHydra({ feedStrudel: 1, canvas: hydraCanvas });
} else {
const hydra = new Hydra({ canvas: hydraCanvas, detectAudio:false });
}
sceneCalm();
} catch (e) {
console.warn('Hydra init failed:', e);
}
})();
/* ---------- Web Audio: waveform visualizer ---------- */
const audioEl = document.getElementById('player');
const fileInput = document.getElementById('filePicker');
const fileLabel = document.getElementById('fileLabel');
const playBtn = document.getElementById('playBtn');
const pauseBtn = document.getElementById('pauseBtn');
const waveCanvas = document.getElementById('wave-canvas');
const wCtx = waveCanvas.getContext('2d');
// scale waveform canvas to device pixel ratio for crisp lines
function fitWaveCanvas() {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const cssW = Math.min(720, innerWidth * 0.92);
const cssH = 160;
waveCanvas.width = Math.floor(cssW * dpr);
waveCanvas.height = Math.floor(cssH * dpr);
waveCanvas.style.width = cssW + 'px';
waveCanvas.style.height = cssH + 'px';
wCtx.setTransform(dpr,0,0,dpr,0,0);
}
fitWaveCanvas();
addEventListener('resize', fitWaveCanvas);
let audioCtx, srcNode, analyser, dataArray, rafId;
let isDJ = false;
const WAVE_GAIN = 1.8; // expand amplitude (1.0 = normal)
function ensureAudioGraph(){
if (audioCtx) return;
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
srcNode = audioCtx.createMediaElementSource(audioEl);
analyser = audioCtx.createAnalyser();
analyser.fftSize = 4096; // smoother line
analyser.smoothingTimeConstant = 0.85; // mellow motion
const bufferLen = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLen);
srcNode.connect(analyser);
analyser.connect(audioCtx.destination);
}
function drawWave(){
analyser.getByteTimeDomainData(dataArray);
const w = waveCanvas.width;
const h = waveCanvas.height;
const center = h / 2;
// clear panel (we keep black via CSS; no fill needed)
wCtx.clearRect(0,0,w,h);
// center baseline (subtle)
wCtx.beginPath();
wCtx.moveTo(0, center);
wCtx.lineTo(w, center);
wCtx.strokeStyle = 'rgba(255,255,255,0.08)';
wCtx.lineWidth = 1;
wCtx.stroke();
// waveform path
wCtx.beginPath();
const slice = w / dataArray.length;
for (let i=0; i<dataArray.length; i++){
const v = (dataArray[i] - 128) / 128; // -1..+1 centered
const y = center + v * center * WAVE_GAIN; // expand around center
const x = i * slice;
if (i === 0) wCtx.moveTo(x,y); else wCtx.lineTo(x,y);
}
wCtx.lineWidth = 3;
wCtx.strokeStyle = '#ff2aa1'; // bright pink
wCtx.shadowColor = 'rgba(255,42,161,0.55)'; // glow
wCtx.shadowBlur = 8;
wCtx.stroke();
wCtx.shadowBlur = 0;
rafId = requestAnimationFrame(drawWave);
}
function startViz(){
if (!audioCtx) ensureAudioGraph();
if (rafId) cancelAnimationFrame(rafId);
rafId = requestAnimationFrame(drawWave);
}
function stopViz(){
if (rafId) cancelAnimationFrame(rafId);
rafId = null;
}
// Default file (optional)
audioEl.src = 'music/demo.mp3';
// File picker
fileInput.addEventListener('change', () => {
const f = fileInput.files && fileInput.files[0];
if (!f) return;
const url = URL.createObjectURL(f);
audioEl.src = url;
fileLabel.textContent = f.name.length > 26 ? f.name.slice(0,23) + '…' : f.name;
pauseBtn.style.display = 'none';
playBtn.style.display = 'inline-block';
});
// Play/Pause handlers
playBtn.addEventListener('click', async () => {
try {
ensureAudioGraph();
await audioEl.play();
startViz();
playBtn.style.display = 'none';
pauseBtn.style.display = 'inline-block';
} catch (e) {
console.warn('Play failed (user gesture or src missing):', e);
}
});
pauseBtn.addEventListener('click', () => {
audioEl.pause();
stopViz();
pauseBtn.style.display = 'none';
playBtn.style.display = 'inline-block';
});
audioEl.addEventListener('ended', () => {
stopViz();
pauseBtn.style.display = 'none';
playBtn.style.display = 'inline-block';
});
/* ---------- DJ Mode toggle ---------- */
const djBtn = document.getElementById('djBtn');
djBtn.addEventListener('click', () => {
isDJ = !isDJ;
if (isDJ) {
sceneDJ();
djBtn.textContent = 'DJ Mode: ON';
} else {
sceneCalm();
djBtn.textContent = 'DJ Mode';
}
});
</script>
</body>
</html>