client/annotviz/app/js/pianoroll.js
author ymh <ymh.work@gmail.com>
Fri, 16 Jan 2015 03:16:19 +0100
changeset 93 79ae42ad97d4
parent 85 eff9460bd4f2
child 94 e0e514c5470f
permissions -rw-r--r--
optimize and refactor pianoroll component
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/**
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
* js/pianoroll.js
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
*
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
* pianoroll basic component
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
*
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
*/
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
     8
/* global window: false */
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
'use strict';
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    11
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
var PIXI = require('pixi');
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
var randomColor = require('randomColor');
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    14
var _ = require('lodash');
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    16
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    17
function PianoRoll(options) {
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    var _this = this;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    this.container = new PIXI.DisplayObjectContainer();
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    20
    this.container.position.x = options.xInit;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    21
    this.container.position.y = options.yInit;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    22
    options.parentContainer.addChild(this.container);
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    24
    this.linesDown = options.linesDown;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    25
    this.height = options.height;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    26
    this.pixelsPerSecond = options.pixelsPerSecond;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    27
    this.width = options.width;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    28
    this.noteColors = options.noteColors;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    29
    this.colorsReg = options.colorsReg || {};
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    30
    this.lineColor = options.lineColor;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    31
    this.lineInterval = options.lineInterval;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    32
    this.offsetMusic = options.offsetMusic || false;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    33
    this.noteHeight = options.noteHeight;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    34
    this.noteDict = {};
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    35
    this.startTs = options.startTs || Date.now();
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    37
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    38
    //TODO: I do not like the "regColor" object. This should not be global, but local
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    39
    this.getColor = function(canal) {
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        var color = this.colorsReg[canal];
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        if(typeof(color) === 'undefined') {
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            var colorsRegSize = Object.keys(this.colorsReg).length;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            if(colorsRegSize < this.noteColors.length) {
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
                color = this.colorsReg[canal] = this.noteColors[colorsRegSize];
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            }
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            else {
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
                color = this.colorsReg[canal] = parseInt(randomColor({ luminosity: 'light', hue: 'random', format:'hex'}).replace(/^#/, ''), 16);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            }
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        }
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    50
        return color;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    51
    };
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    52
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    53
    this.getNoteRect = function(x, y, color, alpha, width, height) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    54
        var graphics = new PIXI.Graphics();
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    55
        //console.log("beginX = ", beginX, "canal = ", canal, "color = ", noteColor[canal], "width = ", width, "note = ", note, "velocity = ", velocity);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    56
        graphics.beginFill(color, alpha);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    57
        graphics.drawRect(0, 0, width, height);
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        graphics.endFill();
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    59
        graphics.x = x;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    60
        graphics.y = y;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    61
        graphics.width = width;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    62
        graphics.height = height;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    63
        return graphics;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    64
    };
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    65
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    66
    this.addNote = function(note, startTime, sessionTs, velocity, channel, duration) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    67
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    68
        var ts = startTime;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    69
        if(this.offsetMusic) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    70
            ts = this.startTs + sessionTs;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    71
        }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    72
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    73
        var noteDuration = duration;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    74
        var noteVelocity = velocity;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    75
        var graphics;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    76
        if(!duration) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    77
            if(typeof this.noteDict[channel]==='undefined'){
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    78
                this.noteDict[channel] = {};
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    79
            }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    80
            if(velocity===0) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    81
                if(typeof this.noteDict[channel][note] !== 'undefined') {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    82
                    var noteDef = this.noteDict[channel][note];
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    83
                    delete this.noteDict[channel][note];
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    84
                    noteDuration = sessionTs - noteDef.sessionTs;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    85
                    graphics = noteDef.graphics;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    86
                    noteVelocity = noteDef.velocity;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    87
                    ts = noteDef.ts;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    88
                }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    89
            }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    90
            else {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    91
                noteDuration = Date.now() - ts;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    92
                this.noteDict[channel][note] = { ts: ts, velocity: velocity, sessionTs: sessionTs};
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    93
            }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    94
        }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    95
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    96
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    97
        if(!this.offsetMusic || velocity===0) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    98
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    99
            var width = noteDuration * this.pixelsPerSecond / 1000;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   100
            if(!graphics) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   101
                var x = (ts-this.startTs) * this.pixelsPerSecond / 1000;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   102
                if((x+width) <  (Math.abs(this.container.x) - this.width)) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   103
                    // not visible. do nothing
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   104
                    return;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   105
                }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   106
                var y = Math.floor((128-note+0.5) * this.height / 128 - (this.noteHeight/2));
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   107
                var color = this.getColor(channel);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   108
                var alpha = (noteVelocity / 128);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   109
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   110
                graphics = this.getNoteRect(x, y, color, alpha, width, this.noteHeight);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   111
                this.container.addChild(graphics);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   112
                //console.log(color, alpha, graphics.lineColor, graphics.fillAlpha);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   113
            }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   114
            else {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   115
                graphics.width = width;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   116
            }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   117
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   118
            if(!duration && velocity) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   119
                this.noteDict[channel][note].graphics = graphics;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   120
            }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   121
        }
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    };
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    this.addLine = function(ts){
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   125
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   126
        if(typeof(ts) === 'undefined') {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   127
            ts = new Date();
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   128
        }
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
        var graphics = new PIXI.Graphics();
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        var x = -this.container.x;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        graphics.beginFill(0xFFFF00);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        graphics.lineStyle(1, this.lineColor);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
        var y = this.linesDown ? this.height - 20 : 0;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        graphics.moveTo(x, y);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
        graphics.lineTo(x, y + 20);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        graphics.endFill();
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        this.container.addChild(graphics);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        // Add text
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        //var totalSec = lineNb * this.lineInterval / 1000;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        var hours = ts.getHours();
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        var minutes =ts.getMinutes();
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
        var seconds = ts.getSeconds();
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
        var timeStr = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds  < 10 ? '0' + seconds : seconds);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
        var fontObj = { font: '10pt Arial', fill: '#444444' };
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
        var t = new PIXI.Text(timeStr, fontObj);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
        t.x = x + 2;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
        t.y = this.linesDown ? this.height - 15 : 2;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        this.container.addChild(t);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    };
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    this.moveTo = function(diffTime){
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   153
        var oldX = this.container.x;
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        this.container.x = Math.floor(diffTime*this.pixelsPerSecond);
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   155
        var deltaX = Math.abs(oldX-this.container.x);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   156
        _.forOwn(this.noteDict, function(channelDict) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   157
            _.forOwn(channelDict, function(noteDef) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   158
                if(noteDef.graphics) {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   159
                    noteDef.graphics.width = noteDef.graphics.width + deltaX;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   160
                }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   161
            });
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   162
        });
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   163
    };
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   164
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   165
    this.move = function() {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   166
        var diff = (this.startTs - Date.now())/1000;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   167
        this.moveTo(diff);
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
    };
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    this.removePassedObjets = function(){
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        var nbChilds = _this.container.children.length;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        var i = 0, childIsNowDisplayed = false, childrenToRemove = [];
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
        while(i<nbChilds && !childIsNowDisplayed){
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            var child = _this.container.children[i++];
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
            //console.log("remove ? ", child.x, child.width, ((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)));
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   176
            if(typeof(child) === 'undefined') {
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
                continue;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
            }
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
            if((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)){
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
                childrenToRemove.push(child);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
                //console.log("    remove !!!");
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
            }
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
            else {
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
                childIsNowDisplayed = true;
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
                //console.log("    childIsNowDisplayed");
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
            }
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        }
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        childrenToRemove.forEach(function(child) {
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
            _this.container.removeChild(child);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        });
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
        //console.log("before : ", nbChilds, ", after : ", _this.container.children.length);
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
    };
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
93
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   194
    this.start = function() {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   195
        if(typeof(this.startTs) === 'undefined') {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   196
            console.log('Container Started');
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   197
            this.startTs = Date.now();
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   198
            this.addLine();
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   199
        }
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   200
        var _this = this;
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   201
        this.verticalLinesInterval = window.setInterval(function() { _this.addLine(); }, this.lineInterval);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   202
        this.cleanInterval = window.setInterval(function () { _this.removePassedObjets(); }, 1000 * this.width / this.pixelsPerSecond );
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   203
    };
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   204
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   205
    this.stop = function() {
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   206
        //window.clearInterval(this.moveInterval);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   207
        window.clearInterval(this.verticalLinesInterval);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   208
        window.clearInterval(this.cleanInterval);
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   209
    };
79ae42ad97d4 optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   210
85
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
}
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
eff9460bd4f2 add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
module.exports = PianoRoll;