-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
116 lines (103 loc) · 3.2 KB
/
index.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
/**
* Created by Erdem Celikten & Fabian Schweizer on 06.05.17.
*/
const apis = {
audio: [],
battery: null
};
const audioSuccess = function (localMediaStream) {
const audioCtx = new AudioContext();
const audioSrc = audioCtx.createMediaStreamSource(localMediaStream);
const analyser = audioCtx.createAnalyser();
analyser.fftSize = 256;
audioSrc.connect(analyser);
const frequencyData = new Uint8Array(analyser.frequencyBinCount);
const bins = [];
let loop = true;
frequencyData.forEach(function (e) {
e = document.createElement('div');
e.classList.add('bin');
document.getElementById('bins').appendChild(e);
bins.push(e);
});
setTimeout(() => {
loop = false;
}, 1800);
function renderFrame() {
analyser.getByteFrequencyData(frequencyData);
apis.audio.push(frequencyData);
frequencyData.forEach(function (data, index) {
let width = '1px';
if (data !== 0) {
width = ((data * 100) / 256) + '%';
// width = '400px';
}
bins[index].style.width = width;
});
if (loop) {
requestAnimationFrame(renderFrame);
} else {
gameoflife();
}
}
renderFrame();
function gameoflife() {
let cells = apis.audio;
setInterval(() => {
cells = apis.audio;
apis.audio = draw(cells.map((row, rowIdx) => {
return row.map((col, colIdx) => {
return check(rowIdx, colIdx);
})
}));
console.log('loop');
},200);
function check(rI, cI) {
let alive = 0;
const neighbours = [
[rI - 1, cI - 1],
[rI - 1, cI],
[rI - 1, cI + 1],
[rI, cI - 1],
[rI, cI + 1],
[rI + 1, cI - 1],
[rI + 1, cI],
[rI + 1, cI + 1]
];
neighbours.forEach((p) => {
if (cells[p[0]] !== undefined && cells[p[0]][p[1]] !== 0) {
alive++;
}
});
if (alive === 2 || alive === 3) {
return 1;
}
return 0;
}
function draw(nc) {
const bins = document.getElementById('bins');
bins.innerHTML = '';
nc.forEach((row, rI) => {
const line = document.createElement('div');
const id = `row${rI}`;
line.id = id;
line.classList.add('row');
document.getElementById('bins').appendChild(line);
row.forEach((col) => {
const dot = document.createElement('div');
if (col === 1) {
dot.classList.add('alive');
}
document.getElementById(id).appendChild(dot);
});
});
return nc;
}
}
};
const audioFail = function () {
console.log('Fail');
};
navigator.getUserMedia({audio: true}, function (localMediaStream) {
audioSuccess(localMediaStream)
}, audioFail);