precision highp float;
uniform vec2 res;
uniform float tm,family,density,irregular,sustain,poly,leap,lead,pad,bass,piano,drums,pitch,onset,active,section,deform,detail,glow;
uniform float interval,direction,accentX,accentY,phrase,twist,cut;
uniform vec4 notesA,notesB,agesA,agesB;
uniform vec3 dark,accent,second;
#define PI 3.14159265359
float sat(float x){return clamp(x,0.,1.);}
float bell(float x,float w){return exp(-x*x/max(.0001,w));}
float noteAt(int i){if(i==0)return notesA.x;if(i==1)return notesA.y;if(i==2)return notesA.z;if(i==3)return notesA.w;if(i==4)return notesB.x;if(i==5)return notesB.y;if(i==6)return notesB.z;return notesB.w;}
float ageAt(int i){if(i==0)return agesA.x;if(i==1)return agesA.y;if(i==2)return agesA.z;if(i==3)return agesA.w;if(i==4)return agesB.x;if(i==5)return agesB.y;if(i==6)return agesB.z;return agesB.w;}
void main(){
vec2 uv=(gl_FragCoord.xy-res*.5)/min(res.x,res.y);float t=tm;
float response=deform*(.24+.95*active);float r=length(uv),ang=atan(uv.y,uv.x);
// Past eight attacks form a deterministic, fading, asymmetric deformation map.
float scoreWarp=0.,scoreMark=0.,historyEnergy=0.;
for(int i=0;i<8;i++){
 float n=noteAt(i),a=ageAt(i);float alive=exp(-a*.43)*step(0.,n);float lane=float(i);
 float spoke=ang-(n-.5)*3.7-lane*.44;
 scoreWarp+=sin(spoke*(2.+floor(lane*.5))+r*(4.+n*7.)-a*.34)*alive*(.055+.035*n);
 scoreMark+=bell(sin(spoke+r*(2.+n*2.)-a*.22),.075)*alive*.18;
 historyEnergy+=alive;
}
scoreWarp*=response;scoreMark*=response;
vec2 shift=uv+vec2(accentX,accentY)*.24*response;
shift+=vec2(sin(ang*3.+t*.18),cos(ang*2.-t*.11))*scoreWarp*.39;
shift+=vec2(-uv.y,uv.x)*(.12*direction+.13*twist+.06*interval)*response;
float radius=length(shift),theta=atan(shift.y,shift.x);
float geom=0.,edge=0.,secondary=0.;
float k=1.+detail*(.75+2.0*density);
float stripeWidth=.033+.020*sustain+.014*pad;
if(family<.5){
 // Ribbon: note interval changes path turns and branches, rather than overall brightness.
 float branches=3.+floor(poly*3.+density*4.);
 float curve=theta*branches+radius*(8.+7.*interval)+2.4*scoreWarp+sin(radius*6.+t*.23)*(.25+pitch*.9);
 float path=sin(curve+sin(theta*2.+t*.22)*1.4);
 geom=bell(path,.035+stripeWidth*.4)*(.65+.35*lead)+scoreMark*.25;
 edge=bell(sin(curve*1.8+direction*2.5),.09)*.33;
 secondary=bell(sin(curve*.5-radius*8.),.16)*.22;
}else if(family<1.5){
 // Bloom: actual chord/pad role affects petal width, current pitches create lobes.
 float petals=4.+floor(poly*4.+density*2.);
 float contour=sin(theta*petals+1.5*scoreWarp+t*.22)*( .085+.13*pitch+.09*interval);
 float petals2=sin(theta*(petals+2.)-t*.17+direction*2.)*(.03+.06*lead);
 float outline=radius-(.23+.14*pad+.065*phrase+contour+petals2);
 geom=bell(outline,.0015+.003*pad+.002*piano)+.54*bell(outline+.17,.003);
 edge=bell(sin(theta*petals*2.+radius*13.+scoreWarp*3.),.07)*(.24+.30*piano);
 secondary=bell(radius-.12-.09*sin(theta*(petals-1.)-t*.2),.004)*.6;
}else if(family<2.5){
 // Tunnel: note-driven bends and side branches break the uniform concentric rings.
 vec2 bent=shift+vec2(.19*sin(theta*2.+t*.17+scoreWarp*5.),.16*cos(theta*3.-t*.13+scoreWarp*6.))*response;
 float br=length(bent),ba=atan(bent.y,bent.x);
 float depth=1./(.095+br);
 float crooked=sin(ba*(3.+floor(poly*2.))+depth*.45+t*.18)*(.25+.65*interval)+scoreWarp*(3.+4.*pitch);
 float rings=sin(depth*(2.+k*.75)+crooked*2.-t*.8+direction*1.8);
 float gate=sin(ba*(4.+floor(density*5.))+scoreWarp*2.+t*.15);
 geom=bell(rings,.030+.020*sustain)*(.56+.44*smoothstep(-.5,.5,gate));
 edge=bell(sin(depth*.7+ba*(3.+ceil(leap*5.))+scoreWarp*6.),.065)*.34;
 secondary=bell(sin(ba*(2.+floor(poly*3.))+depth*.9+direction*2.),.04)*.42*(.3+.7*lead);
}else if(family<3.5){
 // Lattice: subdivisions, breaks and swing reshape cell topology.
 vec2 q=shift*(3.5+k*2.8);q.x+=sin(q.y*.8+t*.2+scoreWarp*6.)*(.16+.64*interval);
 q.y+=cos(q.x*.5-t*.2)*( .10+.45*drums+.20*direction);
 float xx=sin(q.x*PI+scoreWarp*2.5),yy=sin(q.y*PI-scoreWarp*2.);
 float marks=max(bell(xx,.028),bell(yy,.028));
 float breaks=smoothstep(-.50,.32,sin(q.x*.65+q.y*1.12+cut*3.+t*.15));
 geom=marks*breaks;
 edge=bell(sin(q.x+q.y+direction*2.),.05)*.38;
 secondary=bell(sin(q.x*1.7-q.y*.8+pitch*5.),.08)*.4;
}else if(family<4.5){
 float sectorCount=floor(8.+k*5.);float sector=sin(theta*sectorCount);
 float crack=sin(sector*12.8+floor(radius*18.)*9.1+scoreWarp*14.+direction*4.);
 float slash=sin(theta*floor(7.+k*2.)+radius*(15.+10.*interval)+crack*(.5+pitch));
 geom=bell(slash,.016+.015*pad)*(.6+.4*crack);
 edge=bell(sin(sector*.6+radius*22.+scoreWarp*6.),.06)*.46;
 secondary=bell(sin(radius*37.+sector*2.),.035)*.4;
}else{
 // Flowing field: waves use note-shape traces rather than only uniform oscillation.
 float stream=shift.y+sin(shift.x*5.+t*.22+scoreWarp*5.)*(.09+.14*pitch)+sin(shift.x*2.-t*.16)*(.06+.18*pad);
 float lines=sin(stream*(14.+density*11.)+scoreWarp*6.);
 geom=bell(lines,.045)+.14*scoreMark;
 edge=bell(sin(lines*1.7+stream*7.),.12)*.28;
 secondary=bell(sin(stream*(7.+poly*9.)+direction*3.),.08)*.35;
}
// Independent instrument layers influence structure and the color mix.
float field=bell(radius-(.22+.16*pad),.09+.11*pad)*pad;
float core=exp(-radius*(4.+2.*bass))*(.3+.7*bass);
float ripples=bell(sin(radius*(25.+density*15.)-t*1.7-cut*2.5),.10)*(onset*.26+drums*.30);
float phraseLift=.40+.60*section;
float ink=geom*phraseLift*(.43+.34*lead+.25*piano+.18*active);
vec3 col=dark*(.86+.22*field)+accent*(ink+secondary*.27+core*.12)+second*(edge*.45+field*.42+ripples*.65+scoreMark*.22);
col+=mix(accent,second,.5)*scoreMark*.12;
col*=.70+.34*glow;
col*=1.-.30*smoothstep(.52,1.1,r);
gl_FragColor=vec4(clamp(col,0.,1.),1.);
}
