annot-server/static/js/pianoroll.js
author ymh <ymh.work@gmail.com>
Tue, 28 Oct 2014 09:47:11 +0100
changeset 76 029cdbeebf03
parent 74 bdcc964b3c01
child 77 1b578edc1578
permissions -rw-r--r--
filter pianoroll annotation by channel & event
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
        }
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   113
    };
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   114
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   115
}
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   116
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   117
// Init containers
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   118
var container1 = new PianoRoll(uberContainer, 0, 0, prHeight1, true, pixelsPerSecond1, sceneWidth);
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   119
var container2 = new PianoRoll(uberContainer, 0, prHeight1, prHeight2, false, pixelsPerSecond2, sceneWidth);
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   120
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   121
// Line between two containers
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   122
var graphics = new PIXI.Graphics();
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   123
graphics.beginFill(0xFFFF00);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   124
graphics.lineStyle(1, lineColor);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   125
graphics.moveTo(0, prHeight1);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   126
graphics.lineTo(sceneWidth, prHeight1);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   127
graphics.endFill();
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   128
stage.addChild(graphics);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   129
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   130
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   131
function replaceContainers(){
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   132
    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
   133
    //console.log("replace ! diff1 = ", container1.x - Math.floor(-diff*pixelsPerSecond1), ", diff 2 = ", container2.x - Math.floor(-diff*pixelsPerSecond2));
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   134
    container1.moveTo(-diff);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   135
    container2.moveTo(-diff);
46
7cff1f0a6882 some debug
cavaliet
parents: 28
diff changeset
   136
    renderer.render(stage);
7cff1f0a6882 some debug
cavaliet
parents: 28
diff changeset
   137
}
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   138
48
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   139
function addNotes(data){
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   140
    if(!offsetMusic){
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   141
        // 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
   142
        // in order to place in real time
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   143
        var now = Date.now();
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   144
        var timeBetweenNowAndStart = now - timePageLoaded;
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   145
        offsetMusic = timeBetweenNowAndStart - data.content[1];
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   146
        //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
   147
    }
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   148
    var note = data.content[3];
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   149
    var velocity = data.content[4];
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   150
    if(velocity===0){
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   151
        if(typeof noteDict[data.content[2]][note]!=='undefined'){
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   152
            // We close the note in container one
48
a7abfcfd7959 real time with piano roll and new category management
cavaliet
parents: 46
diff changeset
   153
            //console.log("coucou 2", data);
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   154
            var duration = data.content[1] - noteDict[data.content[2]][note].ts;
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   155
            //addNoteInContainer(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, pixelsPerSecond1, container1, prHeight1, data.content[2]);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   156
            //         addNote(note, startTime,                          duration, velocity,                                 canal)
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   157
            container1.addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   158
            //addNoteInContainer(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, pixelsPerSecond2, container2, prHeight2, data.content[2]);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   159
            container2.addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]);
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   160
            // delete entry
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   161
            delete noteDict[data.content[2]][note];
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   162
        }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   163
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   164
    else{
54
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   165
        if(typeof noteDict[data.content[2]]==='undefined'){
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   166
            noteDict[data.content[2]] = {};
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   167
        }
31cea001a298 various channels
cavaliet
parents: 50
diff changeset
   168
        noteDict[data.content[2]][note] = {ts: data.content[1], velocity:velocity};
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
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   172
function addLine(){
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   173
    nbLines++;
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   174
    container1.addLine(nbLines);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   175
    container2.addLine(nbLines);
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   176
}
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   177
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   178
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   179
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   180
// Socket management
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   181
var sock = null;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   182
var ellog = null;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   183
function log(m) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   184
    if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   185
        ellog.innerHTML += m + '\n';
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   186
        ellog.scrollTop = ellog.scrollHeight;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   187
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   188
}
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   189
window.onload = function(){
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   190
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   191
    if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   192
        ellog = document.getElementById('log');
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   193
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   194
    else{
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   195
        document.body.removeChild(document.getElementById('log'));
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   196
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   197
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   198
    var wsuri;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   199
    if (window.location.protocol === "file:") {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   200
       wsuri = "ws://127.0.0.1:8090/broadcast";
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   201
    } else {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   202
       wsuri = "ws://" + window.location.hostname + ":8090/broadcast";
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   203
    }
76
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   204
    wsuri = wsuri + "?channel=PIANOROLL&event_code="+event_code;
029cdbeebf03 filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   205
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   206
    if ("WebSocket" in window) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   207
       sock = new WebSocket(wsuri);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   208
    } else if ("MozWebSocket" in window) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   209
       sock = new MozWebSocket(wsuri);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   210
    } else {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   211
       log("Browser does not support WebSocket!");
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   212
       window.location = "http://autobahn.ws/unsupportedbrowser";
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   213
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   214
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   215
    if (sock) {
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   216
       sock.onopen = function(){
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   217
           if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   218
               log("Connected to " + wsuri);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   219
           }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   220
       }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   221
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   222
       sock.onclose = function(e) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   223
           if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   224
               log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   225
           }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   226
          sock = null;
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
       sock.onmessage = function(e) {
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   230
           if(logger){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   231
               log("Got message: " + e.data);
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   232
           }
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   233
           addNotes(JSON.parse(e.data));
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   234
       }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   235
    }
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   236
};
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   237
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   238
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   239
// Init page and intervals
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   240
addLine();
49
5c1702e8d2a4 move with time and not framerate iteration
cavaliet
parents: 48
diff changeset
   241
var moveInterval = window.setInterval(replaceContainers, 1000/manualFramerate);
74
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   242
var verticalLinesInterval = window.setInterval(addLine, lineInterval);
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   243
var removeInterval1 = window.setInterval(container1.removePassedObjets, 1000 * sceneWidth / pixelsPerSecond1 );
bdcc964b3c01 good optimisation for piano roll
cavaliet
parents: 72
diff changeset
   244
var removeInterval2 = window.setInterval(container2.removePassedObjets, 1000 * sceneWidth / pixelsPerSecond2 );
72
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   245
99658249716c Piano roll object instead of several containers
cavaliet
parents: 70
diff changeset
   246
// Little inteval to show time
28
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   247
var nbSec = 0;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   248
var mySpan = document.getElementById("myspan");
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   249
function updateTime(){
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   250
    nbSec++;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   251
    var hours = parseInt( nbSec / 3600 ) % 24;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   252
    var minutes = parseInt( nbSec / 60 ) % 60;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   253
    var seconds = nbSec % 60;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   254
    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
   255
    mySpan.innerHTML = timeStr;
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   256
}
6025b8470d18 pianoroll in webapp
cavaliet
parents:
diff changeset
   257
var secondInterval = window.setInterval(updateTime, 1000);