author | cavaliet |
Tue, 28 Oct 2014 15:31:55 +0100 | |
changeset 77 | 1b578edc1578 |
parent 76 | 029cdbeebf03 |
child 79 | bd2f2c3f205c |
permissions | -rw-r--r-- |
28 | 1 |
// Config vars |
2 |
var logger = false; |
|
3 |
var sceneWidth = 1920; |
|
4 |
var prHeight1 = 435; |
|
5 |
var prHeight2 = 645; |
|
6 |
var sceneHeight = prHeight1 + prHeight2; |
|
7 |
var sceneBgColor = 0xFFFFFF; |
|
8 |
var lineColor = 0x444444; |
|
46 | 9 |
var pixelsPerSecond1 = Math.floor(sceneWidth / 10); // nb of pixels per second |
28 | 10 |
var manualFramerate = pixelsPerSecond1 / 4; |
11 |
var pixelsPerSecond2 = Math.floor(sceneWidth / 60); // nb of pixels per second |
|
12 |
var lineInterval = 5000; // means line every 5 seconds |
|
13 |
var nbLines = -1; |
|
14 |
var noteHeight = 110; |
|
58 | 15 |
var noteColor = [0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991, |
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
16 |
0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991, |
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
17 |
0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991, |
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
18 |
0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991, |
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
19 |
0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991, |
58 | 20 |
0xB90000, 0x4BDD71, 0xAF931E, 0x1C28BA, 0x536991]; |
72 | 21 |
// Vars |
22 |
var noteDict = []; |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
23 |
// Timecode method |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
24 |
var timePageLoaded = Date.now(); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
25 |
var offsetMusic = false; |
28 | 26 |
|
27 |
||
28 |
//create an new instance of a pixi stage |
|
29 |
var stage = new PIXI.Stage(sceneBgColor); |
|
30 |
||
31 |
//create a renderer instance. |
|
32 |
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight); |
|
33 |
||
34 |
//add the renderer view element to the DOM |
|
35 |
document.getElementById("canvasContainer").appendChild(renderer.view); |
|
36 |
||
37 |
var uberContainer = new PIXI.DisplayObjectContainer(); |
|
38 |
uberContainer.position.x = Math.floor(sceneWidth*9/10); |
|
39 |
uberContainer.position.y = 0; |
|
40 |
stage.addChild(uberContainer); |
|
41 |
||
42 |
||
74 | 43 |
function PianoRoll(parentContainer, xInit, yInit, height, linesDown, pixelsPerSecond, width){ |
44 |
var _this = this; |
|
72 | 45 |
this.container = new PIXI.DisplayObjectContainer(); |
46 |
this.container.position.x = xInit; |
|
47 |
this.container.position.y = yInit; |
|
48 |
parentContainer.addChild(this.container); |
|
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
49 |
|
72 | 50 |
this.linesDown = linesDown; |
51 |
this.height = height; |
|
52 |
this.pixelsPerSecond = pixelsPerSecond; |
|
74 | 53 |
this.width = width; |
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
54 |
|
72 | 55 |
this.addNote = function(note, startTime, duration, velocity, canal){ |
56 |
//console.log("coucou 1", note, timeFromZero, ts, velocity, pixelsPerSecond, container, prHeight); |
|
57 |
var beginX = (offsetMusic + startTime) * this.pixelsPerSecond / 1000; |
|
58 |
var width = duration * this.pixelsPerSecond / 1000; |
|
59 |
// We draw the rectangle |
|
60 |
var graphics = new PIXI.Graphics(); |
|
61 |
//console.log("beginX = ", beginX, "canal = ", canal, "color = ", noteColor[canal], "width = ", width, "note = ", note, "velocity = ", velocity); |
|
62 |
graphics.beginFill(noteColor[canal], (velocity / 128)); |
|
63 |
var y = (128-note) * this.height / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
|
74 | 64 |
graphics.drawRect(0, Math.floor(y - (noteHeight/2) + ((this.height / 128)/2)), width, noteHeight); |
72 | 65 |
graphics.endFill(); |
74 | 66 |
graphics.x = beginX; |
72 | 67 |
this.container.addChild(graphics); |
68 |
}; |
|
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
69 |
|
72 | 70 |
this.addLine = function(lineNb){ |
71 |
var graphics = new PIXI.Graphics(); |
|
72 |
var x = -this.container.x; |
|
73 |
graphics.beginFill(0xFFFF00); |
|
74 |
graphics.lineStyle(1, lineColor); |
|
75 |
var y = this.linesDown ? this.height - 20 : 0; |
|
76 |
graphics.moveTo(x, y); |
|
77 |
graphics.lineTo(x, y + 20); |
|
78 |
graphics.endFill(); |
|
79 |
this.container.addChild(graphics); |
|
80 |
// Add text |
|
81 |
var totalSec = lineNb * lineInterval / 1000; |
|
82 |
var hours = parseInt( totalSec / 3600 ) % 24; |
|
83 |
var minutes = parseInt( totalSec / 60 ) % 60; |
|
84 |
var seconds = totalSec % 60; |
|
85 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
86 |
var fontObj = { font: "10pt Arial", fill: "#444444" }; |
|
87 |
var t = new PIXI.Text(timeStr, fontObj); |
|
88 |
t.x = x + 2; |
|
89 |
t.y = this.linesDown ? this.height - 15 : 2; |
|
90 |
this.container.addChild(t); |
|
74 | 91 |
}; |
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
92 |
|
72 | 93 |
this.moveTo = function(diffTime){ |
94 |
this.container.x = Math.floor(diffTime*this.pixelsPerSecond); |
|
95 |
}; |
|
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
96 |
|
74 | 97 |
this.removePassedObjets = function(){ |
98 |
var nbChilds = _this.container.children.length; |
|
99 |
var i = 0, childIsNowDisplayed = false; |
|
100 |
while(i<nbChilds && !childIsNowDisplayed){ |
|
101 |
var child = _this.container.children[0]; |
|
102 |
//console.log("remove ? ", child.x, child.width, ((child.x + child.width) < (Math.abs(_this.container.x) - _this.width))); |
|
103 |
if((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)){ |
|
104 |
_this.container.removeChild(child); |
|
105 |
//console.log(" remove !!!"); |
|
106 |
} |
|
107 |
else{ |
|
108 |
childIsNowDisplayed = true; |
|
109 |
//console.log(" childIsNowDisplayed"); |
|
110 |
} |
|
111 |
i++; |
|
112 |
} |
|
77 | 113 |
//console.log("before : ", nbChilds, ", after : ", _this.container.children.length); |
74 | 114 |
}; |
77 | 115 |
|
116 |
// remove notes each scene width |
|
117 |
var removeInterval = window.setInterval(this.removePassedObjets, 1000 * sceneWidth / this.pixelsPerSecond ); |
|
118 |
|
|
72 | 119 |
} |
120 |
||
121 |
// Init containers |
|
77 | 122 |
var containerList = []; |
123 |
containerList.push(new PianoRoll(uberContainer, 0, 0, prHeight1, true, pixelsPerSecond1, sceneWidth)); |
|
124 |
containerList.push(new PianoRoll(uberContainer, 0, prHeight1, prHeight2, false, pixelsPerSecond2, sceneWidth)); |
|
28 | 125 |
|
126 |
// Line between two containers |
|
127 |
var graphics = new PIXI.Graphics(); |
|
128 |
graphics.beginFill(0xFFFF00); |
|
129 |
graphics.lineStyle(1, lineColor); |
|
130 |
graphics.moveTo(0, prHeight1); |
|
131 |
graphics.lineTo(sceneWidth, prHeight1); |
|
132 |
graphics.endFill(); |
|
133 |
stage.addChild(graphics); |
|
134 |
||
49 | 135 |
|
136 |
function replaceContainers(){ |
|
137 |
var diff = (Date.now() - timePageLoaded)/1000;// nb of seconds since page loaded |
|
138 |
//console.log("replace ! diff1 = ", container1.x - Math.floor(-diff*pixelsPerSecond1), ", diff 2 = ", container2.x - Math.floor(-diff*pixelsPerSecond2)); |
|
77 | 139 |
var nb = containerList.length; |
140 |
for(var i=0;i<nb;i++){ |
|
141 |
containerList[i].moveTo(-diff); |
|
142 |
} |
|
46 | 143 |
renderer.render(stage); |
144 |
} |
|
28 | 145 |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
146 |
function addNotes(data){ |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
147 |
if(!offsetMusic){ |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
148 |
// get difference between the current note timecode and my zero to set the difference between the canvas's zero and the music's zero |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
149 |
// in order to place in real time |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
150 |
var now = Date.now(); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
151 |
var timeBetweenNowAndStart = now - timePageLoaded; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
152 |
offsetMusic = timeBetweenNowAndStart - data.content[1]; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
153 |
//console.log("start: ", timePageLoaded, ", now: ", now, ", timeBetweenNowAndStart = ", timeBetweenNowAndStart, ", offsetMusic = ", offsetMusic); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
154 |
} |
28 | 155 |
var note = data.content[3]; |
156 |
var velocity = data.content[4]; |
|
157 |
if(velocity===0){ |
|
54 | 158 |
if(typeof noteDict[data.content[2]][note]!=='undefined'){ |
28 | 159 |
// We close the note in container one |
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
160 |
//console.log("coucou 2", data); |
54 | 161 |
var duration = data.content[1] - noteDict[data.content[2]][note].ts; |
77 | 162 |
var nb = containerList.length; |
163 |
for(var i=0;i<nb;i++){ |
|
164 |
// addNote(note, startTime, duration, velocity, canal) |
|
165 |
containerList[i].addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]); |
|
166 |
} |
|
28 | 167 |
// delete entry |
54 | 168 |
delete noteDict[data.content[2]][note]; |
28 | 169 |
} |
170 |
} |
|
171 |
else{ |
|
54 | 172 |
if(typeof noteDict[data.content[2]]==='undefined'){ |
173 |
noteDict[data.content[2]] = {}; |
|
174 |
} |
|
175 |
noteDict[data.content[2]][note] = {ts: data.content[1], velocity:velocity}; |
|
28 | 176 |
} |
177 |
} |
|
178 |
||
72 | 179 |
function addLine(){ |
180 |
nbLines++; |
|
77 | 181 |
var nb = containerList.length; |
182 |
for(var i=0;i<nb;i++){ |
|
183 |
containerList[i].addLine(nbLines); |
|
184 |
} |
|
72 | 185 |
} |
186 |
||
74 | 187 |
|
188 |
||
28 | 189 |
// Socket management |
190 |
var sock = null; |
|
191 |
var ellog = null; |
|
192 |
function log(m) { |
|
193 |
if(logger){ |
|
194 |
ellog.innerHTML += m + '\n'; |
|
195 |
ellog.scrollTop = ellog.scrollHeight; |
|
196 |
} |
|
197 |
} |
|
49 | 198 |
window.onload = function(){ |
28 | 199 |
|
200 |
if(logger){ |
|
201 |
ellog = document.getElementById('log'); |
|
202 |
} |
|
203 |
else{ |
|
204 |
document.body.removeChild(document.getElementById('log')); |
|
205 |
} |
|
206 |
||
207 |
var wsuri; |
|
208 |
if (window.location.protocol === "file:") { |
|
209 |
wsuri = "ws://127.0.0.1:8090/broadcast"; |
|
210 |
} else { |
|
211 |
wsuri = "ws://" + window.location.hostname + ":8090/broadcast"; |
|
212 |
} |
|
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
213 |
wsuri = wsuri + "?channel=PIANOROLL&event_code="+event_code; |
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
214 |
|
28 | 215 |
if ("WebSocket" in window) { |
216 |
sock = new WebSocket(wsuri); |
|
217 |
} else if ("MozWebSocket" in window) { |
|
218 |
sock = new MozWebSocket(wsuri); |
|
219 |
} else { |
|
220 |
log("Browser does not support WebSocket!"); |
|
221 |
window.location = "http://autobahn.ws/unsupportedbrowser"; |
|
222 |
} |
|
223 |
||
224 |
if (sock) { |
|
77 | 225 |
sock.onopen = function(){ |
226 |
if(logger){ |
|
227 |
log("Connected to " + wsuri); |
|
228 |
} |
|
229 |
} |
|
28 | 230 |
|
77 | 231 |
sock.onclose = function(e) { |
232 |
if(logger){ |
|
233 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
234 |
} |
|
235 |
sock = null; |
|
236 |
} |
|
28 | 237 |
|
77 | 238 |
sock.onmessage = function(e) { |
239 |
if(logger){ |
|
240 |
log("Got message: " + e.data); |
|
241 |
} |
|
242 |
addNotes(JSON.parse(e.data)); |
|
243 |
} |
|
28 | 244 |
} |
245 |
}; |
|
246 |
||
247 |
||
72 | 248 |
// Init page and intervals |
28 | 249 |
addLine(); |
49 | 250 |
var moveInterval = window.setInterval(replaceContainers, 1000/manualFramerate); |
74 | 251 |
var verticalLinesInterval = window.setInterval(addLine, lineInterval); |
72 | 252 |
|
253 |
// Little inteval to show time |
|
28 | 254 |
var nbSec = 0; |
255 |
var mySpan = document.getElementById("myspan"); |
|
256 |
function updateTime(){ |
|
257 |
nbSec++; |
|
258 |
var hours = parseInt( nbSec / 3600 ) % 24; |
|
259 |
var minutes = parseInt( nbSec / 60 ) % 60; |
|
260 |
var seconds = nbSec % 60; |
|
261 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
262 |
mySpan.innerHTML = timeStr; |
|
263 |
} |
|
264 |
var secondInterval = window.setInterval(updateTime, 1000); |