Arx 13 Basic Sound - noooway/love2d_arkanoid_tutorial GitHub Wiki

In this part, I'm going to add some basic sound effects to the game.

Sound effects are either synthesized from scratch, or some prerecorded samples are used.

I don't have any experience with sound synthesis, and the only thing I can recommend in this aspect is sfxr/bfxr programs, which are excellent chiptune effects generators (a Lua port of sfxr exists).

Speaking of sound samples, a couple of sites to look for them are OpenGameArt and Freesound. Another possibility is to look for instrument samples pack, bundled with music-making software (such as LMMS or Hydrogen). In dealing with samples, Audacity is immensely helpful tool. It allows to extract a part of the track, normalize volume, suppress certain frequencies, and so on. The number of available editing options is more than enough. Of course, instead of using someone else's samples, you can record your own. Such possibility should not be completely discarded: it is possible to get a decent result (enough to convey the idea) with minimal effort. For example, one of the sounds for this game is a tea cup hit by a pen, recorded on the internal microphone of my computer. Audacity makes the recording process simple enough. When dealing with samples, my advice to keep track of all file renames. They will be necessary to compile a proper credits list.

In LÖVE the sounds are stored and played by audio sources, which are a part of the love.audio module. Each source stores a single sound, which is usually specified on source creation. When needed, the sound can be played with the play method of the source.

For now, I'm going to add sound effects only on ball-bricks collisions and I'll use different sounds for collisions with different brick types.

First, we need to load the sounds from a disk and initialize the audio sources. It is convenient to make the sources class variables, just like the tileset image.

local Brick = {}
.....
simple_break_sound = love.audio.newSource(
   "sounds/recordered_glass_norm.ogg",
   "static")                                   --(*1)
armored_hit_sound = love.audio.newSource(
   "sounds/qubodupImpactMetal_short_norm.ogg",
   "static")
armored_break_sound = love.audio.newSource(
   "sounds/armored_glass_break_short_norm.ogg",
   "static")
ball_heavyarmored_sound = love.audio.newSource(
   "sounds/cast_iron_clangs_11_short_norm.ogg",
   "static")

(*1): "static" means that the sound is decompressed and stored in the memory, instead of being streamed from the file. This is the preferred method for the small sounds, that are played frequently.

A good place to play the sound is react_on_ball_collision method. The sound is chosen according to the brick type.

function Brick:react_on_ball_collision( another_shape, separating_vector )
   local big_enough_overlap = 0.5
   local dx, dy = separating_vector.x, separating_vector.y
   if ( math.abs( dx ) > big_enough_overlap ) or
      ( math.abs( dy ) > big_enough_overlap ) then
         if self:is_simple() then
            self.to_destroy = true
            simple_break_sound:play()
         elseif self:is_armored() then
            self:armored_to_scrathed()
            armored_hit_sound:play()
         elseif self:is_scratched() then
            self:scrathed_to_cracked()
            armored_hit_sound:play()
         elseif self:is_cracked() then
            self.to_destroy = true
            armored_break_sound:play()
         elseif self:is_heavyarmored() then
            ball_heavyarmored_sound:play()
         end
   end
end

Sounds on other collisions: ball-wall, ball-platform and platform-wall -- can be coded in a similar way.