GLSLでカエルの歌。

GLSLSoundの練習がてら,子どもが喜びそうなカエルの歌でも書いてみた。 一応ステレオを使って輪唱感を出しているつもり。。。

#define pi2 6.2831
#define bpm 150.0

float calf(float i) {
  return pow(2.,i/12.0);
}
float rand(float time) {
  return fract(sin(time*1e3)*1e6)-0.5;
}
vec2 mainSound(float time) {
  float t = time * bpm / 60.0 + 20.0;

  float[24] ml = float[24](
    3., 5., 7., 8.,
    7., 5., 3., 0.,
    7., 8., 10., 12.,
    10., 8., 7., 0.,
    3., 0., 3., 0.,
    3., 0., 3., 0.);
  
  float[24] lv = float[24](
    1., 1., 1., 1.,
    1., 1., 1., 0.,
    1., 1., 1., 1.,
    1., 1., 1., 0.,
    1., 0., 1., 0.,
    1., 0., 1., 0.);
  
  float f1 = 0.;
  float f2 = 0.;
  int i = int(mod(t, 24.0));
  int i2 = int(mod(t - 4.0, 24.0));
  int i3 = int(mod(t - 8.0, 24.0));
  int i4 = int(mod(t - 12.0, 24.0));
  int i5 = int(mod(t - 16.0, 24.0));
  int i6 = int(mod(t - 20.0, 24.0));

  f1 += sin(pi2*440.*calf(ml[i])*time)*fract(-t)*lv[i];
  f2 += sin(pi2*440.*calf(ml[i2])*time)*fract(-t)*lv[i2] * 0.7;
  f1 += sin(pi2*440.*calf(ml[i3])*time)*fract(-t*2.0)*lv[i3] * 0.49;
  f2 += sin(pi2*440.*calf(ml[i4])*time)*fract(-t*2.0)*lv[i4] * 0.49 * 0.7;
  f1 += sin(pi2*440.*calf(ml[i5])*time)*fract(-t*4.0)*lv[i5] * 0.49;
  f2 += sin(pi2*440.*calf(ml[i6])*time)*fract(-t*4.0)*lv[i6] * 0.49 * 0.7;
  f1 *= 0.6;
  f2 *= 0.6;
  
  float f3 = 0.0;
  f3 += rand(time) * pow(fract(-t), 8.0);
  f3 += rand(time) * pow(fract(-t * 2.0), 16.0);
  f3 += rand(time) * pow(fract(-t * 4.0), 32.0);
  f3 *= 0.2;
  return vec2(f1 + f3, f2 + f3);
}