-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.html
230 lines (210 loc) · 7.48 KB
/
web.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/p5.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js" type="text/javascript"></script>
<script src="js/Base64.js" type="text/javascript"></script>
<script src="js/Base64binary.js" type="text/javascript"></script>
<script src="js/WebAudioAPI.js" type="text/javascript"></script>
<script src="js/MIDI.js" type="text/javascript"></script>
<script src="js/dom_request_xhr.js" type="text/javascript"></script>
<script src="js/dom_request_script.js" type="text/javascript"></script>
</head>
<body style="display:none">
<script type="text/javascript">
var loaded = false; //for making sure function draw doesn't run until the file is loaded
var xmlHttp=createXmlHttpObject();
var mac = []; //array for assigning notes and stuff
var channel = [];
var signal = [];
var ssid = [];
var frame;
//intrument array for midi.js
var instrumentlist = ["acoustic_grand_piano", "xylophone", "vibraphone", "agogo", "acoustic_bass"];
//wave illustration stuff
var x = []; //every circle has its variable for lerp (size)
var tx = []; //calculate the width of the text displaying the ssid then used to draw circle
var c = []; //every circle has its variable for lerp (opacity)
var play = []; //condition for whether the circle is enlarging
window.onload = function() {
MIDI.loadPlugin({
soundfontUrl: "./soundfont/",
instruments: instrumentlist,
onsuccess: function() {
document.body.style.display = 'block'; //make sure it shows the web only after it is done loading
readlist(); //for reading list
frame = 0;
loaded = true;
alert('ok!'); //show ok
}
});
};
function createXmlHttpObject(){
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}
return xmlHttp;
}
function textmode(size, inp, convert, x, y, a) //function for calculating/styling text
{
noStroke();
textSize(size);
if (convert == true) {
return textWidth(inp);
} else {
fill(a);
textAlign(CENTER, CENTER);
text(inp, x, y);
}
}
function refresh() {
loaded = false; //make sure to stop funntion draw running
readlist(); //call readlist function
loaded = true; //indicator for function draw to run
}
function readlist() {
channel.length = 0;
signal.length = 0;
ssid.length = 0;
mac.length = 0;
x.length = 0;
c.length = 0;
play.length = 0;
tx.length = 0;
//reset all the data before getting the file, also make sure it stops playing old data
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
xmlHttp.open('GET','http://192.168.1.4/xml',true);
xmlHttp.onreadystatechange=handleServerResponse;
xmlHttp.send(null);
}
//same method used in my arduino sketch
}
function handleServerResponse(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
xmldoc = xmlHttp.responseXML;
var wlist = xmldoc.getElementsByTagName('wifi');
for (var i=0;i < wlist.length;i++) {
ssid[i] = wlist[i].getElementsByTagName('ssid')[0].childNodes[0].nodeValue;
mac[i] = wlist[i].getElementsByTagName('bssid')[0].childNodes[0].nodeValue;
channel[i] = wlist[i].getElementsByTagName('channel')[0].childNodes[0].nodeValue;
signal[i] = wlist[i].getElementsByTagName('signal')[0].childNodes[0].nodeValue;
}
for (i = 0; i < wlist.length; i++) { //total no. of scanned wifi
mac[i] = split(mac[i], ":"); //split is to split the array into smaller nested array, so that FF:FF is into mac[1][1] and mac[1][2]
for (j = 0; j < mac[i].length; j++) {
mac[i][j] = parseInt(mac[i][j], 16); //**convert all hex values to decimal for easier data manipulation
}
}
for (i = 0; i < mac.length; i++) {
x[i] = [];
c[i] = [];
play[i] = [];
tx[i] = textmode(30, ssid[i], true, 0, 0, 0) + 20;
for (j = 0; j < mac[i].length; j++) {
x[i][j] = tx[i];
c[i][j] = 255;
play[i][j] = false;
}
}
}
}
function setup() {
noiseSeed(2017); //make sure everytime it runs the same
createCanvas(1280, 720);
background(0);
frameRate(60);
}
function nmac(x, y, m) { //function for converting and fitting using noise
return map(noise(x, y), 0, 1, 0, m);
}
function draw() {
if (loaded == true) {
background(0, 20);
text("Frame:", 20, 40)
text(frame, 120, 40);
for (i = 0; i < mac.length; i++) {
for (j = 0; j < mac[i].length; j++) {
if (frame % (140 * 10) == int(map(mac[i][j], 0, 255, 0, 128)) * 10 || frame == int(map(mac[i][j], 0, 255, 0, 128)) * 10) {
play[i][j] = false;
x[i][j] = tx[i]; //reset the value!
play[i][j] = true;
}
ring(nmac(mac[i][j], i, width), nmac(mac[i][j], j, height), 300, mac[i][j], i, j, mac[i][j]);
}
}
if (frame % 1350 == 0) {
refresh(); //***each 1350 frame update the wifi.csv!
}
frame++;
}
}
function ring(dx, dy, size, int, n1, n2, col) {
if (play[n1][n2] == true) { //if it is trigger, it can start playing(enlarging the circle)
if (x[n1][n2] == tx[n1]) { //if it just started(the circle is its initial size), play the sound
playins(channel[n1], mac[n1][n2], signal[n1]);
}
//if the circle the within the range of that(*0.8), the text appear
if (x[n1][n2] <= tx[n1] * (map(signal[n1], 20, 400, 1.5, 5) * 0.8)) {
textmode(20, ssid[n1], false, dx, dy, map(signal[n1], 20, 100, 100, 255));
}
//lerp to let it change every frame.
x[n1][n2] = lerp(x[n1][n2], tx[n1] * map(signal[n1], 20, 400, 1.5, 5), 0.03);
c[n1][n2] = lerp(c[n1][n2], 0, 0.05);
//stroke use c as alpha channel.
stroke(nmac(col, 0, 255), nmac(col, 1, 255), nmac(col, 2, 255), c[n1][n2]);
strokeWeight(10);
noFill();
ellipse(dx, dy, x[n1][n2], x[n1][n2]);
// if it reach the max size, since it cannot reach its max value according to lerp, i have to minus 10
if (x[n1][n2] >= tx[n1] * map(signal[n1], 20, 400, 1.5, 5) - 10) {
x[n1][n2] = tx[n1]; //reset size
c[n1][n2] = 255; //back to 255
play[n1][n2] = false; //stop playing the circle(stop enlarging)
}
}
}
function playins(instrument, mvalue, strength) {
var noterange = []; //note range(usually 21-108, but i adjust it)
switch (instrument) {
case "1":
case "3":
case "5":
instrument = 1; //xylophone
noterange = [21, 108];
break;
case "7":
case "9":
case "11":
instrument = 2; //vibraphone
noterange = [21, 108];
break;
case "2":
case "4":
case "6":
instrument = 3; //agogo
noterange = [21, 108];
break;
case "8":
case "10":
case "12":
instrument = 4; //acoustic_bass
noterange = [30, 50];
//strength = strength/2;
break;
default: //other channel
instrument = 0; //piano
dcol = color(255, 0, 255);
noterange = [21, 108];
break;
}
mvalue = int(map(nmac(mvalue, 0, 255), 0, 255, noterange[0], noterange[1])); //mvalue is treated as note, then converted to fit the note range
MIDI.channels[instrument].instrument = MIDI.GM.byName[instrumentlist[instrument]].number; //give each channel a different instrument
MIDI.noteOn(instrument, mvalue, map(strength, 20, 80, 50, 127), 0); //play note
MIDI.noteOff(instrument, mvalue, 0.5); //it's duration
}
</script>
</body>
</html>