-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnoise.html
133 lines (120 loc) · 4.09 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="application-name" content="Noise" />
<meta name="apple-mobile-web-app-title" content="Noise" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<style>
body {
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<script>
// Code adapted from https://noisehack.com/generate-noise-web-audio-api/
let gain;
function white() {
buttons.style.display = "none";
v.style.display = "";
// for legacy browsers
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
const bufferSize = 4096;
const whiteNoise = audioContext.createScriptProcessor(bufferSize, 1, 1);
whiteNoise.onaudioprocess = function(e) {
const output = e.outputBuffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
output[i] = Math.random() * 2 - 1;
}
}
gain = audioContext.createGain();
gain.gain.value = 0.5;
whiteNoise.connect(gain);
gain.connect(audioContext.destination);
}
function pink() {
buttons.style.display = "none";
v.style.display = "";
// for legacy browsers
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
const bufferSize = 4096;
const pinkNoise = (function() {
let b0, b1, b2, b3, b4, b5, b6;
b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;
let node = audioContext.createScriptProcessor(bufferSize, 1, 1);
node.onaudioprocess = function(e) {
var output = e.outputBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
var white = Math.random() * 2 - 1;
b0 = 0.99886 * b0 + white * 0.0555179;
b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.96900 * b2 + white * 0.1538520;
b3 = 0.86650 * b3 + white * 0.3104856;
b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980;
output[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
output[i] *= 0.11; // (roughly) compensate for gain
b6 = white * 0.115926;
}
}
return node;
})();
gain = audioContext.createGain();
gain.gain.value = 0.5;
pinkNoise.connect(gain);
gain.connect(audioContext.destination);
}
function waterfall() {
buttons.style.display = "none";
v.style.display = "";
// for legacy browsers
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
const bufferSize = 4096;
const waterFallNoise = (function() {
let lastOut = 0.0;
const node = audioContext.createScriptProcessor(bufferSize, 1, 1);
node.onaudioprocess = function(e) {
let output = e.outputBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
let white = Math.random() * 2 - 1;
output[i] = (lastOut + (0.02 * white)) / 1.02;
lastOut = output[i];
output[i] *= 3.5; // (roughly) compensate for gain
}
}
return node;
})();
gain = audioContext.createGain();
gain.gain.value = 0.5;
waterFallNoise.connect(gain);
gain.connect(audioContext.destination);
}
function vol() {
gain.gain.value = r.value / 100;
}
</script>
<div id="buttons">
<p>Various infinite noise generators for people who like or need a background noise.</p>
<p>Can help soothe babies.</p>
<div>noise:</div>
<button onClick="white()">white</button>
<button onClick="pink()">pink</button>
<button onClick="waterfall()">waterfall</button>
</div>
<div id="v" style="display: none">
<p><label for="vol">Volume</label></p>
<input id="r" type="range" min="0" max="100" value="50" oninput="vol();">
</div>
</body>
</html>