Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Accurate Metronome
- succulentim
- Scratcher
6 posts
Accurate Metronome
Call me crazy but I decided I wanted to try finally solving the issue of syncing animation to music in Scratch!
The issue is that every script takes time to happen, and so if you tell Scratch to wait a fraction of a second before doing the animation, it will wait that fraction of a second plus the extra time it took to run. Over time this builds up and the animation goes out of sync.
A solution to this kind of issue (that I learned from a youtube video about building a drum machine in excel) is to use the time from your computer (or, in Scratch, I've been trying to use the timer) to have a steady flow of time to schedule the animations to. Instead of something like
we have something more like
(BPM stands for beats per minute, a measure of tempo. Divided by 60, we get beats per second.)
This way the costume will change on the beat exactly when we want it to.
However, in the project I was making trying to put this into practice, it hasn't been working. I'll post the wip so that hopefully anyone willing to help or give it a go can tinker with it. I'm not very good at math so that might be a problem, but I think the roadblock I'm running into might be due to the timer moving too fast for Scratch to pick up on it?
Either way, if anyone has any ideas or suggestions on how to make this work, or can point out any mistakes I'm making, I'd really appreciate it! Thank you!
The issue is that every script takes time to happen, and so if you tell Scratch to wait a fraction of a second before doing the animation, it will wait that fraction of a second plus the extra time it took to run. Over time this builds up and the animation goes out of sync.
A solution to this kind of issue (that I learned from a youtube video about building a drum machine in excel) is to use the time from your computer (or, in Scratch, I've been trying to use the timer) to have a steady flow of time to schedule the animations to. Instead of something like
wait (0.3) secs
next costume
we have something more like
wait until <(timer)=((timer) + ((BPM) / (60)))>
next costume
(BPM stands for beats per minute, a measure of tempo. Divided by 60, we get beats per second.)
This way the costume will change on the beat exactly when we want it to.
However, in the project I was making trying to put this into practice, it hasn't been working. I'll post the wip so that hopefully anyone willing to help or give it a go can tinker with it. I'm not very good at math so that might be a problem, but I think the roadblock I'm running into might be due to the timer moving too fast for Scratch to pick up on it?
Either way, if anyone has any ideas or suggestions on how to make this work, or can point out any mistakes I'm making, I'd really appreciate it! Thank you!
Last edited by succulentim (Jan. 9, 2020 02:30:38)
- nolbarry
- Scratcher
100+ posts
Accurate Metronome
A couple issues that I've noticed:
For starters:
Also, you should use 60 / BPM instead of BPM / 60 to get the number of seconds between each beat.
My solution resets the timer each “frame” and then plays the next frame when the timer passes the length of a single beat:
For starters:
<(timer)=((timer) + ((BPM) / (60)))>This will never evaluate to true: timer cannot both be itself and a value slightly more than itself.
Also, you should use 60 / BPM instead of BPM / 60 to get the number of seconds between each beat.
My solution resets the timer each “frame” and then plays the next frame when the timer passes the length of a single beat:
forever
reset timer
wait until <(timer) > ((60) / (BPM))>
next frame :: custom
end
Last edited by nolbarry (Jan. 9, 2020 06:42:57)
- succulentim
- Scratcher
6 posts
Accurate Metronome
A couple issues that I've noticed:
For starters:<(timer)=((timer) + ((BPM) / (60)))>This will never evaluate to true: timer cannot both be itself and a value slightly more than itself.
Also, you should use 60 / BPM instead of BPM / 60 to get the number of seconds between each beat.
My solution resets the timer each “frame” and then plays the next frame when the timer passes the length of a single beat:forever
reset timer
wait until <(timer) > ((60) / (BPM))>
next frame :: custom
end
thank you so much! this helped a ton!
- nolbarry
- Scratcher
100+ posts
Accurate Metronome
I think my using the “reset timer” block is offsetting it a little bit.
Instead of resetting the timer, try using a variable.
Instead of resetting the timer, try using a variable.
reset timerThis way, your beat variable remains totally independent of the loop waiting time.
set [nextBeatTime v] to (0)
forever
change [nextBeatTime v] by ((60) / (BPM))
wait until <(timer) > (nextBeatTime)>
next frame :: custom
end
Last edited by nolbarry (Jan. 10, 2020 19:46:13)
- succulentim
- Scratcher
6 posts
Accurate Metronome
I think my using the “reset timer” block is offsetting it a little bit.
Instead of resetting the timer, try using a variable.reset timerThis way, your beat variable remains totally independent of the loop waiting time.
set [nextBeatTime v] to (0)
forever
change [nextBeatTime v] by ((60) / (BPM))
wait until <(timer) > (nextBeatTime)>
next frame :: custom
end
thank you so much, it works great now!! it no longer needs to be in turbo mode to work, and i could add back in the graphic effects that were causing program-breaking lag. thank you for the help, i learned a lot from it!
this thread is now resolved! (sorry, i can't edit the first post yet, haha)
Last edited by succulentim (Jan. 11, 2020 22:04:22)
- 3DemonGamer37
- Scratcher
1 post
Accurate Metronome
The Scratch Wiki really needs a metronome tutorial page, but I don't have an account, and by the time I set it up, some dude'll beat me to it.
- BigNate469
- Scratcher
500+ posts
Accurate Metronome
Please don't necropost. This topic is over 4 years old, and the OP seems to have gotten what they wanted. The Scratch Wiki really needs a metronome tutorial page, but I don't have an account, and by the time I set it up, some dude'll beat me to it.
Last edited by BigNate469 (March 29, 2024 00:09:26)
Highlight any part of this signature and press ctrl+shift+down arrow to see the rest of it
foreverPlease read the list of Officially Rejected Suggestions before posting a suggestion for Scratch! 100th post
if <person asks [what's a signature] :: sensing> then
Redirect to [https://en.scratch-wiki.info/wiki/Signature] :: motion
end
end
This signature is designed to be as helpful as possible.
View all of the topics you've posted in:
https://scratch.mit.edu/discuss/search/?action=show_user&show_as=topics
View all of your posts:
https://scratch.mit.edu/discuss/search/?action=show_user&show_as=posts
Forum tips:
Don't title topics something like “HELP ME!!!”. It's not helpful, and won't get you as many responses.
Don't post in topics where the latest post is over ~2 months old, unless you have something critical to add. Especially in topics that are several years old- it isn't helpful, and is known as necroposting.
Don't post unrelated things in topics, including questions of your own. Make a new topic for your questions.
You can use the
[code] and [/code]
You can use the
[color=color name or hexadecimal value here] and [/color]
Little-known Scratch URLs:
scratch.pizza (redirects to main page)
https://scratch.mit.edu/projects/PROJECT ID HERE/remixtree (replace “PROJECT ID HERE” with project id number. Shows all the remixes of the project, and the remixes of those projects, and the remixes of those projects, and so on, as a chart. Link currently redirects to one of my projects)
- Discussion Forums
- » Help with Scripts
- » Accurate Metronome