-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume-control.html
More file actions
222 lines (213 loc) · 13 KB
/
Copy pathvolume-control.html
File metadata and controls
222 lines (213 loc) · 13 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
<!DOCTYPE html>
<html>
<head>
<title>Volume Engine V34 - File Playback Fixed</title>
<style>
body { font-family: monospace; background: #050505; color: #00ff00; padding: 10px; display: flex; flex-direction: column; align-items: center; margin: 0; }
.panel { background: #111; border: 1px solid #00ff00; padding: 15px; width: 620px; box-shadow: 0 0 20px #00ff0033; box-sizing: border-box; overflow: hidden; }
.row { margin-bottom: 8px; display: flex; flex-direction: column; }
label { font-size: 9px; color: #008800; text-transform: uppercase; margin-bottom: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
input, select { background: #000; border: 1px solid #008800; color: #00ff00; padding: 6px; font-size: 11px; font-family: monospace; outline: none; box-sizing: border-box; width: 100%; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.master-readout { border: 1px solid #008800; padding: 10px; text-align: center; margin-bottom: 10px; background: #000; }
#db_readout { font-size: 2.2rem; font-weight: bold; line-height: 1; }
.bar-container { height: 12px; width: 100%; background: #000; border: 1px solid #004400; position: relative; margin-top: 8px; }
#volume_bar { height: 100%; width: 0%; background: #00ff00; box-shadow: 0 0 10px #00ff0066; }
.engine-view { display: flex; gap: 15px; height: 260px; margin-bottom: 15px; align-items: stretch; }
.viz, .ctrl-container { flex: 1; aspect-ratio: 1 / 1; border: 1px solid #004400; background: #080808; display: flex; justify-content: center; align-items: center; overflow: hidden; position: relative; }
svg { height: 100%; width: 100%; display: block; overflow: visible; }
input[type="range"] { appearance: none; background: #002200; height: 10px; width: 85%; cursor: pointer; }
input[type="range"]::-webkit-slider-thumb { appearance: none; height: 20px; width: 10px; background: #00ff00; border: 1px solid #000; }
.slider-v { transform: rotate(-90deg); transform-origin: center; cursor: ns-resize; }
.slider-h { cursor: ew-resize; }
.knob-svg { cursor: ns-resize; touch-action: none; overflow: visible; display: block; }
.knob-svg * { pointer-events: none; }
.knob-track { fill: none; stroke: #1a1a1a; stroke-width: 6; stroke-linecap: round; }
.knob-value { fill: none; stroke: #00ff00; stroke-width: 6; stroke-linecap: round; }
.knob-center { fill: #111; stroke: #008800; stroke-width: 2; }
.knob-indicator { fill: #00ff00; }
#audio_btn { width: 100%; padding: 12px; background: #003300; color: #00ff00; border: 1px solid #00ff00; cursor: pointer; font-family: monospace; font-weight: bold; margin-bottom: 8px; font-size: 11px; }
#audio_btn.active { background: #ff0000; color: #fff; }
.hidden { display: none !important; }
</style>
</head>
<body>
<div class="panel">
<button id="audio_btn">INITIALIZE ENGINE</button>
<div class="master-readout">
<div id="db_readout">-- dBFS</div>
<div class="bar-container"><div id="volume_bar"></div></div>
</div>
<div class="engine-view">
<div class="viz">
<svg viewBox="0 0 100 100">
<line x1="50" y1="0" x2="50" y2="100" stroke="#003300" stroke-width="0.5" />
<path id="curve_path" d="" fill="none" stroke="#00ff00" stroke-width="2" />
<circle id="tracker_dot" r="3" fill="#fff" />
<rect id="pivot_box" width="6" height="6" fill="none" stroke="#ff0000" stroke-width="1.5" style="display:none;" />
</svg>
</div>
<div class="ctrl-container" id="work_area">
<input type="range" id="slider" min="0" max="100" step="0.0001" value="50">
<div id="knob_wrapper">
<svg id="knob_svg" class="knob-svg" viewBox="0 0 100 100">
<path class="knob-track" d="M 21.7 78.3 A 40 40 0 1 1 78.3 78.3" />
<path id="knob_arc" class="knob-value" d="" />
<circle class="knob-center" cx="50" cy="50" r="32" />
<circle id="knob_ptr" class="knob-indicator" cx="50" cy="18" r="3" />
</svg>
</div>
</div>
</div>
<div class="grid">
<div class="row"><label>Interface Type</label><select id="ui_type"><option value="knob" selected>Rotary Dial</option><option value="slider_h">H Slider</option><option value="slider_v">V Slider</option></select></div>
<div class="row"><label>Algorithm</label><select id="logic_type"><option value="inv_sigmoid" selected>Inv-Sigmoid (Cosine)</option><option value="s_curve">S-Curve (Cosine)</option><option value="linear">Linear</option><option value="log">Logarithmic</option></select></div>
</div>
<div class="grid">
<div class="row"><label>Audio Source</label>
<select id="source_type"><option value="440">440Hz Sine</option><option value="1000">1kHz Sine</option><option value="white">White Noise</option><option value="pink">Pink Noise</option><option value="file">Local File (URI)</option></select>
<div id="file_ui" class="hidden"><input type="file" id="file_input" accept="audio/*"></div>
</div>
<div class="row"><label>Control Size (%)</label><input type="number" id="ctrl_size_pct" value="85"></div>
</div>
<div class="grid">
<div class="row"><label>Min dBFS</label><input type="number" id="min_db" value="-80"></div>
<div class="row"><label>Max dBFS</label><input type="number" id="max_db" value="0"></div>
<div class="row pivot-ui"><label>Pivot dB</label><input type="number" id="pivot_db" value="-40.0"></div>
<div class="row pivot-ui"><label>Pivot %</label><input type="number" id="pivot_pos" value="50"></div>
</div>
</div>
<script>
const el = id => document.getElementById(id);
let audioCtx, currentSource, gainNode, isRunning = false;
function createNoise(type) {
const bufSize = (audioCtx?.sampleRate || 44100) * 2;
const buf = audioCtx.createBuffer(1, bufSize, audioCtx.sampleRate);
const d = buf.getChannelData(0);
let b0=0, b1=0, b2=0, b3=0, b4=0, b5=0, b6=0;
for (let i = 0; i < bufSize; i++) {
const w = Math.random() * 2 - 1;
if(type==='white') { d[i] = w; } else {
b0 = 0.99886 * b0 + w * 0.0555179; b1 = 0.99332 * b1 + w * 0.0750312;
b2 = 0.96900 * b2 + w * 0.1538520; b3 = 0.86650 * b3 + w * 0.3104856;
b4 = 0.55000 * b4 + w * 0.5329522; b5 = -0.7616 * b5 - w * 0.0168980;
d[i] = (b0 + b1 + b2 + b3 + b4 + b5 + b6 + w * 0.5362) * 0.11;
b6 = w * 0.115926;
}
}
const node = audioCtx.createBufferSource(); node.buffer = buf; node.loop = true; return node;
}
async function setupSource() {
if (currentSource) { try { currentSource.stop(); } catch(e){} currentSource.disconnect(); }
const type = el('source_type').value;
if (type === 'file') {
const files = el('file_input').files;
if (!files || files.length === 0) {
console.log("No file selected.");
return;
}
try {
const arrayBuf = await files[0].arrayBuffer();
const audioBuf = await audioCtx.decodeAudioData(arrayBuf);
currentSource = audioCtx.createBufferSource();
currentSource.buffer = audioBuf;
currentSource.loop = true;
} catch(e) {
console.error("Audio decoding failed", e);
return;
}
} else if (type === 'white' || type === 'pink') {
currentSource = createNoise(type);
} else {
currentSource = audioCtx.createOscillator();
currentSource.frequency.value = parseFloat(type);
}
currentSource.connect(gainNode);
if (isRunning) currentSource.start();
}
function calculateDb(x) {
const max = parseFloat(el('max_db').value), min = parseFloat(el('min_db').value);
const pDb = parseFloat(el('pivot_db').value), pPos = parseFloat(el('pivot_pos').value)/100;
const mode = el('logic_type').value, val = x / 100;
if (mode === 'linear') return min + (val * (max - min));
if (mode === 'log') return Math.max(min, max + (20 * Math.log10(val || 1e-10)));
const pRatio = (pDb - min) / (max - min);
let ratio;
if (val <= pPos) {
const norm = val / (pPos || 0.001);
const cosFactor = mode === 's_curve' ? (1 - Math.cos(norm * Math.PI / 2)) : Math.sin(norm * Math.PI / 2);
ratio = pRatio * cosFactor;
} else {
const norm = (val - pPos) / (1 - pPos || 0.001);
const cosFactor = mode === 's_curve' ? Math.sin(norm * Math.PI / 2) : (1 - Math.cos(norm * Math.PI / 2));
ratio = pRatio + (1 - pRatio) * cosFactor;
}
return min + (ratio * (max - min));
}
function update() {
const x = parseFloat(el('slider').value), db = calculateDb(x);
const max = parseFloat(el('max_db').value), min = parseFloat(el('min_db').value);
const uiMode = el('ui_type').value, pct = parseFloat(el('ctrl_size_pct').value) / 100;
el('db_readout').innerText = db.toFixed(2) + " dBFS";
el('volume_bar').style.width = Math.max(0, Math.min(100, ((db - min) / (max - min)) * 100)) + "%";
if (isRunning && gainNode) gainNode.gain.setTargetAtTime(Math.pow(10, db/20), audioCtx.currentTime, 0.025);
el('slider').classList.toggle('hidden', uiMode === 'knob');
el('knob_wrapper').classList.toggle('hidden', uiMode !== 'knob');
el('file_ui').classList.toggle('hidden', el('source_type').value !== 'file');
const area = el('work_area');
const dim = Math.min(area.offsetWidth, area.offsetHeight) * pct;
if (uiMode === 'slider_v') { el('slider').className = 'slider-v'; el('slider').style.width = dim + "px"; }
else if (uiMode === 'slider_h') { el('slider').className = 'slider-h'; el('slider').style.width = dim + "px"; }
else { el('knob_svg').style.width = dim + "px"; el('knob_svg').style.height = dim + "px"; }
const sweep = (x/100)*270, endRad = (135+sweep)*(Math.PI/180);
el('knob_arc').setAttribute('d', `M ${50+40*Math.cos(135*Math.PI/180)} ${50+40*Math.sin(135*Math.PI/180)} A 40 40 0 ${sweep>180?1:0} 1 ${50+40*Math.cos(endRad)} ${50+40*Math.sin(endRad)}`);
el('knob_ptr').setAttribute('cx', 50+24*Math.cos(endRad)); el('knob_ptr').setAttribute('cy', 50+24*Math.sin(endRad));
let d = "M ";
for (let i=0; i<=100; i++) {
const y = 100 - (((calculateDb(i)-min)/(max-min))*100);
d += `${i},${y} `;
if (Math.abs(i-x) < 0.5) { el('tracker_dot').setAttribute('cx', i); el('tracker_dot').setAttribute('cy', y); }
}
el('curve_path').setAttribute('d', d);
const isPivot = ['s_curve', 'inv_sigmoid'].includes(el('logic_type').value);
document.querySelectorAll('.pivot-ui').forEach(u => u.classList.toggle('hidden', !isPivot));
if (isPivot) {
el('pivot_box').style.display = 'block';
el('pivot_box').setAttribute('x', parseFloat(el('pivot_pos').value) - 3);
el('pivot_box').setAttribute('y', (100 - (((parseFloat(el('pivot_db').value)-min)/(max-min))*100)) - 3);
} else el('pivot_box').style.display = 'none';
}
let drag = { active: false, lastY: 0 };
const startDrag = y => { drag.active = true; drag.lastY = y; };
const moveDrag = y => { if (!drag.active) return; el('slider').value = Math.max(0, Math.min(100, parseFloat(el('slider').value) + (drag.lastY - y) * 0.5)); drag.lastY = y; update(); };
window.addEventListener('mousedown', e => { if(e.target.closest('#knob_svg') || (e.target.closest('#work_area') && el('ui_type').value!=='knob')) startDrag(e.clientY); });
el('knob_svg').addEventListener('touchstart', e => { startDrag(e.touches[0].clientY); e.preventDefault(); }, {passive:false});
window.addEventListener('mousemove', e => moveDrag(e.clientY));
window.addEventListener('touchmove', e => { if(drag.active) { moveDrag(e.touches[0].clientY); e.preventDefault(); }}, {passive:false});
window.addEventListener('mouseup', () => drag.active = false);
window.addEventListener('touchend', () => drag.active = false);
async function toggleAudio() {
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
if (!isRunning) {
isRunning = true; gainNode = audioCtx.createGain(); gainNode.connect(audioCtx.destination);
await setupSource();
el('audio_btn').innerText = "STOP ENGINE"; el('audio_btn').classList.add('active');
} else {
isRunning = false; if(currentSource) { try{currentSource.stop();}catch(e){} }
el('audio_btn').innerText = "INITIALIZE ENGINE"; el('audio_btn').classList.remove('active');
}
update();
}
const bind = id => {
el(id).addEventListener('change', async () => {
if (isRunning) await setupSource();
update();
});
el(id).addEventListener('keydown', e => { if(e.key==='Enter') { update(); e.target.blur(); }});
};
el('audio_btn').addEventListener('click', toggleAudio); el('slider').addEventListener('input', update);
['min_db', 'max_db', 'pivot_db', 'pivot_pos', 'logic_type', 'ctrl_size_pct', 'ui_type', 'source_type', 'file_input'].forEach(bind);
update();
</script>
</body>
</html>