Skip to content

Instantly share code, notes, and snippets.

@mikaelbr
Created September 15, 2013 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mikaelbr/6569804 to your computer and use it in GitHub Desktop.
Save mikaelbr/6569804 to your computer and use it in GitHub Desktop.
A simple piano-key to sound script based on michaelmp/js-piano. Index.html shows how the actual sounds are included.
<!DOCTYPE html>
<html>
<head>
<title>FRP Piano</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<audio id="sound-A2" src="samples/A2.ogg" preload="auto"></audio>
<audio id="sound-B2" src="samples/B2.ogg" preload="auto"></audio>
<audio id="sound-A3" src="samples/A3.ogg" preload="auto"></audio>
<audio id="sound-B3" src="samples/B3.ogg" preload="auto"></audio>
<audio id="sound-C3" src="samples/C3.ogg" preload="auto"></audio>
<audio id="sound-D3" src="samples/D3.ogg" preload="auto"></audio>
<audio id="sound-E3" src="samples/E3.ogg" preload="auto"></audio>
<audio id="sound-F3" src="samples/F3.ogg" preload="auto"></audio>
<audio id="sound-G3" src="samples/G3.ogg" preload="auto"></audio>
<audio id="sound-A4" src="samples/A4.ogg" preload="auto"></audio>
<audio id="sound-B4" src="samples/B4.ogg" preload="auto"></audio>
<audio id="sound-C4" src="samples/C4.ogg" preload="auto"></audio>
<audio id="sound-D4" src="samples/D4.ogg" preload="auto"></audio>
<audio id="sound-E4" src="samples/E4.ogg" preload="auto"></audio>
<audio id="sound-F4" src="samples/F4.ogg" preload="auto"></audio>
<audio id="sound-G4" src="samples/G4.ogg" preload="auto"></audio>
<audio id="sound-C5" src="samples/C5.ogg" preload="auto"></audio>
</body>
</html>
# Based on js-piano (https://github.com/michaelmp/js-piano) by:
# Copyright 2012 Michael Morris-Pearce
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
keys = [
'A2', 'Bb2', 'B2', 'C3', 'Db3', 'D3', 'Eb3', 'E3', 'F3', 'Gb3', 'G3', 'Ab3',
'A3', 'Bb3', 'B3', 'C4', 'Db4', 'D4', 'Eb4', 'E4', 'F4', 'Gb4', 'G4', 'Ab4',
'A4', 'Bb4', 'B4', 'C5'
]
intervals = {}
depressed = {}
sound = (id) ->
document.getElementById('sound-' + id)
fade = (key) ->
audio = sound key
stepfade = ->
if audio
if audio.volume > 0.2
audio.volume = audio.volume * 0.95
else
audio.volume = audio.volume - 0.01
->
clearInterval(intervals[key])
intervals[key] = setInterval stepfade, 5
window.player = (key) ->
audio = sound key
clearTimeout depressed[key]
if audio
audio.pause()
audio.volume = 1.0
if audio.readyState >= 2
audio.currentTime = 0
audio.play()
fade key
pause = ->
audio.pause()
audio.currentTime = 0
depressed[key] = setTimeout pause, 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment