Skip to content

Instantly share code, notes, and snippets.

@soenkehahn
Created December 8, 2016 04:48
Show Gist options
  • Save soenkehahn/f2947c5e37a8fff8fedbbd9a1def4d98 to your computer and use it in GitHub Desktop.
Save soenkehahn/f2947c5e37a8fff8fedbbd9a1def4d98 to your computer and use it in GitHub Desktop.
<html>
<body id="main">
hi
<script src="sound.js"></script>
<script>
/*
var node = document.getElementById('main');
console.log(node);
var app = Elm.Main.embed(node);
console.log(app.ports.eval);
app.ports.eval.subscribe((string) => {
console.log('eval');
console.log(string);
eval(string);
}); */
</script>
</body>
</html>
const context = new window.AudioContext
const oscillators = {}
const addOscillator = (key, freq) => {
var osc = context.createOscillator();
osc.frequency.value = freq;
osc.connect(context.destination);
oscillators[key] = osc;
};
const keys = "x2v3l4c5w6k7h8g9f0q";
let keyNumber = 0;
for (key of keys) {
console.log(key, keyNumber);
freq = 300 * Math.pow(Math.pow(2, 1 / 12), keyNumber);
addOscillator(key, freq);
keyNumber += 1;
};
document.body.onkeyup = (k) => {
if (oscillators.hasOwnProperty(k.key)) {
osc = oscillators[k.key];
osc.stop();
const freq = osc.frequency.value;
addOscillator(k.key, freq);
};
};
document.body.onkeydown = (k) => {
if (oscillators.hasOwnProperty(k.key)) {
osc = oscillators[k.key];
osc.start();
};
}
/*
const osc = context.createOscillator()
osc.frequency.value = 440
osc.connect(context.destination)
osc.start()
console.log(osc)
setTimeout(() => {
osc.stop();
}, 1000);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment