Skip to content

Instantly share code, notes, and snippets.

@brunosan
Created August 4, 2019 10:24
Show Gist options
  • Save brunosan/0f4373c06a65605b6cb8659bc6a32685 to your computer and use it in GitHub Desktop.
Save brunosan/0f4373c06a65605b6cb8659bc6a32685 to your computer and use it in GitHub Desktop.
Make pink noise
<!DOCTYPE html>
<html>
<body>
<h2>Make (pink) noise</h2>
<p>To sleep, to quiet crying babies, to concentrate, ...</p>
<button onclick="myFunction()" >Start pink noise</button>
<script>
var bufferSize = 4096;
var pinkNoise = (function() {
var b0, b1, b2, b3, b4, b5, b6;
b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;
var 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;
})();
pinkNoise.connect(audioContext.destination);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment