Create your own music using Python!

retro-musicFor this challenge we will investigate how we can use Python code to create our own background music and sound effects to be used in a retro arcade game.

Most music editing software (e.g. GarageBand, Cubase, FL Studio, Logic Pro etc.), are Digital Audio Workstations (DAW) and let you create your own music by adding audio clips, virtual instruments and effects to a multi-track timeline. Editing and mixing your music is done through a Graphical User Interface.

On occasions, you may try a different approach to control the DAW through code. Websites such as EarSketch let you create your code online using either Python or JavaScript and import it to a DAW to visualise your timeline and play it.

Digitial Audio Workstation

Digitial Audio Workstation

In the example below, we are using Python code with basic audio clips from the “Eigthbit” library to recreate some retro arcade game music to be used on a game intro / splash screen.

It demonstrates some of the key basic features of EarSketch:

  • Set the tempo
  • Import audio clips (on different tracks and at specified times)
  • Add audio clips to the timeline
  • Add effects (including fade in, fade out and delay effects)
  • Use a loop to repeat a clip

Python Code

Test this code on EarSketch


You can test this code on EarSketch.

Adding Background Music with PyGame


To add a background soundtrack, download a wav file or mp3 file into a folder called “Sounds”.

At the start of your code (main.py), after importing the pygame library, add the following lines of code:

Note that you can stop the music at any time using the following instruction:

You may prefer to pause the music:

… or unpause the music:

Adding Sound Effects using PyGame


To add sound effects, download some wav or mp3 files into a folder called “Sounds” and add the following code when you want to play the sound effect:
Share Button