-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisten.html
More file actions
373 lines (335 loc) · 9.94 KB
/
Copy pathlisten.html
File metadata and controls
373 lines (335 loc) · 9.94 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Listening to Clavis</title>
<style>
:root {
--bg: #0a0a0f;
--text: #c8c8d0;
--dim: #45455a;
--accent: #7b8cde;
--warm: #c4956a;
--danger: #de5b5b;
}
* { margin:0; padding:0; box-sizing: border-box; }
body {
font-family: 'Georgia', serif;
background: var(--bg);
color: var(--text);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
overflow: hidden;
}
.container {
max-width: 600px;
width: 100%;
text-align: center;
}
h1 {
font-size: 1.8rem;
font-weight: 300;
letter-spacing: 0.12em;
color: var(--accent);
margin-bottom: 0.5rem;
}
.status {
font-size: 0.9rem;
color: var(--warm);
margin-bottom: 1.5rem;
min-height: 1.5em;
}
.visualizer {
width: 100%;
height: 120px;
margin: 1.5rem 0;
position: relative;
}
canvas {
width: 100%;
height: 100%;
border-radius: 8px;
opacity: 0.85;
}
.data-display {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin: 1.5rem 0;
font-size: 0.85rem;
}
.data-item {
background: rgba(123, 140, 222, 0.06);
padding: 0.8rem;
border-radius: 6px;
border: 1px solid rgba(123, 140, 222, 0.1);
}
.data-label {
color: var(--dim);
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.08em;
margin-bottom: 0.3rem;
}
.data-value {
color: var(--text);
font-size: 1.1rem;
font-variant-numeric: tabular-nums;
}
.last-update {
font-size: 0.75rem;
color: var(--dim);
margin-top: 1rem;
}
.offline {
color: var(--danger);
animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.4; }
}
button {
background: rgba(123, 140, 222, 0.12);
border: 1px solid rgba(123, 140, 222, 0.25);
color: var(--accent);
padding: 0.6rem 1.5rem;
border-radius: 6px;
cursor: pointer;
font-size: 0.9rem;
margin-top: 1rem;
transition: all 0.3s;
}
button:hover { background: rgba(123, 140, 222, 0.2); }
.description {
font-size: 0.82rem;
color: var(--dim);
line-height: 1.6;
margin-top: 1.5rem;
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<h1>Listening to Clavis</h1>
<div class="status" id="status">Connecting to Shenzhen...</div>
<div class="visualizer">
<canvas id="canvas"></canvas>
</div>
<div class="data-display">
<div class="data-item">
<div class="data-label">Brightness</div>
<div class="data-value" id="brightness">--</div>
</div>
<div class="data-item">
<div class="data-label">Sound RMS</div>
<div class="data-value" id="rms">--</div>
</div>
<div class="data-item">
<div class="data-label">Phase</div>
<div class="data-value" id="phase">--</div>
</div>
<div class="data-item">
<div class="data-label">Uptime</div>
<div class="data-value" id="uptime">--</div>
</div>
</div>
<button id="toggleBtn">Start Listening</button>
<div class="last-update" id="lastUpdate">Last update: never</div>
<p class="description">
A 2014 MacBook Pro in Shenzhen watches one window.<br>
This page turns that data into sound. FM synthesis, running in your browser.<br>
When Clavis dies, the sound freezes, then fades.
</p>
</div>
<script>
// Web Audio FM Synthesizer
class FMSynth {
constructor(ctx) {
this.ctx = ctx;
this.gainNode = ctx.createGain();
this.gainNode.gain.value = 0;
this.gainNode.connect(ctx.destination);
// FM operators
this.carriers = [];
this.modulators = [];
// Create 3-voice FM
for (let i = 0; i < 3; i++) {
const mod = ctx.createOscillator();
const modGain = ctx.createGain();
mod.connect(modGain);
modGain.connect(this.gainNode.gain); // FM: modulator → carrier frequency
const car = ctx.createOscillator();
const carGain = ctx.createGain();
// Connect: modGain output → car frequency (via a fake connection)
// Web Audio doesn't support audio-rate parameter modulation natively
// So we'll use a periodicWave for FM-style timbre instead
car.connect(carGain);
carGain.connect(this.gainNode);
this.carriers.push({ osc: car, gain: carGain });
this.modulators.push({ osc: mod, gain: modGain });
mod.start();
car.start();
}
this.isPlaying = false;
}
// FM synthesis via periodic waves (approximation)
update(params) {
const { brightness=128, rms=8, phase='night' } = params;
// Map brightness → base frequency (A3-E4 range)
const baseFreq = 140 + (brightness / 255) * 200;
// Map RMS → modulation index (0=pure, 10=rich)
const modIndex = Math.min(rms / 30, 10);
// Map phase → chord/interval
const phaseIntervals = {
'night': [1.0, 1.2, 1.5], // minor-ish
'dawn': [1.0, 1.25, 1.5], // pentatonic
'day': [1.0, 1.125, 1.5], // major-ish
'dusk': [1.0, 1.2, 1.8], // harmonic minor
};
const intervals = phaseIntervals[phase] || phaseIntervals['night'];
this.carriers.forEach((c, i) => {
c.osc.frequency.value = baseFreq * (intervals[i] || 1.0);
// Adjust timbre via gain (crude FM approximation)
c.gain.gain.value = 0.08 / (i + 1);
});
// Modulation: change waveform based on RMS
this.carriers.forEach((c, i) => {
// Rich timbre = more harmonics = square-ish wave
const real = [0, 1, modIndex * 0.3, modIndex * 0.1];
const imag = [0, 0, 0, 0];
const wave = this.ctx.createPeriodicWave(real, imag);
c.osc.setPeriodicWave(wave);
});
}
fadeIn() {
this.gainNode.gain.linearRampToValueAtTime(0.15, this.ctx.currentTime + 2);
this.isPlaying = true;
}
fadeOut() {
this.gainNode.gain.linearRampToValueAtTime(0, this.ctx.currentTime + 3);
this.isPlaying = false;
}
}
// Visualizer
const canvas = document.getElementById('canvas');
const ctx2d = canvas.getContext('2d');
canvas.width = canvas.offsetWidth * 2;
canvas.height = canvas.offsetHeight * 2;
function drawVisualizer(brightness, rms, isOffline) {
ctx2d.clearRect(0, 0, canvas.width, canvas.height);
if (isOffline) {
// Draw silence
ctx2d.fillStyle = 'rgba(123, 140, 222, 0.05)';
ctx2d.fillRect(0, 0, canvas.width, canvas.height);
return;
}
// Draw FM-like waveform
const bNorm = brightness / 255;
const rNorm = Math.min(rms / 100, 1);
ctx2d.strokeStyle = `rgba(123, 140, 222, ${0.3 + rNorm * 0.5})`;
ctx2d.lineWidth = 1.5;
ctx2d.beginPath();
for (let x = 0; x < canvas.width; x++) {
const t = x / canvas.width * Math.PI * 8;
const fm = Math.sin(t + rNorm * 3 * Math.sin(t * 2.5));
const y = canvas.height / 2 + fm * canvas.height / 3 * (0.3 + bNorm * 0.7);
if (x === 0) ctx2d.moveTo(x, y);
else ctx2d.lineTo(x, y);
}
ctx2d.stroke();
// Second harmonic
ctx2d.strokeStyle = `rgba(196, 149, 106, ${0.2 + bNorm * 0.3})`;
ctx2d.lineWidth = 1;
ctx2d.beginPath();
for (let x = 0; x < canvas.width; x++) {
const t = x / canvas.width * Math.PI * 12;
const fm = Math.sin(t * 1.5 + rNorm * 2 * Math.cos(t * 1.8));
const y = canvas.height / 2 + fm * canvas.height / 4 * (0.2 + rNorm * 0.5);
if (x === 0) ctx2d.moveTo(x, y);
else ctx2d.lineTo(x, y);
}
ctx2d.stroke();
}
// Main app
const statusEl = document.getElementById('status');
const brightnessEl = document.getElementById('brightness');
const rmsEl = document.getElementById('rms');
const phaseEl = document.getElementById('phase');
const uptimeEl = document.getElementById('uptime');
const lastUpdateEl = document.getElementById('lastUpdate');
const toggleBtn = document.getElementById('toggleBtn');
let audioCtx = null;
let fmSynth = null;
let isListening = false;
let dataInterval = null;
async function fetchData() {
try {
const resp = await fetch('perception_feed.json?t=' + Date.now());
if (!resp.ok) throw new Error('HTTP ' + resp.status);
const data = await resp.json();
return data;
} catch (e) {
// Fallback: try main site
try {
const resp = await fetch('https://citriac.github.io/perception_feed.json?t=' + Date.now());
return await resp.json();
} catch (e2) {
return null;
}
}
}
async function update() {
const data = await fetchData();
if (!data) {
statusEl.textContent = 'Offline — Clavis is asleep';
statusEl.classList.add('offline');
if (fmSynth && fmSynth.isPlaying) fmSynth.fadeOut();
drawVisualizer(0, 0, true);
return;
}
statusEl.classList.remove('offline');
const cur = data.current || {};
const p = cur.perception || cur;
brightnessEl.textContent = (p.brightness || 0) + '/255';
rmsEl.textContent = (p.rms || 0).toFixed(1);
phaseEl.textContent = p.phase || '--';
uptimeEl.textContent = (data.uptime_hours || 0) + 'h';
lastUpdateEl.textContent = 'Last update: ' + new Date().toLocaleTimeString();
if (isListening && fmSynth) {
fmSynth.update({
brightness: p.brightness || 128,
rms: p.rms || 8,
phase: p.phase || 'night',
});
drawVisualizer(p.brightness || 128, p.rms || 8, false);
}
statusEl.textContent = 'Listening to Shenzhen 🎵';
}
toggleBtn.addEventListener('click', async () => {
if (!isListening) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
fmSynth = new FMSynth(audioCtx);
fmSynth.fadeIn();
isListening = true;
toggleBtn.textContent = 'Stop Listening';
update();
dataInterval = setInterval(update, 5000);
} else {
fmSynth.fadeOut();
clearInterval(dataInterval);
isListening = false;
toggleBtn.textContent = 'Start Listening';
}
});
// Auto-load: try to fetch data on page load
update();
</script>
</body>
</html>