Author Topic: BPM timer: do stuff synced to music  (Read 2893 times)

Offline r0ber7

  • Prof. Inline
  • *****
  • Posts: 552
    • View Profile
BPM timer: do stuff synced to music
« on: 2012-Sep-25 »
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]
« Last Edit: 2012-Sep-25 by r0ber7 »

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2747
    • View Profile
Re: BPM timer: do stuff synced to music
« Reply #1 on: 2012-Sep-26 »
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)

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
Re: BPM timer: do stuff synced to music
« Reply #2 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

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Re: BPM timer: do stuff synced to music
« Reply #3 on: 2012-Nov-02 »
Thanks I think can be usefull!!!  :nw: :nw:

Offline r0ber7

  • Prof. Inline
  • *****
  • Posts: 552
    • View Profile
Re: BPM timer: do stuff synced to music
« Reply #4 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