Skip to content

Commit 340b32a

Browse files
committed
Adds audio vec3 uniform
1 parent 1a9ade8 commit 340b32a

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

server/assets/js/audio.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../static/js/audio.js

static/index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
<link rel="stylesheet" href="css/codemirror.css">
5858
<link rel="stylesheet" href="css/default.css">
5959

60+
<script src="https://unpkg.com/meyda/dist/web/meyda.min.js"></script>
6061
<script src="js/lzma.js"></script>
6162
<script src='js/jquery.js'></script>
6263
<script src='js/helpers.js'></script>
6364
<script src="js/codemirror.js"></script>
6465
<script src="js/glsl.js"></script>
66+
<script src="js/audio.js"></script>
6567

6668
<script id="example" type="x-shader/x-fragment">precision mediump float;
6769

@@ -70,6 +72,7 @@
7072
uniform float time;
7173
uniform vec2 mouse;
7274
uniform vec2 resolution;
75+
uniform vec3 audio;
7376

7477
void main( void ) {
7578

@@ -81,7 +84,7 @@
8184
color += sin( position.x * sin( time / 5.0 ) * 10.0 ) + sin( position.y * sin( time / 35.0 ) * 80.0 );
8285
color *= sin( time / 10.0 ) * 0.5;
8386

84-
gl_FragColor = vec4( vec3( color, color * 0.5, sin( color + time / 3.0 ) * 0.75 ), 1.0 );
87+
gl_FragColor = vec4( audio + vec3( color, color * 0.5, sin( color + time / 3.0 ) * 0.75 ), 1.0 );
8588

8689
}</script>
8790

@@ -167,7 +170,7 @@
167170
var code, canvas, gl, buffer, currentProgram, vertexPosition, screenVertexPosition, panButton,
168171
parameters = { startTime: Date.now(), time: 0, mouseX: 0.5, mouseY: 0.5, screenWidth: 0, screenHeight: 0 },
169172
surface = { centerX: 0, centerY: 0, width: 1, height: 1, isPanning: false, isZooming: false, lastX: 0, lastY: 0 },
170-
frontTarget, backTarget, screenProgram, getWebGL, resizer = {}, compileOnChangeCode = true;
173+
frontTarget, backTarget, screenProgram, getWebGL, resizer = {}, compileOnChangeCode = true, audio;
171174

172175
init();
173176
if (gl) { animate(); }
@@ -464,6 +467,8 @@
464467

465468
compileScreenProgram();
466469

470+
audio = new Audio();
471+
467472
}
468473

469474
function isCodeVisible() {
@@ -615,6 +620,7 @@
615620

616621
// Cache uniforms
617622

623+
cacheUniformLocation( program, 'audio' );
618624
cacheUniformLocation( program, 'time' );
619625
cacheUniformLocation( program, 'mouse' );
620626
cacheUniformLocation( program, 'resolution' );
@@ -873,11 +879,13 @@
873879
if ( !currentProgram ) return;
874880

875881
parameters.time = Date.now() - parameters.startTime;
882+
audio.tick();
876883

877884
// Set uniforms for custom shader
878885

879886
gl.useProgram( currentProgram );
880887

888+
gl.uniform3f( currentProgram.uniformsCache[ 'audio' ], audio.fft[0], audio.fft[1], audio.fft[2]);
881889
gl.uniform1f( currentProgram.uniformsCache[ 'time' ], parameters.time / 1000 );
882890
gl.uniform2f( currentProgram.uniformsCache[ 'mouse' ], parameters.mouseX, parameters.mouseY );
883891
gl.uniform2f( currentProgram.uniformsCache[ 'resolution' ], parameters.screenWidth, parameters.screenHeight );

static/js/audio.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
function Audio(numBins, cutoff, smooth, scale) {
4+
5+
numBins = numBins || 3;
6+
smooth = smooth || 0.4;
7+
8+
function tick() {
9+
if (this.meyda) {
10+
var features = this.meyda.get()
11+
if (features) {
12+
var reducer = (accumulator, currentValue) => accumulator + currentValue;
13+
14+
var spacing = Math.floor(features.loudness.specific.length / this.bins.length);
15+
this.prevBins = this.bins.slice(0);
16+
17+
this.bins = this.bins.map((bin, index) =>
18+
features.loudness.specific.slice(index * spacing, (index + 1) * spacing).reduce(reducer) / spacing
19+
).map((bin, index) =>
20+
bin * (1.0 - smooth) + this.prevBins[index] * smooth);
21+
22+
this.fft = this.bins;
23+
}
24+
}
25+
}
26+
27+
function init() {
28+
this.bins = Array(numBins).fill(0)
29+
this.prevBins = Array(numBins).fill(0)
30+
this.fft = Array(numBins).fill(0)
31+
32+
window.navigator.mediaDevices.getUserMedia({ video: false, audio: true })
33+
.then((stream) => {
34+
var context = new AudioContext()
35+
var audio_stream = context.createMediaStreamSource(stream)
36+
37+
this.meyda = Meyda.createMeydaAnalyzer({
38+
audioContext: context,
39+
source: audio_stream,
40+
featureExtractors: [
41+
'loudness',
42+
]
43+
})
44+
})
45+
.catch((err) => console.log('ERROR', err))
46+
47+
return this;
48+
}
49+
50+
return { init: init, tick: tick }.init();
51+
}

0 commit comments

Comments
 (0)