Skip to content

Instantly share code, notes, and snippets.

@gmeluski
Forked from anonymous/index.html
Created June 11, 2016 19:25
Show Gist options
  • Save gmeluski/03b3e967672f73e0acedd7ab799dd3ae to your computer and use it in GitHub Desktop.
Save gmeluski/03b3e967672f73e0acedd7ab799dd3ae to your computer and use it in GitHub Desktop.
854byte synth 854byte synth - No HTML or CSS is needed. Everything is rendered by Javascript. // source http://jsbin.com/qecemepuzi
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="854byte synth - No HTML or CSS is needed. Everything is rendered by Javascript.">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>854byte synth</title>
</head>
<body>
<h1>854byte synth</h1>
<p>No HTML or CSS is needed. Everything is rendered by Javascript.</p>
<a href="http://closure-compiler.appspot.com/code/jsc69623bc0dd0390c015615586a1b18cac/default.js">minified source</a> made by <a href="http://twitter.com-reimertz"> reimertz</a>
<script id="jsbin-javascript">
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==
var d=document;d.body.innerHTML += '<p id="p"></p><style>#p{display:flex;flex-direction:row}#p>*{align-items:flex-end;justify-content:center;display:flex;padding-bottom:5px;width:50px;height:100px;color:#000;background:#fea;border-right:1px solid #fff;opacity:.8}#p>[class$="_"]{color:#fff;background:#000;position:absolute;height:50px;margin-left:-25px;z-index:1}[s*="t"]{opacity:1; margin-top: 25px;}</style>';
var piano = d.querySelector('#p'),
hzMap = [],
noteMap = ['c','c_','d','d_','e','f','f_','g','g_','a','a_','b'],
keyMap = ['q','2','w','3','e','r','5','t','6','y','7','u','z','s','x','d','c','v','g','b','h','n','j','m'],
t = 0,
activeTones = [],
ac = new (window.AudioContext || window.webkitAudioContext)(),
o = ac.createOscillator(),
gn = ac.createGain();
o.connect(gn); o.type = 'square'; o.start(0);
gn.connect(ac.destination); gn.gain.value = 0;
function playNote(f) {
clearTimeout(t);
o.frequency.value = f;
gn.gain.value = .5;
}
function stopeNote() {
gn.gain.value -= .05;
if(gn.gain.value > 0 && activeTones.length === 0)
t = setTimeout(stopeNote, 50);
}
function onKey(event) {
var keyPressed = String.fromCharCode(event.keyCode).toLowerCase(),
index = keyMap.indexOf(keyPressed),
tone = noteMap[index],
isDown = event.type === 'keydown';
if (!tone) return;
if (activeTones.indexOf(tone) >= 0 && isDown) return;
d.querySelector('.' + tone).setAttribute('s', !!isDown);
if (isDown) {
activeTones.push(tone);
playNote(hzMap[index]);
}
else {
activeTones.splice(activeTones.indexOf(tone),1);
if (activeTones.length === 0) stopeNote();
else playNote(hzMap[noteMap.indexOf(activeTones[activeTones.length-1])]);
}
}
noteMap = noteMap.concat(noteMap.map(function(note){return '_' + note;}));
piano.innerHTML = noteMap.map(function(note, index) {
return '<p class="' + note + '">' + keyMap[index] + '</p>';
}).join('');
while(t++ < 24) {
hzMap.push(440*Math.pow(2,(t-33)/12));
}
d.addEventListener('keydown', onKey);
d.addEventListener('keyup', onKey);
</script>
<script id="jsbin-source-javascript" type="text/javascript">// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==
var d=document;d.body.innerHTML += '<p id="p"></p><style>#p{display:flex;flex-direction:row}#p>*{align-items:flex-end;justify-content:center;display:flex;padding-bottom:5px;width:50px;height:100px;color:#000;background:#fea;border-right:1px solid #fff;opacity:.8}#p>[class$="_"]{color:#fff;background:#000;position:absolute;height:50px;margin-left:-25px;z-index:1}[s*="t"]{opacity:1; margin-top: 25px;}</style>';
var piano = d.querySelector('#p'),
hzMap = [],
noteMap = ['c','c_','d','d_','e','f','f_','g','g_','a','a_','b'],
keyMap = ['q','2','w','3','e','r','5','t','6','y','7','u','z','s','x','d','c','v','g','b','h','n','j','m'],
t = 0,
activeTones = [],
ac = new (window.AudioContext || window.webkitAudioContext)(),
o = ac.createOscillator(),
gn = ac.createGain();
o.connect(gn); o.type = 'square'; o.start(0);
gn.connect(ac.destination); gn.gain.value = 0;
function playNote(f) {
clearTimeout(t);
o.frequency.value = f;
gn.gain.value = .5;
}
function stopeNote() {
gn.gain.value -= .05;
if(gn.gain.value > 0 && activeTones.length === 0)
t = setTimeout(stopeNote, 50);
}
function onKey(event) {
var keyPressed = String.fromCharCode(event.keyCode).toLowerCase(),
index = keyMap.indexOf(keyPressed),
tone = noteMap[index],
isDown = event.type === 'keydown';
if (!tone) return;
if (activeTones.indexOf(tone) >= 0 && isDown) return;
d.querySelector('.' + tone).setAttribute('s', !!isDown);
if (isDown) {
activeTones.push(tone);
playNote(hzMap[index]);
}
else {
activeTones.splice(activeTones.indexOf(tone),1);
if (activeTones.length === 0) stopeNote();
else playNote(hzMap[noteMap.indexOf(activeTones[activeTones.length-1])]);
}
}
noteMap = noteMap.concat(noteMap.map(function(note){return '_' + note;}));
piano.innerHTML = noteMap.map(function(note, index) {
return '<p class="' + note + '">' + keyMap[index] + '</p>';
}).join('');
while(t++ < 24) {
hzMap.push(440*Math.pow(2,(t-33)/12));
}
d.addEventListener('keydown', onKey);
d.addEventListener('keyup', onKey);</script></body>
</html>
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==
var d=document;d.body.innerHTML += '<p id="p"></p><style>#p{display:flex;flex-direction:row}#p>*{align-items:flex-end;justify-content:center;display:flex;padding-bottom:5px;width:50px;height:100px;color:#000;background:#fea;border-right:1px solid #fff;opacity:.8}#p>[class$="_"]{color:#fff;background:#000;position:absolute;height:50px;margin-left:-25px;z-index:1}[s*="t"]{opacity:1; margin-top: 25px;}</style>';
var piano = d.querySelector('#p'),
hzMap = [],
noteMap = ['c','c_','d','d_','e','f','f_','g','g_','a','a_','b'],
keyMap = ['q','2','w','3','e','r','5','t','6','y','7','u','z','s','x','d','c','v','g','b','h','n','j','m'],
t = 0,
activeTones = [],
ac = new (window.AudioContext || window.webkitAudioContext)(),
o = ac.createOscillator(),
gn = ac.createGain();
o.connect(gn); o.type = 'square'; o.start(0);
gn.connect(ac.destination); gn.gain.value = 0;
function playNote(f) {
clearTimeout(t);
o.frequency.value = f;
gn.gain.value = .5;
}
function stopeNote() {
gn.gain.value -= .05;
if(gn.gain.value > 0 && activeTones.length === 0)
t = setTimeout(stopeNote, 50);
}
function onKey(event) {
var keyPressed = String.fromCharCode(event.keyCode).toLowerCase(),
index = keyMap.indexOf(keyPressed),
tone = noteMap[index],
isDown = event.type === 'keydown';
if (!tone) return;
if (activeTones.indexOf(tone) >= 0 && isDown) return;
d.querySelector('.' + tone).setAttribute('s', !!isDown);
if (isDown) {
activeTones.push(tone);
playNote(hzMap[index]);
}
else {
activeTones.splice(activeTones.indexOf(tone),1);
if (activeTones.length === 0) stopeNote();
else playNote(hzMap[noteMap.indexOf(activeTones[activeTones.length-1])]);
}
}
noteMap = noteMap.concat(noteMap.map(function(note){return '_' + note;}));
piano.innerHTML = noteMap.map(function(note, index) {
return '<p class="' + note + '">' + keyMap[index] + '</p>';
}).join('');
while(t++ < 24) {
hzMap.push(440*Math.pow(2,(t-33)/12));
}
d.addEventListener('keydown', onKey);
d.addEventListener('keyup', onKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment