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

(Added stop(), pause(), resume(), and an example program)
(Edited first two paragraphs, bit of the third; fixed top link.)
Line 1: Line 1:
=[[User:Zorg/Manual|Sound - Basics]]=
+
=[[User:Zorg/Manual:Audio/1|Sound - Beginner - Loading & Basic Playback]]=
  
 
== Introduction ==
 
== Introduction ==
Line 13: Line 13:
 
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 Source object, and we'll use a file called "sound.ogg" which contains the previously mentioned tunes.
  
 
<source lang="lua">
 
<source lang="lua">

Revision as of 23:57, 28 November 2016

Sound - Beginner - Loading & Basic Playback

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 Source object, and we'll use a file called "sound.ogg" which contains the previously mentioned tunes.

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

Navigation

Next Chapter
Home