author | cavaliet |
Tue, 14 Oct 2014 16:24:49 +0200 | |
changeset 20 | a2525a44ec94 |
parent 19 | 3b70b46e8044 |
child 21 | 89d235bcbbf3 |
permissions | -rw-r--r-- |
14 | 1 |
var sock = null; |
2 |
var ellog = null; |
|
3 |
||
4 |
||
5 |
function log(m) { |
|
6 |
ellog.innerHTML += m + '\n'; |
|
7 |
ellog.scrollTop = ellog.scrollHeight; |
|
8 |
}; |
|
9 |
||
18 | 10 |
// Config vars |
19 | 11 |
var sceneWidth = 1920; |
20 | 12 |
//var sceneHeight = 128 * 4; // multiple of 128 because of 128 levels in midi signals -> better look |
13 |
var prHeight1 = 435; |
|
14 |
var prHeight2 = 645; |
|
15 |
var sceneHeight = prHeight1 + prHeight2; |
|
19 | 16 |
var sceneBgColor = 0xFFFFFF; |
20 | 17 |
var lineColor = 0x444444; |
14 | 18 |
var manualFramerate = 24; |
20 | 19 |
var pixelsPerSecond1 = Math.floor(sceneWidth / 30); // nb of pixels per second |
20 |
var pixelsPerSecond2 = Math.floor(sceneWidth / 60); // nb of pixels per second |
|
19 | 21 |
var lineInterval = 5000; // means line every 5 seconds |
17 | 22 |
var nbLines = -1; |
19 | 23 |
var noteHeight = 110; |
24 |
var noteColor = 0xB74141; |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
25 |
//var speed = 1; // container -x position at each frame. Speed = 1 ~ 100px for 2 seconds |
19 | 26 |
//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
|
27 |
var noteDict = {}; |
18 | 28 |
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 |
29 |
// Visual config |
|
30 |
var drawPianoNotes = false; |
|
19 | 31 |
var drawHorizontalLines = false; |
32 |
var drawVerticalLines = true; |
|
14 | 33 |
|
34 |
||
35 |
//create an new instance of a pixi stage |
|
36 |
var stage = new PIXI.Stage(sceneBgColor); |
|
37 |
||
38 |
//create a renderer instance. |
|
39 |
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight); |
|
40 |
||
41 |
//add the renderer view element to the DOM |
|
18 | 42 |
document.getElementById("canvasContainer").appendChild(renderer.view); |
14 | 43 |
|
44 |
//requestAnimFrame( animate ); |
|
45 |
||
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
46 |
|
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
47 |
//Draw 127 lines |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
48 |
var delta = sceneHeight / 128; |
19 | 49 |
if(drawHorizontalLines){ |
50 |
for(var i=1;i<128;i++){ |
|
51 |
var graphics = new PIXI.Graphics(); |
|
52 |
graphics.beginFill(0xFFFF00); |
|
53 |
graphics.lineStyle(1, 0xAAAAAA); |
|
54 |
var y = delta * i; |
|
55 |
graphics.moveTo(0, y); |
|
56 |
graphics.lineTo(sceneWidth, y); |
|
57 |
graphics.endFill(); |
|
58 |
stage.addChild(graphics); |
|
59 |
} |
|
17 | 60 |
} |
19 | 61 |
|
18 | 62 |
//Draw piano notes on the left |
63 |
if(drawPianoNotes){ |
|
64 |
for(var i=0;i<128;i++){ |
|
65 |
var graphics = new PIXI.Graphics(); |
|
66 |
var color = pianoNotes[i%12]==1 ? 0xFFFFFF : 0x000000; |
|
67 |
if(i==60){ |
|
68 |
color = 0xFFD700; |
|
69 |
} |
|
70 |
graphics.beginFill(color); |
|
71 |
graphics.lineStyle(1, 0xAAAAAA); |
|
72 |
var y = sceneHeight - delta * (i+1); |
|
73 |
graphics.drawRect(0, y, 20, delta); |
|
74 |
graphics.endFill(); |
|
75 |
stage.addChild(graphics); |
|
76 |
} |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
77 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
78 |
|
14 | 79 |
var uberContainer = new PIXI.DisplayObjectContainer(); |
80 |
uberContainer.position.x = Math.floor(sceneWidth*4/5); |
|
81 |
uberContainer.position.y = 0; |
|
82 |
stage.addChild(uberContainer); |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
83 |
|
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
84 |
|
20 | 85 |
var container1 = new PIXI.DisplayObjectContainer(); |
86 |
container1.position.x = 0; |
|
87 |
container1.position.y = 0; |
|
88 |
uberContainer.addChild(container1); |
|
89 |
var container2 = new PIXI.DisplayObjectContainer(); |
|
90 |
container2.position.x = 0; |
|
91 |
container2.position.y = prHeight1; |
|
92 |
uberContainer.addChild(container2); |
|
14 | 93 |
|
20 | 94 |
// Line between two containers |
95 |
var graphics = new PIXI.Graphics(); |
|
96 |
graphics.beginFill(0xFFFF00); |
|
97 |
graphics.lineStyle(1, lineColor); |
|
98 |
var y = delta * i; |
|
99 |
graphics.moveTo(0, prHeight1); |
|
100 |
graphics.lineTo(sceneWidth, prHeight1); |
|
101 |
graphics.endFill(); |
|
102 |
stage.addChild(graphics); |
|
14 | 103 |
|
104 |
function moveContainer() { |
|
20 | 105 |
container1.x -= pixelsPerSecond1/manualFramerate; |
106 |
container2.x -= pixelsPerSecond2/manualFramerate; |
|
14 | 107 |
renderer.render(stage); |
108 |
} |
|
109 |
||
110 |
function addBunny(data){ |
|
18 | 111 |
var timeFromZero = data.content[1]; |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
112 |
var note = data.content[3]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
113 |
var velocity = data.content[4]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
114 |
if(velocity===0){ |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
115 |
if(note in noteDict){ |
20 | 116 |
// We close the note in container one |
18 | 117 |
/*var beginTime = new Date(noteDict[note].ts).getTime(); |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
118 |
var beginDelta = beginTime - zeroTime; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
119 |
var durationDelta = new Date(data.ts).getTime() - beginTime; |
20 | 120 |
var beginX = beginDelta * pixelsPerSecond1 / 1000; |
121 |
var width = durationDelta * pixelsPerSecond1 / 1000;*/ |
|
122 |
var beginX = noteDict[note].ts * pixelsPerSecond1 / 1000; |
|
123 |
var width = (timeFromZero - noteDict[note].ts) * pixelsPerSecond1 / 1000; |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
124 |
// We draw the rectangle |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
125 |
var graphics = new PIXI.Graphics(); |
19 | 126 |
graphics.beginFill(noteColor, (noteDict[note].velocity / 128)); |
20 | 127 |
var y = (128-note) * prHeight1 / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
128 |
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
|
129 |
graphics.endFill(); |
20 | 130 |
container1.addChild(graphics); |
131 |
// container 2 |
|
132 |
beginX = noteDict[note].ts * pixelsPerSecond2 / 1000; |
|
133 |
width = (timeFromZero - noteDict[note].ts) * pixelsPerSecond2 / 1000; |
|
134 |
// We draw the rectangle |
|
135 |
graphics = new PIXI.Graphics(); |
|
136 |
graphics.beginFill(noteColor, (noteDict[note].velocity / 128)); |
|
137 |
y = (128-note) * prHeight2 / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
|
138 |
graphics.drawRect(beginX, Math.floor(y - (noteHeight/2) + ((prHeight2 / 128)/2)), width, noteHeight); |
|
139 |
graphics.endFill(); |
|
140 |
container2.addChild(graphics); |
|
141 |
// delete entry |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
142 |
delete noteDict[note]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
143 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
144 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
145 |
else{ |
18 | 146 |
noteDict[note] = {ts: timeFromZero, velocity:velocity}; |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
147 |
} |
14 | 148 |
} |
149 |
||
150 |
window.onload = function() { |
|
151 |
||
152 |
ellog = document.getElementById('log'); |
|
153 |
||
154 |
var wsuri; |
|
155 |
if (window.location.protocol === "file:") { |
|
156 |
wsuri = "ws://127.0.0.1:8090/broadcast"; |
|
157 |
} else { |
|
158 |
wsuri = "ws://" + window.location.hostname + ":8090/broadcast"; |
|
159 |
} |
|
160 |
if ("WebSocket" in window) { |
|
161 |
sock = new WebSocket(wsuri); |
|
162 |
} else if ("MozWebSocket" in window) { |
|
163 |
sock = new MozWebSocket(wsuri); |
|
164 |
} else { |
|
165 |
log("Browser does not support WebSocket!"); |
|
166 |
window.location = "http://autobahn.ws/unsupportedbrowser"; |
|
167 |
} |
|
168 |
||
169 |
if (sock) { |
|
170 |
sock.onopen = function() { |
|
171 |
log("Connected to " + wsuri); |
|
172 |
} |
|
173 |
||
174 |
sock.onclose = function(e) { |
|
175 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
176 |
sock = null; |
|
177 |
} |
|
178 |
||
179 |
sock.onmessage = function(e) { |
|
180 |
log("Got message: " + e.data); |
|
181 |
addBunny(JSON.parse(e.data)); |
|
182 |
} |
|
183 |
} |
|
184 |
}; |
|
185 |
||
186 |
||
187 |
function addLine(){ |
|
188 |
nbLines++; |
|
189 |
var graphics = new PIXI.Graphics(); |
|
20 | 190 |
var x = nbLines * (lineInterval/1000) * pixelsPerSecond1; |
16 | 191 |
//console.log("nbLines = ",nbLines, "x = ", x); |
14 | 192 |
graphics.beginFill(0xFFFF00); |
20 | 193 |
graphics.lineStyle(1, lineColor); |
194 |
graphics.moveTo(x, prHeight1 - 20); |
|
195 |
graphics.lineTo(x, prHeight1); |
|
14 | 196 |
graphics.endFill(); |
20 | 197 |
container1.addChild(graphics); |
16 | 198 |
// Add text |
199 |
var totalSec = nbLines * lineInterval / 1000; |
|
200 |
var hours = parseInt( totalSec / 3600 ) % 24; |
|
201 |
var minutes = parseInt( totalSec / 60 ) % 60; |
|
202 |
var seconds = totalSec % 60; |
|
203 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
20 | 204 |
var fontObj = { font: "10pt Arial", fill: "#444444" }; |
205 |
var t = new PIXI.Text(timeStr, fontObj); |
|
16 | 206 |
t.x = x + 2; |
20 | 207 |
t.y = prHeight1 - 15; |
208 |
container1.addChild(t); |
|
209 |
// Second container |
|
210 |
graphics = new PIXI.Graphics(); |
|
211 |
x = nbLines * (lineInterval/1000) * pixelsPerSecond2; |
|
212 |
graphics.beginFill(0xFFFF00); |
|
213 |
graphics.lineStyle(1, lineColor); |
|
214 |
graphics.moveTo(x, 0); |
|
215 |
graphics.lineTo(x, 20); |
|
216 |
graphics.endFill(); |
|
217 |
container2.addChild(graphics); |
|
218 |
var t = new PIXI.Text(timeStr, fontObj); |
|
219 |
t.x = x + 2; |
|
220 |
t.y = 2; |
|
221 |
container2.addChild(t); |
|
222 |
|
|
14 | 223 |
} |
224 |
var moveInterval = window.setInterval(moveContainer, 1000/manualFramerate); |
|
18 | 225 |
var verticalLinesInterval = false; |
226 |
if(drawVerticalLines){ |
|
227 |
verticalLinesInterval = window.setInterval(addLine, lineInterval); |
|
228 |
} |