Discuss Scratch
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
RokCoder's MIDI Toolkit
Add and control any MIDI music from within your own project
Add customised visualisations
All music is played solely using the note blocks

Add and control any MIDI music from within your own project
Add customised visualisations
All music is played solely using the note blocks

About the project
This project is meant to be used to add MIDI music into your own projects. You can either remix the project and adapt the well-commented example code or you can backpack the MIDI sprite and add it directly into your own existing project. Note that Scratch 3.0 still has various issues with regards to backpacking sprites and I've sometimes found variable scope or names mysteriously change in the process…
Adding your own music
To sum this up -
- Download the MIDI file that you want to use
- Drag it into this online converter to convert it to hex and copy to the clipboard
- Paste it into the Scratch project from the import option of the Toolkit menu
- There are many free sources for MIDI files on the internet including MidiWorld, FreeMidi, MidiTune, mFiles and BitMidi.
- Convert your MIDI file to he so that it can be imported into the project. I've found an online tool that will do this without the need to download or install anything to your computer. Simply go to the convertor, drag the .mid file onto the drop file here button and click the copy output to clipboard button.
- Run the Scratch project from the editor (i.e. you need to click See Inside). Press = to enter the menu, press 1 to select the import option, paste the hex into the box and press enter.
Using the MIDI Toolkit within your own project
To do this, you need to have the MIDI sprite in your project. You can either remix the MIDI Toolkit project and use the well-commented examples as a start off point or you can put the MIDI sprite into the backpack and add it to your own existing project.
All interaction with the toolkit is done by setting variables and broadcasting messages. Here's a list of what can be done -
Variables used with the toolkit
(MIDI: Song)Set the name or number of the song you want to play or get the number of the song that is playing
(MIDI: Song count)The number of songs available in the song list
Lists used with the toolkit
(MIDI: Midi songs :: list)The song titles of all available tracks in the song list
(MIDI: Visualisation data :: list)Information about notes currently being played - see the visualisation section for more details
Broadcasts you can use with the toolkit
broadcast [MIDI: Menu v] and waitDisplays the tookit to allow music to be added to or removed from the project
broadcast [MIDI: Play v]Plays the song indicated by the MIDI: Song variable
broadcast [MIDI: Stop v]Stops the currently playing song
broadcast [MIDI: Toggle pause v]Pauses or unpauses the currently playing song
Receivers you can use with the toolkit
when I receive [MIDI: Song completed callback v]This will be called when the currently playing song has finished
when I receive [MIDI: reset visualisation v]This is called when a song is about to start - see the visualisation section for more details
when I receive [MIDI: Update visualisation v]This is called every frame while a song is playing - see the visualisation section for more details
Simple examples
Set a random song playing from your song list -
set [MIDI: Song v] to (pick random (1) to (MIDI: Song count))
broadcast [MIDI: Play v]
Set the next song playing when the current one has completed -
when I receive [MIDI: Song completed callback v]
set [MIDI: Song v] to ((MIDI: Song) + (1))
if <(MIDI: Song) > (MIDI: Sound count)> then
set [MIDI: Song v] to (1)
end
broadcast [MIDI: Play v]
Visualisations
Note that you don't need to include any visualisations in your own projects at all.
If you want to implement your own visualisations then you should make use of the MIDI: Reset visualisation and MIDI: Update visualisation broadcast callbacks (as described above).
Use the MIDI:Reset visualisation callback to do any initialisations necessary before the visualisations begin.
Use the MIDI:Update visualisation callback to refresh the visualisations every frame. When you receive this callback you can optionally check the data contained in the MIDI: Visualisation data list. The list contains data for every note that has started on that particular frame in this order -
pitch: a number between 0 and 127 which represents the note being played
duration: the length that the note will be played in seconds
instrument: the instrument being used. Instruments 0 to 127 are musical instruments. Anything above that are percussion instruments
volume: how loud the note is being played (from 0 to 127)
Have a look at the Get visualisation data helper function in the Visualisation sprite for a good method of accessing this data. What that function does is to pull the first four items from the list and to put them into variables called pitch, duration, instrument and volume. It then removes those items from the list. By repeatedly calling that function until the list is empty, you are able to access and use whatever data you wish about all the notes being played on that frame.
You can use any, all or none of the MIDI: Visualisation data as you see fit. The project contains various well-commented examples of how the data can be used.
Any other questions, issues, problems or feedback
Please feel free to add comments to this thread. It's always nice to know that people are using a project (and that it was actually worth my while typing out all of this information!)
Last edited by RokCoder (Feb. 23, 2019 19:22:13)
- HogeezHoagies
-
Scratcher
38 posts
RokCoder's MIDI Toolkit
I keep getting this error while importing a midi: Only format type 1 midi files are currently supported. I'm not exactly sure how to solve this issue.
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
I keep getting this error while importing a midi: Only format type 1 midi files are currently supported. I'm not exactly sure how to solve this issue.
There are three types of MIDI file - type 1 stores an entire composition in one track, type 2 uses multiple tracks and tends to store individual instruments on each track and type 3 stores multiple compositions in multiple tracks. This project only works with the most common type which is type 2.
Do you have a URL for one of the MIDI files you're trying to use? I can take a quick look here to double check it.
- nostickykeys
-
Scratcher
25 posts
RokCoder's MIDI Toolkit
Nice! Someone else made a similar project like this but after Scratch 3.0 was released, His account was unavalible.
- naterpotater26
-
Scratcher
9 posts
RokCoder's MIDI Toolkit
So, when I put this in my project, it played the songs with only one drum and one instrument, and I couldn't change them. Please help!! 

- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
So, when I put this in my project, it played the songs with only one drum and one instrument, and I couldn't change them. Please help!!Can you give me the link to download the MIDI file so I can take a look for you?
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
I always frequently get this kind of error ‘Patch map unassigned for (Number)’ when I backpack it, but not when I remixed the project, it's kinda weird, and I don't know how to fix this, so is there anyway to not do this?
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
Give
I always frequently get this kind of error ‘Patch map unassigned for (Number)’ when I backpack it, but not when I remixed the project, it's kinda weird, and I don't know how to fix this, so is there anyway to not do this?Okay - give me the link to the song you're using and I'll have a go from here. If it works in the remix then there's no reason it shouldn't work in a backpacked version so I'll need to investigate.
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
Here: https://musescore.com/user/25506826/scores/5003742I can't believe nobody else has reported this! Thank you! Some of the lists were for all sprites so the data in those lists wasn't being backpacked. I've fixed that issue so the patch maps for instruments and drums will backpack with the sprite now.
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
Yay! I am so happy that I can finally use it! And I am always happy to help!Here: https://musescore.com/user/25506826/scores/5003742I can't believe nobody else has reported this! Thank you! Some of the lists were for all sprites so the data in those lists wasn't being backpacked. I've fixed that issue so the patch maps for instruments and drums will backpack with the sprite now.
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
Another weird glitch, is now I can't do that broadcast ‘MIDI: Play’ it doesn't seem to work anymore.
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
Another weird glitch, is now I can't do that broadcast ‘MIDI: Play’ it doesn't seem to work anymore.Can you share your project so that I can see what's happening?
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
Here: https://scratch.mit.edu/projects/296325133/Another weird glitch, is now I can't do that broadcast ‘MIDI: Play’ it doesn't seem to work anymore.Can you share your project so that I can see what's happening?
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
Looks fine to me.Here: https://scratch.mit.edu/projects/296325133/Another weird glitch, is now I can't do that broadcast ‘MIDI: Play’ it doesn't seem to work anymore.Can you share your project so that I can see what's happening?
set [MIDI: Song v] to [Test]
broadcast [MIDI: Player v]
or
set [MIDI: Song v] to [1]
broadcast [MIDI: Player v]
Either of those seems to work fine.
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
OKay let me try that.Looks fine to me.Here: https://scratch.mit.edu/projects/296325133/Another weird glitch, is now I can't do that broadcast ‘MIDI: Play’ it doesn't seem to work anymore.Can you share your project so that I can see what's happening?set [MIDI: Song v] to [Test]
broadcast [MIDI: Player v]
orset [MIDI: Song v] to [1]
broadcast [MIDI: Player v]
Either of those seems to work fine.
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
And how do you go to a certain position of a song?
- Kismat12
-
Scratcher
100+ posts
RokCoder's MIDI Toolkit
Like is there any specific variable that goes through the length of the song?
- RokCoder
-
Scratcher
1000+ posts
RokCoder's MIDI Toolkit
And how do you go to a certain position of a song?The Toolkit isn't really designed to let people jump around to different places in songs. It's to import MIDI, play it back, pause it, change songs, etc. That's not to say that it can't be done though. Have a look at how it's done in My Music Player when the sliding bar is moved around.
Like is there any specific variable that goes through the length of the song?




