client/annotviz/app/js/pianoroll.js
changeset 146 192d7d7f7bb4
parent 145 a8052f8ab19c
child 162 44320144951a
--- a/client/annotviz/app/js/pianoroll.js	Fri Apr 10 16:45:34 2015 +0200
+++ b/client/annotviz/app/js/pianoroll.js	Mon Apr 13 17:11:47 2015 +0200
@@ -36,7 +36,8 @@
     this.noteHeight = options.noteHeight;
     this.noteDict = {};
     this.startTs = options.startTs || Date.now();
-    
+    this.dynamicRange = options.dynamicRange;
+    this.initRange = options.range;
     this.range = options.range;
     
 
@@ -65,6 +66,7 @@
 
     this.getNoteRect = function(note, x, y, color, alpha, width, height) {
         var graphics = new PIXI.Graphics();
+        graphics.note = note;
         graphics.beginFill(color, alpha);
         graphics.drawRect(0, 0, width, height);
         graphics.endFill();
@@ -72,7 +74,6 @@
         graphics.y = y;
         graphics.width = width;
         graphics.height = height;
-        graphics.note = note;
         return graphics;
     };
 
@@ -126,17 +127,15 @@
                     // not visible. do nothing
                     return;
                 }
-            	if (this.range.bottom > note || note > this.range.top){
-            		if (note < this.range.bottom)
-            			this.range.bottom = note;
-            		else
-            			this.range.top = note;
-            		this.noteHeight = this.height / (this.range.top - this.range.bottom);
-            		this.rescaleScene();
+            	if (this.dynamicRange && (this.range.bottom > note || note > this.range.top)){
+            		var newScale = {};
+            		newScale['bottom'] = Math.min(note, this.range.bottom);
+            		newScale['top'] = Math.max(note, this.range.top);
+            		this.rescaleScene(newScale);
             	}
-            	var y = Math.floor(((this.range.top-this.range.bottom)-(note-this.range.bottom)+0.5) * (this.height / (this.range.top-this.range.bottom)) - (this.noteHeight/2));
+            	var y = Math.floor(((this.range.top-this.range.bottom)-(note-this.range.bottom)+0.5) * (this.noteHeight) - (this.noteHeight/2));
                 var color = this.getColor(channel);
-                //128 hardecoded because its the maximum velocity of MIDI
+                
                 var alpha = (noteVelocity / 128);
 
                 graphics = this.getNoteRect(note, x, y, color, alpha, width, this.noteHeight);
@@ -152,18 +151,31 @@
         }
     };
     
-    this.rescaleScene = function(){
+    //rescale scene in case a note out of range is added or a note out of the range is removed
+    this.rescaleScene = function(newScale){
     	var _this = this;
     	var childrenToUpdate = [];
+    	var top = this.initRange.top;
+    	var bottom = this.initRange.bottom;
     	_(_this.container.children).forEach(function(child) {
-            return typeof(child) === 'undefined' ||
-                (!isHidden(child) && childrenToUpdate.push(child));
+    		if (typeof(child) !== 'undefined' && child.note && !isHidden(child)){
+    			top = Math.max(child.note, top);
+    			bottom = Math.min(child.note, bottom);
+    			return childrenToUpdate.push(child);
+    		}
         });
+    	if (newScale){
+    		this.range = newScale;
+    	}else{
+    		this.range.top = top; 
+    		this.range.bottom = bottom; 
+    	}
+    	this.noteHeight = this.height / (this.range.top - this.range.bottom + 1);
     	childrenToUpdate.forEach(function(child) {
-            child.y = Math.floor(((_this.range.top-_this.range.bottom)-(child.note-_this.range.bottom)+0.5) * (_this.height / (_this.range.top-_this.range.bottom)) - (_this.noteHeight/2));
-            child.height = _this.noteHeight;
+			child.y = Math.floor(((_this.range.top-_this.range.bottom)-(child.note-_this.range.bottom)+0.5) * (_this.noteHeight) - (_this.noteHeight/2));
+			child.height = _this.noteHeight;
         });
-    }
+    };
 
     this.addLine = function(ts){
 
@@ -223,13 +235,21 @@
 
     this.removePassedObjets = function(){
         var childrenToRemove = [];
+        var rescale = false;
         _(_this.container.children).forEach(function(child) {
             return typeof(child) === 'undefined' ||
                 (isHidden(child) && childrenToRemove.push(child));
         });
         childrenToRemove.forEach(function(child) {
+        	if (_this.dynamicRange && (_this.range.bottom === child.note || child.note === _this.range.top)){
+                rescale = true;
+            }
             _this.container.removeChild(child);
         });
+        //externalize this test to avoid repeated call to the function rescaleScene in the previous loop
+        if (rescale){
+            _this.rescaleScene();
+        }
     };
 
     this.start = function() {
@@ -243,12 +263,10 @@
     };
 
     this.stop = function() {
-        //window.clearInterval(this.moveInterval);
         clearInterval(this.verticalLinesInterval);
         clearInterval(this.cleanInterval);
     };
 
-
 }
 
 module.exports = PianoRoll;