add a comment about stardate rules
authorRobert Kaiser <robert@widebook.box.kairo.at>
Sat, 11 Oct 2014 21:04:05 +0000 (23:04 +0200)
committerRobert Kaiser <robert@widebook.box.kairo.at>
Sat, 11 Oct 2014 21:04:05 +0000 (23:04 +0200)
TODO
index.html
js/tricorder.js
manifest.webapp
package/make-package.sh
package/manifest.package
style/tricorder.css

diff --git a/TODO b/TODO
index 464d6c413182c843d3b15591e45de3701b7b3290..3c384fb00ddd360034000dabc1e9d1fec809e52b 100644 (file)
--- a/TODO
+++ b/TODO
@@ -7,12 +7,10 @@ Tasks to complete for Web Tricorder:
 *** WiFi Networks: http://groups.google.com/group/mozilla.dev.webapi/browse_thread/thread/ed980c42261c5f4a
 *** Nearby Bluetooth devices: https://wiki.mozilla.org/WebAPI/WebBluetooth
 ** Device properties:
-*** Battery Status: http://dvcs.w3.org/hg/dap/raw-file/tip/battery/Overview.html
 *** Storage: https://github.com/mozilla-b2g/gaia/blob/master/apps/settings/js/app_storage.js
 ** Environment:
 *** Ambient Light: http://www.w3.org/TR/ambient-light/
 *** Proximity: http://www.w3.org/TR/2012/WD-proximity-20120712/ - https://hacks.mozilla.org/2013/06/the-proximity-api/
-** Audio / Video analysis
+** Video analysis
 *** histograms, etc.
-*** via WebRTC: https://wiki.mozilla.org/Platform/Features/VideoConferencing
 * Make output more beautiful
index b426b25f6f25bbf3e26c3266c98557db8e1b609d..265df47fccaa315ab69c994c9c409f79e5bb0781 100644 (file)
@@ -34,6 +34,7 @@
 <ul id="navlist">
 <li id="navPos">Position</li>
 <li id="navGrav">Gravity</li>
+<li id="navSound">Sound</li>
 <li id="navDev">Device</li>
 <li id="navOther">Other</li>
 </ul>
@@ -99,6 +100,17 @@ a better device.
 </div>
 </section>
 
+<section id="sectSound">
+<p id="soundunavail">
+Sound sensors are unavailable on this tricorder device.
+<br/>Please talk to your superior officer or Starfleet contact to acquire
+a better device.
+</p>
+<div id="soundavail" style="display:none">
+<canvas id="soundcanvas" width="300" height="300"></canvas>
+</div>
+</section>
+
 <section id="sectDev">
 <table>
 <tr><td>Battery Level:</td><td id="devBattLevel" class="deviceVal">...</td></tr>
index 2574b01f6fb7e334618e130079fa19b991ddb7bc..dbd188d93bb5ae05ff326113bb7ab5e70f4a9c3d 100644 (file)
@@ -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,6 +33,12 @@ 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.
@@ -41,11 +49,10 @@ function updateStardate() {
 
   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)
-    // Star Trek premiered on Thursday, September 8, 1966, at 7:30PM or 8:30PM
-    // depending on time zone in the United States.
-    // See http://www.startrek.com/article/star-trek-and-newspapers-part-2
-    gSDBase = new Date("September 8, 1966 19:30:00 EST");
+    gSDBase = new Date("September 8, 1966 20:30:00 EST");
 
   var sdateval = (curDate - gSDBase) / (86400 * 365.2425);
   gStardate.textContent = sdateval.toFixed(1);
@@ -221,6 +228,86 @@ 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 = {
   activate: function() {
@@ -259,15 +346,6 @@ var gModDev = {
   batteryTimer: null,
 }
 
-var gModNull = {
-  activate: function() {
-    //gSounds.scan.play();
-  },
-  deactivate: function() {
-    gSounds.scan.pause();
-  },
-}
-
 var gModOther = {
   activate: function() {
     //gSounds.scan.play();
index e74b2580eaac2e0ef5c1b5eca112d02452e99561..29c1c1825b4bf3ab360cbe309ef86becab2912ec 100644 (file)
@@ -1,10 +1,14 @@
 {
   "name": "Tricorder",
   "description": "Display data from your device sensors in Star Trek style.",
+  "version": "1.DVER",
   "launch_path": "/index.html",
   "permissions": {
     "geolocation": {
       "description": "Determine the global position to display in the tricorder."
+    },
+    "audio-capture": {
+      "description": "Capture microphone audio for the sound module."
     }
   },
   "developer": {
index e5e813954076bed2f6d26dd2433d418d11b8d6b7..03405d6ad22fde4bd208b5b3a034530ea7437f43 100755 (executable)
@@ -6,8 +6,13 @@ cd ..
 origdir=`pwd`
 
 rm -v $pkgdir/*
-zip -r -D -0 $pkgdir/package.zip * -x package/{,.}*
+zip -r -D -0 $pkgdir/package.zip * -x package/{,.}* *.appcache
 dver=`date +%y.%j.%H`
 pkgsize=`wc -c $pkgdir/package.zip | awk '{ print $1 }'`
 sed -e "s/DVER/$dver/" -e "s/PKGSIZE/$pkgsize/" package/manifest.package > $pkgdir/manifest.package
+sed -e "s/DVER/$dver/" -e "s/PKGSIZE/$pkgsize/" manifest.webapp > $pkgdir/manifest.webapp
+cd $pkgdir
+zip -f -0 package.zip manifest.webapp
+rm -v manifest.webapp
+cd $origdir
 cp package/.htaccess $pkgdir/
index ea99a6cf68cda1c8d207c6f8c8a21ee23590ded3..9c0a7803b5470ab017eee2f3c3f541d4c1e8fb65 100644 (file)
@@ -1,7 +1,7 @@
 {
   "name": "Tricorder",
   "package_path": "http://tricorder.kairo.at/package.zip",
-  "version": "0.DVER",
+  "version": "1.DVER",
   "size": "PKGSIZE",
   "developer": {
     "name": "Robert Kaiser",
index 0c730618552b2f9de2b45b3f5c60fcc6e169c851..99696f6f34a9b88d88942ac322832086c5cc9bfd 100644 (file)
@@ -141,6 +141,14 @@ a:hover, a:active { color: #FFCF00; }
   background-color: #C09070;
   color: #000000;
 }
+#navSound {
+  background-color: #E7ADE7;
+  color: #000000;
+}
+#navDev {
+  background-color: #A09090;
+  color: #000000;
+}
 #navOther {
   background-color: #6000CF;
   color: #000000;
@@ -222,12 +230,18 @@ a:hover, a:active { color: #FFCF00; }
 section {
   display: none;
   background-color: rgba(0,0,0,0.8);
+  height: 100%;
 }
 
 section.active {
   display: block;
 }
 
+section > p:first-child {
+  margin-top: 0;
+  padding-top: 1em;
+}
+
 .posVal, .gravVal {
   text-align: right;
 }
@@ -244,6 +258,10 @@ section.active {
   font-size: .667em;
 }
 
+#soundavail {
+  height: 100%;
+}
+
 @media screen and (min-width: 500px) {
   body {
     font-size: 24px;