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