A LEGOWeDo 2.0 floating spaceship model based on tensegrity
Tensegrity with LEGO: Floating Spaceship
April 27, 2020
Show all

Adding custom sounds to LEGO MINDSTORMS Robot Inventor / SPIKE projects

add custom sounds to SPIKE and MINDSTORMS

Adding custom sounds in MIT Scratch 3.0

If you want to add a custom sound to your Scratch project, in the online Scratch programming IDE there’s a pretty straightforward way to do it.

how to upload a custom sound file to your online MIT Scratch 3.0 project
How to record a custom sound in the LEGO MINDSTORMS Robot Inventor App

Adding custom sounds to LEGO MINDSTORMS /SPIKE Prime projects

The LEGO MINDSTORMS Robot Inventor App allows you to record custom sounds to be played by your device (not by the hub). The LEGO SPIKE Prime App also  allows you to record custom sounds to be played by your device.This feature is available only in the Word Blocks (Scratch 3.0) Programming Environment, while it’s not available when using Python.

What if you want to play a whole song, your favorite MP3, a sound effect downlaoded from internet, or a prerecorded and long professionally mastered WAV sound file? 

How to record a custom sound in the LEGO SPIKE Prime app

Adding custom sounds to LEGO SPIKE / LEGO MINDSTORMS

step by step hacking tutorial

To add a custom sound to your LEGO MINDSTORMS projects, or to add a custom sound to your LEGO SPIKE Prime Scratch projects, follow these steps. You can watch the video tutorial if you prefer.

  1. In the LEGO MINDSTORMS (SPIKE PRIME) create a new Word Blocks (Scratch) project.
  2. Save it with a name of your choice, e.g. “soundsOriginal”
  3. Add a Play Sound until done block
  4. Save and close the project
SPIKE / LEGO MINDSTORMS program that plays a WAV sound from the library
  1. Locate the project customSounds.lms and create a backup copy
  2. Rename the extension to .zip and extract the content to a folder
any SPIKE .llsp project or MINDSTORMS .lms project is actually a .zip archive
  1. Navigate into this folder, that should include 3 files:
    • icon.svg
    • manifest.json
    • scratch.sb3
a LEGO SPIKE or LEGO MINDSTORMS project includes these three files
  1. Rename the scratch.sb3 to scratch.zip and extract its content
any Scratch .sb3 file is actually a .zip archive in disguise
  1. Inside the scratch folder you will find:
    • an .svg file
    • a project.json file
    • one or more .wav files (the default cat meow sound, and any sounds you might have recorded in the app)
the content of the inner LEGO MINDSTORMS Scratch project
  1. Open the project.json file with any text editor, or an online prettifier like JSON Pretty Print, and locate the audio assets section

"sounds": [
{
"assetId": "1b8b032b06360a6cf7c31d86bddd144b",
"name": "Cat Meow 1",
"dataFormat": "wav",
"rate": 48000,
"sampleCount": 60000,
"md5ext": "1b8b032b06360a6cf7c31d86bddd144b.wav"
},
],

To support my work, please consider buying your LEGO sets from these Amazon affiliate links. I may earn a little commission, at no extra charge for you. THANKS!

  1. With an audio editor of your choice (I use the free Audacity on Win 10), create/edit/convert the audio file you want to add to the project.
    You need to note down the sample rate of the file, the number of samples of the file
Prepare the audio file you want to import into your LEGO MINDSTORMS or LEGO SPIKE project with your favorite audio editor
  1. Now compute the audio file MD5 hash.
    • On Windows 10, you can use WinMD5
    • on Mac, you can use the md5 command from bash
    • on Linux, you can use the md5sum command from bash
  2. Save and move the WAV file into the scratch folder (the same where the project.JSON file is), and rename the sound file as <MD5hash>.wav. If the MD5 hash is b746b0e5635e7cd6ec20f2e7dcb34a07, then the file should be renamed as b746b0e5635e7cd6ec20f2e7dcb34a07.wav
WinMD5 is a free Windows utility to compute the MD5 Hash Sum of any file
  1. In the project.json file you opened with the text editor, add a new sound entry to the sounds assets.
  2. In the new entry, edit the following fields to match your audio file
    • sample rate
    • number of samples
    • assetId
    • md5ext fields
    • name (the sound name you want to see in the LEGO Word Blocks)
  3. Save the project.JSON file

If you mess up with the JSON formatting, you will make the project unusable. That’s why you made a backup. You did it, right?

"sounds": [
{
"assetId": "1b8b032b06360a6cf7c31d86bddd144b",
"name": "Cat Meow 1",
"dataFormat": "wav",
"rate": 48000,
"sampleCount": 60000,
"md5ext": "1b8b032b06360a6cf7c31d86bddd144b.wav"
},
{
"assetId": "b746b0e5635e7cd6ec20f2e7dcb34a07",
"name": "Custom Sound",
"dataFormat": "wav",
"rate": 44100,
"sampleCount": 140839,
"md5ext": "b746b0e5635e7cd6ec20f2e7dcb34a07.wav"
}
],

  1. Now select the whole content of the scratch folder, and add everything to a ZIP archive.
the LEGO scratch project content after having added the new custom sound file
  1. Rename the newly created archive as scratch.sb3
  2. Move the scratch.sb3 file up to the folder where the manifest.JSON file is, and eventually overwrite the old scratch.sb3 file.
create a zip archive for your LEGO scratch project and rename it to scratch.sb3
  1. Create a ZIP archive that should include 3 files:
    • icon.svg
    • manifest.json
    • scratch.sb3
  2. Rename the ZIP archive as customSounds.llsp (for SPIKE). If you started with a LEGO MINDSTORMS Robot Inventor project, rename it with the .lms extension.
create a zip archive for your hacked LEGO MINDSTORMS / LEGO SPIKE project and rename it with the original extension
  1. Open the LEGO SPIKE project, and enjoy your custom sound.
The LEGO SPIKE / LEGO MINDSTORMS program after the hack, showing the custom sound available

This tutorial was made after studying the official MIT Scratch reference WIKI on the SCRATCH File Format and on the JSON data structure.

To support my work, please consider buying your LEGO sets from these Amazon affiliate links. I may earn a little commission, at no extra charge for you. THANKS!

0
    0
    Your Cart
    Your cart is emptyReturn to Shop