-
Notifications
You must be signed in to change notification settings - Fork 8
/
noise.html
234 lines (191 loc) · 6.51 KB
/
noise.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
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Noise</title>
<link href="css/audio.css" rel="stylesheet" />
<style>
.label {
width: 80px;
}
.filter-slider {
width: 400px;
}
</style>
</head>
<body>
<div>
<span class="title">Noise</span>
</div>
<div>
<span class="label">Type</span>
<select id="type-select">
<option>white</option>
<option>pink</option>
<option>brown</option>
</select>
</div>
<div>
<span class="label">High pass</span> <input type="range" id="hi-filter-slider" class="filter-slider" min="0" max="400" step="1" value="0" />
<span id="hi-filter-display" class="label freq-label"></span>
<br/>
<span class="label">Low pass</span> <input type="range" id="lo-filter-slider" class="filter-slider" min="0" max="400" step="1" value="400" />
<span id="lo-filter-display" class="label freq-label"></span>
</div>
<div>
<input type="button" id="start" value="start" />
<input type="button" id="stop" value="stop" />
</div>
<div>
<canvas id="visualizer" width="1024" height="300"></canvas>
</div>
<div class="nav">
<a href="button.html">> Button Click</a><br/>
<a href="index.html">> Index</a>
</div>
</body>
<script type="text/javascript" src="js/audio-helper.js"></script>
<script type="text/javascript" src="js/frequency-visualizer.js"></script>
<script type="text/javascript">
(function () {
var context = new AudioContext();
var visualizer = new App.FrequencyVisualizer(context, document.getElementById('visualizer'));
var randomLeft, randomRight;
var scriptNode, loFilterNode, hiFilterNode, gainNode;
var typeSelect = document.getElementById('type-select');
typeSelect.addEventListener('change', selectType);
var hiFilterDisplay = document.getElementById('hi-filter-display');
var hiFilterSlider = document.getElementById('hi-filter-slider');
hiFilterSlider.addEventListener('input', function () {
updateFilter(hiFilterSlider, hiFilterDisplay, hiFilterNode);
});
var loFilterDisplay = document.getElementById('lo-filter-display');
var loFilterSlider = document.getElementById('lo-filter-slider');
loFilterSlider.addEventListener('input', function () {
updateFilter(loFilterSlider, loFilterDisplay, loFilterNode);
});
document.getElementById('start').addEventListener('click', start);
document.getElementById('stop').addEventListener('click', stop);
function createRandomBrown() {
var n = 0;
return function () {
var white = randomWhite();
var output = (n + (0.02 * white)) / 1.02;
n = output;
return output *= 3.5;
};
}
function createRandomPink(dbPerPole, dbOffset) {
var coeff = [];
var gain = [];
var state = [];
var twoPI = Math.PI * 2;
var freq = (context.sampleRate / twoPI) - 1;
var db = 0;
var sumGain = 0;
var i = 0;
while (freq > 1) {
state[i] = 0;
coeff[i] = twoPI * freq / context.sampleRate;
freq /= 4;
i++;
}
while (i-- > 0) {
gain[i] = dbToGain(db);
sumGain += gain[i];
db += dbPerPole;
}
var gainAdjust = dbToGain(-gainToDb(sumGain) + dbOffset);
return function () {
var white = randomWhite();
var pink = 0;
i = 0;
while (++i < coeff.length) {
state[i] = coeff[i] * white + (1 - coeff[i]) * state[i];
pink += state[i] * gain[i];
}
return pink * gainAdjust * 10;
}
}
function dbToGain(db) {
return Math.pow(10, db / 20);
}
function gainToDb(g) {
return Math.log(g) * 20 / Math.log(10);
}
function getFrequency(slider) {
return Math.min(Math.round((Math.pow(2, slider.value / 40)) * 20), 20000);
}
function randomWhite() {
return Math.random() * 2 - 1;
}
function selectType() {
var restart = scriptNode && true;
if (restart) {
stop();
}
switch (typeSelect.value) {
case 'white':
randomLeft = randomWhite;
randomRight = randomWhite;
break;
case 'pink':
randomLeft = createRandomPink(-6, 0);
randomRight = createRandomPink(-6, 0);
break;
case 'brown':
randomLeft = createRandomPink(-12, 9);
randomRight = createRandomPink(-12, 9);
break;
}
if (restart) {
start();
}
}
function start() {
if (!scriptNode) {
scriptNode = context.createScriptProcessor(4096, 0, 2);
scriptNode.onaudioprocess = function (e) {
var left = e.outputBuffer.getChannelData(0);
var right = e.outputBuffer.getChannelData(1);
for (var i = 0; i < left.length; i++) {
left[i] = randomLeft();
right[i] = randomRight();
}
};
hiFilterNode = context.createBiquadFilter();
hiFilterNode.frequency.value = getFrequency(hiFilterSlider);
hiFilterNode.type = 'highpass';
loFilterNode = context.createBiquadFilter();
loFilterNode.frequency.value = getFrequency(loFilterSlider);
loFilterNode.type = 'lowpass';
gainNode = context.createGain();
gainNode.gain.value = 1;
scriptNode.connect(hiFilterNode);
hiFilterNode.connect(loFilterNode);
loFilterNode.connect(gainNode);
gainNode.connect(context.destination);
visualizer.acceptConnection(gainNode);
}
}
function stop() {
if (scriptNode) {
gainNode.disconnect(context.destination);
visualizer.releaseConnection(gainNode);
scriptNode = undefined;
gainNode = undefined;
}
}
function updateFilter(slider, label, filterNode) {
var filterFrequency = getFrequency(slider);
label.textContent = filterFrequency + ' hz';
if (filterNode) {
filterNode.frequency.value = filterFrequency;
}
}
selectType();
updateFilter(hiFilterSlider, hiFilterDisplay);
updateFilter(loFilterSlider, loFilterDisplay);
})();
</script>
</html>