For 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.
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 # script_name: Game_Intro.py # # author: 101Computing # description: Background music for intro # from earsketch import * init() setTempo(120) clip1 = EIGHT_BIT_ATARI_SFX_004 clip2 = EIGHT_BIT_ATARI_LEAD_011 clip3 = EIGHT_BIT_ATARI_LEAD_010 pointA = 1.75 repeat = 3 pointD = pointA + repeat #fitMedia(Clip,Track,StartMeasure,EndMeasure) fitMedia(clip1,1,1,2) for i in range(0,repeat): fitMedia(clip2,2,pointA + i,pointA + i + 1) fitMedia(clip3,3,pointA,pointD + 1) #setEffect(Track,effect,parmeter,value) setEffect(3, VOLUME, GAIN, -10) #setEffect(Track,effect,parmeter,value,start measure,value,end measure) #Fade in setEffect(1, VOLUME, GAIN, -40, 1, 0, 1.75) #Delay Effect setEffect(1, DELAY, DELAY_TIME, 250) setEffect(2, DELAY, DELAY_TIME, 250) #Fade Out setEffect(3, VOLUME, GAIN, -10, pointD, -60, pointD+1) finish()
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:
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096) pygame.mixer.music.load('Sounds/soundtrack.mp3') pygame.mixer.music.play(-1) #-1 means loops for ever, 0 means play just once)
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:
effect = pygame.mixer.Sound('Sounds/beep.wav') effect.play()