With the release of the Processing 2.0 the integration of GLSL shaders through the PShader has sort of forced me into properly learning GLSL (not a bad thing - just a steep learning curve) but has thrown up a few questions which if anybody has a pointer would be much appreciated. Ok brief outline of what I'm trying to achieve, I took one of the the examples from the
http://thndl.com
GLSL tutorials and started hacking away to try and make an audio visualization rendered entirely in a fragment shader.
To do this I pass the left and right audio buffer data as float arrays into a GLSL shader and then use this to visualization of the waveforms. The first question is that when trying to index into the array containing the audio data I keep getting runtime errors - I think this is to do with dynamic access and that I need to create a dynamic uniform expression to index the array. I tried to get around this by creating a new array of floats but kept getting a redefinition error saying that I cannot convert a uniform float[] to a float[].
I've posted the code below if anybody could point me in the right direction the last 5 hours of slowly removing my hair will have not been in vain
.
Rich
GLSL Fragment shader
- #ifdef GL_ES
- precision highp float;
- precision highp int;
- #endif
-
- #define PROCESSING_COLOR_SHADER
-
- uniform float time;
- uniform vec2 resolution;
- uniform vec2 mouse;
- uniform float audioLeft[1024];
- uniform float audioRight[1024];
- uniform float bufferSize;
-
- // Required by Processing's default vertex shader
- varying vec4 vertColor;
-
- // f will be used to store the color of the current fragment
- vec4 f = vec4(1.,1.,1.,1.);
-
- void main(void)
- {
- // c will contain the position information for the current fragment (from -1,-1 to 1,1)
- vec2 c = vec2 (-1.0 + 2.0 * gl_FragCoord.x / resolution.x, 1.0 - 2.0 * gl_FragCoord.y / resolution.y) ;
- // mousepos will contain the current mouse position (from -1,-1 to 1,1)
- vec2 mousepos = -1.0 + 2.0 * mouse.xy / resolution.xy;
- float t = time;
- // second problem line of code
- float audioBufferLeft[1024] = audioLeft;
- // problem line of code
- int bufX = int(bufferSize * gl_FragCoord.x / resolution.x);
-
- float yDiff = c.y - audioLeft[bufX];
- float yDiff1 = c.y - audioRight[bufX];
- float y = c.y + yDiff;
- float g = abs(0.3 / y);
- float h = abs(c.x);
- vec3 col = vec3(g * (1.0 - h), g * h, g * 1.5);
- f = vec4(col, 1.0);
- // sin wave
- //float y = c.y + 0.5 * sin(c.x * 3.14 - t);
- //float g = abs(0.1 / y);
- //float h = abs(c.x);
- //vec3 col = vec3(g * (1.0 - h), g * h, g * 1.5);
- //f = vec4(col, 1.0);
- gl_FragColor = f;
- }
Processing Sketch
- import ddf.minim.*;
-
- Minim minim;
- AudioInput in;
-
- PShader myShader;
-
- void setup() {
- size(1024, 1080, P2D);
- noSmooth();
-
- minim = new Minim(this);
-
- in = minim.getLineIn();
-
- myShader = loadShader("shader.glsl");
- myShader.set("resolution", float(width), float(height));
- myShader.set("bufferSize", 1024.0);
- shader(myShader);
- }
-
- void draw() {
- background(0);
- float[] left = new float[in.bufferSize()];
- float[] right = new float[in.bufferSize()];
- for (int i = 0; i < in.bufferSize(); i++)
- {
- left[i] = in.left.get(i);
- right[i] = in.right.get(i);
- }
-
- myShader.set("audioLeft", left);
- myShader.set("audioRight", right);
- myShader.set("time", millis() / 1000.0);
- myShader.set("mouse", float(mouseX), float(mouseY));
- shader(myShader);
-
- noStroke();
- fill(0);
- rect(0, 0, width, height);
-
- resetShader();
- }