Skip to content

Commit f482bc1

Browse files
committed
refine and add more meaningful documentation to demo_spatialization.cpp
1 parent 794fe06 commit f482bc1

File tree

1 file changed

+104
-10
lines changed

1 file changed

+104
-10
lines changed

demo/demo_spatialization.cpp

+104-10
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,66 @@ class Demo : public olc::PixelGameEngine
1515
public:
1616
bool OnUserCreate() override
1717
{
18+
/**
19+
* turn on background play so sound
20+
* continues to play even if the app
21+
* doesn't have focus.
22+
*/
1823
ma.SetBackgroundPlay(true);
1924

25+
/**
26+
* load assets/sounds/song1.mp3 and
27+
* keep track of it's id for later use
28+
*/
2029
song1 = ma.LoadSound("assets/sounds/song1.mp3");
30+
31+
/**
32+
* ADVANCED NOTES
33+
*
34+
* Using ma.GetSound to get a pointer to the ma_sound
35+
* associated with the sample ID, in this case song1.
36+
*
37+
* this allows us to use the miniaudio apis directly
38+
* for anything that the PGEX doesn't abstract.
39+
*
40+
* Note: in miniaudio spatialization, y is the
41+
* world UP axis. since this demo is 2d
42+
* we set the Y of all the miniaudio coords
43+
* to 0.0f.
44+
*/
2145
ma_sound_set_position(ma.GetSound(song1), 0.0f, 0.0f, 0.0f);
46+
47+
/**
48+
* using a linear attenuation allows us to accurately
49+
* set the min/max distance at which the sound plays
50+
*
51+
* side effect of this method is it doesn't change
52+
* volume of the sample as you move away. solution
53+
* detailed below.
54+
*/
2255
ma_sound_set_attenuation_model(ma.GetSound(song1), ma_attenuation_model_linear);
56+
57+
/**
58+
* min distance = 0
59+
* max distance = 20 (15 for radius of sound, 5 to cover radius of listener)
60+
*/
2361
ma_sound_set_min_distance(ma.GetSound(song1), 0.0f);
2462
ma_sound_set_max_distance(ma.GetSound(song1), 20.0f);
25-
ma_sound_set_max_gain(ma.GetSound(song1), 1.0f);
26-
ma_sound_set_min_gain(ma.GetSound(song1), 0.0f);
27-
63+
64+
// calculate the center of the screen
2865
centerScreen = GetScreenSize() / 2;
2966
return true;
3067
}
3168

32-
olc::vf2d position{0.0f, 0.0f};
33-
float direction = 0.0f;
34-
olc::vf2d centerScreen;
35-
3669
bool OnUserUpdate(float fElapsedTime) override
3770
{
3871
fElapsedTime = (fElapsedTime > thirtyFramesPerSecond) ? thirtyFramesPerSecond : fElapsedTime;
3972

4073
if(GetKey(olc::SPACE).bPressed)
4174
{
75+
/**
76+
* start or stop playback of song1
77+
*/
4278
ma.Toggle(song1);
4379
}
4480

@@ -50,31 +86,79 @@ class Demo : public olc::PixelGameEngine
5086
if(GetKey(olc::A).bHeld || GetKey(olc::LEFT).bHeld) rotationVelocity -= 1.0f;
5187
if(GetKey(olc::D).bHeld || GetKey(olc::RIGHT).bHeld) rotationVelocity += 1.0f;
5288

53-
direction += rotationVelocity * 5.0f * fElapsedTime;
54-
olc::vf2d directionVector = olc::vf2d{ cosf(direction), sinf(direction) };
89+
rotation += rotationVelocity * 5.0f * fElapsedTime;
90+
/**
91+
* change the direction our listener is facing
92+
* using the newly calculated rotation
93+
*/
94+
olc::vf2d directionVector = olc::vf2d{ cosf(rotation), sinf(rotation) };
95+
96+
/**
97+
* calculate the velocity our listener is moving
98+
* in the newly calculated direction vector
99+
*/
55100
olc::vf2d velocity = directionVector * forwardVelocity * 20.0f * fElapsedTime;
101+
102+
/**
103+
* add velocity to position, thus moving the listener
104+
*/
56105
position += velocity;
57106

107+
/**
108+
* As noted above, linear attenuation doesn't account
109+
* sample's volume. this is perfectly fine for situations
110+
* where you simply want a sound to play or not. however
111+
* if you want to simulate distance, we need to calculate
112+
* the volume of the sample.
113+
*/
114+
float volume = std::clamp(1.0f - (position.mag() / 20.0f), 0.0f, 1.0f);
115+
ma_sound_set_volume(ma.GetSound(song1), volume);
116+
117+
/**
118+
* ADVANCED NOTES
119+
*
120+
* Using ma.Engine to get a pointer to the ma_engine
121+
* associated with the pgex.
122+
*
123+
* this allows us to use the miniaudio apis directly
124+
* for anything that the PGEX doesn't abstract.
125+
*/
58126
ma_engine_listener_set_direction(ma.GetEngine(), 0, directionVector.x, 0.0f, directionVector.y);
59127
ma_engine_listener_set_position(ma.GetEngine(), 0, position.x, 0.0f, position.y);
60128
ma_engine_listener_set_velocity(ma.GetEngine(), 0, velocity.x, 0.0f, velocity.y);
61129

62130
Clear(olc::BLACK);
63131

132+
/**
133+
* draw a cirlce representing the sound
134+
* offset by the center of the screen.
135+
*/
64136
DrawCircle(centerScreen, 5, olc::YELLOW);
137+
138+
/**
139+
* draw a circle representing the range of
140+
* the sound offset by the center of the
141+
* screen.
142+
*/
65143
DrawCircle(centerScreen, 15, olc::MAGENTA);
66144

145+
/**
146+
* draw a circle at the listener position offset
147+
* by the center of the screen along with a line
148+
* to indicate the direction the listener is facing.
149+
*/
67150
DrawCircle(centerScreen + position, 5, olc::WHITE);
68151
DrawLine(centerScreen + position, centerScreen + position + (directionVector * 5), olc::WHITE);
69152

70153

71154
DrawStringDecal({5, 5}, \
72155
"-------- INFO --- CONTROLS -\n" "\n"
73156
"Forward/Backward (" + std::to_string(forwardVelocity) + ") W,S or UP,DOWN\n" "\n"
74-
"Rotation (" + std::to_string(direction) + ") A,D or LEFT,RIGHT\n" "\n"
157+
"Rotation (" + std::to_string(rotation) + ") A,D or LEFT,RIGHT\n" "\n"
75158
"Position " + position.str() + "\n" "\n"
76159
"Position Mag (" + std::to_string(position.mag()) + ")\n" "\n"
77160
"Toggle Sound (" + (ma.IsPlaying(song1) ? "Playing) " : "Not Playing)") + " SPACE\n"
161+
"Volume (" + std::to_string(volume) + ")\n" "\n"
78162
,
79163
olc::WHITE, {0.5f, 0.5f});
80164

@@ -91,8 +175,18 @@ class Demo : public olc::PixelGameEngine
91175
#endif
92176
}
93177

178+
// add the miniaudio pgex to PGE
94179
olc::MiniAudio ma;
180+
// keep track of the id the sample
95181
int song1;
182+
// position of our listener
183+
olc::vf2d position{0.0f, 0.0f};
184+
// rotational direction of our listener
185+
float rotation = 0.0f;
186+
// center of the screen
187+
olc::vf2d centerScreen;
188+
189+
96190
};
97191

98192
int main()

0 commit comments

Comments
 (0)