- [Narrator] Hi, and welcome to Section Four:
Sound Spatialization and HUD.
In this section, we will first take a look
at what is spatialization and how SFML
handles spatialization.
We would then move onto building a SoundManager class
and deploying emitters.
Later, we'll learn using the SoundManager class.
Lastly, we'd see building a HUD class and using it.
Now, we move onto the first video of this section,
Spatialization and SFML.
In this video, we're going to take a look
at spatialization and how SFML uses spatialization.
Spatialization is the act of making something relative
to the space it is a part of or within.
In our daily lives, everything in the natural world,
by default, is spatialized.
If a motorbike whizzes past from left to right,
we'll hear the sound grow from faint to loud
from one side, and as it passes it by,
it will become more prominent in the other ear
before fading into the distance once more.
If we can make our videogames a little bit more
like the real world, our players can become more immersed.
It is probably obvious that the mathematics
of spatialization will be complex.
How do we calculate how loud a given sound
will be in a specific speaker based on the direction
the sound is coming from and the distance
from the player, the hearer of the sound to the object
that is making the sound, the emitter?
Fortunately, SFML does all the complicated stuff for us.
All we need to do is get familiar
with a few technical terms.
And then, we can start using SFML
to spatialize our sound effects.
Let's now take a look at emitters,
attenuation, and listeners.
We will need to be aware of a few pieces of information
in order to give SFML what it needs to do its work.
We will need to be aware of where the sound
is coming from in our game world.
This source of the sound is called an emitter.
In a game, the emitter could be a zombie,
a vehicle, or in the case of our current project,
a fire tile.
We already keep track of the position
of objects in our game, so giving SFML
the emitter location will be quite straightforward.
The next factor we need to be aware of
is attenuation.
Attenuation is the rate at which the wave deteriorates.
You could simplify that statement
and make it specific to sound by saying
that attenuation is how quickly the sound
reduces in volume.
It isn't technically exact, but it's a good enough
description for the purposes of this section.
The final factor we need to consider
is the listener.
When SFML spatializes the sound,
where is it spatializing it relative to?
In most games, the logical thing to do
is use the player character.
In our game, we will use Thomas.
Moving onto how SFML handles spatialization,
SFML has a number of functions
that allow us to handle emitters,
attenuation, and listeners.
Let's take a look at them hypothetically.
Then, we will write some code
to add spatialized sound to our project for real.
We can set up a sound effect ready to be played
as we have done so often,
as you can see in this highlighted code.
We can set the position of the emitter
using the set position function
as shown in the code here.
As suggested in the comments,
how exactly you obtain the coordinates
of the emitter will probably be dependent
upon the type of game.
As shown here, this would be quite simple
in the zombie arena project.
We will have a few challenges to overcome
when we set the position in this project.
We can set the attenuation level
using the line of code.
The actual attenuation level can be
a little ambiguous.
The effect that you want the player to get
might be different from the accurate scientific formula
used to reduce the volume over distance
based on attenuation.
Getting the right attenuation level
is usually achieved by experimenting.
Generally speaking, the higher the level of attenuation,
the quicker the sound lever reduces to silence.
Also, you might want to set a zone
around the emitter where the volume
is not attenuated at all.
You might do this if the feature isn't appropriate
beyond a certain range or you have a large number
of sound sources and don't want to overdo the feature.
To do so, we can use the setMinimumDistance function
as shown here.
With this highlighted code, attenuation would not begin
to be calculated until the listener
is 150 pixels units away from the emitter.
Some other useful functions from the SFML library
include the setLoop function.
This function will tell SFML to keep playing
the sound over and over when true is passed in
as a parameter as demonstrated by this line of code.
The sound will continue to play
until we ended it with this line.
From time to time, we will want to know
the status of a sound, playing or stopped.
We can achieve this with the getStatus function
as demonstrated in this code.
There is just one more aspect
of using sound spatialization with SFML
that we need to cover.
Where is the listener?
We can set the position of the listener
with this code.
This code will make all the sounds play relative
to that location.
See, this is the code I'm talking about.
This is just what we need for the distant roar
of a fire tile or incoming zombie.
But for regular sound effects such as jumping,
this is a problem.
We could start handling an emitter
for the location of the player.
Whenever we want to play a normal sound,
we simply call setRelative to listener
as I've shown here, and then play the sound
in the exact same way we have done so far.
Here is how we might play a normal unspatialized
jump sound effect.
All we need to do is call Listener:setPosition again
before we play any spatialized sounds.
We now have a wide repetoire of SFML sound functions.
And we're ready to make some specialized noise for real.
In this video, we have learned what is spatialization
and how SFML handles spatialization.
Great!
In the next video, we'll see building
the SoundManager class and adding it to Game Engine.