client/annotviz/app/js/pianoroll.js
changeset 85 eff9460bd4f2
child 89 b4bd49f01837
child 93 79ae42ad97d4
equal deleted inserted replaced
84:d7c5bffdd2d8 85:eff9460bd4f2
       
     1 /**
       
     2 * js/pianoroll.js
       
     3 *
       
     4 * pianoroll basic component
       
     5 *
       
     6 */
       
     7 
       
     8 'use strict';
       
     9 
       
    10 var PIXI = require('pixi');
       
    11 var randomColor = require('randomColor');
       
    12 
       
    13 function PianoRoll(parentContainer, xInit, yInit, height, linesDown, pixelsPerSecond, width, noteColors, colorsReg, lineColor, lineInterval, offsetMusic, noteHeight){
       
    14     var _this = this;
       
    15     this.container = new PIXI.DisplayObjectContainer();
       
    16     this.container.position.x = xInit;
       
    17     this.container.position.y = yInit;
       
    18     parentContainer.addChild(this.container);
       
    19 
       
    20     this.linesDown = linesDown;
       
    21     this.height = height;
       
    22     this.pixelsPerSecond = pixelsPerSecond;
       
    23     this.width = width;
       
    24     this.noteColors = noteColors;
       
    25     this.colorsReg = colorsReg || {};
       
    26     this.lineColor = lineColor;
       
    27     this.lineInterval = lineInterval;
       
    28     this.offsetMusic = offsetMusic || 0;
       
    29     this.noteHeight = noteHeight;
       
    30 
       
    31     this.addNote = function(note, startTime, duration, velocity, canal){
       
    32         //console.log("coucou 1", note, timeFromZero, ts, velocity, pixelsPerSecond, container, prHeight);
       
    33         var beginX = (this.offsetMusic + startTime) * this.pixelsPerSecond / 1000;
       
    34         var width = duration * this.pixelsPerSecond / 1000;
       
    35         if((beginX+width) <  (Math.abs(this.container.x) - this.width)) {
       
    36             // not visible. do nothing
       
    37             return;
       
    38         }
       
    39         // We draw the rectangle
       
    40         var graphics = new PIXI.Graphics();
       
    41         //console.log("beginX = ", beginX, "canal = ", canal, "color = ", noteColor[canal], "width = ", width, "note = ", note, "velocity = ", velocity);
       
    42         var color = this.colorsReg[canal];
       
    43         if(typeof(color) === 'undefined') {
       
    44             var colorsRegSize = Object.keys(this.colorsReg).length;
       
    45             if(colorsRegSize < this.noteColors.length) {
       
    46                 color = this.colorsReg[canal] = this.noteColors[colorsRegSize];
       
    47             }
       
    48             else {
       
    49                 color = this.colorsReg[canal] = parseInt(randomColor({ luminosity: 'light', hue: 'random', format:'hex'}).replace(/^#/, ''), 16);
       
    50             }
       
    51         }
       
    52         graphics.beginFill(color, (velocity / 128));
       
    53         var y = (128-note) * this.height / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0
       
    54         graphics.drawRect(0, Math.floor(y - (noteHeight/2) + ((this.height / 128)/2)), width, noteHeight);
       
    55         graphics.endFill();
       
    56         graphics.x = beginX;
       
    57         this.container.addChild(graphics);
       
    58     };
       
    59 
       
    60     this.addLine = function(ts){
       
    61         var graphics = new PIXI.Graphics();
       
    62         var x = -this.container.x;
       
    63         graphics.beginFill(0xFFFF00);
       
    64         graphics.lineStyle(1, this.lineColor);
       
    65         var y = this.linesDown ? this.height - 20 : 0;
       
    66         graphics.moveTo(x, y);
       
    67         graphics.lineTo(x, y + 20);
       
    68         graphics.endFill();
       
    69         this.container.addChild(graphics);
       
    70         // Add text
       
    71         //var totalSec = lineNb * this.lineInterval / 1000;
       
    72         var hours = ts.getHours();
       
    73         var minutes =ts.getMinutes();
       
    74         var seconds = ts.getSeconds();
       
    75         var timeStr = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds  < 10 ? '0' + seconds : seconds);
       
    76 
       
    77         var fontObj = { font: '10pt Arial', fill: '#444444' };
       
    78         var t = new PIXI.Text(timeStr, fontObj);
       
    79         t.x = x + 2;
       
    80         t.y = this.linesDown ? this.height - 15 : 2;
       
    81         this.container.addChild(t);
       
    82     };
       
    83 
       
    84     this.moveTo = function(diffTime){
       
    85         this.container.x = Math.floor(diffTime*this.pixelsPerSecond);
       
    86     };
       
    87 
       
    88     this.removePassedObjets = function(){
       
    89         var nbChilds = _this.container.children.length;
       
    90         var i = 0, childIsNowDisplayed = false, childrenToRemove = [];
       
    91         while(i<nbChilds && !childIsNowDisplayed){
       
    92             var child = _this.container.children[i++];
       
    93             //console.log("remove ? ", child.x, child.width, ((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)));
       
    94             if(typeof(child) == 'undefined') {
       
    95                 continue;
       
    96             }
       
    97             if((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)){
       
    98                 childrenToRemove.push(child);
       
    99                 //console.log("    remove !!!");
       
   100             }
       
   101             else {
       
   102                 childIsNowDisplayed = true;
       
   103                 //console.log("    childIsNowDisplayed");
       
   104             }
       
   105         }
       
   106         childrenToRemove.forEach(function(child) {
       
   107             _this.container.removeChild(child);
       
   108         });
       
   109         //console.log("before : ", nbChilds, ", after : ", _this.container.children.length);
       
   110     };
       
   111 
       
   112     // remove notes each scene width
       
   113     //var removeInterval = window.setInterval(this.removePassedObjets, 1000 * sceneWidth / this.pixelsPerSecond );
       
   114     window.setInterval(this.removePassedObjets, 1000 * this.width / this.pixelsPerSecond );
       
   115 
       
   116 }
       
   117 
       
   118 module.exports = PianoRoll;