Discuss Scratch

IndyMine
Scratcher
3 posts

Calculating Accurate Tempo

So, not everyone is aware of this, but Scratch's Music Block plugin doesn't sync with Actual music (in the form of audio files), even when the tempo values are exactly the same. This is possibly due to the fact that Scratch runs at 30FPS and not 60.

Anyways, I'm trying to figure out a way around this. Here's what I have so far, for 175 BPM:

play sound [music that is 175bpm lol v] :: #cf63cf
forever :: #ffab19
Find Time not waited ((120.75) / (91) :: #59c059) :: #ff6680
wait (timeResult :: #ff8c1a) secs :: #ffab19
if <not <(missWait :: #ff8c1a) < (0.04) :: #59c059> :: #59c059> then :: #ffab19
Find Time not waited (missWait :: #ff8c1a) :: #ff6680
wait (timeResult :: #ff8c1a) secs :: #ffab19
change [missWait v] by ((timeResult :: #ff8c1a) * (-1) :: #59c059) :: #ff8c1a
end
play sound [pop v] :: #cf63cf
end

(The Pop Sound effect helps with comparing the tempo with the audio. It's not required.)

(vv Couldn't get this block to change it's color lol vv)
define Find Time not waited (time :: #ff6680) :: #ff6680
set [timeResult v] to (0) :: #ff8c1a
repeat until <((timeResult :: #ff8c1a) + (0.04) :: #59c059) > (time :: #ff6680) :: #59c059> :: #ffab19
change [timeResult v] by (0.04) :: #ff8c1a
end
if <not <(time :: #ff6680) = (missWait :: #ff8c1a) :: #59c059> :: #59c059> then :: #ffab19
change [missWait v] by ((time :: #ff6680) - (timeResult :: #ff8c1a) :: #59c059) :: #ff8c1a
end

You may argue that this would work for every tempo, and you would be correct. However, I want to make it accurate with only one number having to be changed (In this case, I would like it to be compatible with a “Tempo” Variable). From how this script works as of right now, I have to constantly change the numbers in the
 Find Time not waited ((120.75) / (91) :: #59c059) :: #ff6680 
block until it's accurate. What I want changed here, as I previously said, is to be able to accurately change only one variable being changed, and theoretically, that would require a fair bit of detailed math that I do not know how to do. Would anyone be willing to help me figure it out?

Last edited by IndyMine (Feb. 15, 2022 14:51:33)


IndeeMyne
Wait, that doesn't seem right
Hmm… Nah, I totally spelled that correctly



i make cool projects
:]
Scratch-Minion
Scratcher
1000+ posts

Calculating Accurate Tempo

Scratch runs at 30 frames per second.
If you are updating the screen, you cannot check the beats more than 30 times per second.

You can divide the Timer reporter variable by Beats Per Minute to find which beat you are up to.

set [Beat Number v] to (round ((timer) / (Beats Per Minute)))


If you are not updating the screen, you can check the beats more than 30 times per second.
You cannot use “reset timer” within a loop as this slows the code down but you can reference the Timer variable.

Your “Repeat Until” block would repeat until Timer > Beat Number / Beats Per Second and you would then increment Beat Number.
Note that you need Beats Per Second = Beats Per Minute / 60 as the Timer runs in seconds

NB. I suspect that playing the Pop sound inside the loop would wreck the timing as it would slow the code.

Powered by DjangoBB