client/annotviz/app/js/doubleroll.js
author ymh <ymh.work@gmail.com>
Mon, 19 Jan 2015 13:31:28 +0100
changeset 95 806739a26858
parent 94 e0e514c5470f
child 98 72d767c5142d
permissions -rw-r--r--
add vertical version for pianoroll
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/**
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
* scripts/doubleroll.js
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
*
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
* This is the starting point for your application.
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
* Take a look at http://browserify.org/ for more info
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
*/
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
/* global window: false */
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/* global document: false */
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
/* global WebSocket: false */
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
/* global MozWebSocket: false */
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
'use strict';
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
var PIXI = require('pixi');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
var _ = require('lodash');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
var PianoRoll = require('./pianoroll.js');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
var NTP_EPOCH_DELTA = 2208988800; //c.f. RFC 868
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
var defaultConfig = {
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    23
    orientation: 'horizontal',
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    logger: false,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    sceneWidth: 1920,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    pianorolls : [
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
      {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        height: 435,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        timeWidth: 10,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        lineInterval: 5000,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        noteHeight: undefined
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
      },
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
      {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        height: 645,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        timeWidth: 60,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        lineInterval: 5000,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        noteHeight: undefined
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
      },
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    ],
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    framerate: 25,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    offsetMusic: false,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    sceneBgColor: 0xFFFFFF,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    lineColor: 0x444444,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    lineFillColor: 0xFFFF00,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    noteColors: [0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991],
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    canvasContainer: 'canvasContainer',
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    logContainer: 'log',
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    timeContainer: 'timeStarted',
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    noteHeight: undefined,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    zeroShift: 0.9,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    timeWidth: 60,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    lineInterval: 5000,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    annotationChannel: 'PIANOROLL'
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
//    wsUri: undefined,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
//    eventCode: undefined
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
};
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
function DoubleRoll(options) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    var _this = this;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    var opts = _(options).defaults(defaultConfig).value();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    64
    var orientation = opts.orientation;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    65
    var isHorizontal = (orientation !== 'vertical');
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    66
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    this.logger = opts.logger;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    this.lineColor = opts.lineColor;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    this.lineFillColor = opts.lineFillColor;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    this.framerate = opts.framerate;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    this.offsetMusic = opts.offsetMusic;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    this.noteColors = opts.noteColors;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    var noteHeight = opts.noteHeight;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    var sceneBgColor = opts.sceneBgColor;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    var sceneHeight = opts.sceneHeight || _(opts.pianorolls).reduce(function(s,p) { return s + p.height; }, 0);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    var timeWidth = opts.timeWidth;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    var lineInterval = opts.lineInterval;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    var offsetMusic = opts.offsetMusic;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    var sceneWidth = opts.sceneWidth;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    var canvasContainer = opts.canvasContainer;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    var logContainer = opts.logContainer;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    var timeContainer = opts.timeContainer;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    var zeroShift = opts.zeroShift;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    var eventCode = opts.eventCode;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    var annotationChannel = opts.annotationChannel;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    var wsUri = opts.wsUri;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    if(!wsUri) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        if (window.location.protocol === 'file:') {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            wsUri = 'ws://127.0.0.1:8090/broadcast';
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        else {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
            wsUri = 'ws://' + window.location.hostname + ':8090/broadcast';
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        wsUri += '?channel='+annotationChannel+'&event_code='+eventCode;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    var colorsReg = {};
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
    //create an new instance of a pixi stage
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    var stage = new PIXI.Stage(sceneBgColor);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
    //create a renderer instance.
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    var uberContainer = new PIXI.DisplayObjectContainer();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    uberContainer.x = Math.floor(sceneWidth*zeroShift);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    uberContainer.y = 0;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    stage.addChild(uberContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    var pianorollList = [];
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
    var pianorollOptions = {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        parentContainer: uberContainer,
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   118
        orientation: orientation,
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        xInit: 0,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        width: sceneWidth,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        noteColors: this.noteColors,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        colorsReg: colorsReg,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        lineColor: this.lineColor,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        lineInterval: lineInterval,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
        offsetMusic: offsetMusic,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    var yInit = 0;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    var linesDown = true;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    _(opts.pianorolls).forEach(function(prDef, i) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        var prNoteHeight = noteHeight || prDef.noteHeight || prDef.height / 128;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        var prTimeWidth = prDef.timeWidth || timeWidth;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
        pianorollList.push(new PianoRoll(_({
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
            yInit: yInit,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            height: prDef.height,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
            linesDown: linesDown,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
            pixelsPerSecond: Math.floor(sceneWidth / prTimeWidth),
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
            noteHeight: prNoteHeight,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
            lineInterval: prDef.lineInterval
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        }).defaults(pianorollOptions).value()));
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        yInit += prDef.height;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
        linesDown = !linesDown;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
        if(i<(opts.pianorolls.length-1)) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
            var lineGraphics = new PIXI.Graphics()
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
                .beginFill(_this.lineFillColor)
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
                .lineStyle(1, _this.lineColor)
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   148
                .moveTo(Math.floor(sceneWidth*zeroShift), yInit)
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   149
                .lineTo(-sceneWidth - Math.floor(sceneWidth*zeroShift), yInit)
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
                .endFill();
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   151
            uberContainer.addChild(lineGraphics);
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   155
    if(!isHorizontal) {
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   156
        uberContainer.rotation = Math.PI/2;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   157
        uberContainer.y = sceneHeight;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   158
        uberContainer.x = sceneWidth;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   159
    }
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   160
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   161
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
    this.init = function() {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
        if(typeof(canvasContainer) === 'string') {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
            canvasContainer = document.getElementById(canvasContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
        if(typeof(logContainer) === 'string') {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            logContainer = document.getElementById(logContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        if(typeof(timeContainer) === 'string') {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
            timeContainer = document.getElementById(timeContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        if(!this.logger){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
            document.body.removeChild(logContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
            logContainer = undefined;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        var sock;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        canvasContainer.appendChild(renderer.view);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        if ('WebSocket' in window) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
            sock = new WebSocket(wsUri);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        } else if ('MozWebSocket' in window) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
            sock = new MozWebSocket(wsUri);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        } else {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
            this.log('Browser does not support WebSocket!');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
            window.location = 'http://autobahn.ws/unsupportedbrowser';
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        if (!sock) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
            return;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
        sock.onopen = function(){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
            if(_this.logger){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
                _this.log('Connected to ' + _this.wsUri);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
            }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        sock.onclose = function(e) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
            if(_this.logger){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
                _this.log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = \'' + e.reason + '\')');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
            }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
            sock = null;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
        };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
        sock.onmessage = function(e) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
            var dataJson = JSON.parse(e.data);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
            if(_this.logger){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
                var dataDate = new Date((dataJson.content[0]-NTP_EPOCH_DELTA)*1000);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
                _this.log('Got message: ' + e.data + ' - ' + dataDate.toISOString());
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
            }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
            _this.addNotes(dataJson);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
        };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    this.addNotes = function(data) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
        var note = data.content[3];
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        var velocity = data.content[4];
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        var ts = (data.content[0] - NTP_EPOCH_DELTA)*1000;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        var channel = data.content[2];
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        var sessionTs = data.content[1];
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
            c.addNote(note, ts, sessionTs, velocity, channel, 0);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
    this.refreshStage = function() {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
            c.move();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        renderer.render(stage);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    // Init page and intervals
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    var refreshInterval;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
    var refreshTimeInterval;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    var startTs;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    this.updateTime = function(){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        var nbSec = (Date.now() - startTs) / 1000;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        var hours = Math.floor( nbSec / 3600 ) % 24;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
        var minutes = Math.floor( nbSec / 60 ) % 60;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        var seconds = Math.floor(nbSec % 60);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        var timeStr = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds  < 10 ? '0' + seconds : seconds);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
        timeContainer.innerHTML = timeStr;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
    this.start = function() {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
        startTs = Date.now();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        refreshInterval = window.setInterval(function() {_this.refreshStage();}, 1000/this.framerate);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        refreshTimeInterval = window.setInterval(function() {_this.updateTime();}, 1000);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
            c.start();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
    this.stop = function() {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        window.clearInterval(refreshInterval);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        window.clearInterval(refreshTimeInterval);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
            c.stop();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    this.log = function(m) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
        if(this.logger){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
            this.logContainer.innerHTML += m + '\n';
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
            this.logContainer.scrollTop = logContainer.scrollHeight;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
    window.onload = function() {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        _this.init();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        _this.start();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
    return this;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
}
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
module.exports = {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
    DoubleRoll: DoubleRoll
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
};