Discuss Scratch
- Discussion Forums
- » Show and Tell
- » How to use SongBlocks to make creating music in Scratch easy
- D-ScratchNinja
-
Scratcher
1000+ posts
How to use SongBlocks to make creating music in Scratch easy
SongBlocks is a procedural music generation toolkit built on top of Scratch's Music extension, with a focus on music timing and concurrent tracks.
Scratch's Music extension is very simple. You can play notes and drums and rest for certain stretches of time, change the tempo, choose between several instruments, and that's about it. You can make pretty complex songs if you put the work in, but one thing that you may have tried to figure out how to avoid is keeping your tracks from getting out of sync. It's a bit complicated to explain, but sometimes your music just gets desynced, even though you have supposedly set all the lengths right. Another potential challenge is playing multiple notes at the same time because you have to separate your tracks into dedicated scripts so they run simultaneously. Even if you can do that, it can be hard to manage them all.
It's time that you meet SongBlocks, where I have done all the workarounds for you. Its use of Scratch's clones feature allows notes to be played without your scripts waiting for them to finish, and it automatically tracks and regulates the time and rhythm of your song to completely solve the desync issues. It also has a tracks system so you can separate your melody, drums, bass line, and everything else into its own independent script.
Playing music
To play music with SongBlocks, you will use the custom blocks provided by SongBlocks, which are all available for you to use.
As you start off your song, you'll want to do a little reset:
Tip: This script is already made for you in SongBlocks' start-off template.
Below that, you can start coding your song using SongBlocks' custom blocks. Let's go over them…
You can play a single note and wait for it to end with this block:
You can chain them together to play multiple notes one after another.
If you don't want to wait for your note to finish, use the one that doesn't say “and wait”:
Play your notes all at once like this:
…or all in one block with space-separated values:
Tip: One disadvantage of the SongBlocks custom blocks is that you don't get the keyboard menu when clicking on the number inputs. However, you can always use the Music extension just to get the keyboard menu and then copy the number into a SongBlocks block.
You can pause your melody for a moment with the following blocks. You can rest for either a certain number of beats or a certain number of bars.
You can also pause your melody until a certain beat number with this block:
Tip: You always want to use these blocks to control the rhythm of your song, because they are timed by SongBlocks in a special way to avoid desyncs.
And finally, you can play percussion with this block:
Put them all together to make a song!
Playing multiple tracks
Here's the thing, most songs have many different instruments playing different progressions at the same time. How are you supposed to manage all that with a single script? Well, you branch it out into different scripts, each independent of each other, with SongBlocks' tracks feature.
To start a track, you just need to use this block:
You can set the value to whatever you want, and it doesn't have to be a number; it just identifies the track. The value specified is passed into the “track id” variable.
To play notes in the track you created, wrap the code around this:
You can start more tracks anywhere in your song's code, and you can even start the same track multiple times.
Another reason branching out into tracks is useful is because they each have their own settings.
Track and master settings
SongBlocks allows you to control the volume and other properties about your music. Here's a list of settings that can be set per track and for the entire song:
Any track settings apply only to the track they were set in, while master settings apply to the entire song. Track settings stack on top of master settings.
Tip: Set the master settings in the root script (the script that starts your tracks). This ensures that the properties apply to all tracks on time.
Scripting
You're not limited to SongBlocks features when you write code for your song. You can incorporate other code in your tracks as long as it doesn't interrupt or otherwise interfere with playback (wait blocks and loops outside custom blocks set to run without refreshing the screen are a no-go because they cause the music to pause during execution. Most likely nothing horrible will happen, but just mess up the music for a moment).
For example, you could use variables inside your song's scripts to change the notes played by your song as it progresses. To make songwriting in SongBlocks less repetitive, you'll probably find yourself using local variables inside your tracks to change the progression depending on, say, how many times it has played.
Using non-SongBlocks code inside your song's scripts might also be useful for timing events to music in a game, among other things.
Limitations
SongBlocks relies on Scratch's tempo and timer variables, so if your project already changes those, you could experience some problems. Reading the values of those variables is fine, though.
SongBlocks does not support variable tempo. In fact, changing it mid-song can cause some issues.
Every track and note in SongBlocks takes up a clone, and Scratch allows up to 300 clones to exist at once. If you import SongBlocks into a project that uses a lot of clones, you may encounter issues by adding SongBlocks to it. If you experience issues, try starting less tracks or improving your project's clone management.
Every note played will also be delayed by 1/30th of a second. This is caused by Scratch behavior - anytime you change the volume of a sprite, the script pauses for 1/30th of a second for some reason, and I can't really avoid this. There's also a Scratch bug where if you create a clone of a sprite with its (actual) volume changed, the new clone will not inherit the volume from the parent sprite, so you have to set it again in the new clone.
That concludes the basics of SongBlocks, and I think you're ready to make your own songs! If you need help, check out the SongBlocks project to leave a comment or open the editor and take a look at the template or even the demo song. Feel free to also share your creations in this forum topic or the project comments section. Happy songwriting!
Scratch's Music extension is very simple. You can play notes and drums and rest for certain stretches of time, change the tempo, choose between several instruments, and that's about it. You can make pretty complex songs if you put the work in, but one thing that you may have tried to figure out how to avoid is keeping your tracks from getting out of sync. It's a bit complicated to explain, but sometimes your music just gets desynced, even though you have supposedly set all the lengths right. Another potential challenge is playing multiple notes at the same time because you have to separate your tracks into dedicated scripts so they run simultaneously. Even if you can do that, it can be hard to manage them all.
It's time that you meet SongBlocks, where I have done all the workarounds for you. Its use of Scratch's clones feature allows notes to be played without your scripts waiting for them to finish, and it automatically tracks and regulates the time and rhythm of your song to completely solve the desync issues. It also has a tracks system so you can separate your melody, drums, bass line, and everything else into its own independent script.
Playing music
To play music with SongBlocks, you will use the custom blocks provided by SongBlocks, which are all available for you to use.
As you start off your song, you'll want to do a little reset:
set [role v] to [script]
set [track volume v] to [100]
set [Master Volume v] to [100]
set [next time v] to [0]
reset timer
Tip: This script is already made for you in SongBlocks' start-off template.
Below that, you can start coding your song using SongBlocks' custom blocks. Let's go over them…
You can play a single note and wait for it to end with this block:
play note [60] for (1) beats and wait :: custom
You can chain them together to play multiple notes one after another.
play note [60] for (1) beats and wait :: custom
play note [62] for (1) beats and wait :: custom
play note [64] for (1) beats and wait :: custom
If you don't want to wait for your note to finish, use the one that doesn't say “and wait”:
play note [60] for (1) beats :: custom
Play your notes all at once like this:
play note [60] for (1) beats :: custom
play note [64] for (1) beats :: custom
play note [67] for (1) beats :: custom
…or all in one block with space-separated values:
play note [60 64 67] for (1) beats and wait :: custom
Tip: One disadvantage of the SongBlocks custom blocks is that you don't get the keyboard menu when clicking on the number inputs. However, you can always use the Music extension just to get the keyboard menu and then copy the number into a SongBlocks block.
You can pause your melody for a moment with the following blocks. You can rest for either a certain number of beats or a certain number of bars.
rest for (1) beats :: custom
rest for (1) bars :: custom
You can also pause your melody until a certain beat number with this block:
wait until beat (5) :: custom
Tip: You always want to use these blocks to control the rhythm of your song, because they are timed by SongBlocks in a special way to avoid desyncs.
And finally, you can play percussion with this block:
play drum (1) :: custom
Put them all together to make a song!
Playing multiple tracks
Here's the thing, most songs have many different instruments playing different progressions at the same time. How are you supposed to manage all that with a single script? Well, you branch it out into different scripts, each independent of each other, with SongBlocks' tracks feature.
To start a track, you just need to use this block:
start track [1] :: custom
You can set the value to whatever you want, and it doesn't have to be a number; it just identifies the track. The value specified is passed into the “track id” variable.
To play notes in the track you created, wrap the code around this:
when I start as a clone
if <<(role) = [script]> and <(track id) = [1]>> then
your code here :: grey
delete this clone
end
You can start more tracks anywhere in your song's code, and you can even start the same track multiple times.
Another reason branching out into tracks is useful is because they each have their own settings.
Track and master settings
SongBlocks allows you to control the volume and other properties about your music. Here's a list of settings that can be set per track and for the entire song:
set [track instrument v] to [1] // Set the instrument this track plays
set [track volume v] to [100] // Set the volume of the track
set [track transpose v] to [2] // Move notes on the track up this many semitones
set tempo to (110) :: pen // (Music extension block) Set the tempo in beats per minute
set [Beats Per Bar v] to [4] // Set the time signature
set [Master Volume v] to [100] // Set the volume of the entire song
set [Master Transpose v] to [2] // Move notes on all tracks up this many semitones
Any track settings apply only to the track they were set in, while master settings apply to the entire song. Track settings stack on top of master settings.
Tip: Set the master settings in the root script (the script that starts your tracks). This ensures that the properties apply to all tracks on time.
Scripting
You're not limited to SongBlocks features when you write code for your song. You can incorporate other code in your tracks as long as it doesn't interrupt or otherwise interfere with playback (wait blocks and loops outside custom blocks set to run without refreshing the screen are a no-go because they cause the music to pause during execution. Most likely nothing horrible will happen, but just mess up the music for a moment).
For example, you could use variables inside your song's scripts to change the notes played by your song as it progresses. To make songwriting in SongBlocks less repetitive, you'll probably find yourself using local variables inside your tracks to change the progression depending on, say, how many times it has played.
set [repeats v] to [0]
repeat (2)
change [repeats v] by (1)
play note [61] for (0.5) beats and wait :: custom
play note [61] for (0.5) beats and wait :: custom
if <(repeats) = [1]> then // Do something different after looping
play note [57] for (1) beats :: custom
play note [61] for (1) beats :: custom
play note [64] for (1) beats :: custom
else
play note [59] for (1) beats :: custom
play note [64] for (1) beats :: custom
play note [68] for (1) beats :: custom
end
rest for (1) beats :: custom
end
Using non-SongBlocks code inside your song's scripts might also be useful for timing events to music in a game, among other things.
Limitations
SongBlocks relies on Scratch's tempo and timer variables, so if your project already changes those, you could experience some problems. Reading the values of those variables is fine, though.
SongBlocks does not support variable tempo. In fact, changing it mid-song can cause some issues.
Every track and note in SongBlocks takes up a clone, and Scratch allows up to 300 clones to exist at once. If you import SongBlocks into a project that uses a lot of clones, you may encounter issues by adding SongBlocks to it. If you experience issues, try starting less tracks or improving your project's clone management.
Every note played will also be delayed by 1/30th of a second. This is caused by Scratch behavior - anytime you change the volume of a sprite, the script pauses for 1/30th of a second for some reason, and I can't really avoid this. There's also a Scratch bug where if you create a clone of a sprite with its (actual) volume changed, the new clone will not inherit the volume from the parent sprite, so you have to set it again in the new clone.
That concludes the basics of SongBlocks, and I think you're ready to make your own songs! If you need help, check out the SongBlocks project to leave a comment or open the editor and take a look at the template or even the demo song. Feel free to also share your creations in this forum topic or the project comments section. Happy songwriting!
Last edited by D-ScratchNinja (Feb. 24, 2024 03:33:32)
- Vaibhs11
-
Scratcher
1000+ posts
How to use SongBlocks to make creating music in Scratch easy
This is awesome!
I just have one suggestion,
Add a “play chord” block to the music sprite. Not like the one in the demo sprite, but something like this:
I just have one suggestion,
Add a “play chord” block to the music sprite. Not like the one in the demo sprite, but something like this:
play chord (R::#FF6680) (T::#FF6680) (F::#FF6680) for (time::#FF6680) beats and wait::#FF4D6A
play chord (60) (64) (67) for (1) beats and wait::#FF4D6A
define (play chord (R::#FF6680) (T::#FF6680) (F::#FF6680) for (time::#FF6680) beats and wait::#F24B66 stack)::#FF6680 hat
play note (R::#FF6680) for (time::#FF6680) beats::#FF4D6A
play note (T::#FF6680) for (time::#FF6680) beats::#FF4D6A
play note (F::#FF6680) for (time::#FF6680) beats::#FF4D6A
rest for (time::#FF6680) beats::#FF4D6A
Last edited by Vaibhs11 (Feb. 18, 2024 03:29:48)
- roomsfan17_alt2
-
Scratcher
4 posts
How to use SongBlocks to make creating music in Scratch easy
This seems rather interesting! I'll be trying to see how to use it soon!
wait until [soon v] :: control
learn how to use (SongBlocks by @D-ScratchNinja v) :: sensing
- CozmoGaming
-
Scratcher
36 posts
How to use SongBlocks to make creating music in Scratch easy
how can I download/use it?
- NguyenNgocCatLam
-
Scratcher
1 post
How to use SongBlocks to make creating music in Scratch easy
Cool!!!

- Discussion Forums
- » Show and Tell
-
» How to use SongBlocks to make creating music in Scratch easy
cool stuff