homepage/toys/starship/index.html

112 lines
3.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic">
<link rel="stylesheet" media="screen" href="/assets/css/default.css">
<link rel="icon" href="icon.svg">
<link rel="manifest" href="manifest.webmanifest">
<style>
#play {
padding: 0;
border: 0;
margin: 1em;
font-size: 2em;
background: inherit;
}
</style>
<title>Starship Noise Generator</title>
<script>
function whiteNoise(audioCtx) {
var bufferSize = 17 * audioCtx.sampleRate,
noiseBuffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate),
output = noiseBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
output[i] = Math.random() * 2 - 1;
}
var whiteNoise = audioCtx.createBufferSource();
whiteNoise.buffer = noiseBuffer;
whiteNoise.loop = true;
whiteNoise.start(0);
return whiteNoise;
}
function bandpassFilter(audioCtx, freq, gain) {
var filt = audioCtx.createBiquadFilter();
filt.type = "bandpass";
filt.frequency.value = freq;
filt.gain.value = gain;
return filt;
}
function init() {
var audioCtx = new window.AudioContext();
var synth = whiteNoise(audioCtx);
var filterA = bandpassFilter(audioCtx, 100, 20);
var filterB = bandpassFilter(audioCtx, 50, 20);
var gainA = audioCtx.createGain();
whiteNoise(audioCtx).connect(filterA);
filterA.connect(filterB);
filterB.connect(gainA);
gainA.connect(audioCtx.destination);
audioCtx.suspend();
function setFade() {
var faderPos = document.querySelector("#fader").value;
gainA.gain.value = faderPos;
}
document.querySelector("#fader").addEventListener("input", setFade);
setFade();
document.querySelector("#play").addEventListener("click", e => {
console.log(audioCtx.state);
if (audioCtx.state == 'running') {
audioCtx.suspend();
} else {
audioCtx.resume();
}
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
</script>
</head>
<body>
<h1 id="title">
<span>Starship Noise Generator</span>
</h1>
<main id="content">
<div>
<button id="play">⏯️</button>
🔉
<input id="fader" type="range" min="0" max="10" step="0.01" />
🔊
</div>
<p>I work in a building with no HVAC,
which means we can hear <em>everything</em> people are saying,
anywhere in the building.</p>
<p>This page is a low-CPU noise generator that runs entirely in JavaScript.
Once it starts,
you dont need an Internet connection to keep it going.
You can leave it running forever, if you like.</p>
<p>For those interested,
it uses the new (in 2017) and seriously perfect for this application
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API">Web Audio API</a>.</p>
</main>
</body>
</html>