If you are new to Microsoft MakeCode, you can learn the basics of MakeCode here.

Currently MakeCode has a whole block group, MUSIC, devoted to making sounds:

The main groups of blocks provide the following:

  • Setting the volume (loudness) of your tones
  • Play sounds at startup or stop playing all sounds
  • Play any frequency tone
  • Play a musical note
  • Play music faster or slower
  • Note duration (beat) and pause between notes (rest)

The following code will play a 220 Hz tone when you press the A button and a 440 Hz tone when you press the B button. Within the INPUT group are the loops used for button presses. Select two on button blocks and add to each a play tone at block from the MUSIC group.

Change the value of the button A play tone at to 220 and it will replace the number with the value Low A. Replace the value of the Button B play tone to 440 and MakeCode will change the number to Middle A. MakeCode recognizes certain sound frequencies as musical notes and will change those accordingly. Alternatively MakeCode will put up a small keyboard when you work with the play tone at block's value to help you select musical notes if you like.

makecode_music_2.PNG
You can type in a numeric frequency value or use the provided keyboard to select musical note frequencies

Pull up this example and press the Download button to save it to your Circuit Playground Express. Press the A and B buttons. You get two different sounds! Feel free to change them up.

Predefined Sounds

MakeCode comes with several predefined sounds that have several musical notes. Change the Button A and Button B contents both to use the play sound until done block. Change them so each button plays a different sound:

Push the buttons and you get two melodies. Feel free to use the predefined sounds in your code.

Music

Typing in your own musical melody is possible but you need to know a bit about musical notation. I have found that some Internet sites are good at providing the notes, some are not. You can look to someone who knows how to read music to help if all you find is sheet music.

The following program plays a well known tune. See how many notes it takes to do the short portion? The code also uses the A and B buttons to change how fast or slow the music plays.

Using MakeCode, it is a bit tedious to put in lots of custom sounds. On the next page, CircuitPython will be used which also allows use of pre-recorded sound files which MakeCode cannot use at present.

This guide was first published on Jul 24, 2018. It was last updated on Jul 24, 2018.

This page (Music and Sound in MakeCode) was last updated on Nov 06, 2020.