BPM timer: do stuff synced to music

Previous topic - Next topic

r0ber7

Hey,

I made this little thing that takes the BPM of a music track and counts the beats, so you can do stuff synced to your music in a program. This could be useful for doing demo-like stuff. Me, I'm thinking of making the trees in my game bounce to the rhythm of the music. :P

Although I haven't coded it yet, it could be possible to use this in a workaround for the music loop problem GLBasic has. I think, if you know the length of your music file in beats, count until you're at beat n-1, STOPMUSIC, play a soundfile with PLAYSOUND at that point, and PLAYMUSIC after that finishes... But that's just an idea. Anyway, here's the code.

Code (glbasic) Select

// function that returns the ms per beat

FUNCTION beats_init :bpm

LOCAL bps

bps = bpm/60
mspb = 1000/bps

INC mspb, -1

RETURN mspb // milliseconds per beat

ENDFUNCTION

// function that counts the frames and determines whether a beat has been hit

FUNCTION beats_calc :

STATIC totalt //
STATIC oldtotal

IF ISMUSICPLAYING() = FALSE
PLAYMUSIC "Media/qd.mp3", 0
oldtotal = GETTIMERALL()-mspb
totalt = oldtotal+mspb
beat = 4
ELSE
totalt = GETTIMERALL()
ENDIF
// if the difference >= ms per beat, do a beat
IF totalt - oldtotal >= mspb

//STDOUT "dif: "+(totalt - oldtotal)+" mspb: "+mspb+"\n"

// subtract any additional stuff (if difference = 503 and mspb = 500, remove the 3)
IF totalt - oldtotal > mspb
//STDOUT "oldtotal = totalt("+totalt+") - dif("+(totalt - oldtotal)+")-mspb("+mspb+") \n"
//STDOUT "oldtotal = totalt("+totalt+") - "+((totalt - oldtotal)-mspb)+"\n"
oldtotal = totalt - ((totalt - oldtotal)-mspb)
ELSE
oldtotal = totalt
ENDIF

RETURN 1
ENDIF


RETURN 0

ENDFUNCTION


Example in attachment. I made the music in about ten minutes, but it's just a proof of concept. :)

Edit: never mind the //comments, they're old.

[attachment deleted by admin]

bigsofty

Cool, I was just thinking about hosting do this the other day, well done!  :good:
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Hemlos

Nice timer, very accurate and dependable counter, very handy.  :good:

I might incorporate this into a demo im working on.

Is there a way to measure bmp in an mp3 too?

edit: ps. you can extract bpm from mid files if im not mistaking

Bing ChatGpt is pretty smart :O

mentalthink

Thanks I think can be usefull!!!  :nw: :nw:

r0ber7

Quote from: Hemlos on 2012-Nov-02
Nice timer, very accurate and dependable counter, very handy.  :good:

I might incorporate this into a demo im working on.

Is there a way to measure bmp in an mp3 too?

edit: ps. you can extract bpm from mid files if im not mistaking

I have no idea. I usually make my own music, so I know the bpm. Or I ask my musician guy. :P