Created
March 9, 2015 05:36
-
-
Save chikoski/850fcbaadf907609571f to your computer and use it in GitHub Desktop.
光の強さに応じて音階が変わるアプリ
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
const PITCH = [ | |
220 * Math.pow( 1.06, 3 ), // ド | |
220 * Math.pow( 1.06, 4 ), // ド# | |
220 * Math.pow( 1.06, 5 ), // レ | |
220 * Math.pow( 1.06, 6 ), // レ# | |
220 * Math.pow( 1.06, 7 ), // ミ | |
220 * Math.pow( 1.06, 8 ), // ファ | |
220 * Math.pow( 1.06, 9 ), // ファ# | |
220 * Math.pow( 1.06,10 ), // ソ | |
220 * Math.pow( 1.06,11 ), // ソ# | |
440, // ラ | |
440 * Math.pow( 1.06, 1 ), // ラ# | |
440 * Math.pow( 1.06, 2 ) // シ | |
]; | |
const DEFAULT_PITCH = PITCH[0]; | |
var indicator; | |
var audioContext; | |
var oscillator; | |
var displayBrightness = function(element, value){ | |
element.textContent = value; | |
}; | |
var changePitch = function(value){ | |
oscillator.frequency.value = value; | |
}; | |
var lux2Pitch = function(lux){ | |
var index = Math.floor(Math.max(100, Math.min(1100, lux)) / 100); | |
return PITCH[index]; | |
}; | |
var handleDeviceLightEvent = function(event){ | |
var lux = event.value; | |
if(indicator != null){ | |
displayBrightness(indicator, lux); | |
} | |
changePitch(lux2Pitch(lux)); | |
}; | |
var initApp = function(){ | |
indicator = document.querySelector("h1"); | |
audioContext = new AudioContext(); | |
oscillator = audioContext.createOscillator(); | |
oscillator.type = "sawtooth"; | |
oscillator.frequency.value = DEFAULT_PITCH; | |
oscillator.connect(audioContext.destination); | |
oscillator.start(); | |
}; | |
window.addEventListener("load", initApp); | |
window.addEventListener("devicelight", handleDeviceLightEvent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment