1
00:00:01,007 --> 00:00:03,009
- [Narrator] Hi, and
welcome to Section Four:

2
00:00:03,009 --> 00:00:06,003
Sound Spatialization and HUD.

3
00:00:06,003 --> 00:00:08,004
In this section, we will first take a look

4
00:00:08,004 --> 00:00:10,008
at what is spatialization and how SFML

5
00:00:10,008 --> 00:00:12,006
handles spatialization.

6
00:00:12,006 --> 00:00:15,005
We would then move onto
building a SoundManager class

7
00:00:15,005 --> 00:00:17,001
and deploying emitters.

8
00:00:17,001 --> 00:00:20,001
Later, we'll learn using
the SoundManager class.

9
00:00:20,001 --> 00:00:24,009
Lastly, we'd see building
a HUD class and using it.

10
00:00:24,009 --> 00:00:28,002
Now, we move onto the first
video of this section,

11
00:00:28,002 --> 00:00:31,007
Spatialization and SFML.

12
00:00:31,007 --> 00:00:33,006
In this video, we're going to take a look

13
00:00:33,006 --> 00:00:38,002
at spatialization and how
SFML uses spatialization.

14
00:00:38,002 --> 00:00:41,002
Spatialization is the act
of making something relative

15
00:00:41,002 --> 00:00:43,008
to the space it is a part of or within.

16
00:00:43,008 --> 00:00:46,007
In our daily lives, everything
in the natural world,

17
00:00:46,007 --> 00:00:48,005
by default, is spatialized.

18
00:00:48,005 --> 00:00:51,001
If a motorbike whizzes
past from left to right,

19
00:00:51,001 --> 00:00:53,003
we'll hear the sound
grow from faint to loud

20
00:00:53,003 --> 00:00:55,008
from one side, and as it passes it by,

21
00:00:55,008 --> 00:00:57,009
it will become more
prominent in the other ear

22
00:00:57,009 --> 00:01:01,000
before fading into the distance once more.

23
00:01:01,000 --> 00:01:03,002
If we can make our
videogames a little bit more

24
00:01:03,002 --> 00:01:07,002
like the real world, our players
can become more immersed.

25
00:01:07,002 --> 00:01:09,003
It is probably obvious
that the mathematics

26
00:01:09,003 --> 00:01:11,007
of spatialization will be complex.

27
00:01:11,007 --> 00:01:13,009
How do we calculate how loud a given sound

28
00:01:13,009 --> 00:01:16,005
will be in a specific speaker
based on the direction

29
00:01:16,005 --> 00:01:19,001
the sound is coming from and the distance

30
00:01:19,001 --> 00:01:21,001
from the player, the hearer
of the sound to the object

31
00:01:21,001 --> 00:01:23,005
that is making the sound, the emitter?

32
00:01:23,005 --> 00:01:27,004
Fortunately, SFML does all
the complicated stuff for us.

33
00:01:27,004 --> 00:01:28,009
All we need to do is get familiar

34
00:01:28,009 --> 00:01:30,008
with a few technical terms.

35
00:01:30,008 --> 00:01:32,009
And then, we can start using SFML

36
00:01:32,009 --> 00:01:35,006
to spatialize our sound effects.

37
00:01:35,006 --> 00:01:37,005
Let's now take a look at emitters,

38
00:01:37,005 --> 00:01:39,006
attenuation, and listeners.

39
00:01:39,006 --> 00:01:42,003
We will need to be aware of
a few pieces of information

40
00:01:42,003 --> 00:01:45,007
in order to give SFML what
it needs to do its work.

41
00:01:45,007 --> 00:01:48,001
We will need to be
aware of where the sound

42
00:01:48,001 --> 00:01:49,008
is coming from in our game world.

43
00:01:49,008 --> 00:01:52,004
This source of the sound
is called an emitter.

44
00:01:52,004 --> 00:01:54,008
In a game, the emitter could be a zombie,

45
00:01:54,008 --> 00:01:57,003
a vehicle, or in the case
of our current project,

46
00:01:57,003 --> 00:01:58,009
a fire tile.

47
00:01:58,009 --> 00:02:00,005
We already keep track of the position

48
00:02:00,005 --> 00:02:03,000
of objects in our game, so giving SFML

49
00:02:03,000 --> 00:02:06,002
the emitter location will
be quite straightforward.

50
00:02:06,002 --> 00:02:08,001
The next factor we need to be aware of

51
00:02:08,001 --> 00:02:09,004
is attenuation.

52
00:02:09,004 --> 00:02:12,006
Attenuation is the rate at
which the wave deteriorates.

53
00:02:12,006 --> 00:02:14,003
You could simplify that statement

54
00:02:14,003 --> 00:02:16,002
and make it specific to sound by saying

55
00:02:16,002 --> 00:02:18,001
that attenuation is how quickly the sound

56
00:02:18,001 --> 00:02:19,008
reduces in volume.

57
00:02:19,008 --> 00:02:22,005
It isn't technically exact,
but it's a good enough

58
00:02:22,005 --> 00:02:25,001
description for the
purposes of this section.

59
00:02:25,001 --> 00:02:26,007
The final factor we need to consider

60
00:02:26,007 --> 00:02:27,008
is the listener.

61
00:02:27,008 --> 00:02:30,001
When SFML spatializes the sound,

62
00:02:30,001 --> 00:02:32,006
where is it spatializing it relative to?

63
00:02:32,006 --> 00:02:34,009
In most games, the logical thing to do

64
00:02:34,009 --> 00:02:37,001
is use the player character.

65
00:02:37,001 --> 00:02:39,008
In our game, we will use Thomas.

66
00:02:39,008 --> 00:02:42,009
Moving onto how SFML
handles spatialization,

67
00:02:42,009 --> 00:02:44,009
SFML has a number of functions

68
00:02:44,009 --> 00:02:47,001
that allow us to handle emitters,

69
00:02:47,001 --> 00:02:48,008
attenuation, and listeners.

70
00:02:48,008 --> 00:02:50,005
Let's take a look at them hypothetically.

71
00:02:50,005 --> 00:02:52,001
Then, we will write some code

72
00:02:52,001 --> 00:02:55,002
to add spatialized sound
to our project for real.

73
00:02:55,002 --> 00:02:57,006
We can set up a sound
effect ready to be played

74
00:02:57,006 --> 00:02:58,008
as we have done so often,

75
00:02:58,008 --> 00:03:01,005
as you can see in this highlighted code.

76
00:03:01,005 --> 00:03:03,004
We can set the position of the emitter

77
00:03:03,004 --> 00:03:05,001
using the set position function

78
00:03:05,001 --> 00:03:07,003
as shown in the code here.

79
00:03:07,003 --> 00:03:08,008
As suggested in the comments,

80
00:03:08,008 --> 00:03:10,004
how exactly you obtain the coordinates

81
00:03:10,004 --> 00:03:12,008
of the emitter will probably be dependent

82
00:03:12,008 --> 00:03:14,002
upon the type of game.

83
00:03:14,002 --> 00:03:15,009
As shown here, this would be quite simple

84
00:03:15,009 --> 00:03:18,002
in the zombie arena project.

85
00:03:18,002 --> 00:03:20,001
We will have a few challenges to overcome

86
00:03:20,001 --> 00:03:23,001
when we set the position in this project.

87
00:03:23,001 --> 00:03:24,005
We can set the attenuation level

88
00:03:24,005 --> 00:03:26,003
using the line of code.

89
00:03:26,003 --> 00:03:28,001
The actual attenuation level can be

90
00:03:28,001 --> 00:03:29,007
a little ambiguous.

91
00:03:29,007 --> 00:03:31,004
The effect that you want the player to get

92
00:03:31,004 --> 00:03:33,009
might be different from the
accurate scientific formula

93
00:03:33,009 --> 00:03:36,002
used to reduce the volume over distance

94
00:03:36,002 --> 00:03:37,009
based on attenuation.

95
00:03:37,009 --> 00:03:39,006
Getting the right attenuation level

96
00:03:39,006 --> 00:03:41,009
is usually achieved by experimenting.

97
00:03:41,009 --> 00:03:45,000
Generally speaking, the higher
the level of attenuation,

98
00:03:45,000 --> 00:03:48,000
the quicker the sound
lever reduces to silence.

99
00:03:48,000 --> 00:03:50,000
Also, you might want to set a zone

100
00:03:50,000 --> 00:03:51,004
around the emitter where the volume

101
00:03:51,004 --> 00:03:53,006
is not attenuated at all.

102
00:03:53,006 --> 00:03:55,006
You might do this if the
feature isn't appropriate

103
00:03:55,006 --> 00:03:58,003
beyond a certain range or
you have a large number

104
00:03:58,003 --> 00:04:01,004
of sound sources and don't
want to overdo the feature.

105
00:04:01,004 --> 00:04:04,009
To do so, we can use the
setMinimumDistance function

106
00:04:04,009 --> 00:04:06,005
as shown here.

107
00:04:06,005 --> 00:04:09,008
With this highlighted code,
attenuation would not begin

108
00:04:09,008 --> 00:04:11,008
to be calculated until the listener

109
00:04:11,008 --> 00:04:15,006
is 150 pixels units away from the emitter.

110
00:04:15,006 --> 00:04:18,005
Some other useful functions
from the SFML library

111
00:04:18,005 --> 00:04:20,007
include the setLoop function.

112
00:04:20,007 --> 00:04:24,001
This function will tell
SFML to keep playing

113
00:04:24,001 --> 00:04:26,005
the sound over and over
when true is passed in

114
00:04:26,005 --> 00:04:30,001
as a parameter as demonstrated
by this line of code.

115
00:04:30,001 --> 00:04:31,007
The sound will continue to play

116
00:04:31,007 --> 00:04:34,002
until we ended it with this line.

117
00:04:34,002 --> 00:04:36,000
From time to time, we will want to know

118
00:04:36,000 --> 00:04:38,007
the status of a sound, playing or stopped.

119
00:04:38,007 --> 00:04:41,001
We can achieve this with
the getStatus function

120
00:04:41,001 --> 00:04:43,006
as demonstrated in this code.

121
00:04:43,006 --> 00:04:45,000
There is just one more aspect

122
00:04:45,000 --> 00:04:47,009
of using sound spatialization with SFML

123
00:04:47,009 --> 00:04:49,003
that we need to cover.

124
00:04:49,003 --> 00:04:50,008
Where is the listener?

125
00:04:50,008 --> 00:04:52,006
We can set the position of the listener

126
00:04:52,006 --> 00:04:53,009
with this code.

127
00:04:53,009 --> 00:04:56,004
This code will make all
the sounds play relative

128
00:04:56,004 --> 00:04:57,007
to that location.

129
00:04:57,007 --> 00:05:00,003
See, this is the code I'm talking about.

130
00:05:00,003 --> 00:05:03,001
This is just what we
need for the distant roar

131
00:05:03,001 --> 00:05:05,005
of a fire tile or incoming zombie.

132
00:05:05,005 --> 00:05:07,007
But for regular sound
effects such as jumping,

133
00:05:07,007 --> 00:05:09,000
this is a problem.

134
00:05:09,000 --> 00:05:10,006
We could start handling an emitter

135
00:05:10,006 --> 00:05:12,007
for the location of the player.

136
00:05:12,007 --> 00:05:14,003
Whenever we want to play a normal sound,

137
00:05:14,003 --> 00:05:16,005
we simply call setRelative to listener

138
00:05:16,005 --> 00:05:18,009
as I've shown here,
and then play the sound

139
00:05:18,009 --> 00:05:21,002
in the exact same way we have done so far.

140
00:05:21,002 --> 00:05:23,006
Here is how we might play
a normal unspatialized

141
00:05:23,006 --> 00:05:25,003
jump sound effect.

142
00:05:25,003 --> 00:05:28,003
All we need to do is call
Listener:setPosition again

143
00:05:28,003 --> 00:05:30,009
before we play any spatialized sounds.

144
00:05:30,009 --> 00:05:34,005
We now have a wide repetoire
of SFML sound functions.

145
00:05:34,005 --> 00:05:38,004
And we're ready to make some
specialized noise for real.

146
00:05:38,004 --> 00:05:41,001
In this video, we have
learned what is spatialization

147
00:05:41,001 --> 00:05:44,002
and how SFML handles spatialization.

148
00:05:44,002 --> 00:05:45,002
Great!

149
00:05:45,002 --> 00:05:47,004
In the next video, we'll see building

150
00:05:47,004 --> 00:05:50,006
the SoundManager class and
adding it to Game Engine.

