Difference between revisions of "User:Zorg/Manual:Audio/1"

(Added stop(), pause(), resume(), and an example program)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=[[User:Zorg/Manual|Sound - Basics]]=
 
 
 
== Introduction ==
 
== Introduction ==
  
Line 13: Line 11:
 
Löve has two namespaces for sound related objects and methods. [[love.sound]] and [[love.audio]].
 
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.
+
The former deals with loading, storing and manipulating the data, while the latter deals with playback and (3D) positioning, for the most part.
  
=== SoundData ===
+
== Our first sound project==
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 ==
+
Since this chapter has a relatively small amount of methods to go over, we can tackle all of them with one example project.
  
=== Our vocal player ===
+
=== Our protagonist ===
  
Insert baker Sven joke here...
+
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 <s>sick beats</s> 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.
  
 
<source lang="lua">
 
<source lang="lua">
 
local source = love.audio.newSource('sound.ogg')
 
local source = love.audio.newSource('sound.ogg')
 
</source>
 
</source>
 +
 +
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 ===
 
=== Play a sound ===
Line 63: Line 65:
 
=== Putting it all together ===
 
=== Putting it all together ===
  
Now that you know the basics, let's make a program that uses all of these functions.
+
Now that you know the basics, let's make a program that uses all of these functions together.
  
 
<source lang="lua">
 
<source lang="lua">
local source
+
local samsMusic
  
 
function love.load()
 
function love.load()
     source = love.audio.newSource('sound.ogg')
+
     samsMusic = love.audio.newSource('sound.ogg')
 
end
 
end
  
 
function love.keypressed(key)
 
function love.keypressed(key)
 
     if key == "p" then
 
     if key == "p" then
         source:pause()  
+
         samsMusic:pause()  
 
     end
 
     end
  
     if key == "return" then
+
     if key == "r" then
         source:play()
+
         samsMusic:resume()
 
     end
 
     end
  
     if key == "backspace" then
+
    -- Replay the sound
         source:stop()
+
     if key == "return" then
 +
         samsMusic:stop()
 +
        samsMusic:play()
 
     end
 
     end
 
end
 
end
 
 
</source>
 
</source>
  

Latest revision as of 04:12, 17 December 2016

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