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; |
|
11 |
var pixelsPerSecond1 = Math.floor(sceneWidth / 30); // nb of pixels per second |
|
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; |
|
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 |
//requestAnimFrame( animate ); |
|
38 |
|
|
39 |
|
|
40 |
//Draw 127 lines |
|
41 |
var delta = sceneHeight / 128; |
|
42 |
if(drawHorizontalLines){ |
|
43 |
for(var i=1;i<128;i++){ |
|
44 |
var graphics = new PIXI.Graphics(); |
|
45 |
graphics.beginFill(0xFFFF00); |
|
46 |
graphics.lineStyle(1, 0xAAAAAA); |
|
47 |
var y = delta * i; |
|
48 |
graphics.moveTo(0, y); |
|
49 |
graphics.lineTo(sceneWidth, y); |
|
50 |
graphics.endFill(); |
|
51 |
stage.addChild(graphics); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
//Draw piano notes on the left |
|
56 |
if(drawPianoNotes){ |
|
57 |
for(var i=0;i<128;i++){ |
|
58 |
var graphics = new PIXI.Graphics(); |
|
59 |
var color = pianoNotes[i%12]==1 ? 0xFFFFFF : 0x000000; |
|
60 |
if(i==60){ |
|
61 |
color = 0xFFD700; |
|
62 |
} |
|
63 |
graphics.beginFill(color); |
|
64 |
graphics.lineStyle(1, 0xAAAAAA); |
|
65 |
var y = sceneHeight - delta * (i+1); |
|
66 |
graphics.drawRect(0, y, 20, delta); |
|
67 |
graphics.endFill(); |
|
68 |
stage.addChild(graphics); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
var uberContainer = new PIXI.DisplayObjectContainer(); |
|
73 |
uberContainer.position.x = Math.floor(sceneWidth*9/10); |
|
74 |
uberContainer.position.y = 0; |
|
75 |
stage.addChild(uberContainer); |
|
76 |
|
|
77 |
|
|
78 |
var container1 = new PIXI.DisplayObjectContainer(); |
|
79 |
container1.position.x = 0; |
|
80 |
container1.position.y = 0; |
|
81 |
uberContainer.addChild(container1); |
|
82 |
var container2 = new PIXI.DisplayObjectContainer(); |
|
83 |
container2.position.x = 0; |
|
84 |
container2.position.y = prHeight1; |
|
85 |
uberContainer.addChild(container2); |
|
86 |
|
|
87 |
// Line between two containers |
|
88 |
var graphics = new PIXI.Graphics(); |
|
89 |
graphics.beginFill(0xFFFF00); |
|
90 |
graphics.lineStyle(1, lineColor); |
|
91 |
var y = delta * i; |
|
92 |
graphics.moveTo(0, prHeight1); |
|
93 |
graphics.lineTo(sceneWidth, prHeight1); |
|
94 |
graphics.endFill(); |
|
95 |
stage.addChild(graphics); |
|
96 |
|
|
97 |
function moveContainer() { |
|
98 |
container1.x -= pixelsPerSecond1/manualFramerate; |
|
99 |
container2.x -= pixelsPerSecond2/manualFramerate; |
|
100 |
renderer.render(stage); |
|
101 |
} |
|
102 |
|
|
103 |
function addBunny(data){ |
|
104 |
var timeFromZero = data.content[1]; |
|
105 |
var note = data.content[3]; |
|
106 |
var velocity = data.content[4]; |
|
107 |
if(velocity===0){ |
|
108 |
if(note in noteDict){ |
|
109 |
// We close the note in container one |
|
110 |
/*var beginTime = new Date(noteDict[note].ts).getTime(); |
|
111 |
var beginDelta = beginTime - zeroTime; |
|
112 |
var durationDelta = new Date(data.ts).getTime() - beginTime; |
|
113 |
var beginX = beginDelta * pixelsPerSecond1 / 1000; |
|
114 |
var width = durationDelta * pixelsPerSecond1 / 1000;*/ |
|
115 |
var beginX = noteDict[note].ts * pixelsPerSecond1 / 1000; |
|
116 |
var width = (timeFromZero - noteDict[note].ts) * pixelsPerSecond1 / 1000; |
|
117 |
// We draw the rectangle |
|
118 |
var graphics = new PIXI.Graphics(); |
|
119 |
graphics.beginFill(noteColor, (noteDict[note].velocity / 128)); |
|
120 |
var y = (128-note) * prHeight1 / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
|
121 |
graphics.drawRect(beginX, Math.floor(y - (noteHeight/2) + ((prHeight1 / 128)/2)), width, noteHeight); |
|
122 |
graphics.endFill(); |
|
123 |
container1.addChild(graphics); |
|
124 |
// container 2 |
|
125 |
beginX = noteDict[note].ts * pixelsPerSecond2 / 1000; |
|
126 |
width = (timeFromZero - noteDict[note].ts) * pixelsPerSecond2 / 1000; |
|
127 |
// We draw the rectangle |
|
128 |
graphics = new PIXI.Graphics(); |
|
129 |
graphics.beginFill(noteColor, (noteDict[note].velocity / 128)); |
|
130 |
y = (128-note) * prHeight2 / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
|
131 |
graphics.drawRect(beginX, Math.floor(y - (noteHeight/2) + ((prHeight2 / 128)/2)), width, noteHeight); |
|
132 |
graphics.endFill(); |
|
133 |
container2.addChild(graphics); |
|
134 |
// delete entry |
|
135 |
delete noteDict[note]; |
|
136 |
} |
|
137 |
} |
|
138 |
else{ |
|
139 |
noteDict[note] = {ts: timeFromZero, velocity:velocity}; |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
// Socket management |
|
144 |
var sock = null; |
|
145 |
var ellog = null; |
|
146 |
function log(m) { |
|
147 |
if(logger){ |
|
148 |
ellog.innerHTML += m + '\n'; |
|
149 |
ellog.scrollTop = ellog.scrollHeight; |
|
150 |
} |
|
151 |
} |
|
152 |
window.onload = function() { |
|
153 |
|
|
154 |
if(logger){ |
|
155 |
ellog = document.getElementById('log'); |
|
156 |
} |
|
157 |
else{ |
|
158 |
document.body.removeChild(document.getElementById('log')); |
|
159 |
} |
|
160 |
|
|
161 |
var wsuri; |
|
162 |
if (window.location.protocol === "file:") { |
|
163 |
wsuri = "ws://127.0.0.1:8090/broadcast"; |
|
164 |
} else { |
|
165 |
wsuri = "ws://" + window.location.hostname + ":8090/broadcast"; |
|
166 |
} |
|
167 |
if ("WebSocket" in window) { |
|
168 |
sock = new WebSocket(wsuri); |
|
169 |
} else if ("MozWebSocket" in window) { |
|
170 |
sock = new MozWebSocket(wsuri); |
|
171 |
} else { |
|
172 |
log("Browser does not support WebSocket!"); |
|
173 |
window.location = "http://autobahn.ws/unsupportedbrowser"; |
|
174 |
} |
|
175 |
|
|
176 |
if (sock) { |
|
177 |
sock.onopen = function() { |
|
178 |
if(logger){ |
|
179 |
log("Connected to " + wsuri); |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
sock.onclose = function(e) { |
|
184 |
if(logger){ |
|
185 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
186 |
} |
|
187 |
sock = null; |
|
188 |
} |
|
189 |
|
|
190 |
sock.onmessage = function(e) { |
|
191 |
if(logger){ |
|
192 |
log("Got message: " + e.data); |
|
193 |
} |
|
194 |
addBunny(JSON.parse(e.data)); |
|
195 |
} |
|
196 |
} |
|
197 |
}; |
|
198 |
|
|
199 |
|
|
200 |
function addLine(){ |
|
201 |
nbLines++; |
|
202 |
var graphics = new PIXI.Graphics(); |
|
203 |
var x = nbLines * (lineInterval/1000) * pixelsPerSecond1; |
|
204 |
//console.log("nbLines = ",nbLines, "x = ", x); |
|
205 |
graphics.beginFill(0xFFFF00); |
|
206 |
graphics.lineStyle(1, lineColor); |
|
207 |
graphics.moveTo(x, prHeight1 - 20); |
|
208 |
graphics.lineTo(x, prHeight1); |
|
209 |
graphics.endFill(); |
|
210 |
container1.addChild(graphics); |
|
211 |
// Add text |
|
212 |
var totalSec = nbLines * lineInterval / 1000; |
|
213 |
var hours = parseInt( totalSec / 3600 ) % 24; |
|
214 |
var minutes = parseInt( totalSec / 60 ) % 60; |
|
215 |
var seconds = totalSec % 60; |
|
216 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
217 |
var fontObj = { font: "10pt Arial", fill: "#444444" }; |
|
218 |
var t = new PIXI.Text(timeStr, fontObj); |
|
219 |
t.x = x + 2; |
|
220 |
t.y = prHeight1 - 15; |
|
221 |
container1.addChild(t); |
|
222 |
// Second container |
|
223 |
graphics = new PIXI.Graphics(); |
|
224 |
x = nbLines * (lineInterval/1000) * pixelsPerSecond2; |
|
225 |
graphics.beginFill(0xFFFF00); |
|
226 |
graphics.lineStyle(1, lineColor); |
|
227 |
graphics.moveTo(x, 0); |
|
228 |
graphics.lineTo(x, 20); |
|
229 |
graphics.endFill(); |
|
230 |
container2.addChild(graphics); |
|
231 |
var t = new PIXI.Text(timeStr, fontObj); |
|
232 |
t.x = x + 2; |
|
233 |
t.y = 2; |
|
234 |
container2.addChild(t); |
|
235 |
|
|
236 |
} |
|
237 |
addLine(); |
|
238 |
var moveInterval = window.setInterval(moveContainer, 1000/manualFramerate); |
|
239 |
var verticalLinesInterval = false; |
|
240 |
if(drawVerticalLines){ |
|
241 |
verticalLinesInterval = window.setInterval(addLine, lineInterval); |
|
242 |
} |
|
243 |
var nbSec = 0; |
|
244 |
var mySpan = document.getElementById("myspan"); |
|
245 |
function updateTime(){ |
|
246 |
nbSec++; |
|
247 |
var hours = parseInt( nbSec / 3600 ) % 24; |
|
248 |
var minutes = parseInt( nbSec / 60 ) % 60; |
|
249 |
var seconds = nbSec % 60; |
|
250 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
251 |
mySpan.innerHTML = timeStr; |
|
252 |
} |
|
253 |
var secondInterval = window.setInterval(updateTime, 1000); |