-
Notifications
You must be signed in to change notification settings - Fork 20
Basic Sound
In this part, I'm going to add some basic sound effects.
The sound is managed by the love.audio
module.
From the coding point of view, it works like this: you create a new audio source with love.audio.newSource
function. On creation you specify a sound file you want to play. Then you call play method of this source, which starts playing the sound.
There is a music, playing in the background and sound effects, produced on certain events in the game.
Let's start from the effects. Typically, you use a prerecorded samples. Or synthetise from scratch.
A couple of cites to look for samples are: OGA[link], Freesound[link]. Another possibility is to look for instrument samples pack, which are often boundled with music-making software (such as LMMS or Hydrogen).
To deal with samples, is Audacity[link] immensively helpful tool. It allows to extract a part of a track, normalize volume, suppress certain frequencies, and so on. Number of available effects is more than enough.
Of course, instead of using someone else's samples, you can record your own. You can get a decent result (enought to convey your idea) with minimal effort, so do not completely discard such possibility. Audacity makes recording simple enough. For example, one of the sounds for this game is a tea cup hit by a pen, recordered on internal microphone of my computer.
If you are looking for chiptune effects, bfxr[link]/sfxr[link] programs can be helpful.
When dealing with samples, my advice is to keep track of all file renames. They would be necessary to compile proper credits list.
In our game, sound effects are produced only on collisions. I'll desribe only ball collision with brick of simple type. The rest is similar.
Typically first you load sound. This is a class variable. ( code )
Then in collision response you play it: ( code snd:play() )
However, I want several different sound and select randomly one of them to play. Insted of doing it manually, I use TAsound library [link]. It takes automatically manages random sounds.
Besides, it simplifies music playing. We to take care and ensure that there is only instance of TEsound across all gamestates.
First, we need to load sounds from the HDD. It is good to make them class variables, just like tileset image. I have more than one sound for ball-brick collision and I want to randomly play one of them. I create a table to store the sounds.
simple_break_sound = {
love.audio.newSource("sounds/simple_break/recordered_glass_norm.ogg",
"static"),
love.audio.newSource("sounds/simple_break/edgardedition_glass_hit_norm.ogg",
"static") }
A good place to play the sound is react_on_ball_collision method. We select a random sound from the table and play it.
......
if self:is_simple() then
self.to_destroy = true
local snd = simple_break_sound[ math.random( #simple_break_sound ) ]
snd:play()
elseif self:is_armored() then
......
Same for collision of the ball with other brick types and platform-ball, ball-wall, and platform-bonus collisions.
Since we are using math.random, don't forget to seed it. This is done in love.load with math.randomseed function.
function love.load() math.randomseed( os.time() ) local love_window_width = 800
....
end
Now to the music.
On OGA you can also find some music. Another great resource is Jamendo[link], where you can find a lot of CreativeCommons-licensed music.
The game is short, so I use only one song. I set it to loop. It starts playing in the menu and goes on until you quit the game.
music = love.audio.newSource( "sounds/music/S31-Night Prowler.ogg" ) music:setLooping( true )
function menu:enter() ..... music:play() end
The game is simple, so I've used love.sound callbacks directly instead of any external sound management libraries. But in more complex cases it might be a good idea to use TEsound [link] library, which could simplify sound management.
Feedback is crucial to improve the tutorial!
Let me know if you have any questions, critique, suggestions or just any other ideas.
Chapter 1: Prototype
- The Ball, The Brick, The Platform
- Game Objects as Lua Tables
- Bricks and Walls
- Detecting Collisions
- Resolving Collisions
- Levels
Appendix A: Storing Levels as Strings
Appendix B: Optimized Collision Detection (draft)
Chapter 2: General Code Structure
- Splitting Code into Several Files
- Loading Levels from Files
- Straightforward Gamestates
- Advanced Gamestates
- Basic Tiles
- Different Brick Types
- Basic Sound
- Game Over
Appendix C: Stricter Modules (draft)
Appendix D-1: Intro to Classes (draft)
Appendix D-2: Chapter 2 Using Classes.
Chapter 3 (deprecated): Details
- Improved Ball Rebounds
- Ball Launch From Platform (Two Objects Moving Together)
- Mouse Controls
- Spawning Bonuses
- Bonus Effects
- Glue Bonus
- Add New Ball Bonus
- Life and Next Level Bonuses
- Random Bonuses
- Menu Buttons
- Wall Tiles
- Side Panel
- Score
- Fonts
- More Sounds
- Final Screen
- Packaging
Appendix D: GUI Layouts
Appendix E: Love-release and Love.js
Beyond Programming: