Given that we don’t know anything about your background or skill level in animation or programming or music, any answers you get will almost certainly be either already blatantly obvious to you or confusingly vague. You simply have to excuse the former and ask questions about the latter. I assure you, no condescension is intended – I simply don’t know how much you already know, while at the same time I’m trying to figure out what and how I want to say it.
I’m not quite sure how you want to use that timer, so let’s back up a little. My simple definition of “beats” is coming from a computer programmer mind-set where everything is very lock-step rigid in terms of time. That works great if the music is computer generated or is strictly quantized, but is far too simplistic if this is a recording of a live performance where the beat can wander all over the place either through human imprecision or intentionally for artistic effect.
For the general case, I would imagine you want to have an array of timestamps, measured from the start of the song, with a count of the number of beats in each interval. If the drummer tends to speed up at the start of each 64-beat bridge section and slow as they leave, you can put an extra timestamp 32 beats in and compute the actual bpm separately for each section. If the music is quantized, you can stick with the strictly linear function of time, but you’ll still want the timestamp intervals to drive your animation.
Give yourself a global variable int interval
that tracks which section you’re in. float timestamp[i]
tells you the start time of each interval (include interval[0] = 0). Include a timestamp for each time that you want to change your animation. At the start of draw() after computing time t
, you advance your interval:
if( t > timestamp[ interval+1 ] ) interval++;
Then you can have a switch statement that controls what animation code you run for each interval:
float intervalTime = t - timestamp[ interval ]; // starts from 0 at the start of each interval
switch( interval ) {
case 0: fadeIn( intervalTime ); break;
case 1: dramaticFlash( intervalTime ); break;
case 2: wavyLines( intervalTime ); break;
}
and so on. Maybe define intervalTime
as a global so you don’t have to pass it around everywhere. Likewise, you might want to make beat
a global.
beat
is a linear (or approximately linear for live music) function that rises 128 beats vertically for each 60 second horizontally. beat % 4
turns it into a triangle wave rising from 0 at the start to just under 4 at the end of each 4/4 time measure. floor( beat % 4 )
would count from 0 to 3 for each beat in each measure. floor( beat / 4 )
would id which measure number you’re in.
colorMode( HSB, 1, 1, 1, 1 );
fill( 1., pow( 1.- (beat % 1), 2 ) );
circle( x, y, r );
would give you a pulse per beat that quickly fades out.