Skip to content

Instantly share code, notes, and snippets.

@paulhoux
Last active March 2, 2018 13:19
Show Gist options
  • Save paulhoux/7544133 to your computer and use it in GitHub Desktop.
Save paulhoux/7544133 to your computer and use it in GitHub Desktop.
Adaptation of ShaderToy's AudioSurf shader for Cinder. Actual audio data is replaced with random noise to keep it simple. I kept the original shader code without making any changes. Note that ```iResolution``` must be passed as a Vec3f.
// by nikos papadopoulos, 4rknova / 2013
// Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
uniform vec3 iResolution; // viewport resolution (in pixels)
uniform float iGlobalTime; // shader playback time (in seconds)
//uniform float iChannelTime[4]; // channel playback time (in seconds)
//uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
//uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
//uniform vec4 iDate; // (year, month, day, time in seconds)
#ifdef GL_ES
precision highp float;
#endif
#define PI 3.14159
#define EPS .001
#define T .03 // Thickness
#define W 2. // Width
#define A .09 // Amplitude
#define V 1. // Velocity
void main(void)
{
vec2 c = gl_FragCoord.xy / iResolution.xy;
vec4 s = texture2D(iChannel0, c * .5);
c = vec2(0., A*s.y*sin((c.x*W+iGlobalTime*V)* 2.5)) + (c*2.-1.);
float g = max(abs(s.y/(pow(c.y, 2.1*sin(s.x*PI))))*T,
abs(.1/(c.y+EPS)));
gl_FragColor = vec4(g*g*s.y*.6, g*s.w*.44, g*g*.7, 1.);
}
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
#include "cinder/app/AppNative.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/GlslProg.h"
#include "cinder/gl/Texture.h"
#include "cinder/Rand.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class AudioShaderApp : public AppNative {
public:
void setup();
void update();
void draw();
private:
gl::GlslProg mShader;
gl::Texture mTexture;
};
void AudioShaderApp::setup()
{
// load and compile shader
try {
mShader = gl::GlslProg( loadAsset("audio_surf_vert.glsl"), loadAsset("audio_surf_frag.glsl") );
}
catch( const std::exception& e )
{
console() << e.what() << std::endl;
}
}
void AudioShaderApp::update()
{
// create a random FFT signal for test purposes
unsigned char signal[1024];
for(int i=0;i<512;++i)
signal[i] = (unsigned char) (Rand::randUint() & 0xFF);
// add an audio signal for test purposes
for(int i=0;i<512;++i)
signal[512+i] = (unsigned char) (Rand::randUint() & 0xFF);
// store it as a 512x2 texture
mTexture = gl::Texture( signal, GL_LUMINANCE, 512, 2 );
}
void AudioShaderApp::draw()
{
gl::clear();
// bind texture to slot 0
mTexture.enableAndBind();
// bind shader and set uniforms
mShader.bind();
mShader.uniform( "iResolution", Vec3f( getWindowWidth(), getWindowHeight(), 0.0f ) );
mShader.uniform( "iGlobalTime", float( getElapsedSeconds() ) );
mShader.uniform( "iChannel0", 0 );
// run full screen pass
gl::drawSolidRect( getWindowBounds() );
// unbind shader and texture
mShader.unbind();
mTexture.unbind();
}
CINDER_APP_NATIVE( AudioShaderApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment