author | ymh <ymh.work@gmail.com> |
Tue, 28 Oct 2014 09:47:11 +0100 | |
changeset 76 | 029cdbeebf03 |
parent 74 | bdcc964b3c01 |
child 77 | 1b578edc1578 |
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 |
} |
|
113 |
}; |
|
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
114 |
|
72 | 115 |
} |
116 |
||
117 |
// Init containers |
|
74 | 118 |
var container1 = new PianoRoll(uberContainer, 0, 0, prHeight1, true, pixelsPerSecond1, sceneWidth); |
119 |
var container2 = new PianoRoll(uberContainer, 0, prHeight1, prHeight2, false, pixelsPerSecond2, sceneWidth); |
|
28 | 120 |
|
121 |
// Line between two containers |
|
122 |
var graphics = new PIXI.Graphics(); |
|
123 |
graphics.beginFill(0xFFFF00); |
|
124 |
graphics.lineStyle(1, lineColor); |
|
125 |
graphics.moveTo(0, prHeight1); |
|
126 |
graphics.lineTo(sceneWidth, prHeight1); |
|
127 |
graphics.endFill(); |
|
128 |
stage.addChild(graphics); |
|
129 |
||
49 | 130 |
|
131 |
function replaceContainers(){ |
|
132 |
var diff = (Date.now() - timePageLoaded)/1000;// nb of seconds since page loaded |
|
133 |
//console.log("replace ! diff1 = ", container1.x - Math.floor(-diff*pixelsPerSecond1), ", diff 2 = ", container2.x - Math.floor(-diff*pixelsPerSecond2)); |
|
72 | 134 |
container1.moveTo(-diff); |
135 |
container2.moveTo(-diff); |
|
46 | 136 |
renderer.render(stage); |
137 |
} |
|
28 | 138 |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
139 |
function addNotes(data){ |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
140 |
if(!offsetMusic){ |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
141 |
// 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
|
142 |
// in order to place in real time |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
143 |
var now = Date.now(); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
144 |
var timeBetweenNowAndStart = now - timePageLoaded; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
145 |
offsetMusic = timeBetweenNowAndStart - data.content[1]; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
146 |
//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
|
147 |
} |
28 | 148 |
var note = data.content[3]; |
149 |
var velocity = data.content[4]; |
|
150 |
if(velocity===0){ |
|
54 | 151 |
if(typeof noteDict[data.content[2]][note]!=='undefined'){ |
28 | 152 |
// We close the note in container one |
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
153 |
//console.log("coucou 2", data); |
54 | 154 |
var duration = data.content[1] - noteDict[data.content[2]][note].ts; |
72 | 155 |
//addNoteInContainer(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, pixelsPerSecond1, container1, prHeight1, data.content[2]); |
156 |
// addNote(note, startTime, duration, velocity, canal) |
|
157 |
container1.addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]); |
|
158 |
//addNoteInContainer(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, pixelsPerSecond2, container2, prHeight2, data.content[2]); |
|
159 |
container2.addNote(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, data.content[2]); |
|
28 | 160 |
// delete entry |
54 | 161 |
delete noteDict[data.content[2]][note]; |
28 | 162 |
} |
163 |
} |
|
164 |
else{ |
|
54 | 165 |
if(typeof noteDict[data.content[2]]==='undefined'){ |
166 |
noteDict[data.content[2]] = {}; |
|
167 |
} |
|
168 |
noteDict[data.content[2]][note] = {ts: data.content[1], velocity:velocity}; |
|
28 | 169 |
} |
170 |
} |
|
171 |
||
72 | 172 |
function addLine(){ |
173 |
nbLines++; |
|
174 |
container1.addLine(nbLines); |
|
175 |
container2.addLine(nbLines); |
|
176 |
} |
|
177 |
||
74 | 178 |
|
179 |
||
28 | 180 |
// Socket management |
181 |
var sock = null; |
|
182 |
var ellog = null; |
|
183 |
function log(m) { |
|
184 |
if(logger){ |
|
185 |
ellog.innerHTML += m + '\n'; |
|
186 |
ellog.scrollTop = ellog.scrollHeight; |
|
187 |
} |
|
188 |
} |
|
49 | 189 |
window.onload = function(){ |
28 | 190 |
|
191 |
if(logger){ |
|
192 |
ellog = document.getElementById('log'); |
|
193 |
} |
|
194 |
else{ |
|
195 |
document.body.removeChild(document.getElementById('log')); |
|
196 |
} |
|
197 |
||
198 |
var wsuri; |
|
199 |
if (window.location.protocol === "file:") { |
|
200 |
wsuri = "ws://127.0.0.1:8090/broadcast"; |
|
201 |
} else { |
|
202 |
wsuri = "ws://" + window.location.hostname + ":8090/broadcast"; |
|
203 |
} |
|
76
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
204 |
wsuri = wsuri + "?channel=PIANOROLL&event_code="+event_code; |
029cdbeebf03
filter pianoroll annotation by channel & event
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
205 |
|
28 | 206 |
if ("WebSocket" in window) { |
207 |
sock = new WebSocket(wsuri); |
|
208 |
} else if ("MozWebSocket" in window) { |
|
209 |
sock = new MozWebSocket(wsuri); |
|
210 |
} else { |
|
211 |
log("Browser does not support WebSocket!"); |
|
212 |
window.location = "http://autobahn.ws/unsupportedbrowser"; |
|
213 |
} |
|
214 |
||
215 |
if (sock) { |
|
49 | 216 |
sock.onopen = function(){ |
28 | 217 |
if(logger){ |
218 |
log("Connected to " + wsuri); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
sock.onclose = function(e) { |
|
223 |
if(logger){ |
|
224 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
225 |
} |
|
226 |
sock = null; |
|
227 |
} |
|
228 |
||
229 |
sock.onmessage = function(e) { |
|
230 |
if(logger){ |
|
231 |
log("Got message: " + e.data); |
|
232 |
} |
|
72 | 233 |
addNotes(JSON.parse(e.data)); |
28 | 234 |
} |
235 |
} |
|
236 |
}; |
|
237 |
||
238 |
||
72 | 239 |
// Init page and intervals |
28 | 240 |
addLine(); |
49 | 241 |
var moveInterval = window.setInterval(replaceContainers, 1000/manualFramerate); |
74 | 242 |
var verticalLinesInterval = window.setInterval(addLine, lineInterval); |
243 |
var removeInterval1 = window.setInterval(container1.removePassedObjets, 1000 * sceneWidth / pixelsPerSecond1 ); |
|
244 |
var removeInterval2 = window.setInterval(container2.removePassedObjets, 1000 * sceneWidth / pixelsPerSecond2 ); |
|
72 | 245 |
|
246 |
// Little inteval to show time |
|
28 | 247 |
var nbSec = 0; |
248 |
var mySpan = document.getElementById("myspan"); |
|
249 |
function updateTime(){ |
|
250 |
nbSec++; |
|
251 |
var hours = parseInt( nbSec / 3600 ) % 24; |
|
252 |
var minutes = parseInt( nbSec / 60 ) % 60; |
|
253 |
var seconds = nbSec % 60; |
|
254 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
255 |
mySpan.innerHTML = timeStr; |
|
256 |
} |
|
257 |
var secondInterval = window.setInterval(updateTime, 1000); |