client/annotviz/app/js/main.js
changeset 93 79ae42ad97d4
parent 87 9611905b58fe
child 94 e0e514c5470f
--- a/client/annotviz/app/js/main.js	Tue Jan 13 15:27:20 2015 +0100
+++ b/client/annotviz/app/js/main.js	Fri Jan 16 03:16:19 2015 +0100
@@ -5,11 +5,20 @@
  * Take a look at http://browserify.org/ for more info
  */
 
+ /* global window: false */
+ /* global document: false */
+ /* global WebSocket: false */
+ /* global MozWebSocket: false */
+
+ /* global eventCode: false */
 'use strict';
 
 
 var PIXI = require('pixi');
+var _ = require('lodash');
 
+
+var NTP_EPOCH_DELTA = 2208988800; //c.f. RFC 868
 // Config vars
 var logger = false;
 var sceneWidth = 1920;
@@ -22,14 +31,10 @@
 var manualFramerate = pixelsPerSecond1 / 4;
 var pixelsPerSecond2 = Math.floor(sceneWidth / 60); // nb of pixels per second
 var lineInterval = 5000; // means line every 5 seconds
-var nbLines = -1;
-var noteHeight = 110;
+//var noteHeight = 110;
 var noteColors = [0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991];
 var colorsReg = {};
-// Vars
-var noteDict = [];
 // Timecode method
-var timePageLoaded = Date.now();
 var offsetMusic = false;
 
 
@@ -47,12 +52,38 @@
 uberContainer.position.y = 0;
 stage.addChild(uberContainer);
 
-var PianoRoll = require('./pianoroll.js')
+var PianoRoll = require('./pianoroll.js');
 
 // Init containers
 var containerList = [];
-containerList.push(new PianoRoll(uberContainer, 0, 0, prHeight1, true, pixelsPerSecond1, sceneWidth, noteColors, colorsReg, lineColor, lineInterval, offsetMusic, prHeight1 / 128));
-containerList.push(new PianoRoll(uberContainer, 0, prHeight1, prHeight2, false, pixelsPerSecond2, sceneWidth, noteColors, colorsReg, lineColor, lineInterval, offsetMusic, prHeight2 / 128));
+
+var pianorollOptions = {
+    parentContainer: uberContainer,
+    xInit: 0,
+    width: sceneWidth,
+    noteColors: noteColors,
+    colorsReg: colorsReg,
+    lineColor: lineColor,
+    lineInterval: lineInterval,
+    offsetMusic: offsetMusic
+};
+
+
+containerList.push(new PianoRoll(_.extend(_.clone(pianorollOptions), {
+    yInit: 0,
+    height: prHeight1,
+    linesDown: true,
+    pixelsPerSecond: pixelsPerSecond1,
+    noteHeight: prHeight1 / 128
+})));
+containerList.push(new PianoRoll(_.extend(_.clone(pianorollOptions), {
+    yInit: prHeight1,
+    height: prHeight2,
+    linesDown: false,
+    pixelsPerSecond: pixelsPerSecond2,
+    noteHeight: prHeight2 / 128
+})));
+
 
 // Line between two containers
 var graphics = new PIXI.Graphics();
@@ -64,52 +95,16 @@
 stage.addChild(graphics);
 
 
-function replaceContainers(){
-    var diff = (Date.now() - timePageLoaded)/1000;// nb of seconds since page loaded
-    //console.log("replace ! diff1 = ", container1.x - Math.floor(-diff*pixelsPerSecond1), ", diff 2 = ", container2.x - Math.floor(-diff*pixelsPerSecond2));
-    for(var i=0;i<containerList.length;i++){
-        containerList[i].moveTo(-diff);
-    }
-    renderer.render(stage);
-}
-
 function addNotes(data){
-    if(!offsetMusic){
-        // get difference between the current note timecode and my zero to set the difference between the canvas's zero and the music's zero
-        // in order to place in real time
-        var now = Date.now();
-        var timeBetweenNowAndStart = now - timePageLoaded;
-        offsetMusic = timeBetweenNowAndStart - data.content[1];
-        //console.log("start: ", timePageLoaded, ", now: ", now, ", timeBetweenNowAndStart = ", timeBetweenNowAndStart, ", offsetMusic = ", offsetMusic);
-    }
     var note = data.content[3];
     var velocity = data.content[4];
-    if(velocity===0){
-        if(typeof noteDict[data.content[2]][note]!=='undefined'){
-            // We close the note in container one
-            //console.log("coucou 2", data);
-            var duration = data.content[1] - noteDict[data.content[2]][note].ts;
-            for(var i=0;i<containerList.length;i++){
-                //               addNote(note, startTime,                          duration, velocity,                                 canal)
-                containerList[i].addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]);
-            }
-            // delete entry
-            delete noteDict[data.content[2]][note];
-        }
-    }
-    else{
-        if(typeof noteDict[data.content[2]]==='undefined'){
-            noteDict[data.content[2]] = {};
-        }
-        noteDict[data.content[2]][note] = {ts: data.content[1], velocity:velocity};
-    }
-}
+    var ts = (data.content[0] - NTP_EPOCH_DELTA)*1000;
+    var channel = data.content[2];
+    var sessionTs = data.content[1];
 
-function addLine(){
-    var ts = new Date();
-    for(var i=0;i<containerList.length;i++){
-        containerList[i].addLine(ts);
-    }
+    containerList.forEach(function(c) {
+        c.addNote(note, ts, sessionTs, velocity, channel, 0);
+    });
 }
 
 
@@ -164,21 +159,42 @@
         };
 
         sock.onmessage = function(e) {
+            var dataJson = JSON.parse(e.data);
+            var dataDate = new Date((dataJson.content[0]-NTP_EPOCH_DELTA)*1000);
             if(logger){
-                log('Got message: ' + e.data);
+                log('Got message: ' + e.data + ' - ' + dataDate.toISOString());
             }
-            addNotes(JSON.parse(e.data));
+            addNotes(dataJson);
         };
     }
 };
 
+function refreshStage() {
+    containerList.forEach(function(c) {
+        c.move();
+    });
+    renderer.render(stage);
+}
+
 
 // Init page and intervals
-addLine();
-var moveInterval = window.setInterval(replaceContainers, 1000/manualFramerate);
-var verticalLinesInterval = window.setInterval(addLine, lineInterval);
+var refreshInterval;
+
+function start() {
+    refreshInterval = window.setInterval(refreshStage, 1000/manualFramerate);
+    containerList.forEach(function(c) {
+        c.start();
+    });
+}
 
-// Little inteval to show time
+function stop() {
+    window.clearInterval(refreshInterval);
+    containerList.forEach(function(c) {
+        c.stop();
+    });
+}
+
+// Little interval to show time
 var nbSec = 0;
 var mySpan = document.getElementById('myspan');
 function updateTime(){
@@ -189,10 +205,11 @@
     var timeStr = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds  < 10 ? '0' + seconds : seconds);
     mySpan.innerHTML = timeStr;
 }
-var secondInterval = window.setInterval(updateTime, 1000);
+window.setInterval(updateTime, 1000);
+
+start();
 
 module.exports = {
-    moveInterval: moveInterval,
-    verticalLinesInterval: verticalLinesInterval,
-    secondInterval: secondInterval
+    start: start,
+    stop: stop,
 };