author | cavaliet |
Thu, 23 Oct 2014 09:16:16 +0200 | |
changeset 54 | 31cea001a298 |
parent 50 | 5ff6273e3626 |
child 58 | 726ee64f18a9 |
permissions | -rw-r--r-- |
28 | 1 |
// Config vars |
2 |
var logger = false; |
|
3 |
var sceneWidth = 1920; |
|
4 |
//var sceneHeight = 128 * 4; // multiple of 128 because of 128 levels in midi signals -> better look |
|
5 |
var prHeight1 = 435; |
|
6 |
var prHeight2 = 645; |
|
7 |
var sceneHeight = prHeight1 + prHeight2; |
|
8 |
var sceneBgColor = 0xFFFFFF; |
|
9 |
var lineColor = 0x444444; |
|
10 |
//var manualFramerate = 24; |
|
46 | 11 |
var pixelsPerSecond1 = Math.floor(sceneWidth / 10); // nb of pixels per second |
28 | 12 |
var manualFramerate = pixelsPerSecond1 / 4; |
13 |
var pixelsPerSecond2 = Math.floor(sceneWidth / 60); // nb of pixels per second |
|
14 |
var lineInterval = 5000; // means line every 5 seconds |
|
15 |
var nbLines = -1; |
|
16 |
var noteHeight = 110; |
|
54 | 17 |
var noteColor = [0x2222FF, 0xB74141, |
18 |
0x2222FF, 0xB74141, |
|
19 |
0x2222FF, 0xB74141, |
|
20 |
0x2222FF, 0xB74141, |
|
21 |
0x2222FF, 0xB74141, |
|
22 |
0x2222FF, 0xB74141, |
|
23 |
0x2222FF, 0xB74141, |
|
24 |
0x2222FF, 0xB74141, |
|
25 |
0x2222FF, 0xB74141, |
|
26 |
0x2222FF, 0xB74141, |
|
27 |
0x2222FF, 0xB74141, |
|
28 |
0x2222FF, 0xB74141, |
|
29 |
0x2222FF, 0xB74141, |
|
30 |
0x2222FF, 0xB74141, |
|
31 |
0x2222FF, 0xB74141, |
|
32 |
0x2222FF, 0xB74141, |
|
33 |
0x2222FF, 0xB74141, |
|
34 |
0x2222FF, 0xB74141, |
|
35 |
0x2222FF, 0xB74141, |
|
36 |
0x2222FF, 0xB74141, |
|
37 |
0x2222FF, 0xB74141, |
|
38 |
0x2222FF, 0xB74141]; |
|
28 | 39 |
//var speed = 1; // container -x position at each frame. Speed = 1 ~ 100px for 2 seconds |
40 |
//var zeroTime = new Date("2014-10-06T12:16:43.000000Z").getTime(); |
|
54 | 41 |
var noteDict = [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]; |
28 | 42 |
var pianoNotes = [1,0,1,0,1,1,0,1,0,1,0,1];//Do, Do#, Ré, Ré#, Mi, Fa, Fa#, Sol, Sol#, La, La#, Si |
43 |
// Visual config |
|
44 |
var drawPianoNotes = false; |
|
45 |
var drawHorizontalLines = false; |
|
46 |
var drawVerticalLines = true; |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
47 |
// Timecode method |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
48 |
var timePageLoaded = Date.now(); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
49 |
var offsetMusic = false; |
28 | 50 |
|
51 |
||
52 |
//create an new instance of a pixi stage |
|
53 |
var stage = new PIXI.Stage(sceneBgColor); |
|
54 |
||
55 |
//create a renderer instance. |
|
56 |
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight); |
|
57 |
||
58 |
//add the renderer view element to the DOM |
|
59 |
document.getElementById("canvasContainer").appendChild(renderer.view); |
|
60 |
||
61 |
//requestAnimFrame( animate ); |
|
62 |
||
63 |
||
64 |
//Draw 127 lines |
|
65 |
var delta = sceneHeight / 128; |
|
66 |
if(drawHorizontalLines){ |
|
67 |
for(var i=1;i<128;i++){ |
|
68 |
var graphics = new PIXI.Graphics(); |
|
69 |
graphics.beginFill(0xFFFF00); |
|
70 |
graphics.lineStyle(1, 0xAAAAAA); |
|
71 |
var y = delta * i; |
|
72 |
graphics.moveTo(0, y); |
|
73 |
graphics.lineTo(sceneWidth, y); |
|
74 |
graphics.endFill(); |
|
75 |
stage.addChild(graphics); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
//Draw piano notes on the left |
|
80 |
if(drawPianoNotes){ |
|
81 |
for(var i=0;i<128;i++){ |
|
82 |
var graphics = new PIXI.Graphics(); |
|
83 |
var color = pianoNotes[i%12]==1 ? 0xFFFFFF : 0x000000; |
|
84 |
if(i==60){ |
|
85 |
color = 0xFFD700; |
|
86 |
} |
|
87 |
graphics.beginFill(color); |
|
88 |
graphics.lineStyle(1, 0xAAAAAA); |
|
89 |
var y = sceneHeight - delta * (i+1); |
|
90 |
graphics.drawRect(0, y, 20, delta); |
|
91 |
graphics.endFill(); |
|
92 |
stage.addChild(graphics); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
var uberContainer = new PIXI.DisplayObjectContainer(); |
|
97 |
uberContainer.position.x = Math.floor(sceneWidth*9/10); |
|
98 |
uberContainer.position.y = 0; |
|
99 |
stage.addChild(uberContainer); |
|
100 |
||
101 |
||
102 |
var container1 = new PIXI.DisplayObjectContainer(); |
|
103 |
container1.position.x = 0; |
|
104 |
container1.position.y = 0; |
|
105 |
uberContainer.addChild(container1); |
|
106 |
var container2 = new PIXI.DisplayObjectContainer(); |
|
107 |
container2.position.x = 0; |
|
108 |
container2.position.y = prHeight1; |
|
109 |
uberContainer.addChild(container2); |
|
110 |
||
111 |
// Line between two containers |
|
112 |
var graphics = new PIXI.Graphics(); |
|
113 |
graphics.beginFill(0xFFFF00); |
|
114 |
graphics.lineStyle(1, lineColor); |
|
115 |
var y = delta * i; |
|
116 |
graphics.moveTo(0, prHeight1); |
|
117 |
graphics.lineTo(sceneWidth, prHeight1); |
|
118 |
graphics.endFill(); |
|
119 |
stage.addChild(graphics); |
|
120 |
||
49 | 121 |
|
122 |
function moveContainer(){ |
|
28 | 123 |
container1.x -= pixelsPerSecond1/manualFramerate; |
124 |
container2.x -= pixelsPerSecond2/manualFramerate; |
|
125 |
renderer.render(stage); |
|
126 |
} |
|
49 | 127 |
function replaceContainers(){ |
128 |
var diff = (Date.now() - timePageLoaded)/1000;// nb of seconds since page loaded |
|
129 |
//console.log("replace ! diff1 = ", container1.x - Math.floor(-diff*pixelsPerSecond1), ", diff 2 = ", container2.x - Math.floor(-diff*pixelsPerSecond2)); |
|
130 |
container1.x = Math.floor(-diff*pixelsPerSecond1); |
|
131 |
container2.x = Math.floor(-diff*pixelsPerSecond2); |
|
132 |
renderer.render(stage); |
|
133 |
} |
|
134 |
function moveContainerMore(){ |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
135 |
container1.x -= 50*(pixelsPerSecond1/manualFramerate); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
136 |
container2.x -= 50*(pixelsPerSecond2/manualFramerate); |
46 | 137 |
renderer.render(stage); |
138 |
} |
|
49 | 139 |
function moveContainerRight(){ |
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
140 |
container1.x += 50*(pixelsPerSecond1/manualFramerate); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
141 |
container2.x += 50*(pixelsPerSecond2/manualFramerate); |
46 | 142 |
renderer.render(stage); |
143 |
} |
|
28 | 144 |
|
54 | 145 |
function addNoteInContainer(note, startTime, duration, velocity, pixelsPerSecond, container, prHeight, canal){ |
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
146 |
//console.log("coucou 1", note, timeFromZero, ts, velocity, pixelsPerSecond, container, prHeight); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
147 |
var beginX = (offsetMusic + startTime) * pixelsPerSecond / 1000; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
148 |
var width = duration * pixelsPerSecond / 1000; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
149 |
// We draw the rectangle |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
150 |
var graphics = new PIXI.Graphics(); |
54 | 151 |
graphics.beginFill(noteColor[canal], (velocity / 128)); |
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
152 |
var y = (128-note) * prHeight / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
153 |
graphics.drawRect(beginX, Math.floor(y - (noteHeight/2) + ((prHeight / 128)/2)), width, noteHeight); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
154 |
graphics.endFill(); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
155 |
container.addChild(graphics); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
156 |
} |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
157 |
|
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
158 |
function addNotes(data){ |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
159 |
if(!offsetMusic){ |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
160 |
// 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
|
161 |
// in order to place in real time |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
162 |
var now = Date.now(); |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
163 |
var timeBetweenNowAndStart = now - timePageLoaded; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
164 |
offsetMusic = timeBetweenNowAndStart - data.content[1]; |
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
165 |
//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
|
166 |
} |
28 | 167 |
var note = data.content[3]; |
168 |
var velocity = data.content[4]; |
|
169 |
if(velocity===0){ |
|
54 | 170 |
if(typeof noteDict[data.content[2]][note]!=='undefined'){ |
28 | 171 |
// We close the note in container one |
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
172 |
//console.log("coucou 2", data); |
54 | 173 |
var duration = data.content[1] - noteDict[data.content[2]][note].ts; |
174 |
addNoteInContainer(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, pixelsPerSecond1, container1, prHeight1, data.content[2]); |
|
175 |
addNoteInContainer(note, noteDict[data.content[2]][note].ts, duration, noteDict[data.content[2]][note].velocity, pixelsPerSecond2, container2, prHeight2, data.content[2]); |
|
28 | 176 |
// delete entry |
54 | 177 |
delete noteDict[data.content[2]][note]; |
28 | 178 |
} |
179 |
} |
|
180 |
else{ |
|
54 | 181 |
if(typeof noteDict[data.content[2]]==='undefined'){ |
182 |
noteDict[data.content[2]] = {}; |
|
183 |
} |
|
184 |
noteDict[data.content[2]][note] = {ts: data.content[1], velocity:velocity}; |
|
28 | 185 |
} |
186 |
} |
|
187 |
||
188 |
// Socket management |
|
189 |
var sock = null; |
|
190 |
var ellog = null; |
|
191 |
function log(m) { |
|
192 |
if(logger){ |
|
193 |
ellog.innerHTML += m + '\n'; |
|
194 |
ellog.scrollTop = ellog.scrollHeight; |
|
195 |
} |
|
196 |
} |
|
49 | 197 |
window.onload = function(){ |
28 | 198 |
|
199 |
if(logger){ |
|
200 |
ellog = document.getElementById('log'); |
|
201 |
} |
|
202 |
else{ |
|
203 |
document.body.removeChild(document.getElementById('log')); |
|
204 |
} |
|
205 |
||
206 |
var wsuri; |
|
207 |
if (window.location.protocol === "file:") { |
|
208 |
wsuri = "ws://127.0.0.1:8090/broadcast"; |
|
209 |
} else { |
|
210 |
wsuri = "ws://" + window.location.hostname + ":8090/broadcast"; |
|
211 |
} |
|
212 |
if ("WebSocket" in window) { |
|
213 |
sock = new WebSocket(wsuri); |
|
214 |
} else if ("MozWebSocket" in window) { |
|
215 |
sock = new MozWebSocket(wsuri); |
|
216 |
} else { |
|
217 |
log("Browser does not support WebSocket!"); |
|
218 |
window.location = "http://autobahn.ws/unsupportedbrowser"; |
|
219 |
} |
|
220 |
||
221 |
if (sock) { |
|
49 | 222 |
sock.onopen = function(){ |
28 | 223 |
if(logger){ |
224 |
log("Connected to " + wsuri); |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
sock.onclose = function(e) { |
|
229 |
if(logger){ |
|
230 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
231 |
} |
|
232 |
sock = null; |
|
233 |
} |
|
234 |
||
235 |
sock.onmessage = function(e) { |
|
236 |
if(logger){ |
|
237 |
log("Got message: " + e.data); |
|
238 |
} |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
46
diff
changeset
|
239 |
addNotes(JSON.parse(e.data)); |
28 | 240 |
} |
241 |
} |
|
242 |
}; |
|
243 |
||
244 |
||
245 |
function addLine(){ |
|
246 |
nbLines++; |
|
247 |
var graphics = new PIXI.Graphics(); |
|
50 | 248 |
//var x = nbLines * (lineInterval/1000) * pixelsPerSecond1; |
249 |
var x = -container1.x; |
|
28 | 250 |
//console.log("nbLines = ",nbLines, "x = ", x); |
251 |
graphics.beginFill(0xFFFF00); |
|
252 |
graphics.lineStyle(1, lineColor); |
|
253 |
graphics.moveTo(x, prHeight1 - 20); |
|
254 |
graphics.lineTo(x, prHeight1); |
|
255 |
graphics.endFill(); |
|
256 |
container1.addChild(graphics); |
|
257 |
// Add text |
|
258 |
var totalSec = nbLines * lineInterval / 1000; |
|
259 |
var hours = parseInt( totalSec / 3600 ) % 24; |
|
260 |
var minutes = parseInt( totalSec / 60 ) % 60; |
|
261 |
var seconds = totalSec % 60; |
|
262 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
263 |
var fontObj = { font: "10pt Arial", fill: "#444444" }; |
|
264 |
var t = new PIXI.Text(timeStr, fontObj); |
|
265 |
t.x = x + 2; |
|
266 |
t.y = prHeight1 - 15; |
|
267 |
container1.addChild(t); |
|
268 |
// Second container |
|
269 |
graphics = new PIXI.Graphics(); |
|
50 | 270 |
//x = nbLines * (lineInterval/1000) * pixelsPerSecond2; |
271 |
x = -container2.x; |
|
28 | 272 |
graphics.beginFill(0xFFFF00); |
273 |
graphics.lineStyle(1, lineColor); |
|
274 |
graphics.moveTo(x, 0); |
|
275 |
graphics.lineTo(x, 20); |
|
276 |
graphics.endFill(); |
|
277 |
container2.addChild(graphics); |
|
278 |
var t = new PIXI.Text(timeStr, fontObj); |
|
279 |
t.x = x + 2; |
|
280 |
t.y = 2; |
|
281 |
container2.addChild(t); |
|
282 |
} |
|
283 |
addLine(); |
|
49 | 284 |
var moveInterval = window.setInterval(replaceContainers, 1000/manualFramerate); |
285 |
// To be sure of synchronism, we replace the container with time calculting every minute |
|
286 |
//var replaceInterval = window.setInterval(replaceContainers, 60*1000); |
|
28 | 287 |
var verticalLinesInterval = false; |
288 |
if(drawVerticalLines){ |
|
289 |
verticalLinesInterval = window.setInterval(addLine, lineInterval); |
|
290 |
} |
|
291 |
var nbSec = 0; |
|
292 |
var mySpan = document.getElementById("myspan"); |
|
293 |
function updateTime(){ |
|
294 |
nbSec++; |
|
295 |
var hours = parseInt( nbSec / 3600 ) % 24; |
|
296 |
var minutes = parseInt( nbSec / 60 ) % 60; |
|
297 |
var seconds = nbSec % 60; |
|
298 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
299 |
mySpan.innerHTML = timeStr; |
|
300 |
} |
|
301 |
var secondInterval = window.setInterval(updateTime, 1000); |