implement a sound module with a graphical display
[tricorder.git] / js / tricorder.js
CommitLineData
feec2d1c
RK
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5var gStardate, gSDBase;
14a57547 6var gSounds = {};
feec2d1c 7
39b2fdef
RK
8navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
9
feec2d1c
RK
10window.onload = function() {
11 setTimeout(updateStardate, 0);
14a57547
RK
12 gSounds.scan = new Audio("sound/scan.opus");
13 gSounds.scan.loop = true;
14 gSounds.launch = new Audio("sound/launch.opus");
15 gSounds.shutdown = new Audio("sound/shutdown.opus");
16 gSounds.keyaction = new Audio("sound/key-action.opus");
17 gSounds.keypress = new Audio("sound/key-press.opus");
18
84436ccb
RK
19 document.getElementById("fullScreenButton").addEventListener("click",
20 function(aEvent) { toggleFullscreen(); }, false);
21
22 var navItems = document.getElementById("navlist").children;
23 for (var i = 0; i <= navItems.length - 1; i++) {
24 navItems[i].addEventListener("click",
25 function(aEvent) {
26 switchModule(aEvent.target.id.replace("nav", ""));
27 }, false);
28 }
29
14a57547
RK
30 gSounds.launch.play();
31 window.addEventListener("beforeunload", function( event ) {
32 gSounds.shutdown.play();
33 }, false);
feec2d1c
RK
34}
35
36function updateStardate() {
37 if (!gStardate)
38 gStardate = document.getElementById("stardate");
39
40 var curDate = new Date();
41
d9387df2
RK
42 // Star Trek famously premiered on Thursday, September 8, 1966, at 8:30 p.m.
43 // See http://www.startrek.com/article/what-if-the-original-star-trek-had-debuted-on-friday-nights
feec2d1c 44 if (!gSDBase)
d9387df2 45 gSDBase = new Date("September 8, 1966 20:30:00 EST");
feec2d1c
RK
46
47 var sdateval = (curDate - gSDBase) / (86400 * 365.2425);
48 gStardate.textContent = sdateval.toFixed(1);
49
50 setTimeout(updateStardate, 5*60*1000);
eabed1a4
RK
51}
52
14a57547
RK
53function toggleFullscreen() {
54 gSounds.keyaction.play();
55 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
56 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
57 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
58 if (document.cancelFullScreen) {
59 document.cancelFullScreen();
60 } else if (document.mozCancelFullScreen) {
61 document.mozCancelFullScreen();
62 } else if (document.webkitCancelFullScreen) {
63 document.webkitCancelFullScreen();
64 }
65 }
66 else {
67 var elem = document.getElementById("body");
68 if (elem.requestFullScreen) {
69 elem.requestFullScreen();
70 } else if (elem.mozRequestFullScreen) {
71 elem.mozRequestFullScreen();
72 } else if (elem.webkitRequestFullScreen) {
73 elem.webkitRequestFullScreen();
74 }
75 }
76}
77
84436ccb 78function switchModule(aModname) {
14a57547 79 gSounds.keyaction.play();
eabed1a4
RK
80 var sections = document.getElementsByTagName('section');
81 for (var i = 0; i <= sections.length - 1; i++) {
1753d94a
RK
82 if (sections[i].classList.contains("active")) {
83 window["gMod" + sections[i].id.replace("sect", "")].deactivate();
84 sections[i].classList.remove("active");
85 }
eabed1a4
RK
86 }
87 var navs = document.getElementById('navlist').children;
88 for (var i = 0; i <= navs.length - 1; i++) {
89 navs[i].classList.remove("active");
90 }
91
84436ccb 92 var navItem = document.getElementById("nav" + aModname);
1b0ec1c4 93 navItem.classList.add("active");
84436ccb 94 document.getElementById("sect" + aModname).classList.add("active");
1b0ec1c4 95 document.getElementById("mainHeader").textContent =
84436ccb 96 (aModname == "Other") ? "Web Tricorder" : navItem.textContent;
1753d94a 97
84436ccb 98 window["gMod" + aModname].activate();
1753d94a
RK
99}
100
101var gModPos = {
102 activate: function() {
103 if (navigator.geolocation) {
14a57547 104 gSounds.scan.play();
1753d94a
RK
105 document.getElementById("posunavail").style.display = "none";
106 document.getElementById("posavail").style.display = "block";
107 this.watchID = navigator.geolocation.watchPosition(
108 function(position) {
33a80b5e
RK
109 document.getElementById("posLat").textContent =
110 position.coords.latitude + "°";
111 document.getElementById("posLong").textContent =
112 position.coords.longitude + "°";
113 document.getElementById("posAlt").textContent =
114 position.coords.altitude.toFixed(0) + " m";
115 document.getElementById("posAcc").textContent =
116 position.coords.accuracy.toFixed(0) + " m";
117 document.getElementById("posAltAcc").textContent =
118 position.coords.altitudeAccuracy.toFixed(0) + " m";
119 document.getElementById("posHead").textContent =
120 position.coords.heading ? position.coords.heading.toFixed(0) + "°" : "---";
121 document.getElementById("posSpd").textContent =
122 position.coords.speed ? position.coords.speed.toFixed(1) + " m/s" : "---";
1753d94a
RK
123 var locTime = new Date(position.timestamp);
124 document.getElementById("posTime").textContent = locTime.toISOString();
125 },
126 function(error) {
127 // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors
730b3151
RK
128 if (error.message) {
129 document.getElementById("posLat").textContent = error.message;
130 document.getElementById("posLong").textContent = "...";
131 document.getElementById("posAlt").textContent = "...";
132 document.getElementById("posAcc").textContent = "...";
133 document.getElementById("posAltAcc").textContent = "...";
134 document.getElementById("posHead").textContent = "...";
135 document.getElementById("posSpd").textContent = "...";
136 document.getElementById("posTime").textContent = "...";
48fd8f1a 137 setTimeout(function() { gModPos.deactivate(); }, 5000);
730b3151
RK
138 }
139 else {
140 document.getElementById("posunavail").style.display = "block";
141 document.getElementById("posavail").style.display = "none";
142 }
e6b9b946 143 gSounds.scan.pause();
1753d94a 144 },
48fd8f1a 145 {enableHighAccuracy: true, maximumAge: 10000, timeout: 60000}
1753d94a
RK
146 );
147 }
148 else {
149 document.getElementById("posunavail").style.display = "block";
150 document.getElementById("posavail").style.display = "none";
151 }
152 },
153 deactivate: function() {
14a57547 154 gSounds.scan.pause();
1753d94a
RK
155 if (this.watchID) {
156 navigator.geolocation.clearWatch(this.watchID);
157 }
158 document.getElementById("posunavail").style.display = "block";
159 document.getElementById("posavail").style.display = "none";
160 document.getElementById("posLat").textContent = "...";
161 document.getElementById("posLong").textContent = "...";
162 document.getElementById("posAlt").textContent = "...";
163 document.getElementById("posAcc").textContent = "...";
164 document.getElementById("posAltAcc").textContent = "...";
165 document.getElementById("posHead").textContent = "...";
166 document.getElementById("posSpd").textContent = "...";
167 document.getElementById("posTime").textContent = "...";
168 },
169 watchID: null,
170}
171
172var gModGrav = {
173 activate: function() {
14a57547 174 gSounds.scan.play();
1753d94a
RK
175 document.getElementById("gravunavail").style.display = "none";
176 document.getElementById("gravavail").style.display = "block";
177 window.addEventListener("deviceorientation", this.orientEvent, true);
178 window.addEventListener("devicemotion", this.motionEvent, true);
730b3151
RK
179 setTimeout(function() {
180 if ((document.getElementById("gravAlpha").textContent == "...") &&
181 (document.getElementById("gravX").textContent == "...")) {
182 gModGrav.deactivate();
183 }
184 }, 3000);
1753d94a
RK
185 },
186 deactivate: function() {
14a57547 187 gSounds.scan.pause();
1753d94a
RK
188 window.removeEventListener("deviceorientation", this.orientEvent, true);
189 window.removeEventListener("devicemotion", this.motionEvent, true);
190 document.getElementById("gravunavail").style.display = "block";
191 document.getElementById("gravavail").style.display = "none";
730b3151
RK
192 //document.getElementById("gravAbs").textContent = "...";
193 document.getElementById("gravAlpha").textContent = "...";
194 document.getElementById("gravBeta").textContent = "...";
195 document.getElementById("gravGamma").textContent = "...";
33a80b5e 196 document.getElementById("gravTotal").textContent = "...";
730b3151
RK
197 document.getElementById("gravX").textContent = "...";
198 document.getElementById("gravY").textContent = "...";
199 document.getElementById("gravZ").textContent = "...";
200 //document.getElementById("gravRot").textContent = "...";
1753d94a
RK
201 },
202 orientEvent: function(orientData) {
b5c483f2
RK
203 //document.getElementById("gravAbs").textContent = orientData.absolute;
204 document.getElementById("gravAlpha").textContent = orientData.alpha.toFixed(1) + "°";
205 document.getElementById("gravBeta").textContent = orientData.beta.toFixed(1) + "°";
206 document.getElementById("gravGamma").textContent = orientData.gamma.toFixed(1) + "°";
1753d94a
RK
207 },
208 motionEvent: function(event) {
33a80b5e
RK
209 var gravTotal =
210 Math.sqrt(Math.pow(event.accelerationIncludingGravity.x, 2) +
211 Math.pow(event.accelerationIncludingGravity.y, 2) +
212 Math.pow(event.accelerationIncludingGravity.z, 2));
213 document.getElementById("gravTotal").textContent = gravTotal.toFixed(2) + " m/s²";
b5c483f2
RK
214 document.getElementById("gravX").textContent = event.accelerationIncludingGravity.x.toFixed(2) + " m/s²";
215 document.getElementById("gravY").textContent = event.accelerationIncludingGravity.y.toFixed(2) + " m/s²";
216 document.getElementById("gravZ").textContent = event.accelerationIncludingGravity.z.toFixed(2) + " m/s²";
217 //document.getElementById("gravRot").textContent = event.rotationRate;
1753d94a
RK
218 },
219}
220
39b2fdef
RK
221var gModSound = {
222 activate: function() {
223 //gSounds.scan.play();
224 if (navigator.getUserMedia) {
225 document.getElementById("soundunavail").style.display = "none";
226 document.getElementById("soundavail").style.display = "block";
227 navigator.getUserMedia({ audio: true },
228 function(aLocalMediaStream) {
229 gModSound.mAudio.stream = aLocalMediaStream;
230 gModSound.mAudio.context = new (window.AudioContext || window.webkitAudioContext)();
231 gModSound.mAudio.input = gModSound.mAudio.context.createMediaStreamSource(gModSound.mAudio.stream);
232 // Could put a filter in between like in http://webaudioapi.com/samples/microphone/
233 gModSound.mDisplay.canvas = document.getElementById("soundcanvas");
234 gModSound.mDisplay.context = gModSound.mDisplay.canvas.getContext("2d");
235 gModSound.mDisplay.canvas.height = document.getElementById("soundavail").clientHeight - 2;
236 gModSound.mDisplay.canvas.width = document.getElementById("soundavail").clientWidth;
237 gModSound.mAudio.frequencySlices = (gModSound.mDisplay.canvas.width > 512) ?
238 512 :
239 Math.pow(2, Math.floor(Math.log(gModSound.mDisplay.canvas.width) / Math.LN2));
240 console.log("slices: " + gModSound.mAudio.frequencySlices);
241 gModSound.mAudio.analyzer = gModSound.mAudio.context.createAnalyser();
242 // Make the FFT four times as large as needed as the upper three quarters turn out to be useless.
243 gModSound.mAudio.analyzer.fftSize = gModSound.mAudio.frequencySlices * 4;
244 console.log("FFT: " + gModSound.mAudio.analyzer.fftSize);
245 gModSound.mAudio.input.connect(gModSound.mAudio.analyzer);
246 gModSound.mDisplay.context.setTransform(1, 0, 0, -(gModSound.mDisplay.canvas.height/256), 0, gModSound.mDisplay.canvas.height);
247 gModSound.mDisplay.active = true;
248 window.requestAnimationFrame(gModSound.paintAnalyzerFrame);
249 },
250 function(err) {
251 document.getElementById("soundunavail").style.display = "block";
252 document.getElementById("soundavail").style.display = "none";
253 console.log(err);
254 }
255 );
256 }
257 else {
258 document.getElementById("soundunavail").style.display = "block";
259 document.getElementById("soundavail").style.display = "none";
260 }
261 },
262 mAudio: {
263 frequencySlices: 32, // Must be a multiple of 2 (see AnalyserNode.fftSize)
264 },
265 mDisplay: {
266 active: false,
267 },
268 paintAnalyzerFrame: function(aTimestamp) {
269 var data = new Uint8Array(gModSound.mAudio.frequencySlices);
270 gModSound.mAudio.analyzer.getByteFrequencyData(data);
271 gModSound.mDisplay.context.clearRect(0, 0, gModSound.mDisplay.canvas.width, gModSound.mDisplay.canvas.height);
272 // Out of experience, only the first half of the slices are actually useful.
273 var wid = gModSound.mDisplay.canvas.width / gModSound.mAudio.frequencySlices;
274 var fill = "#9C9CFF";
275 for (var i = 0; i < gModSound.mAudio.frequencySlices; ++i) {
276 var newFill = (data[i] > 200) ? "#FF0000" : ((data[i] > 100) ? "#FFCF00" : "#9C9CFF");
277 if (fill != newFill) { gModSound.mDisplay.context.fillStyle = newFill; fill = newFill; }
278 gModSound.mDisplay.context.fillRect(i*wid, 0, wid, data[i]);
279 }
280 if (gModSound.mDisplay.active)
281 window.requestAnimationFrame(gModSound.paintAnalyzerFrame);
282 },
283 deactivate: function() {
284 gModSound.mDisplay.active = false;
285 gModSound.mAudio.stream.stop();
286 gSounds.scan.pause();
287 },
288}
84436ccb
RK
289
290var gModDev = {
291 activate: function() {
292 gSounds.scan.play();
293 this.batteryTimer =
294 setInterval(function () { gModDev.updateBattery(); }, 100);
295 },
296 deactivate: function() {
297 clearTimeout(this.batteryTimer);
298 gSounds.scan.pause();
299 },
300 updateBattery: function() {
06b21738
RK
301 document.getElementById("devBattLevel").textContent =
302 (navigator.battery.level * 100).toFixed(1) + "%";
303 if (navigator.battery.charging) {
304 if (navigator.battery.chargingTime == 0 ||
305 navigator.battery.chargingTime == Infinity) {
306 document.getElementById("devBattStatus").textContent = "charging";
307 }
308 else {
309 document.getElementById("devBattStatus").textContent =
310 "charging, " + navigator.battery.chargingTime + "s remaining";
311 }
312 }
313 else {
314 if (navigator.battery.dischargingTime == 0 ||
315 navigator.battery.dischargingTime == Infinity) {
316 document.getElementById("devBattStatus").textContent = "discharging";
317 }
318 else {
319 document.getElementById("devBattStatus").textContent =
320 navigator.battery.dischargingTime + "s usage remaining";
321 }
322 }
84436ccb
RK
323 },
324 batteryTimer: null,
325}
326
d3e5ba11 327var gModOther = {
14a57547
RK
328 activate: function() {
329 //gSounds.scan.play();
330 },
331 deactivate: function() {
332 gSounds.scan.pause();
333 },
1753d94a
RK
334}
335
336var gModNull = {
14a57547
RK
337 activate: function() {
338 //gSounds.scan.play();
339 },
340 deactivate: function() {
341 gSounds.scan.pause();
342 },
eabed1a4 343}