Last active
August 29, 2015 14:00
-
-
Save Osmose/2664ed7e919e91a29909 to your computer and use it in GitHub Desktop.
HTML5 Piano + Recording Mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://bits.potch.me/piano.html | |
// Copy and paste into console, start playing via keyboard. | |
// Then, press Z to play back your music! | |
// Playback deletes recorded music. | |
var log = []; | |
var sinceLast = null; | |
document.body.addEventListener('keydown', function(e) { | |
if (e.keyCode == 90) { | |
playBack(); | |
} else { | |
var key = getKey(e); | |
var i = keyboard.indexOf(key); | |
if (i > -1) { | |
var now = (new Date()).getTime(); | |
if (sinceLast !== null) { | |
var duration = now - sinceLast; | |
log.push(duration) | |
} | |
sinceLast = now; | |
log.push(i + 40); | |
} | |
} | |
}); | |
function playBack() { | |
if (log.length > 0) { | |
var note = log.shift(); | |
var duration = log.shift(); | |
playNote(note, 0, .5); | |
if (duration !== undefined) { | |
setTimeout(playBack, duration); | |
} else { | |
sinceLast = null; | |
} | |
} | |
} | |
// Type log.toSource() to get your recorded work if you haven't played it back yet. | |
// Sample tune: Paste this in after and hit Z! | |
log = [45, 434, 49, 440, 51, 751, 45, 461, 49, 482, 51, 762, 45, 451, 49, 545, 51, 476, 56, 466, 54, 706, 51, 449, 52, 456, 51, 442, 47, 448, 44, 1293, 42, 456, 44, 414, 47, 439, 44]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment