Programming Fundamentals  
Lecture 08 Music and Audio Effects  
Edirlei Soares de Lima  
<edirlei.lima@universidadeeuropeia.pt>  
Music and Audio Effects  
The love.audio module provides an interface to output  
sound to the user's speakers.  
Audio workflow:  
Music and Audio Effects  
Löve functions to handle audio:  
Load a new audio source:  
source = love.audio.newSource(filename, type)  
Play an audio source:  
love.audio.play(source)  
Stop an audio source:  
love.audio.stop(source)  
Pause an audio source:  
love.audio.pause(source)  
Music and Audio Effects  
It’s also possible to control each audio source individually:  
Play an audio source:  
success = source:play()  
Stop an audio source:  
Pause an audio source:  
source:stop()  
source:pause()  
Check if the audio is playing:  
playing = source:isPlaying()  
Music and Audio Effects  
It’s also possible to control each audio source individually:  
Sets whether the audio source should loop:  
source:setLooping(loop)  
Sets the current volume of the audio source:  
source:setVolume(volume)  
Sets the currently playing position of the audio source:  
source:seek(offset, unit)  
More functions: https://love2d.org/wiki/Source  
Music and Audio Effects Example  
http://www.inf.puc-rio.br/~elima/intro-eng/exemplo_audio.zip  
Music and Audio Effects Example  
local icon_1up  
local icon_super  
local audio_1up  
local audio_super  
function love.load()  
icon_1up = love.graphics.newImage("1up.png")  
icon_super = love.graphics.newImage("super.png")  
audio_1up = love.audio.newSource("1up.mp3", "static")  
audio_super = love.audio.newSource("super.mp3")  
end  
function CheckClick(x1,y1,w1,h1,x2,y2)  
return x1 < x2+1 and x2 < x1+w1 and y1 < y2+1 and y2 < y1+h1  
end  
function love.draw()  
love.graphics.draw(icon_1up, 100, 200)  
love.graphics.draw(icon_super, 550, 200)  
end  
Music and Audio Effects Example  
function love.mousepressed(x, y, button)  
if button == 1 then  
if CheckClick(550,200,128,128,x,y) then  
love.audio.play(audio_super)  
end  
if CheckClick(100,200,128,128,x,y) then  
love.audio.play(audio_1up)  
end  
elseif button == 2 then  
if CheckClick(550,200,128,128,x,y) then  
love.audio.stop(audio_super)  
end  
if CheckClick(100,200,128,128,x,y) then  
love.audio.stop(audio_1up)  
end  
end  
end  
Exercise 1  
1
) Add a background music and a jump sound to the “Collision  
Detection with Physics” example program.  
Search for free music and jump sounds on the internet.  
The background music must be played in a loop.  
Audio Resources:  
https://incompetech.com/music/royalty-free/  
https://opengameart.org/  
https://freesound.org/browse/  
http://soundbible.com/