-
Notifications
You must be signed in to change notification settings - Fork 0
/
musalizer.html
469 lines (417 loc) · 17.8 KB
/
musalizer.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title id="titre">Muzaliser</title>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- END Bootstrap -->
<link rel="stylesheet" href="css/stylesheet.css">
<script src="js/commonFunctions.js"></script>
<script src="js/gl-matrix-min.js"></script>
<script src="js/webglTools.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec4 aColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
varying vec3 vPos;
void main(void) {
gl_PointSize = 5.0;
vColor = aColor;
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vPos = aVertexPosition;
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec4 vColor;
varying vec3 vPos;
void main(void) {
//gl_FragColor = vColor;
float increment = vPos.z/2.0;
//gl_FragColor = vec4(vColor.r + increment,vColor.b + increment,vColor.g + increment,vColor.a);
gl_FragColor = vec4(
vColor.r - ((1.0 - vColor.r)/2.0)*vPos.z,
vColor.g - ((1.0 - vColor.g)/2.0)*vPos.z,
vColor.b - ((1.0 - vColor.b)/2.0)*vPos.z,
vColor.a);
}
</script>
<script>
var vertexBuffer = null;
var indexBuffer = null;
var colorBuffer = null;
var indices = [];
var vertices = [];
var colors = [];
//Tableau servant de liste d'attente
var tabLignes = [];
var verticesTemp = [];
var colorsTemp = [];
var rowsVertices = [];
var rowsColors= [];
var rotY = 0;
var rotX = 0;
//On choisi si on veut de la perspective ou non
var perspective = true;
//Couleur de base :
var red = 0.96;
var green = 0.496;
var blue = 0.184;
//Couleur destination :
var redDest = 0.96;
var greenDest = 0.496;
var blue = 0.184;
//Pas couleur
var pasRed = 0.0;
var pasGreen = 0.0;
var pasBlue = 0.0;
var mvMatrix = mat4.create();
var pMatrix = mat4.create();
var frequencyData = [];
const MAX_VALUE = 255;
const MIN_VALUE = 0;
//const NB_FREQUENCIES = 1024;
const NB_FREQUENCIES = 660;
const NB_ROWS = 30;
const NB_ITERATION_FOR_COLOR_CHANGING = 300;
const INVERSE_NB_ITERATION_FOR_COLOR_CHANGING = 1/NB_ITERATION_FOR_COLOR_CHANGING;
window.onkeydown = checkKey;
//On regarde pour la rotation
function checkKey(ev){
//Pour garder la compatibilité IE...
var key = ev.which || ev.keyCode;
switch(key)
{
case 65:{
rotY -= 2.0;
break;
}
case 68:{
rotY += 2.0;
break;
}
case 87:{
rotX -= 2.0;
break;
}
case 83:{
rotX += 2.0;
break;
}
case 79:{
option_modal();
break;
}
default:
break;
}
}
function cameraFct(perspective)
{
var stretch_width = 0.45;
translationMat = mat4.create();
mat4.identity(translationMat);
if( perspective )
{
//La multiplication par stretch_width permet d'étirer l'affichage sur la largeur
mat4.perspective(pMatrix, degToRad(40.0), c_width*stretch_width / c_height, 0.1, 10000.0);
var tx = 0.0;
var ty = 0.0;
var tz = -4.0;
mat4.translate(translationMat,translationMat, [tx, ty, tz]);
}
else
{
mat4.identity(pMatrix);
mat4.ortho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0, pMatrix);
}
rotateModelViewMatrixUsingQuaternion(true);
glContext.uniformMatrix4fv(prg.pMatrixUniform, false, pMatrix);
glContext.uniformMatrix4fv(prg.mvMatrixUniform, false, mat4.multiply(mat4.create(), translationMat, mvMatrix));
}
function initShaderParameters(prg) {
prg.vertexPositionAttribute = glContext.getAttribLocation(prg, "aVertexPosition");
glContext.enableVertexAttribArray(prg.vertexPositionAttribute);
prg.colorAttribute = glContext.getAttribLocation(prg, "aColor");
glContext.enableVertexAttribArray(prg.colorAttribute);
prg.pMatrixUniform = glContext.getUniformLocation(prg, 'uPMatrix');
prg.mvMatrixUniform = glContext.getUniformLocation(prg, 'uMVMatrix');
initializeArrayTwoDim();
}
//Initialisation des tableaux à zéro
function initializeArrayTwoDim()
{
var i = 0;
var index = 0;
var j = 0;
var pasX = 2.0 / NB_FREQUENCIES;
var pasZ = 2.0 / NB_ROWS;
for(i = -1.0; i< 1.0;i+=pasZ)
{
rowsVertices.push([]);
rowsColors.push([]);
for(j = -1.0; j< 1.0;j+=pasX)
{
rowsVertices[index].push(j,-1.0,i);
rowsColors[index].push(0.0,0.0,0.0,1.0);
}
index++;
}
//Nos indices ne vont jamais bouger donc on peut les initialiser ici
var nbFreq = NB_FREQUENCIES-2;
for(i = 0; i<NB_ROWS-2;i++)
{
for(j =0;j<nbFreq;j++)
{
indices.push(i*NB_FREQUENCIES + j, (i+1)*NB_FREQUENCIES + j, (i+1)*NB_FREQUENCIES + j + 1);
indices.push(i*NB_FREQUENCIES + j, i*NB_FREQUENCIES + j +1, (i+1)*NB_FREQUENCIES + j + 1);
}
}
}
//On déplace toutes nos lignes vers "le bas". On ne change pas la première ligne car elle va être de toute manière changée par la suite
function shiftRows()
{
var pasZ = 2.0 / NB_ROWS;
var z = -1.0;
var i = NB_ROWS-1;
var j = 0;
for(i = 0; i < NB_ROWS-1;i++)
{
rowsVertices[i] = [];
rowsVertices[i] = rowsVertices[i].concat(rowsVertices[i+1]);
rowsColors[i] = [];
rowsColors[i] = rowsColors[i].concat(rowsColors[i+1]);
for(j =2; j<rowsVertices[i].length;j+=3)
{
rowsVertices[i][j] = z;
}
z += pasZ;
}
}
function initBuffers()
{
var index = -1.0;
var indice = 0;
var y = 0;
colors = [];
vertices = [];
if(frequencyData.length > 0)
{
//On décale nos lignes
shiftRows();
modifyColor();
rowsVertices[NB_ROWS-1] = [];
rowsColors[NB_ROWS-1] = [];
var pas = 2.0 / NB_FREQUENCIES;
for(index = -1.0; index < 1.0;index+=pas)
{
y = (frequencyData[indice]/MAX_VALUE)-1.0;
//On repli des tableaux temporaires afin d'en ajouter 3 à la suite
//Cela permet d'être sûr qu'on ajoute bien 3 éléments à la fois pour créer des triangles
rowsVertices[NB_ROWS-1].push(index,y,1.0);
rowsColors[NB_ROWS-1].push(red,green,blue,1.0);
//On remet nos variables servant de file d'attente à zéro
verticesTemp = [];
colorsTemp = [];
indice++;
}
}
var i = 0;
for(i = 0 ; i< NB_ROWS;i++)
{
vertices = vertices.concat(rowsVertices[i]);
colors = colors.concat(rowsColors[i]);
}
vertexBuffer = getVertexBufferWithVertices(vertices);
colorBuffer = getVertexBufferWithVertices(colors);
indexBuffer = getIndexBufferWithIndices(indices);
}
function modifyColor()
{
if(equals(red,redDest,0.000001))
{
redDest = Math.random();
greenDest = Math.random();
blueDest = Math.random();
pasRed = (redDest - red) * INVERSE_NB_ITERATION_FOR_COLOR_CHANGING;
pasGreen = (greenDest - green) * INVERSE_NB_ITERATION_FOR_COLOR_CHANGING;
pasBlue = (blueDest - blue) * INVERSE_NB_ITERATION_FOR_COLOR_CHANGING;
}
red += pasRed;
blue += pasBlue;
green += pasGreen;
}
function equals(nb1, nb2, epsilon)
{
return (Math.abs(nb1 - nb2) < epsilon);
}
function degToRad(degrees)
{
return( degrees * Math.PI / 180.0 );
}
function drawScene()
{
//Evite une erreur avec Chromium car il semble se lancer dans le dessin de la scène avant que le contexte soit crée...
if(glContext == null)
{
return;
}
glContext.clearColor(0.0, 0.0, 0.0, 1.0);
glContext.enable(glContext.DEPTH_TEST);
glContext.clear(glContext.COLOR_BUFFER_BIT | glContext.DEPTH_BUFFER_BIT);
glContext.viewport(0, 0, c_width, c_height);
glContext.depthFunc(glContext.LESS);
cameraFct(perspective);
glContext.bindBuffer(glContext.ARRAY_BUFFER, vertexBuffer);
glContext.vertexAttribPointer(prg.vertexPositionAttribute, 3, glContext.FLOAT, false, 0, 0);
glContext.bindBuffer(glContext.ARRAY_BUFFER, colorBuffer);
glContext.vertexAttribPointer(prg.colorAttribute, 4, glContext.FLOAT, false, 0, 0);
glContext.bindBuffer(glContext.ELEMENT_ARRAY_BUFFER, indexBuffer);
glContext.drawElements(glContext.TRIANGLES,indices.length,glContext.UNSIGNED_SHORT,0);
}
function initWebGL()
{
glContext = getGLContext('webgl-canvas');
initProgram();
initBuffers();
renderLoop();
}
</script>
</head>
<body onload="onBodyLoad()">
<div class="canvas-and-player full-width" id="canvas-div">
<canvas id="webgl-canvas" height="500">
Your browser doesn't support HTML5 :'(
</canvas>
</div>
<!-- Audio file modal -->
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" id="modal-musique">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Choisissez votre musique</h3>
</div>
<div class="modal-body">
<input id="audio_file" type="file" accept="audio/*" />
</div>
</div>
</div>
</div>
<!-- Option modal -->
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel2" id="modal-option">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Muzalizer</h3>
</div>
<div class="modal-body">
<div class="row">
</div>
<div class="row"
<div id="player-div" class="col-xs-12 col-md-12">
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Ouvre la fenêtre modal des le chargement -->
<script type="text/javascript">
$(window).load(function(){
ouvrirModal();
});
function ouvrirModal()
{
$('#modal-musique').modal({
backdrop: 'static',
keyboard: false
})
$('#modal-musique').modal('show');
}
function option_modal()
{
if(($("#modal-option").data('bs.modal') || {}).isShown)
{
$('#modal-option').modal('hide');
}
else {
$('#modal-option').modal('show');
}
}
</script>
<script>
var canvas = document.getElementById('webgl-canvas');
var index = 0;
var largeurCarre = 4;
function onBodyLoad()
{
updateCanvasSize();
initWebGL();
}
function updateCanvasSize()
{
var canvas = document.getElementById("webgl-canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
c_width = canvas.width;
c_height = canvas.height;
}
audio_file.onchange = function()
{
//On ferme la fenêtre modal
$('#modal-musique').modal('hide');
//Permet de masquer les scrolls bars qui apparaissent sans que je sache pourquoi :/
//Source des deux lignes suivantes : http://stackoverflow.com/questions/26745292/canvas-toggle-filling-whole-page-removing-scrollbar
document.body.scrollTop = 0; // <-- pull the page back up to the top
document.body.style.overflow = 'hidden'; // <-- relevant addition
//On retire les balises audio déjà présente
var audioDiv = document.getElementById("player-div");
while(audioDiv.firstChild)
{
audioDiv.removeChild(audioDiv.firstChild);
}
var files = this.files;
//On regarde si Window.URL existe, si ce n'est pas le cas, on utilie webkit
//ça permet de savoir si on utilise Firefox ou Chrome/Chromium
var file = (window.URL ? URL : webkitURL).createObjectURL(files[0]);
var audio = new Audio();
audio.src = file;
audio.id = 'player';
audio.controls = true;
audio.autoplay = true;
audioDiv.appendChild(audio);
var context = new AudioContext();
var analyser = context.createAnalyser();
analyser.fftSize = 2048;
//La "range" est de [0 , 255] dans le domaine N
frequencyData = new Uint8Array(analyser.frequencyBinCount);
var source = context.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(context.destination);
$(audio).bind("ended",function(){
location.reload();
});
function renderFrame() {
requestAnimationFrame(renderFrame);
// met à jour les données dans frequenCyData
analyser.getByteFrequencyData(frequencyData);
initBuffers();
}
renderFrame();
};
window.onresize = function(event)
{
updateCanvasSize();
};
</script>
<script src="js/mouseMotionHandling.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha256-Sk3nkD6mLTMOF0EOpNtsIry+s1CsaqQC1rVLTAy+0yc= sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
</body>
</html>