Skip to content

Instantly share code, notes, and snippets.

@yo-to
Last active January 1, 2016 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yo-to/8160394 to your computer and use it in GitHub Desktop.
Save yo-to/8160394 to your computer and use it in GitHub Desktop.
Mac用 Firefox 26、Chrome 31 で音を鳴らす (メロディを奏でる〜修正版)
<!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