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