client/annotviz/app/js/doubleroll.js
author ymh <ymh.work@gmail.com>
Tue, 20 Jan 2015 11:57:44 +0100
changeset 98 72d767c5142d
parent 95 806739a26858
child 100 0d7dac03c1a0
permissions -rw-r--r--
refactor and make work annotsroll
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 document: false */
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
'use strict';
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
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
var PIXI = require('pixi');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
var _ = require('lodash');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
var PianoRoll = require('./pianoroll.js');
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
var defaultConfig = {
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    18
    orientation: 'horizontal',
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    19
    externalRefresh: false,
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    20
    logger: undefined,
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    sceneWidth: 1920,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    pianorolls : [
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
      {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        height: 435,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        timeWidth: 10,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        lineInterval: 5000,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        noteHeight: undefined
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
      },
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
      {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        height: 645,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        timeWidth: 60,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        lineInterval: 5000,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        noteHeight: undefined
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
      },
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    ],
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    framerate: 25,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    offsetMusic: false,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    sceneBgColor: 0xFFFFFF,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    lineColor: 0x444444,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    lineFillColor: 0xFFFF00,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    noteColors: [0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991],
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    canvasContainer: 'canvasContainer',
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    timeContainer: 'timeStarted',
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    noteHeight: undefined,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    zeroShift: 0.9,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    timeWidth: 60,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    lineInterval: 5000,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
//    wsUri: undefined,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
//    eventCode: undefined
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
};
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
function DoubleRoll(options) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    var _this = this;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    var opts = _(options).defaults(defaultConfig).value();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    58
    var orientation = opts.orientation;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    59
    var isHorizontal = (orientation !== 'vertical');
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    60
    var externalRefresh = opts.externalRefresh;
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
    61
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    this.logger = opts.logger;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    this.lineColor = opts.lineColor;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    this.lineFillColor = opts.lineFillColor;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    this.framerate = opts.framerate;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    this.offsetMusic = opts.offsetMusic;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    this.noteColors = opts.noteColors;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    var noteHeight = opts.noteHeight;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    var sceneBgColor = opts.sceneBgColor;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    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
    72
    var timeWidth = opts.timeWidth;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    var lineInterval = opts.lineInterval;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    var offsetMusic = opts.offsetMusic;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    var sceneWidth = opts.sceneWidth;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    var canvasContainer = opts.canvasContainer;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    var timeContainer = opts.timeContainer;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    var zeroShift = opts.zeroShift;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    82
    var ws = opts.ws;
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    var colorsReg = {};
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
    //create an new instance of a pixi stage
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    87
    this.stage = new PIXI.Stage(sceneBgColor);
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    //create a renderer instance.
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    var uberContainer = new PIXI.DisplayObjectContainer();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
    uberContainer.x = Math.floor(sceneWidth*zeroShift);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    uberContainer.y = 0;
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    94
    this.stage.addChild(uberContainer);
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    var pianorollList = [];
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
    var pianorollOptions = {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        parentContainer: uberContainer,
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   100
        orientation: orientation,
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        xInit: 0,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        width: sceneWidth,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        noteColors: this.noteColors,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        colorsReg: colorsReg,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        lineColor: this.lineColor,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        lineInterval: lineInterval,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        offsetMusic: offsetMusic,
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
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    var yInit = 0;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    var linesDown = true;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    _(opts.pianorolls).forEach(function(prDef, i) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        var prNoteHeight = noteHeight || prDef.noteHeight || prDef.height / 128;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        var prTimeWidth = prDef.timeWidth || timeWidth;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        pianorollList.push(new PianoRoll(_({
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
            yInit: yInit,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            height: prDef.height,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
            linesDown: linesDown,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            pixelsPerSecond: Math.floor(sceneWidth / prTimeWidth),
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            noteHeight: prNoteHeight,
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
            lineInterval: prDef.lineInterval
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        }).defaults(pianorollOptions).value()));
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        yInit += prDef.height;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        linesDown = !linesDown;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        if(i<(opts.pianorolls.length-1)) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            var lineGraphics = new PIXI.Graphics()
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                .beginFill(_this.lineFillColor)
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                .lineStyle(1, _this.lineColor)
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   130
                .moveTo(Math.floor(sceneWidth*zeroShift), yInit)
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   131
                .lineTo(-sceneWidth - Math.floor(sceneWidth*zeroShift), yInit)
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
                .endFill();
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   133
            uberContainer.addChild(lineGraphics);
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
95
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   137
    if(!isHorizontal) {
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   138
        uberContainer.rotation = Math.PI/2;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   139
        uberContainer.y = sceneHeight;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   140
        uberContainer.x = sceneWidth;
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   141
    }
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   142
806739a26858 add vertical version for pianoroll
ymh <ymh.work@gmail.com>
parents: 94
diff changeset
   143
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    this.init = function() {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
        if(typeof(canvasContainer) === 'string') {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
            canvasContainer = document.getElementById(canvasContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
        }
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        if(typeof(timeContainer) === 'string') {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
            timeContainer = document.getElementById(timeContainer);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        }
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
        canvasContainer.appendChild(renderer.view);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   155
        ws.message(function(data) {
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   156
            _this.addNotes(data);
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   157
        });
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
    this.addNotes = function(data) {
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
        pianorollList.forEach(function(c) {
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   165
            c.addNoteRaw(data);
94
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
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   169
    this.refreshStage = this.refresh = function() {
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
            c.move();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        });
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   173
        renderer.render(this.stage);
94
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
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    // Init page and intervals
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
    var refreshInterval;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    var refreshTimeInterval;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    var startTs;
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
    this.updateTime = function(){
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        var nbSec = (Date.now() - startTs) / 1000;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        var hours = Math.floor( nbSec / 3600 ) % 24;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        var minutes = Math.floor( nbSec / 60 ) % 60;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        var seconds = Math.floor(nbSec % 60);
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        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
   187
        timeContainer.innerHTML = timeStr;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
    this.start = function() {
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
        startTs = Date.now();
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   193
        if(!externalRefresh) {
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   194
            refreshInterval = setInterval(function() {_this.refreshStage();}, 1000/this.framerate);
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   195
        }
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   196
        refreshTimeInterval = setInterval(function() {_this.updateTime();}, 1000);
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
            c.start();
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
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    this.stop = function() {
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   203
        if(!externalRefresh) {
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   204
            clearInterval(refreshInterval);
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   205
        }
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   206
        clearInterval(refreshTimeInterval);
94
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
        pianorollList.forEach(function(c) {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
            c.stop();
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
        });
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    };
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
    this.log = function(m) {
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   214
        if(this.logger) {
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
   215
            this.logger.log(m);
94
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
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    return this;
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
}
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
module.exports = {
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    DoubleRoll: DoubleRoll
e0e514c5470f create doubleroll component and modularize app
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
};