var sock = null;
var ellog = null;
function log(m) {
ellog.innerHTML += m + '\n';
ellog.scrollTop = ellog.scrollHeight;
};
//Config vars
var sceneWidth = 1000;
var sceneHeight = 128 * 4; // multiple of 128 because of 128 levels in midi signals -> better look
var sceneBgColor = 0x66FF99;
var manualFramerate = 24;
var pixelsPerSecond = 50; // nb of pixels per second
var lineInterval = 2000; // means line every 2 seconds
var nbLines = -1;
//var speed = 1; // container -x position at each frame. Speed = 1 ~ 100px for 2 seconds
var zeroTime = new Date("2014-10-06T12:16:43.000000Z").getTime();
var noteDict = {};
var drawPianoNotes = [1,0,1,0,1,1,0,1,0,1,0,1];//Do, Do#, Ré, Ré#, Mi, Fa, Fa#, Sol, Sol#, La, La#, Si
//create an new instance of a pixi stage
var stage = new PIXI.Stage(sceneBgColor);
//create a renderer instance.
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight);
//add the renderer view element to the DOM
document.body.appendChild(renderer.view);
//requestAnimFrame( animate );
//Draw 127 lines
var delta = sceneHeight / 128;
for(var i=1;i<128;i++){
var graphics = new PIXI.Graphics();
graphics.beginFill(0xFFFF00);
graphics.lineStyle(1, 0xAAAAAA);
var y = delta * i;
graphics.moveTo(0, y);
graphics.lineTo(sceneWidth, y);
graphics.endFill();
stage.addChild(graphics);
}
//Draw 127 lines
for(var i=0;i<128;i++){
var graphics = new PIXI.Graphics();
var color = drawPianoNotes[i%12]==1 ? 0xFFFFFF : 0x000000;
if(i==60){
color = 0xFFD700;
}
graphics.beginFill(color);
graphics.lineStyle(1, 0xAAAAAA);
var y = sceneHeight - delta * (i+1);
graphics.drawRect(0, y, 20, delta);
graphics.endFill();
stage.addChild(graphics);
}
var uberContainer = new PIXI.DisplayObjectContainer();
uberContainer.position.x = Math.floor(sceneWidth*4/5);
uberContainer.position.y = 0;
stage.addChild(uberContainer);
var container = new PIXI.DisplayObjectContainer();
container.position.x = 0;
container.position.y = 0;
uberContainer.addChild(container);
//create a texture from an image path
var texture = PIXI.Texture.fromImage("bunny.png");
/*function animate() {
requestAnimFrame( animate );
// just for fun, lets rotate mr rabbit a little
//bunny.rotation += 0.1;
container.x -= speed;
//console.log("container.x = ", container.x);
// render the stage
renderer.render(stage);
}*/
function moveContainer() {
container.x -= pixelsPerSecond/manualFramerate;
renderer.render(stage);
}
function addBunny(data){
var note = data.content[3];
var velocity = data.content[4];
if(velocity===0){
if(note in noteDict){
// We close the note
var beginTime = new Date(noteDict[note].ts).getTime();
var beginDelta = beginTime - zeroTime;
var durationDelta = new Date(data.ts).getTime() - beginTime;
var beginX = beginDelta * pixelsPerSecond / 1000;
var width = durationDelta * pixelsPerSecond / 1000;
// We draw the rectangle
var graphics = new PIXI.Graphics();
graphics.beginFill(0x0000AA, (noteDict[note].velocity / 128));
var y = (128-note) * sceneHeight / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0
graphics.drawRect(beginX, y, width, sceneHeight / 128);
graphics.endFill();
container.addChild(graphics);
delete noteDict[note];
}
}
else{
noteDict[note] = {ts: data.ts, velocity:velocity};
}
}
window.onload = function() {
ellog = document.getElementById('log');
var wsuri;
if (window.location.protocol === "file:") {
wsuri = "ws://127.0.0.1:8090/broadcast";
} else {
wsuri = "ws://" + window.location.hostname + ":8090/broadcast";
}
if ("WebSocket" in window) {
sock = new WebSocket(wsuri);
} else if ("MozWebSocket" in window) {
sock = new MozWebSocket(wsuri);
} else {
log("Browser does not support WebSocket!");
window.location = "http://autobahn.ws/unsupportedbrowser";
}
if (sock) {
sock.onopen = function() {
log("Connected to " + wsuri);
}
sock.onclose = function(e) {
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
sock = null;
}
sock.onmessage = function(e) {
log("Got message: " + e.data);
addBunny(JSON.parse(e.data));
}
}
};
function addLine(){
nbLines++;
var graphics = new PIXI.Graphics();
var x = nbLines * (lineInterval/1000) * pixelsPerSecond;
//console.log("nbLines = ",nbLines, "x = ", x);
graphics.beginFill(0xFFFF00);
graphics.lineStyle(1, 0x444444);
graphics.moveTo(x, 0);
graphics.lineTo(x, sceneHeight);
graphics.endFill();
container.addChild(graphics);
// Add text
var totalSec = nbLines * lineInterval / 1000;
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
var t = new PIXI.Text(timeStr, { font: "10pt Arial", fill: "#444444" });
t.x = x + 2;
t.y = sceneHeight - 20;
container.addChild(t);
}
var moveInterval = window.setInterval(moveContainer, 1000/manualFramerate);
var linesInterval = window.setInterval(addLine, lineInterval);