Last active
August 29, 2015 14:24
-
-
Save takanakahiko/07ca6c71db082bd05921 to your computer and use it in GitHub Desktop.
FirefoxOSでモスキート音を鳴らせた
This file contains hidden or 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
window.addEventListener("load", function() { | |
var audio = { | |
context: null, | |
oscillator: null, | |
gainNode: null | |
} | |
function init(){ | |
audio.context = new AudioContext(); | |
audio.oscillator = audio.context.createOscillator(); | |
audio.gainNode = audio.context.createGain(); | |
audio.oscillator.connect(audio.gainNode); | |
audio.gainNode.connect(audio.context.destination); | |
audio.oscillator.type = 'square'; // 矩形波 | |
audio.oscillator.start(0); | |
} | |
init(); | |
function setScale(num){ | |
var onkai = [ | |
0, | |
261.6255653005986, | |
293.6647679174076, | |
329.6275569128699, | |
349.2282314330039, | |
391.99543598174927, | |
440, | |
493.8833012561241, | |
523.2511306011972, | |
]; | |
audio.oscillator.frequency.value = ( onkai[num]*2 ); | |
} | |
function play(array1,array2){ | |
var counter1 = 0; | |
var counter2 = 0; | |
var timerID = 0; | |
var StartTimer, StopTimer, Timer; | |
StartTimer = function() { | |
timerID = setInterval(Timer, 200); | |
}; | |
StopTimer = function() { | |
clearInterval(timerID); | |
}; | |
Timer = function() { | |
setScale(array1[counter1]); | |
counter2++; | |
if(counter2 == array2[counter1]){ | |
counter1++; | |
counter2=0; | |
if(counter1 == array1.length) StopTimer(); | |
} | |
}; | |
StartTimer(); | |
} | |
//////////////////////////////////////////////// | |
play(array1,array2); | |
var array1 = [1,2,3,2,1,0,1,2,3,2,1,2,0]; | |
var array2 = [1,1,2,1,1,1,1,1,1,1,1,3,0]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment