Repository navigation
Expand file tree
/
Copy pathparams.js
More file actions
310 lines (282 loc) · 10.3 KB
/
Copy pathparams.js
File metadata and controls
310 lines (282 loc) · 10.3 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
/**
* params.js — validated project state, persistence, DOM bindings, track display.
*/
import { createDefaultProject, sanitizeProject, SCHEMA_VERSION } from './project-schema.js';
export const P = createDefaultProject();
const STORAGE_KEY = 'psychograph_params';
/** Columns derived from complexity — the shared lattice width for every visual mode. */
export function getCols() {
return Math.max(2, Math.min(384, Math.round(P.complexity) * 32));
}
function element(id) {
return document.getElementById(id);
}
function setInput(id, value) {
const target = element(id);
if (target) target.value = (value !== undefined && value !== null) ? value : '';
}
function setLabel(id, value) {
const target = element(id);
if (target) target.textContent = value;
}
export function syncControls() {
const bindings = {
FreqScale: 'freqScale',
FreqRange: 'freqRange',
Smoothing: 'smoothing',
MaxDisp: 'maxDisp',
BowlExp: 'bowlExp',
Morph: 'morph',
LfoWaveform: 'lfoWaveform',
LfoRate: 'lfoRate',
LfoDepth: 'lfoDepth',
LfoOffset: 'lfoOffset',
Lfo2Waveform: 'lfo2Waveform',
Lfo2Rate: 'lfo2Rate',
Lfo2Depth: 'lfo2Depth',
Lfo2Offset: 'lfo2Offset',
LfoToDisp: 'lfoToDisp',
LfoToBowl: 'lfoToBowl',
LfoToZoom: 'lfoToZoom',
LfoToOpacity: 'lfoToOpacity',
LfoToPolar: 'lfoToPolar',
LfoToWave: 'lfoToWave',
Rows: 'rows',
Complexity: 'complexity',
ModSub: 'modSub',
ModKick: 'modKick',
ModMid: 'modMid',
ModHigh: 'modHigh',
ModRms: 'modRms',
ModTrans: 'modTrans',
SphereSize: 'sphereSize',
WaveSpacing: 'waveSpacing',
PolarSpacing: 'polarSpacing',
FieldScale: 'fieldScale',
FieldDepth: 'fieldDepth',
FieldTwist: 'fieldTwist',
FieldCurl: 'fieldCurl',
FieldStereo: 'fieldStereo',
FieldFlow: 'fieldFlow',
FieldSymmetry: 'fieldSymmetry',
FieldHistory: 'fieldHistory',
FieldRotation: 'fieldRotation',
FieldOpacity: 'fieldOpacity',
Gain: 'gain',
ColorCycle: 'colorCycle',
UiReactivity: 'uiReactivity',
Reactivity: 'reactivity',
CycleMode: 'cycleMode',
PeakSens: 'peakSens',
CameraMotion: 'cameraMotion',
CameraAmount: 'cameraAmount',
CameraSpeed: 'cameraSpeed',
CameraAudio: 'cameraAudio',
CameraTransition: 'cameraTransition',
CameraFov: 'cameraFov',
ExportPreset: 'exportPreset',
ExportOrient: 'exportOrientation',
ExportAspect: 'exportAspect',
ExportTitleCard: 'exportTitleCard',
AlbumTracklistStyle: 'albumTracklistStyle',
Title: 'title',
Artist: 'artist',
Bpm: 'bpm',
Genre: 'genre',
LookProfile: 'lookProfile',
LookPixelSnap: 'lookPixelSnap',
LookColorBits: 'lookColorBits',
LookDither: 'lookDither',
LookScanlines: 'lookScanlines',
LookInterlace: 'lookInterlace',
LookChroma: 'lookChroma',
LookNoise: 'lookNoise',
LookFeedback: 'lookFeedback',
LookCadence: 'lookCadence',
ItemSpinSpeed: 'itemSpinSpeed',
ItemGlitch: 'itemGlitch',
ItemWobble: 'itemWobble',
ItemRotX: 'itemRotX',
ItemRotY: 'itemRotY',
ItemRotZ: 'itemRotZ',
SkyboxPreset: 'skyboxPreset',
SkyboxLightTone: 'skyboxLightTone',
VhsTracking: 'vhsTracking',
VhsCurvature: 'vhsCurvature',
VhsSyncDrop: 'vhsSyncDrop',
};
for (const [suffix, key] of Object.entries(bindings)) {
setInput(`p${suffix}`, P[key]);
if (key === 'itemRotX' || key === 'itemRotY' || key === 'itemRotZ' || key === 'cameraFov') {
setLabel(`v${suffix}`, `${P[key]}°`);
} else if (key === 'skyboxLightTone' || key === 'reactivity') {
setLabel(`v${suffix}`, Number(P[key]).toFixed(2));
} else {
setLabel(`v${suffix}`, P[key]);
}
}
const lpfPosition = Math.log10(P.lpfCutoff / 200) / Math.log10(20000 / 200);
setInput('pLpfCutoff', Math.max(0, Math.min(1, lpfPosition)));
setLabel(
'vLpfCutoff',
P.lpfCutoff >= 1000
? `${(P.lpfCutoff / 1000).toFixed(P.lpfCutoff >= 10000 ? 0 : 1)}k`
: P.lpfCutoff,
);
setInput('pColorA', P.colorA);
setInput('pColorB', P.colorB);
setLabel('vColorA', (P.colorA || '#e8d5b0').toUpperCase());
setLabel('vColorB', (P.colorB || '#b090c8').toUpperCase());
setInput('pTitle', P.title);
setInput('pArtist', P.artist);
setInput('pBpm', P.bpm);
setInput('pGenre', P.genre);
const fastBoot = element('pFastBoot');
if (fastBoot) fastBoot.checked = P.fastBoot;
const previewOutput = element('pPreviewOutput');
if (previewOutput) previewOutput.checked = P.previewOutput;
const overlayInSignal = element('pLookOverlayInSignal');
if (overlayInSignal) overlayInSignal.checked = P.lookOverlayInSignal;
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent('avatar-controls-synced', { detail: P }));
}
}
export function saveParams() {
const bpmValue = Number(element('pBpm')?.value);
if (Number.isFinite(bpmValue)) P.bpm = Math.max(20, Math.min(400, bpmValue));
const persisted = {
...P,
schemaVersion: SCHEMA_VERSION,
title: element('pTitle')?.value ?? '',
artist: element('pArtist')?.value ?? '',
genre: element('pGenre')?.value ?? '',
isLight: document.body.classList.contains('light-mode'),
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(persisted));
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent('avatar-params-saved', { detail: P }));
}
}
export function loadParams(rebuildVisual) {
try {
const raw = JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}');
const { project, warnings } = sanitizeProject(raw);
Object.assign(P, project);
document.body.setAttribute('data-palette', P.uiPalette);
document.body.classList.toggle('light-mode', P.isLight);
syncControls();
updateTrackDisplay();
rebuildVisual?.();
if (warnings.length) {
console.warn('[AVATAR config]', ...warnings);
setTimeout(() => window.dispatchEvent(new CustomEvent('avatar-status', {
detail: `Config repaired: ${warnings.length} invalid value${warnings.length === 1 ? '' : 's'}`,
})), 0);
}
} catch (error) {
console.error('[AVATAR config] Unable to load saved project', error);
syncControls();
setTimeout(() => window.dispatchEvent(new CustomEvent('avatar-status', {
detail: 'Saved config could not be loaded; defaults restored',
})), 0);
}
}
export function bindRange(id, valueId, key, onChange) {
const input = element(id);
const label = element(valueId);
if (!input) return;
input.addEventListener('input', () => {
let value = Number(input.value);
if (!Number.isFinite(value)) return;
if (key === 'rows') value = Math.max(2, Math.min(160, Math.round(value)));
if (key === 'complexity') value = Math.max(1, Math.min(12, Math.round(value)));
P[key] = value;
input.value = value;
if (label) {
if (key === 'itemRotX' || key === 'itemRotY' || key === 'itemRotZ' || key === 'cameraFov') {
label.textContent = `${value}°`;
} else if (key === 'skyboxLightTone' || key === 'reactivity') {
label.textContent = Number(value).toFixed(2);
} else {
label.textContent = value;
}
}
onChange?.(value);
saveParams();
});
}
export function bindSelect(id, valueId, key, onChange) {
const input = element(id);
const label = element(valueId);
if (!input) return;
input.addEventListener('change', () => {
P[key] = input.value;
if (label) label.textContent = input.value;
onChange?.(input.value);
saveParams();
});
}
export function bindText(id, callback) {
const input = element(id);
if (!input) return;
input.addEventListener('input', () => {
if (id === 'pBpm') {
const value = Number(input.value);
if (Number.isFinite(value)) P.bpm = Math.max(20, Math.min(400, value));
} else if (id === 'pTitle') {
P.title = input.value;
} else if (id === 'pArtist') {
P.artist = input.value;
} else if (id === 'pGenre') {
P.genre = input.value;
}
callback?.();
saveParams();
});
}
export function updateTrackDisplay() {
const title = element('pTitle')?.value?.trim() || P.title || '';
const artist = element('pArtist')?.value?.trim() || P.artist || '';
const bpm = element('pBpm')?.value?.trim() || (P.bpm ? String(P.bpm) : '');
const genre = element('pGenre')?.value?.trim() || P.genre || '';
const time = element('dispCurrent')?.textContent?.trim() ?? '';
const elTitle = element('dispTitle') || element('deckTrackTitle');
if (elTitle) elTitle.textContent = title || 'AVATAR VISUALISER';
const elArtist = element('dispArtist') || element('deckTrackArtist');
if (elArtist) elArtist.textContent = artist || 'SUB-SURFACE TECHNOLOGIES';
if (element('dispBpm')) element('dispBpm').textContent = bpm;
if (element('dispGenre')) element('dispGenre').textContent = genre;
if (element('dispRule')) element('dispRule').style.display = title ? 'block' : 'none';
const showPill = (pillId, dividerId, visible) => {
const p = element(pillId);
if (p) p.style.display = visible ? 'flex' : 'none';
const d = dividerId ? element(dividerId) : null;
if (d) d.style.display = visible ? 'block' : 'none';
};
showPill('pillTime', 'divTime', Boolean(time));
showPill('pillBpm', 'divBpm', Boolean(bpm));
showPill('pillGenre', null, Boolean(genre));
const divTime = element('divTime');
if (divTime) divTime.style.display = time && (bpm || genre) ? 'block' : 'none';
const divBpm = element('divBpm');
if (divBpm) divBpm.style.display = bpm && genre ? 'block' : 'none';
}
export function fmtTime(seconds) {
return `${String(Math.floor(seconds / 60)).padStart(2, '0')}:${String(Math.floor(seconds % 60)).padStart(2, '0')}`;
}
export function setTimeDisplay(current, total) {
const dispCurrent = element('dispCurrent');
if (dispCurrent) {
dispCurrent.textContent = total ? `${fmtTime(current)} / ${fmtTime(total)}` : '';
}
updateTrackDisplay();
}
export function getTrackMeta() {
const title = element('pTitle')?.value?.trim() || P.title || '';
const artist = element('pArtist')?.value?.trim() || P.artist || '';
const bpm = element('pBpm')?.value?.trim() || (P.bpm ? String(P.bpm) : '');
const genre = element('pGenre')?.value?.trim() || P.genre || '';
const titleCard = element('pExportTitleCard')?.value || P.exportTitleCard || 'top';
const albumTracklistStyle = element('pAlbumTracklistStyle')?.value || P.albumTracklistStyle || 'vcr-osd';
return { title, artist, bpm, genre, titleCard, albumTracklistStyle };
}