Last active
January 1, 2016 14:49
-
-
Save yo-to/8160394 to your computer and use it in GitHub Desktop.
Mac用 Firefox 26、Chrome 31 で音を鳴らす (メロディを奏でる〜修正版)
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
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Web Audio API - test</title> | |
<script> | |
window.AudioContext = window.AudioContext || | |
window.webkitAudioContext || | |
window.mozAudioContext; | |
var ctx = new AudioContext(); | |
var vol = ctx.createGain(); | |
vol.gain.value = 0.2; // 0.0 - 1.0 | |
vol.connect(ctx.destination); | |
var osc_freq = new Array(0, 262, 294, 330, 349, 392, 440); | |
var melody = new Array( | |
1, 2, 3, 0, 1, 2, 3, 0, 5, 3, 2, 1, 2, 3, 2, 0, | |
1, 2, 3, 0, 1, 2, 3, 0, 5, 3, 2, 1, 2, 3, 1, 0, | |
5, 5, 3, 5, 6, 6, 5, 0, 3, 3, 2, 2, 1 | |
); | |
var play_length = 0.9; | |
function playSE(i) { | |
var osc = ctx.createOscillator(); | |
osc.type = (typeof osc.type === 'string') ? 'square' : 1; | |
osc.frequency.value = osc_freq[melody[i]]; | |
osc.connect(vol); | |
osc.start(i); | |
osc.stop(i + play_length); | |
} | |
var num_notes = 4; | |
function playMulti(n) { | |
for(var i=n*num_notes; i<(n+1)*num_notes; i++) { | |
if(i<melody.length) { | |
playSE(i); | |
} | |
} | |
} | |
var count = 0; | |
function timer() { | |
var time = ctx.currentTime; | |
if(time > count * num_notes - 1) { | |
if(count <= melody.length/num_notes + 1) { | |
playMulti(count); | |
} | |
count++; | |
} | |
} | |
setInterval("timer()", 200); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment