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