shadermeh/shaders/fft.frag

66 lines
1.4 KiB
GLSL
Raw Normal View History

/*
Linear vs Logarithmic FFT
some good test songs:
https://soundcloud.com/kraddy/winning
https://soundcloud.com/grey-houston/soothing-piano-melody
https://soundcloud.com/pointpoint/life-in-gr
*/
//from https://stackoverflow.com/questions/35799286
float toLog(float value, float min, float max){
float exp = (value-min) / (max-min);
return min * pow(max/min, exp);
}
float getLevel(float samplePos){
// the sound texture is 512x2
int tx = int(samplePos*512.0);
// first row is frequency data (48Khz/4 in 512 texels, meaning 23 Hz per texel)
return texelFetch( iChannel0, ivec2(tx,0), 0 ).x;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xy;
float xPos;
float fft;
if (uv.y > 0.5){
//linear sampling
xPos = uv.x;
fft = getLevel(xPos);
}else{
//crop bottom and top of range
uv.x = mix(0.3,0.7, uv.x);
//logarithmic sampling
xPos = toLog(uv.x, 0.01, 1.0);
fft = getLevel(xPos);
//boost contrast
fft = pow(fft,3.0);
//boost gain
fft *= 1.5;
//contrast / brightness
float contrast = 1.4;
float brightness = 0.;
fft = (fft - 0.5) * contrast + 0.5 + brightness;
}
fragColor = vec4(vec3(fft),1.0);
}