-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
193 lines (152 loc) · 5.11 KB
/
main.js
File metadata and controls
193 lines (152 loc) · 5.11 KB
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
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import colormap from 'colormap'
// init scene
const frequencySamples = 256
const timeSamples = 400
const data = new Uint8Array(frequencySamples)
const nVertices = (frequencySamples + 1) * (timeSamples + 1);
let xSegments = timeSamples;
let ySegments = frequencySamples;
let xSize = 40;
let ySize = 20;
let xHalfSize = xSize / 2;
let yHalfSize = ySize / 2;
let xSegmentSize = xSize / xSegments; //Size of one square
let ySegmentSize = ySize / ySegments;
function initialUserInput() {
return new Promise((resolve) => {
document.getElementById("btn").addEventListener('click', function () {
/// do something to process the answer
let overlay = document.getElementById("overlay")
overlay.style.opacity = 0;
setTimeout(() => {
overlay.style.visibility = "hidden"
}, 500)
resolve(true);
}, { once: true });
});
}
const init = async function() {
await initialUserInput()
console.log("hello")
//Init audio
const ACTX = new AudioContext();
const ANALYSER = ACTX.createAnalyser();
ANALYSER.fftSize = 4 * frequencySamples;
ANALYSER.smoothingTimeConstant = 0.5;
const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: false } })
const SOURCE = ACTX.createMediaStreamSource(mediaStream);
SOURCE.connect(ANALYSER);
console.log(SOURCE)
let paused = false
document.body.onkeyup = function (e) {
if (e.key == " " ||
e.code == "Space" ||
e.keyCode == 32
) {
paused = !paused;
}
}
const width = window.innerWidth;
const height = window.innerHeight;
const camera = new THREE.PerspectiveCamera(20, width / height, 1, 1000);
camera.position.set(0, 0, 75);
const scene = new THREE.Scene();
const geometry = new THREE.BufferGeometry();
let indices = [];
let heights = [];
let vertices = [];
const yPowMax = Math.log(ySize);
const yBase = Math.E
// generate vertices for a simple grid geometry
for (let i = 0; i <= xSegments; i++) {
let x = (i * xSegmentSize) - xHalfSize; //midpoint of mesh is 0,0
for (let j = 0; j <= ySegments; j++) {
let pow = (ySegments - j) / ySegments * yPowMax;
let y = -Math.pow(yBase, pow) + yHalfSize + 1;
vertices.push(x, y, 0);
heights.push(0); // for now our mesh is flat, so heights are zero
}
}
for (let i = 0; i < xSegments; i++) {
for (let j = 0; j < ySegments; j++) {
let a = i * (ySegments + 1) + (j + 1);
let b = i * (ySegments + 1) + j;
let c = (i + 1) * (ySegments + 1) + j;
let d = (i + 1) * (ySegments + 1) + (j + 1);
// generate two faces (triangles) per iteration
indices.push(a, b, d); // face one
indices.push(b, c, d); // face two
}
}
geometry.setIndex(indices);
heights = new Uint8Array(heights);
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
geometry.setAttribute('displacement', new THREE.Uint8BufferAttribute(heights, 1));
const colors = colormap({
//inferno, electric
colormap: 'electric',
nshades: 256,
format: 'rgba',
alpha: 1
})
colors[0] = [0, 0, 0, 0]
console.log(colors)
const lut = colors.map(color => {
const red = color[0] / 255
const green = color[1] / 255
const blue = color[2] / 255
return new THREE.Vector3(red, green, blue)
})
console.log(lut)
//Grab the shaders from the document
var vShader = document.getElementById('vertexshader');
var fShader = document.getElementById('fragmentshader');
// Define the uniforms. V3V gives us a 3vector for RGB color in out LUT
var uniforms = {
vLut: { type: "v3v", value: lut }
}
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
const container = document.getElementById('Spectrogram');
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.maxPolarAngle = Math.PI / 2;
controls.minPolarAngle = Math.PI / 2;
controls.minAzimuthAngle = 5 * Math.PI / 3;
controls.maxAzimuthAngle = -5 * Math.PI / 3;
controls.update();
const material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: vShader.text,
fragmentShader: fShader.text
});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
//mesh.geometry.computeFaceNormals();
mesh.geometry.computeVertexNormals();
// ---
const animate = function () {
requestAnimationFrame(animate);
controls.update();
render()
}
const render = function () {
if (!paused) {
updateGeometry()
};
renderer.render(scene, camera);
}
const updateGeometry = function () {
ANALYSER.getByteFrequencyData(data);
const startVal = frequencySamples + 1;
const endVal = nVertices - startVal;
heights.copyWithin(0, startVal, nVertices + 1);
heights.set(data, endVal - startVal);
mesh.geometry.setAttribute('displacement', new THREE.Uint8BufferAttribute(heights, 1));
}
animate();
}
init()