X-Git-Url: https://git-public.kairo.at/?p=tricorder.git;a=blobdiff_plain;f=js%2Ftricorder.js;h=11ec7062bb0e2d4174aef8cf3562089ac3f9d1be;hp=f88ebb120e470277fc7818a1d35a095e2ff76a5e;hb=b41c095bad1cb176864b6d8e25bcff50faf4634e;hpb=84436ccb5cf636e01658943c44b6af5d063750c4 diff --git a/js/tricorder.js b/js/tricorder.js index f88ebb1..11ec706 100644 --- a/js/tricorder.js +++ b/js/tricorder.js @@ -5,6 +5,8 @@ var gStardate, gSDBase; var gSounds = {}; +navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); + window.onload = function() { setTimeout(updateStardate, 0); gSounds.scan = new Audio("sound/scan.opus"); @@ -31,14 +33,26 @@ window.onload = function() { }, false); } +window.onresize = function() { + if (document.getElementById("sectSound").classList.contains("active")) { + gModSound.resize(); + } +} + function updateStardate() { + // Stardate rules foggy at best. See http://en.wikipedia.org/wiki/Stardate + // and the Memory Alpha article linked there for more details. + // We roughly lean on TNG scale by splitting an Earth year into exactly 1000 + // units, but we put the 0 point at the TV premiere of The Original Series. if (!gStardate) gStardate = document.getElementById("stardate"); var curDate = new Date(); + // Star Trek famously premiered on Thursday, September 8, 1966, at 8:30 p.m. + // See http://www.startrek.com/article/what-if-the-original-star-trek-had-debuted-on-friday-nights if (!gSDBase) - gSDBase = new Date("September 8, 1966 20:00:00 EST"); + gSDBase = new Date("September 8, 1966 20:30:00 EST"); var sdateval = (curDate - gSDBase) / (86400 * 365.2425); gStardate.textContent = sdateval.toFixed(1); @@ -214,33 +228,195 @@ var gModGrav = { }, } +var gModSound = { + activate: function() { + //gSounds.scan.play(); + if (navigator.getUserMedia && (window.AudioContext || window.webkitAudioContext)) { + document.getElementById("soundunavail").style.display = "none"; + document.getElementById("soundavail").style.display = "block"; + navigator.getUserMedia({ audio: true }, + function(aLocalMediaStream) { + gModSound.mAudio.stream = aLocalMediaStream; + gModSound.mAudio.context = new (window.AudioContext || window.webkitAudioContext)(); + gModSound.mAudio.input = gModSound.mAudio.context.createMediaStreamSource(gModSound.mAudio.stream); + // Could put a filter in between like in http://webaudioapi.com/samples/microphone/ + gModSound.mDisplay.canvas = document.getElementById("soundcanvas"); + gModSound.mDisplay.context = gModSound.mDisplay.canvas.getContext("2d"); + gModSound.rebuildCanvas(); + }, + function(err) { + document.getElementById("soundunavail").style.display = "block"; + document.getElementById("soundavail").style.display = "none"; + console.log(err); + } + ); + } + else { + document.getElementById("soundunavail").style.display = "block"; + document.getElementById("soundavail").style.display = "none"; + } + }, + rebuildCanvas: function() { + if (gModSound.mDisplay.AFRequestID) { + window.cancelAnimationFrame(gModSound.mDisplay.AFRequestID); + gModSound.mDisplay.AFRequestID = 0; + } + gModSound.mDisplay.canvas.height = document.getElementById("soundavail").clientHeight - 4; + gModSound.mDisplay.canvas.width = document.getElementById("soundavail").clientWidth; + gModSound.mAudio.frequencySlices = (gModSound.mDisplay.canvas.width > 512) ? + 512 : + Math.pow(2, Math.floor(Math.log(gModSound.mDisplay.canvas.width) / Math.LN2)); + //console.log("slices: " + gModSound.mAudio.frequencySlices); + gModSound.mAudio.analyzer = gModSound.mAudio.context.createAnalyser(); + // Make the FFT four times as large as needed as the upper three quarters turn out to be useless. + gModSound.mAudio.analyzer.fftSize = gModSound.mAudio.frequencySlices * 4; + //console.log("FFT: " + gModSound.mAudio.analyzer.fftSize); + gModSound.mAudio.input.connect(gModSound.mAudio.analyzer); + gModSound.mDisplay.context.setTransform(1, 0, 0, -(gModSound.mDisplay.canvas.height/256), 0, gModSound.mDisplay.canvas.height); + gModSound.mDisplay.active = true; + gModSound.mDisplay.AFRequestID = window.requestAnimationFrame(gModSound.paintAnalyzerFrame); + }, + mAudio: { + frequencySlices: 32, // Must be a multiple of 2 (see AnalyserNode.fftSize) + }, + mDisplay: { + active: false, + }, + paintAnalyzerFrame: function(aTimestamp) { + var data = new Uint8Array(gModSound.mAudio.frequencySlices); + gModSound.mAudio.analyzer.getByteFrequencyData(data); + gModSound.mDisplay.context.clearRect(0, 0, gModSound.mDisplay.canvas.width, gModSound.mDisplay.canvas.height); + // Out of experience, only the first half of the slices are actually useful. + var wid = gModSound.mDisplay.canvas.width / gModSound.mAudio.frequencySlices; + var fill = "#9C9CFF"; + for (var i = 0; i < gModSound.mAudio.frequencySlices; ++i) { + var newFill = (data[i] > 200) ? "#FF0000" : ((data[i] > 100) ? "#FFCF00" : "#9C9CFF"); + if (fill != newFill) { gModSound.mDisplay.context.fillStyle = newFill; fill = newFill; } + gModSound.mDisplay.context.fillRect(i*wid, 0, wid, data[i]); + } + if (gModSound.mDisplay.active) + gModSound.mDisplay.AFRequestID = window.requestAnimationFrame(gModSound.paintAnalyzerFrame); + }, + resize: function() { + gModSound.rebuildCanvas(); + }, + deactivate: function() { + if (gModSound.mDisplay.active) { + gModSound.mAudio.stream.stop(); + gModSound.mDisplay.active = false; + } + gSounds.scan.pause(); + }, +} -var gModDev = { +var gModEnv = { activate: function() { gSounds.scan.play(); - this.batteryTimer = - setInterval(function () { gModDev.updateBattery(); }, 100); + document.getElementById("envunavail").style.display = "none"; + document.getElementById("envavail").style.display = "block"; + window.addEventListener("devicelight", this.lightEvent, true); + window.addEventListener("deviceproximity", this.proxEvent, true); + setTimeout(function() { + if ((document.getElementById("envLight").textContent == "...") && + (document.getElementById("envDistance").textContent == "...")) { + gModEnv.deactivate(); + } + }, 5000); + try { + var cameras = navigator.mozCameras.getListOfCameras(); + for (i = 0; i < cameras.length; i++) { + var promise = navigator.mozCameras.getCamera(cameras[i], {}, + function(aCamera) { + if (aCamera.capabilities.flashModes.indexOf('torch') !== -1) { + gModEnv.foundFlashCamera(aCamera); + } + }, + function(aError) { console.log("camera error: " + aError); } + ); + } + } catch (e) { + // camera api not supported + document.getElementById("envFlashUnavail").style.display = "block"; + document.getElementById("envFlashAvail").style.display = "none"; + } + }, + foundFlashCamera: function(aCamera) { + this.flashCamera = aCamera; + document.getElementById("envFlashOn").onclick = function() { console.log("on"); gModEnv.switchFlashlight(true); }; + document.getElementById("envFlashOff").onclick = function() { console.log("off"); gModEnv.switchFlashlight(false); }; + document.getElementById("envFlashUnavail").style.display = "none"; + document.getElementById("envFlashAvail").style.display = "block"; }, deactivate: function() { - clearTimeout(this.batteryTimer); gSounds.scan.pause(); + window.removeEventListener("devicelight", this.lightEvent, true); + window.removeEventListener("deviceproximity", this.proxEvent, true); + document.getElementById("envunavail").style.display = "block"; + document.getElementById("envavail").style.display = "none"; + document.getElementById("envLight").textContent = "..."; + document.getElementById("envDistance").textContent = "..."; }, - updateBattery: function() { - document.getElementById("devBattLevel").textContent = (navigator.battery.level * 100).toFixed(1) + "%"; - document.getElementById("devBattStatus").textContent = - navigator.battery.charging ? "charging, " + navigator.battery.chargingTime + "s remaining" - : navigator.battery.dischargingTime + "s usage remaining"; + lightEvent: function(lightData) { + // See http://www.w3.org/TR/ambient-light/ + document.getElementById("envLight").textContent = lightData.value + " lux"; }, - batteryTimer: null, + proxEvent: function(proxData) { + // See http://www.w3.org/TR/2012/WD-proximity-20120712/ + if (proxData.value >= proxData.max) { + document.getElementById("envDistance").textContent = "(maximum, >= " + proxData.value + " cm)"; + } + else if (proxData.value <= proxData.min) { + document.getElementById("envDistance").textContent = "(minimum, <= " + proxData.value + " cm)"; + } + else { + document.getElementById("envDistance").textContent = proxData.value + " cm"; + } + }, + flashCamera: null, + switchFlashlight: function(aEnabled) { + if (this.flashCamera) { + this.flashCamera.flashMode = aEnabled ? 'torch' : 'off'; + document.getElementById("envFlashOn").disabled = aEnabled; + document.getElementById("envFlashOff").disabled = !aEnabled; + } + } } -var gModNull = { +var gModDev = { activate: function() { - //gSounds.scan.play(); + gSounds.scan.play(); + this.batteryTimer = + setInterval(function () { gModDev.updateBattery(); }, 100); }, deactivate: function() { + clearTimeout(this.batteryTimer); gSounds.scan.pause(); }, + updateBattery: function() { + document.getElementById("devBattLevel").textContent = + (navigator.battery.level * 100).toFixed(1) + "%"; + if (navigator.battery.charging) { + if (navigator.battery.chargingTime == 0 || + navigator.battery.chargingTime == Infinity) { + document.getElementById("devBattStatus").textContent = "charging"; + } + else { + document.getElementById("devBattStatus").textContent = + "charging, " + navigator.battery.chargingTime + "s remaining"; + } + } + else { + if (navigator.battery.dischargingTime == 0 || + navigator.battery.dischargingTime == Infinity) { + document.getElementById("devBattStatus").textContent = "discharging"; + } + else { + document.getElementById("devBattStatus").textContent = + navigator.battery.dischargingTime + "s usage remaining"; + } + } + }, + batteryTimer: null, } var gModOther = {