author | cavaliet |
Tue, 14 Oct 2014 12:17:34 +0200 | |
changeset 18 | 517e343a86eb |
parent 17 | 4dd9a96a6d3b |
child 19 | 3b70b46e8044 |
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 |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
11 |
var sceneWidth = 1000; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
12 |
var sceneHeight = 128 * 4; // multiple of 128 because of 128 levels in midi signals -> better look |
14 | 13 |
var sceneBgColor = 0x66FF99; |
14 |
var manualFramerate = 24; |
|
15 |
var pixelsPerSecond = 50; // nb of pixels per second |
|
16 |
var lineInterval = 2000; // means line every 2 seconds |
|
17 | 17 |
var nbLines = -1; |
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 |
14 | 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; |
|
24 |
var drawVerticalLines = false; |
|
14 | 25 |
|
26 |
||
27 |
//create an new instance of a pixi stage |
|
28 |
var stage = new PIXI.Stage(sceneBgColor); |
|
29 |
||
30 |
//create a renderer instance. |
|
31 |
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight); |
|
32 |
||
33 |
//add the renderer view element to the DOM |
|
18 | 34 |
document.getElementById("canvasContainer").appendChild(renderer.view); |
14 | 35 |
|
36 |
//requestAnimFrame( animate ); |
|
37 |
||
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
38 |
|
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
39 |
//Draw 127 lines |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
40 |
var delta = sceneHeight / 128; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
41 |
for(var i=1;i<128;i++){ |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
42 |
var graphics = new PIXI.Graphics(); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
43 |
graphics.beginFill(0xFFFF00); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
44 |
graphics.lineStyle(1, 0xAAAAAA); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
45 |
var y = delta * i; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
46 |
graphics.moveTo(0, y); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
47 |
graphics.lineTo(sceneWidth, y); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
48 |
graphics.endFill(); |
17 | 49 |
stage.addChild(graphics); |
50 |
} |
|
18 | 51 |
//Draw piano notes on the left |
52 |
if(drawPianoNotes){ |
|
53 |
for(var i=0;i<128;i++){ |
|
54 |
var graphics = new PIXI.Graphics(); |
|
55 |
var color = pianoNotes[i%12]==1 ? 0xFFFFFF : 0x000000; |
|
56 |
if(i==60){ |
|
57 |
color = 0xFFD700; |
|
58 |
} |
|
59 |
graphics.beginFill(color); |
|
60 |
graphics.lineStyle(1, 0xAAAAAA); |
|
61 |
var y = sceneHeight - delta * (i+1); |
|
62 |
graphics.drawRect(0, y, 20, delta); |
|
63 |
graphics.endFill(); |
|
64 |
stage.addChild(graphics); |
|
65 |
} |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
66 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
67 |
|
14 | 68 |
var uberContainer = new PIXI.DisplayObjectContainer(); |
69 |
uberContainer.position.x = Math.floor(sceneWidth*4/5); |
|
70 |
uberContainer.position.y = 0; |
|
71 |
stage.addChild(uberContainer); |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
72 |
|
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
73 |
|
14 | 74 |
var container = new PIXI.DisplayObjectContainer(); |
75 |
container.position.x = 0; |
|
76 |
container.position.y = 0; |
|
77 |
uberContainer.addChild(container); |
|
78 |
||
79 |
//create a texture from an image path |
|
80 |
var texture = PIXI.Texture.fromImage("bunny.png"); |
|
81 |
||
82 |
||
83 |
/*function animate() { |
|
84 |
requestAnimFrame( animate ); |
|
85 |
// just for fun, lets rotate mr rabbit a little |
|
86 |
//bunny.rotation += 0.1; |
|
87 |
container.x -= speed; |
|
88 |
//console.log("container.x = ", container.x); |
|
89 |
// render the stage |
|
90 |
renderer.render(stage); |
|
91 |
}*/ |
|
92 |
||
93 |
function moveContainer() { |
|
94 |
container.x -= pixelsPerSecond/manualFramerate; |
|
95 |
renderer.render(stage); |
|
96 |
} |
|
97 |
||
98 |
function addBunny(data){ |
|
18 | 99 |
var timeFromZero = data.content[1]; |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
100 |
var note = data.content[3]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
101 |
var velocity = data.content[4]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
102 |
if(velocity===0){ |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
103 |
if(note in noteDict){ |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
104 |
// We close the note |
18 | 105 |
/*var beginTime = new Date(noteDict[note].ts).getTime(); |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
106 |
var beginDelta = beginTime - zeroTime; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
107 |
var durationDelta = new Date(data.ts).getTime() - beginTime; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
108 |
var beginX = beginDelta * pixelsPerSecond / 1000; |
18 | 109 |
var width = durationDelta * pixelsPerSecond / 1000;*/ |
110 |
var beginX = noteDict[note].ts * pixelsPerSecond / 1000; |
|
111 |
var width = (timeFromZero - noteDict[note].ts) * pixelsPerSecond / 1000; |
|
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
112 |
// We draw the rectangle |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
113 |
var graphics = new PIXI.Graphics(); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
114 |
graphics.beginFill(0x0000AA, (noteDict[note].velocity / 128)); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
115 |
var y = (128-note) * sceneHeight / 128; // (128-note) because y = 0 is for note = 128 and y = 128 for note = 0 |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
116 |
graphics.drawRect(beginX, y, width, sceneHeight / 128); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
117 |
graphics.endFill(); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
118 |
container.addChild(graphics); |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
119 |
delete noteDict[note]; |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
120 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
121 |
} |
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
122 |
else{ |
18 | 123 |
noteDict[note] = {ts: timeFromZero, velocity:velocity}; |
15
f1ae020c2872
piano roll works fine... and needs visual enhancement
cavaliet
parents:
14
diff
changeset
|
124 |
} |
14 | 125 |
} |
126 |
||
127 |
window.onload = function() { |
|
128 |
||
129 |
ellog = document.getElementById('log'); |
|
130 |
||
131 |
var wsuri; |
|
132 |
if (window.location.protocol === "file:") { |
|
133 |
wsuri = "ws://127.0.0.1:8090/broadcast"; |
|
134 |
} else { |
|
135 |
wsuri = "ws://" + window.location.hostname + ":8090/broadcast"; |
|
136 |
} |
|
137 |
if ("WebSocket" in window) { |
|
138 |
sock = new WebSocket(wsuri); |
|
139 |
} else if ("MozWebSocket" in window) { |
|
140 |
sock = new MozWebSocket(wsuri); |
|
141 |
} else { |
|
142 |
log("Browser does not support WebSocket!"); |
|
143 |
window.location = "http://autobahn.ws/unsupportedbrowser"; |
|
144 |
} |
|
145 |
||
146 |
if (sock) { |
|
147 |
sock.onopen = function() { |
|
148 |
log("Connected to " + wsuri); |
|
149 |
} |
|
150 |
||
151 |
sock.onclose = function(e) { |
|
152 |
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); |
|
153 |
sock = null; |
|
154 |
} |
|
155 |
||
156 |
sock.onmessage = function(e) { |
|
157 |
log("Got message: " + e.data); |
|
158 |
addBunny(JSON.parse(e.data)); |
|
159 |
} |
|
160 |
} |
|
161 |
}; |
|
162 |
||
163 |
||
164 |
function addLine(){ |
|
165 |
nbLines++; |
|
166 |
var graphics = new PIXI.Graphics(); |
|
167 |
var x = nbLines * (lineInterval/1000) * pixelsPerSecond; |
|
16 | 168 |
//console.log("nbLines = ",nbLines, "x = ", x); |
14 | 169 |
graphics.beginFill(0xFFFF00); |
170 |
graphics.lineStyle(1, 0x444444); |
|
171 |
graphics.moveTo(x, 0); |
|
172 |
graphics.lineTo(x, sceneHeight); |
|
173 |
graphics.endFill(); |
|
174 |
container.addChild(graphics); |
|
16 | 175 |
// Add text |
176 |
var totalSec = nbLines * lineInterval / 1000; |
|
177 |
var hours = parseInt( totalSec / 3600 ) % 24; |
|
178 |
var minutes = parseInt( totalSec / 60 ) % 60; |
|
179 |
var seconds = totalSec % 60; |
|
180 |
var timeStr = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds); |
|
181 |
var t = new PIXI.Text(timeStr, { font: "10pt Arial", fill: "#444444" }); |
|
182 |
t.x = x + 2; |
|
183 |
t.y = sceneHeight - 20; |
|
184 |
container.addChild(t); |
|
14 | 185 |
} |
186 |
var moveInterval = window.setInterval(moveContainer, 1000/manualFramerate); |
|
18 | 187 |
var verticalLinesInterval = false; |
188 |
if(drawVerticalLines){ |
|
189 |
verticalLinesInterval = window.setInterval(addLine, lineInterval); |
|
190 |
} |