-
Notifications
You must be signed in to change notification settings - Fork 0
/
v5.html
225 lines (195 loc) · 7.67 KB
/
v5.html
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
<!--
Part of a suite: noiz (2020)
Copyright (C) 2020 真空 (@ Menhera.org) All rights reserved.
This work is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This work is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>noiz - Version 5</title>
<style>
body {
margin: 0;
}
article {
margin: 1em;
}
</style>
<article>
<h1><i>noiz</i> (2020): <i>Version 5</i></h1>
<p>Work by <span lang="ja">真空</span></p>
<p><strong>This page plays sounds.</strong></p>
<p><label><input type="checkbox" checked id="control_update" autocomplete="off"> Update</label></p>
<p><label>Mean update interval: <input type="number" min="1" step="1" value="5" autocomplete="off" id="control_delay"> s</label></p>
</article>
<noiz-visualizer id="visualizer"></noiz-visualizer>
<script>
const control_update = document.getElementById('control_update');
const control_delay = document.getElementById('control_delay');
const visualizer = document.getElementById('visualizer');
const DIVISION = 12;
const BASE = 32.70319566257483;
const E = 65536;
const R = 32;
const C = .75;
const RANGE = 7;
{
const shadows = new WeakMap;
customElements.define('noiz-visualizer',
class NoizVisualizerElement extends HTMLElement {
constructor()
{
super();
const shadow = Object.create(null);
shadows.set(this, shadow);
shadow.root = this.attachShadow({mode: 'closed'});
shadow.container = document.createElement('div');
shadow.root.append(shadow.container);
shadow.container.style.position = 'relative';
this.aspectRatio = .5625;
shadow.grid = document.createElement('div');
shadow.container.append(shadow.grid);
shadow.grid.style.display = 'grid';
shadow.grid.style.position = 'absolute';
shadow.grid.style.top = '0';
shadow.grid.style.left = '0';
shadow.grid.style.width = '100%';
shadow.grid.style.height = '100%';
shadow.grid.style.gridAutoRows = '1fr';
shadow.recalc = () => void 0;
shadow.size = new Uint32Array(2);
this.resize(0, 0);
}
get aspectRatio()
{
const shadow = shadows.get(this);
return shadow.aspectRatio;
}
set aspectRatio(aspectRatio)
{
const shadow = shadows.get(this);
shadow.aspectRatio = isFinite(aspectRatio) && 0 < aspectRatio
? Number(aspectRatio) : 1;
shadow.container.style.paddingBlockEnd = `${shadow.aspectRatio * 100}%`;
}
get size()
{
const shadow = shadows.get(this);
return new Uint32Array(shadow.size);
}
resize(nCols, nRows)
{
const shadow = shadows.get(this);
shadow.size[0] = nCols;
shadow.size[1] = nRows;
[... shadow.grid.childNodes].forEach(node => node.remove());
for (let i = 0; i < shadow.size[0] * shadow.size[1]; i++) {
const item = document.createElement('div');
shadow.grid.append(item);
item.style.backgroundColor = '#000';
item.style.opacity = '0';
}
shadow.grid.style.gridTemplateColumns = `repeat(${shadow.size[0]}, 1fr)`;
}
set(aCol, aRow, aValue)
{
const [col, row] = new Uint32Array([aCol, aRow]);
const value = isFinite(aValue)
? Math.min(1, Math.max(0, aValue)) : 1;
const shadow = shadows.get(this);
const logValue = Math.max(0, 1 + Math.log(value) / 11.090354888959124951);
shadow.grid.children[col + row * shadow.size[0]].style.opacity = '' + logValue;
}
}
);
}
const ac = new (window.AudioContext || window.webkitAudioContext);
const interval01 = (leftIsClosed, rightIsClosed) => {
let x;
do {
x = Math.random();
} while (x == 0 && !leftIsClosed || x == 1 && !rightIsClosed);
return x;
};
const logRandom = e => {
if (!e || isNaN(e) || e <= 0) {
return (Math.E ** Math.random() - 1) / (Math.E - 1);
} else if (e == 1) {
return Math.random();
} else if (e > 1) {
return (e ** Math.random() - 1) / (e - 1);
} else {
const e1 = 1 / e;
return 1 - (e1 ** Math.random() - 1) / (e1 - 1);
}
};
const logisticRandom = r => {
return 1 / (1 + Math.exp(- r * (Math.random() - C)));
};
const shuf = (() => {
const RAW_RANGE = 1 + 2 ** (RANGE - 1);
const re = new Float32Array(RANGE);
const im = new Float32Array(RANGE);
return levels => {
for (let i = 0; i < RANGE; i++) {
const phase = 2 * Math.PI * interval01(true, false);
const abs = logRandom(E);
const j = 2 ** i;
re[j] = abs * Math.cos(phase);
im[j] = abs * Math.sin(phase);
levels[i] = abs;
}
return ac.createPeriodicWave(re, im);
};
})();
const tones = [];
const gains = [];
const levels = [];
for (let i = 0; i < DIVISION; i++) {
tones[i] = ac.createOscillator();
gains[i] = ac.createGain();
gains[i].gain.value = 0.1;
const f = BASE * 2 ** (i / DIVISION);
tones[i].frequency.value = f;
tones[i].connect(gains[i]);
gains[i].connect(ac.destination);
levels[i] = new Float32Array(RANGE);
}
const update = () => {
tones.forEach(
(osc, i) => osc.setPeriodicWave(shuf(levels[i]))
);
gains.forEach(
(gain, i) => {
const value = 0.2 * logisticRandom(R);
levels[i].forEach((abs, j) => void visualizer.set(i, j, abs * value));
gain.gain.value = value;
}
);
};
(async () => {
visualizer.resize(DIVISION, RANGE);
update();
tones.forEach(osc => osc.start());
while (true) {
const mean = Math.trunc(control_delay.value) * 1000;
const delay = Math.trunc(- Math.log(interval01(false, true)) * mean);
// -ln( [Uniform in (0,1)] ) * [avg]
await new Promise(res => setTimeout(res, delay));
if (control_update.checked) {
update();
}
}
})();
document.body.onclick = () => ac.resume();
</script>
</html>