-
Notifications
You must be signed in to change notification settings - Fork 0
/
crashos.js
323 lines (265 loc) · 7.31 KB
/
crashos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
let cpuHist
let colUi, colServer, colStandby
let vectorCode = []
let vectorServer = []
let font, txtSize
let padd
let vid
let pg
let maxLineCode
let mic, fft
let vecCodePos, vecCodeSize
let vecCpuPos, vecCpuSize
let vecVidPos, vecVidSize
let vecServerPos, vecServerSize
let libs = ["/includes/js/socket.io.js"]
function preload() {
font = loadFont("includes/fonts/press.ttf")
vecVidSize = createVector(640,480); // video size
vid = createVideo("includes/video/01.mp4", vidload);
}
function setup() {
createCanvas(windowWidth, windowHeight)
setupOsc('127.0.0.1', 20000, 20005) // oscHost, oscPortIn, oscPortOut
setAttributes('antialias', true);
bvl = 15 //bevel
padd = 5 // padding
maxLineCode = 13; // maximum of code line displayed
strokeJoin(BEVEL);
cpuHist=[0,5];
colStandby = color(30,212,115);
colServer = color(255,0,0);
colUi = colStandby;
txtSize = 20;
textSize(txtSize);
textFont(font);
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic)
setPositionSize()
}
function draw() {
background(0)
drawFft();
drawPourtour(colUi);
// players Code
drawWindow(vecCodePos, vecCodeSize, colUi)
drawCode(vecCodePos, vecCodeSize)
// Cpu
drawWindow(vecCpuPos, vecCpuSize, colUi)
drawCpu(vecCpuPos, vecCpuSize)
// Video
drawWindow(vecVidPos, vecVidSize, colUi);
image(vid, vecVidPos.x +10, vecVidPos.y, vecVidSize.x-10,vecVidSize.y-10)
// server Code
drawWindow(vecServerPos, vecServerSize, colUi)
drawServer(vecServerPos, vecServerSize)
}
function vidload(){
vid.loop();
vid.volume(0);
vid.hide();
vecVidSize = createVector(640,480); // video size
vid.play();
}
function setPositionSize(){
// Code Players
vecCodePos = createVector(50,50)
vecCodeSize = createVector((width-vecCodePos.x)/2.5,height-vecCodePos.y-8*padd)
// Cpu
vecCpuSize = createVector(200,200)
vecCpuPos = createVector(width-vecCpuSize.x - 10*padd,vecCodePos.y + vecCodeSize.y - vecCpuSize.y)
// Video
vid.size(vecVidSize.x,vecVidSize.y);
vecVidPos = createVector(width-10*padd-vecVidSize.x,50);
// Server Code
vecServerPos = createVector(vecCodePos.x*4+vecCodeSize.x, vecVidPos.y + vecVidSize.y + 8*padd)
vecServerSize = createVector(width - vecServerPos.x - vecCpuSize.x - 32*padd, height-vecServerPos.y - 8*padd)
}
function drawCode(vecPos, vecSize){
push()
translate(vecPos)
stroke(255)
fill(255)
let conCode =""
for (let i=0; i<vectorCode.length; i++){
conCode += "\n"
conCode += vectorCode[i]
}
drawingContext.shadowBlur=8;
drawingContext.shadowColor=colUi;
text(conCode, txtSize, txtSize, vecSize.x-padd,vecSize.y-padd)
pop()
}
function drawServer(vecPos, vecSize){
push()
translate(vecPos)
stroke(255)
fill(255)
let conCode =""
for (let i=0; i<vectorServer.length; i++){
conCode += "\n"
conCode += vectorServer[i]
}
drawingContext.shadowBlur=8;
drawingContext.shadowColor=colUi;
text(conCode, txtSize, txtSize, vecSize.x-padd,vecSize.y-padd)
pop()
}
function drawWindow(vecPos, vecSize, col){
push()
translate(vecPos.x, vecPos.y)
noStroke()
let backCol = color(col.levels)
backCol.setAlpha(90)
fill(backCol)
beginShape() // the background rectangle
vertex(0,0)
vertex(0, vecSize.y-bvl)
vertex(0, vecSize.y-bvl)
vertex(bvl, vecSize.y)
vertex(bvl, vecSize.y)
vertex(vecSize.x, vecSize.y)
vertex(vecSize.x, vecSize.y)
vertex(vecSize.x, 0)
vertex(vecSize.x, 0)
vertex(0,0)
endShape(CLOSE)
strokeWeight(4)
stroke(col)
noFill()
beginShape(LINES) // the L
vertex(0,0)
vertex(0, vecSize.y-bvl)
vertex(0, vecSize.y-bvl)
vertex(bvl, vecSize.y)
vertex(bvl, vecSize.y)
vertex(vecSize.x, vecSize.y)
endShape()
beginShape(LINES) // the outside
vertex(-bvl, vecSize.y - bvl)
vertex(-bvl, -bvl)
vertex(-bvl, -bvl)
vertex(vecSize.x , -bvl)
vertex(vecSize.x, -bvl)
vertex(vecSize.x + bvl, 0)
vertex(vecSize.x + bvl, 0)
vertex(vecSize.x + bvl, vecSize.y + bvl)
endShape()
pop()
}
function drawCpu(vecPos, vecSize){
push()
fill(255)
translate(vecPos.x + vecSize.x/2, vecPos.y + vecSize.y/2)
let maxSize = max(vecSize.x*0.98, vecSize.y*0.98)/2
for (let i=1; i<cpuHist.length;i++){
let alpha = 360 / cpuHist.length
stroke(map(cpuHist[i],0,100,0,255), map(cpuHist[i],100,0,0,255), 0, map(i,0,cpuHist.length,0,255))
line(cos((i-1)*alpha)*map(cpuHist[i-1],0,100,20,maxSize), sin((i-1)*alpha)*map(cpuHist[i-1],0,100,20,maxSize)
, cos(i*alpha)*map(cpuHist[i],0,100,20,maxSize), sin(i*alpha)*map(cpuHist[i],0,100,20,maxSize))
}
textAlign(CENTER, CENTER);
text("CPU: " + cpuHist.slice(-1) + "%",0,0)
pop()
}
function drawPourtour(col){
push()
strokeWeight(4)
stroke(col)
translate(0,0)
beginShape(LINES)
vertex(bvl+padd, 0+padd) // up
vertex(width/2 - 100, 0+padd)
vertex(width/2 - 100, 0+padd)
vertex(width/2 - 100 + bvl*2, bvl*2+padd)
vertex(width/2 - 100 + bvl*2, bvl*2+padd)
vertex(width/2 + 100 - bvl*2, bvl*2+padd)
vertex(width/2 + 100 - bvl*2, bvl*2+padd)
vertex(width/2 + 100, 0+padd)
vertex(width/2 + 100, 0+padd)
vertex(width-bvl-padd, 0+padd)
vertex(width-bvl-padd, 0+padd) // up right bvl
vertex(width-padd, bvl+padd)
vertex(width-padd, bvl+padd) // right
vertex(width-padd, height-bvl-padd)
vertex(width-padd, height-bvl-padd) // down right bvl
vertex(width-bvl-padd, height-padd)
vertex(width-bvl-padd, height-padd) // down
vertex(bvl+padd, height-padd)
vertex(bvl+padd, height-padd) // down left
vertex(0+padd, height-bvl-padd)
vertex(0+padd, height-bvl-padd) // left
vertex(0+padd, bvl+padd)
vertex(0+padd, bvl+padd)
vertex(bvl+padd, 0+padd)
endShape()
textAlign(CENTER, CENTER)
text("CRASH OS", width/2, padd+txtSize/2)
pop()
}
// OSC
function setupOsc(oscHost, oscPortIn, oscPortOut) {
socket = io.connect('http://127.0.0.1:8082')
socket.on('connect', function() {
socket.emit('config', {
server: {
host: oscHost,
port: oscPortIn
},
client: {
host: oscHost,
port: oscPortOut
}
})
console.log('OSC Ready!\n' + oscHost + ', listen: ' + oscPortIn)
})
socket.on('message', function(msg) {
receiveOsc(msg[0], msg.splice(1))
})
}
function receiveOsc(address, value) {
let codeLine
if (address == "/serverCode"){
codeLine = "SERVER: " + String(value[0])
vectorServer.unshift(codeLine)
colUi = colServer
}
else if (address == "/zbdmCode"){
codeLine = "ZBDM: " + String(value[0])
vectorCode.unshift(codeLine)
colUi = colStandby
}
else if (address == "/svdkCode"){
codeLine = "SVDK: " + String(value[0])
vectorCode.unshift(codeLine)
colUi = colStandby
}
else if (address == "/CPU"){
cpuHist.push(round(value[0],1))
}
if (vectorCode.length > maxLineCode){
vectorCode.pop()
}
if (vectorServer.length > maxLineCode){
vectorServer.pop()
}
}
function drawFft(){
push();
translate(width/2, height/2);
let spectrum = fft.analyze();
noFill();
stroke(255);
// circle vertex
beginShape();
for (i = 0; i < spectrum.length; i++) {
let maxSound = map(spectrum[i], 0,255,0, height/2)
let x = cos(i)*maxSound
let y = sin(i)*maxSound
vertex(x,y);
}
endShape();
pop();
}