User:Zorg/Manual:Audio/1

Introduction

So, what's audio?

It's everything regarding sound, a game's background music, ambience, effects, voice acting.

Most released games that had any amount of work put into them have audio in one way or another, with the two exceptions being art-games that have no audio for some philosophical reason, or really old pong-era games where there wasn't a way to have it.

How does LÖVE do it?

Löve has two namespaces for sound related objects and methods. love.sound and love.audio.

The former deals with loading, storing and manipulating the data, while the latter deals with playback and (3D) positioning, for the most part.

Our first sound project

Since this chapter has a relatively small amount of methods to go over, we can tackle all of them with one example project.

Our protagonist

Let's say that our game will have a main character called "Sam", which is of course a very fitting and not at all suspicious name.

Now, Sam is a musician, and while she doesn't have any speaking roles yet, she wants to show us her sick beats tunes. Let's see how we can load in a music file. We'll need to create a new local Source object, called "source", and we'll use a file called "sound.ogg" which contains the previously mentioned tunes.

local source = love.audio.newSource('sound.ogg')

For now, this is enough to know, though as you might assume, there's quite a bit more going on under "the hood", so to speak.

Play a sound

Now that we have a source we'll want to play the sound. Thankfully, there's a function called Source:play() which does just that.

source:play()

When play() is called the sound will continue to play until it reaches the end of the sound. We only have to call play once to start the sound.

Stop a sound

What if we want to stop the sound? As you might guess, there is a corresponding function to play() called Source:stop().

source:stop()

When stop() is called, the sound will stop playing and it will also rewind the sound to the beginning. However, there is an issue. Calling this function will automatically rewind the sound, making it impossible to "pause" the sound and keep the current position. But, there is a solution: Source:pause().

Pause a sound

When pause() is called, the sound will stop playing, but it will keep its current position.

source:pause()

To unpause the sound, you can call Source:resume() or Source:play().

source:resume()

When resume() is called, if the sound is paused, then it will start playing the sound at the location it was paused. The difference between play() and resume() is resume() will only start playing the sound again if it was paused. If stop() was used, then resume() has no effect.

Putting it all together

Now that you know the basics, let's make a program that uses all of these functions together.

local samsMusic
 
function love.load()
    samsMusic = love.audio.newSource('sound.ogg')
end
 
function love.keypressed(key)
    if key == "p" then
        samsMusic:pause() 
    end
 
    if key == "r" then
        samsMusic:resume()
    end
 
    -- Replay the sound
    if key == "return" then
        samsMusic:stop()
        samsMusic:play()
    end
end

Navigation

Next Chapter
Home