Make a Shooter in Lua/Love2D - Music and Sound Effects
Tags: games, gamedev, lua, love2d
Published: 2017-07-24 06:56:18

---
title: "Make a Shooter in Lua/Love2D - Music and Sound Effects"
published: true
cover_image: https://thepracticaldev.s3.amazonaws.com/i/b0pndl16w6ug2emqg7p8.png
description: Developing a simple shoot 'em up game using the Lua programming language and the Love2D framework.
tags: games, gamedev, lua, love2d
---
Hello, and welcome to the final part of this tutorial! In the [last post](https://dev.to/jeansberg/make-a-shooter-in-lualove2d\-\-\-animations-and-particles), we added some visual effects to the game. It won't really feel complete until we add some audio, though. Read on to find out how.
##Loading some files
We will be using the love.audio module to play back sound and music files, so the first thing we'll do is load some files into memory. This is very similar to loading image files, except we are using objects of the type "Source" to store the data.
```Lua
musicTrack = love.audio.newSource("resources/audio/Mercury.wav", "static")
musicTrack:setLooping(true)
shootSfx = love.audio.newSource("resources/audio/Explosion.wav", "static")
shootSfx:setVolume(0.5)
enemyDestroySfx = love.audio.newSource("resources/audio/Shoot.wav", "static")
playerDestroySfx = love.audio.newSource("resources/audio/Lightening.wav", "static")
```
We call the newSource() method and provide the paths to our sound files. The second parameter "static" tells Love2D to read the whole file into memory. It can be omitted for larger files, in order to stream them piece by piece. Since the files I'm using are very small, I decided to just load all of the data at startup. We want the music track to loop and luckily there is a function called setLooping() just for that. One of the audio files had a higher volume level than the others, so we call the setVolume() function to bring it in line with the others.
##Playing music
Since there will only ever be one instance of the music track playing, handling it won't require much code. All we have to do is call `musicTrack:play()` in the startGame() method and `musicTrack:stop()` when the player is hit and the game ends. That's it!
##Playing sound effects
We need to add a bit more logic for playing sound effects, so I decided to create a reusable function.
```Lua
function playSound(sound)
  sound:rewind(sound)
  pitchMod = 0.8 + love.math.random(0, 10)/25
  sound:setPitch(pitchMod)
  sound:play()
end
```
The playSound() function takes a Source object as a parameter. It calls the rewind() function on the Source, which is needed to get Love2D to play the sound effect from the start. This probably happens because the same Source is used for all instances of a sound.

A trick to add some variety to sound effects is to slightly modify the frequency of each sound when it's triggered. We generate a pitchMod value between 0.8 and 1.2 and give it to the setPitch() function. Then we call sound:play() just like when starting the music track.

After that it's just a matter of placing calls to playSound() at appropriate locations in the code.
```Lua
playerAlive = false
musicTrack:stop()
playSound(playerDestroySfx)
```
Stop the music and play a loud explosion sound when the player is hit by an enemy.
```Lua
table.remove(enemies, index)
table.remove(torpedoes, index2)
playSound(enemyDestroySfx)
```
Play a similar sound when an enemy is hit by a torpedo.
```Lua
torpedoTimer = torpedoTimerMax
playSound(shootSfx)
```
Play a whooshing sound whenever a torpedo is spawned.
##Conclusion
This concludes the series on making a shooter using Lua/Love2D! I hope you have found these posts useful. At some point in the future, I will be writing some more advanced tutorials using the same framework. Feel free to contact me at any time with questions and comments.
##Links
[Latest source](https://github.com/jeansberg/GreatDeep)
[Love2D wiki](https://love2d.org/wiki/Main_Page)
[Lua reference](https://www.lua.org/manual/5.1/)
##Credits
* The music track used for this part was made by *SketchyLogic* and is hosted [over here](https://opengameart.org/content/nes-shooter-music-5-tracks-3-jingles). 
* The sound effects are by *bart* and can be found [here](https://opengameart.org/content/8-bit-platformer-sfx).