Skip to content

Basic Sound

noooway edited this page Jan 3, 2017 · 37 revisions

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.

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. 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

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally