annot-server/static/js/pianoroll.js
author ymh <ymh.work@gmail.com>
Thu, 08 Jan 2015 19:15:59 +0100
changeset 80 dd414da0f0bb
parent 79 bd2f2c3f205c
permissions -rw-r--r--
move clent to annot-client subfolder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     1
// Config vars
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     2
var logger = false;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     3
var sceneWidth = 1920;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     4
var prHeight1 = 435;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     5
var prHeight2 = 645;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     6
var sceneHeight = prHeight1 + prHeight2;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     7
var sceneBgColor = 0xFFFFFF;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
     8
var lineColor = 0x444444;
46
7cff1f0a6882 some debug
cavaliet
parents: 28
diff changeset
     9
var pixelsPerSecond1 = Math.floor(sceneWidth / 10); // nb of pixels per second
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    10
var manualFramerate = pixelsPerSecond1 / 4;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    11
var pixelsPerSecond2 = Math.floor(sceneWidth / 60); // nb of pixels per second
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    12
var lineInterval = 5000; // means line every 5 seconds
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    13
var nbLines = -1;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    14
var noteHeight = 110;
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    15
var noteColors = [0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991];
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    16
var colorsReg = {}
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    17
// Vars
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    18
var noteDict = [];
48
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
    19
// Timecode method
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
    20
var timePageLoaded = Date.now();
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
    21
var offsetMusic = false;
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    22
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    23
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    24
//create an new instance of a pixi stage
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    25
var stage = new PIXI.Stage(sceneBgColor);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    26
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    27
//create a renderer instance.
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    28
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    29
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    30
//add the renderer view element to the DOM
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    31
document.getElementById("canvasContainer").appendChild(renderer.view);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    32
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    33
var uberContainer = new PIXI.DisplayObjectContainer();
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    34
uberContainer.position.x = Math.floor(sceneWidth*9/10);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    35
uberContainer.position.y = 0;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    36
stage.addChild(uberContainer);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    37
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
    38
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    39
function PianoRoll(parentContainer, xInit, yInit, height, linesDown, pixelsPerSecond, width, noteColors, colorsReg){
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
    40
    var _this = this;
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    41
    this.container = new PIXI.DisplayObjectContainer();
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    42
    this.container.position.x = xInit;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    43
    this.container.position.y = yInit;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    44
    parentContainer.addChild(this.container);
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    45
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    46
    this.linesDown = linesDown;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    47
    this.height = height;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    48
    this.pixelsPerSecond = pixelsPerSecond;
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
    49
    this.width = width;
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    50
    this.noteColors = noteColors;
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    51
    this.colorsReg = colorsReg || {}
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    52
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    53
    this.addNote = function(note, startTime, duration, velocity, canal){
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    54
        //console.log("coucou 1", note, timeFromZero, ts, velocity, pixelsPerSecond, container, prHeight);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    55
        var beginX = (offsetMusic + startTime) * this.pixelsPerSecond / 1000;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    56
        var width = duration * this.pixelsPerSecond / 1000;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    57
        // We draw the rectangle
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    58
        var graphics = new PIXI.Graphics();
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    59
        //console.log("beginX = ", beginX, "canal = ", canal, "color = ", noteColor[canal], "width = ", width, "note = ", note, "velocity = ", velocity);
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    60
        var color = this.colorsReg[canal];
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    61
        if(typeof(color) === 'undefined') {
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    62
            var colorsRegSize = Object.keys(this.colorsReg).length
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    63
            if(colorsRegSize < this.noteColors.length) {
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    64
                color = this.colorsReg[canal] = this.noteColors[colorsRegSize];
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    65
            }
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    66
            else {
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    67
                color = this.colorsReg[canal] = parseInt(randomColor({ luminosity: 'light', hue: 'random', format:'hex'}).replace(/^#/, ''), 16);
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    68
            }
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    69
        }
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    70
        graphics.beginFill(color, (velocity / 128));
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    71
        var y = (128-note) * this.height / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
    72
        graphics.drawRect(0, Math.floor(y - (noteHeight/2) + ((this.height / 128)/2)), width, noteHeight);
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    73
        graphics.endFill();
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
    74
        graphics.x = beginX;
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    75
        this.container.addChild(graphics);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    76
    };
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    77
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    78
    this.addLine = function(lineNb){
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    79
        var graphics = new PIXI.Graphics();
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    80
        var x = -this.container.x;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    81
        graphics.beginFill(0xFFFF00);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    82
        graphics.lineStyle(1, lineColor);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    83
        var y = this.linesDown ? this.height - 20 : 0;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    84
        graphics.moveTo(x, y);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    85
        graphics.lineTo(x, y + 20);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    86
        graphics.endFill();
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    87
        this.container.addChild(graphics);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    88
        // Add text
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    89
        var totalSec = lineNb * lineInterval / 1000;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    90
        var hours = parseInt( totalSec / 3600 ) % 24;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    91
        var minutes = parseInt( totalSec / 60 ) % 60;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    92
        var seconds = totalSec % 60;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    93
        var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    94
        var fontObj = { font: "10pt Arial", fill: "#444444" };
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    95
        var t = new PIXI.Text(timeStr, fontObj);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    96
        t.x = x + 2;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    97
        t.y = this.linesDown ? this.height - 15 : 2;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
    98
        this.container.addChild(t);
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
    99
    };
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   100
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   101
    this.moveTo = function(diffTime){
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   102
        this.container.x = Math.floor(diffTime*this.pixelsPerSecond);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   103
    };
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   104
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   105
    this.removePassedObjets = function(){
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   106
        var nbChilds = _this.container.children.length;
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   107
        var i = 0, childIsNowDisplayed = false;
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   108
        while(i<nbChilds && !childIsNowDisplayed){
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   109
            var child = _this.container.children[0];
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   110
            //console.log("remove ? ", child.x, child.width, ((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)));
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   111
            if((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)){
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   112
                _this.container.removeChild(child);
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   113
                //console.log("    remove !!!");
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   114
            }
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   115
            else{
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   116
                childIsNowDisplayed = true;
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   117
                //console.log("    childIsNowDisplayed");
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   118
            }
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   119
            i++;
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   120
        }
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   121
        //console.log("before : ", nbChilds, ", after : ", _this.container.children.length);
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   122
    };
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   123
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   124
    // remove notes each scene width
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   125
    var removeInterval = window.setInterval(this.removePassedObjets, 1000 * sceneWidth / this.pixelsPerSecond );
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   126
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   127
}
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   128
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   129
// Init containers
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   130
var containerList = [];
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   131
containerList.push(new PianoRoll(uberContainer, 0, 0, prHeight1, true, pixelsPerSecond1, sceneWidth, noteColors, colorsReg));
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   132
containerList.push(new PianoRoll(uberContainer, 0, prHeight1, prHeight2, false, pixelsPerSecond2, sceneWidth, noteColors, colorsReg));
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   133
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   134
// Line between two containers
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   135
var graphics = new PIXI.Graphics();
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   136
graphics.beginFill(0xFFFF00);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   137
graphics.lineStyle(1, lineColor);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   138
graphics.moveTo(0, prHeight1);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   139
graphics.lineTo(sceneWidth, prHeight1);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   140
graphics.endFill();
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   141
stage.addChild(graphics);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   142
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   143
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   144
function replaceContainers(){
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   145
    var diff = (Date.now() - timePageLoaded)/1000;// nb of seconds since page loaded
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   146
    //console.log("replace ! diff1 = ", container1.x - Math.floor(-diff*pixelsPerSecond1), ", diff 2 = ", container2.x - Math.floor(-diff*pixelsPerSecond2));
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   147
    for(var i=0;i<containerList.length;i++){
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   148
        containerList[i].moveTo(-diff);
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   149
    }
46
7cff1f0a6882 some debug
cavaliet
parents: 28
diff changeset
   150
    renderer.render(stage);
7cff1f0a6882 some debug
cavaliet
parents: 28
diff changeset
   151
}
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   152
48
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   153
function addNotes(data){
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   154
    if(!offsetMusic){
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   155
        // get difference between the current note timecode and my zero to set the difference between the canvas's zero and the music's zero
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   156
        // in order to place in real time
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   157
        var now = Date.now();
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   158
        var timeBetweenNowAndStart = now - timePageLoaded;
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   159
        offsetMusic = timeBetweenNowAndStart - data.content[1];
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   160
        //console.log("start: ", timePageLoaded, ", now: ", now, ", timeBetweenNowAndStart = ", timeBetweenNowAndStart, ", offsetMusic = ", offsetMusic);
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   161
    }
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   162
    var note = data.content[3];
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   163
    var velocity = data.content[4];
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   164
    if(velocity===0){
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   165
        if(typeof noteDict[data.content[2]][note]!=='undefined'){
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   166
            // We close the note in container one
48
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   167
            //console.log("coucou 2", data);
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   168
            var duration = data.content[1] - noteDict[data.content[2]][note].ts;
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   169
            for(var i=0;i<containerList.length;i++){
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   170
                //               addNote(note, startTime,                          duration, velocity,                                 canal)
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   171
                containerList[i].addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]);
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   172
            }
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   173
            // delete entry
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   174
            delete noteDict[data.content[2]][note];
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   175
        }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   176
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   177
    else{
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   178
        if(typeof noteDict[data.content[2]]==='undefined'){
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   179
            noteDict[data.content[2]] = {};
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   180
        }
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   181
        noteDict[data.content[2]][note] = {ts: data.content[1], velocity:velocity};
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   182
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   183
}
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   184
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   185
function addLine(){
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   186
    nbLines++;
79
bd2f2c3f205c Improve color management
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   187
    for(var i=0;i<containerList.length;i++){
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   188
        containerList[i].addLine(nbLines);
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   189
    }
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   190
}
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   191
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   192
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   193
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   194
// Socket management
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   195
var sock = null;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   196
var ellog = null;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   197
function log(m) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   198
    if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   199
        ellog.innerHTML += m + '\n';
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   200
        ellog.scrollTop = ellog.scrollHeight;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   201
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   202
}
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   203
window.onload = function(){
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   204
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   205
    if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   206
        ellog = document.getElementById('log');
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   207
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   208
    else{
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   209
        document.body.removeChild(document.getElementById('log'));
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   210
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   211
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   212
    var wsuri;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   213
    if (window.location.protocol === "file:") {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   214
       wsuri = "ws://127.0.0.1:8090/broadcast";
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   215
    } else {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   216
       wsuri = "ws://" + window.location.hostname + ":8090/broadcast";
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   217
    }
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   218
    wsuri = wsuri + "?channel=PIANOROLL&event_code="+event_code;
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   219
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   220
    if ("WebSocket" in window) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   221
       sock = new WebSocket(wsuri);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   222
    } else if ("MozWebSocket" in window) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   223
       sock = new MozWebSocket(wsuri);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   224
    } else {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   225
       log("Browser does not support WebSocket!");
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   226
       window.location = "http://autobahn.ws/unsupportedbrowser";
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   227
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   228
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   229
    if (sock) {
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   230
        sock.onopen = function(){
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   231
            if(logger){
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   232
                log("Connected to " + wsuri);
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   233
            }
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   234
        }
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   235
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   236
        sock.onclose = function(e) {
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   237
            if(logger){
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   238
                log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   239
            }
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   240
            sock = null;
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   241
        }
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   242
77
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   243
        sock.onmessage = function(e) {
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   244
            if(logger){
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   245
                log("Got message: " + e.data);
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   246
            }
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   247
            addNotes(JSON.parse(e.data));
1b578edc1578 factorize code
cavaliet
parents: 76
diff changeset
   248
        }
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   249
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   250
};
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   251
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   252
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   253
// Init page and intervals
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   254
addLine();
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   255
var moveInterval = window.setInterval(replaceContainers, 1000/manualFramerate);
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   256
var verticalLinesInterval = window.setInterval(addLine, lineInterval);
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   257
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   258
// Little inteval to show time
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   259
var nbSec = 0;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   260
var mySpan = document.getElementById("myspan");
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   261
function updateTime(){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   262
    nbSec++;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   263
    var hours = parseInt( nbSec / 3600 ) % 24;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   264
    var minutes = parseInt( nbSec / 60 ) % 60;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   265
    var seconds = nbSec % 60;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   266
    var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   267
    mySpan.innerHTML = timeStr;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   268
}
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   269
var secondInterval = window.setInterval(updateTime, 1000);