author | cavaliet |
Tue, 14 Oct 2014 18:00:13 +0200 | |
changeset 21 | 89d235bcbbf3 |
parent 20 | a2525a44ec94 |
permissions | -rw-r--r-- |
18 | 1 |
// Config vars |
21 | 2 |
var logger = false; |
19 | 3 |
var sceneWidth = 1920; |
20 | 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; |
|
19 | 8 |
var sceneBgColor = 0xFFFFFF; |
20 | 9 |
var lineColor = 0x444444; |
21 | 10 |
//var manualFramerate = 24; |
20 | 11 |
var pixelsPerSecond1 = Math.floor(sceneWidth / 30); // nb of pixels per second |
21 | 12 |
var manualFramerate = pixelsPerSecond1 / 4; |
20 | 13 |
var pixelsPerSecond2 = Math.floor(sceneWidth / 60); // nb of pixels per second |
19 | 14 |
var lineInterval = 5000; // means line every 5 seconds |
17 | 15 |
var nbLines = -1; |
19 | 16 |
var noteHeight = 110; |
17 |
var noteColor = 0xB74141; |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
18 |
//var speed = 1; // container -x position at each frame. Speed = 1 ~ 100px for 2 seconds |
19 | 19 |
//var zeroTime = new Date("2014-10-06T12:16:43.000000Z").getTime(); |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
20 |
var noteDict = {}; |
18 | 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; |
|
19 | 24 |
var drawHorizontalLines = false; |
25 |
var drawVerticalLines = true; |
|
14 | 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 |
|
18 | 35 |
document.getElementById("canvasContainer").appendChild(renderer.view); |
14 | 36 |
|
37 |
//requestAnimFrame( animate ); |
|
38 |
||
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
39 |
|
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
40 |
//Draw 127 lines |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
41 |
var delta = sceneHeight / 128; |
19 | 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 |
} |
|
17 | 53 |
} |
19 | 54 |
|
18 | 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 |
} |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
70 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
71 |
|
14 | 72 |
var uberContainer = new PIXI.DisplayObjectContainer(); |
21 | 73 |
uberContainer.position.x = Math.floor(sceneWidth*9/10); |
14 | 74 |
uberContainer.position.y = 0; |
75 |
stage.addChild(uberContainer); |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
76 |
|
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
77 |
|
20 | 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); |
|
14 | 86 |
|
20 | 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); |
|
14 | 96 |
|
97 |
function moveContainer() { |
|
20 | 98 |
container1.x -= pixelsPerSecond1/manualFramerate; |
99 |
container2.x -= pixelsPerSecond2/manualFramerate; |
|
14 | 100 |
renderer.render(stage); |
101 |
} |
|
102 |
||
103 |
function addBunny(data){ |
|
18 | 104 |
var timeFromZero = data.content[1]; |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
105 |
var note = data.content[3]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
106 |
var velocity = data.content[4]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
107 |
if(velocity===0){ |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
108 |
if(note in noteDict){ |
20 | 109 |
// We close the note in container one |
18 | 110 |
/*var beginTime = new Date(noteDict[note].ts).getTime(); |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
111 |
var beginDelta = beginTime - zeroTime; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
112 |
var durationDelta = new Date(data.ts).getTime() - beginTime; |
20 | 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; |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
117 |
// We draw the rectangle |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
118 |
var graphics = new PIXI.Graphics(); |
19 | 119 |
graphics.beginFill(noteColor, (noteDict[note].velocity / 128)); |
20 | 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); |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
122 |
graphics.endFill(); |
20 | 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 |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
135 |
delete noteDict[note]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
136 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
137 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
138 |
else{ |
18 | 139 |
noteDict[note] = {ts: timeFromZero, velocity:velocity}; |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
140 |
} |
14 | 141 |
} |
142 |
||
21 | 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 |
} |
|
14 | 152 |
window.onload = function() { |
153 |
||
21 | 154 |
if(logger){ |
155 |
ellog = document.getElementById('log'); |
|
156 |
} |
|
157 |
else{ |
|
158 |
document.body.removeChild(document.getElementById('log')); |
|
159 |
} |
|
14 | 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() { |
|
21 | 178 |
if(logger){ |
179 |
log("Connected to " + wsuri); |
|
180 |
} |
|
14 | 181 |
} |
182 |
||
183 |
sock.onclose = function(e) { |
|
21 | 184 |
if(logger){ |
185 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
186 |
} |
|
14 | 187 |
sock = null; |
188 |
} |
|
189 |
||
190 |
sock.onmessage = function(e) { |
|
21 | 191 |
if(logger){ |
192 |
log("Got message: " + e.data); |
|
193 |
} |
|
14 | 194 |
addBunny(JSON.parse(e.data)); |
195 |
} |
|
196 |
} |
|
197 |
}; |
|
198 |
||
199 |
||
200 |
function addLine(){ |
|
201 |
nbLines++; |
|
202 |
var graphics = new PIXI.Graphics(); |
|
20 | 203 |
var x = nbLines * (lineInterval/1000) * pixelsPerSecond1; |
16 | 204 |
//console.log("nbLines = ",nbLines, "x = ", x); |
14 | 205 |
graphics.beginFill(0xFFFF00); |
20 | 206 |
graphics.lineStyle(1, lineColor); |
207 |
graphics.moveTo(x, prHeight1 - 20); |
|
208 |
graphics.lineTo(x, prHeight1); |
|
14 | 209 |
graphics.endFill(); |
20 | 210 |
container1.addChild(graphics); |
16 | 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); |
|
20 | 217 |
var fontObj = { font: "10pt Arial", fill: "#444444" }; |
218 |
var t = new PIXI.Text(timeStr, fontObj); |
|
16 | 219 |
t.x = x + 2; |
20 | 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 |
|
|
14 | 236 |
} |
21 | 237 |
addLine(); |
14 | 238 |
var moveInterval = window.setInterval(moveContainer, 1000/manualFramerate); |
18 | 239 |
var verticalLinesInterval = false; |
240 |
if(drawVerticalLines){ |
|
241 |
verticalLinesInterval = window.setInterval(addLine, lineInterval); |
|
242 |
} |
|
21 | 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); |