Difference between revisions of "User:Zorg/Manual:Audio/1"
m (removed notice; it's already on the main page, -should- be enough, even if it isn't :3) |
(Added stop(), pause(), resume(), and an example program) |
||
Line 25: | Line 25: | ||
<source lang="lua"> | <source lang="lua"> | ||
− | |||
local source = love.audio.newSource('sound.ogg') | local source = love.audio.newSource('sound.ogg') | ||
− | |||
</source> | </source> | ||
+ | === 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. | 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. | ||
Line 36: | Line 35: | ||
</source> | </source> | ||
− | + | 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 lang="lua"> | ||
+ | source:stop() | ||
+ | </source> | ||
+ | |||
+ | 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 lang="lua"> | ||
+ | source:pause() | ||
+ | </source> | ||
+ | |||
+ | To unpause the sound, you can call Source:resume() or Source:play(). | ||
+ | |||
+ | <source lang="lua"> | ||
+ | source:resume() | ||
+ | </source> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <source lang="lua"> | ||
+ | local source | ||
+ | |||
+ | function love.load() | ||
+ | source = love.audio.newSource('sound.ogg') | ||
+ | end | ||
+ | |||
+ | function love.keypressed(key) | ||
+ | if key == "p" then | ||
+ | source:pause() | ||
+ | end | ||
+ | |||
+ | if key == "return" then | ||
+ | source:play() | ||
+ | end | ||
+ | |||
+ | if key == "backspace" then | ||
+ | source:stop() | ||
+ | end | ||
+ | end | ||
+ | |||
+ | </source> | ||
== Navigation == | == Navigation == |
Revision as of 23:02, 28 November 2016
Contents
Sound - Basics
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.
To understand what's happening, consult the image to the right.
SoundData
are objects that contain individual sound samples. Either short sounds or they can hold even long tracks as well, though they would use a lot of RAM, since they are decoded into samplepoints.
Examples
Our vocal player
Insert baker Sven joke here...
local source = love.audio.newSource('sound.ogg')
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.
local source
function love.load()
source = love.audio.newSource('sound.ogg')
end
function love.keypressed(key)
if key == "p" then
source:pause()
end
if key == "return" then
source:play()
end
if key == "backspace" then
source:stop()
end
end
Next Chapter |
Home |