Skip to content

Commit 7130edc

Browse files
committed
perf: use asset loader and cache menu audio buffers
1 parent 549571c commit 7130edc

5 files changed

Lines changed: 20 additions & 11 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
}
127127
},
128128
"sounds": {
129+
"gun-reload": "assets/sounds/gun_reload.wav",
129130
"portal": "assets/sounds/portal.wav"
130131
}
131132
},

src/common/systems/audio-system.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@ namespace our {
128128
// Set initial position
129129
glm::mat4 M = entity->getLocalToWorldMatrix();
130130
glm::vec3 sourcePos = M[3];
131-
source = playSound(buffer, sourcePos, audioSource->gain, audioSource->pitch, audioSource->loop);
131+
source = playSound(buffer, sourcePos, audioSource->gain, audioSource->pitch, audioSource->loop,
132+
audioSource->refDistance, audioSource->maxDistance);
132133
} else {
133134
source = playSound2D(buffer, audioSource->gain, audioSource->pitch, audioSource->loop);
134135
}
135136

136137
audioSource->alSource = source;
137138
}
138139

139-
ALuint AudioSystem::playSound(AudioBuffer* buffer, const glm::vec3& position, float gain, float pitch, bool loop) {
140+
ALuint AudioSystem::playSound(AudioBuffer* buffer, const glm::vec3& position, float gain, float pitch, bool loop,
141+
float refDistance, float maxDistance) {
140142
if (!buffer) return 0;
141143

142144
ALuint source = findAvailableSource();
@@ -154,9 +156,9 @@ namespace our {
154156
alSource3f(source, AL_POSITION, position.x, position.y, position.z);
155157

156158
// Set attenuation defaults
157-
alSourcef(source, AL_REFERENCE_DISTANCE, 5.0f); // full volume within this distance
158-
alSourcef(source, AL_MAX_DISTANCE, 100.0f); // Attentuation limit
159-
alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f); // how quickly the sound attenuates with distance
159+
alSourcef(source, AL_REFERENCE_DISTANCE, refDistance); // full volume within this distance
160+
alSourcef(source, AL_MAX_DISTANCE, maxDistance); // Attentuation limit
161+
alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f); // how quickly the sound attenuates with distance
160162

161163
alSourcePlay(source);
162164
checkALError("Playing sound");

src/common/systems/audio-system.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ namespace our {
2929
// - gain: volume multiplier
3030
// - pitch: speed multiplier
3131
// - loop: whether to keep looping the sound
32-
ALuint playSound(AudioBuffer* buffer, const glm::vec3& position, float gain, float pitch, bool loop);
32+
ALuint playSound(AudioBuffer* buffer, const glm::vec3& position, float gain, float pitch, bool loop,
33+
float refDistance, float maxDistance);
3334

3435
// On-demand function to play a non-spatial sound (fire-and-forget)
3536
// Best used for UI, menu, or music sounds

src/states/menu-state.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class Menustate : public our::State {
4646
float time;
4747
// An array of the button that we can interact with
4848
std::array<Button, 2> buttons;
49+
50+
our::AudioBuffer* menuMusic = nullptr;
51+
our::AudioBuffer* menuSelectSound = nullptr;
52+
4953
// Index of the currently hovered button (-1 = none). Used to detect hover-enter transitions.
5054
int hoveredButton = -1;
5155

@@ -117,8 +121,10 @@ class Menustate : public our::State {
117121
buttons[1].size = {400.0f, 33.0f};
118122
buttons[1].action = [this]() { this->getApp()->close(); };
119123

120-
getApp()->getAudioSystem().playSound2D(our::audio_utils::loadWAV("assets/sounds/menu-music.wav"), 0.5f, 1.0f,
121-
true);
124+
// TODO: Maybe load them in application.cpp or serialize them from the config json
125+
menuMusic = our::audio_utils::loadWAV("assets/sounds/menu-music.wav");
126+
menuSelectSound = our::audio_utils::loadWAV("assets/sounds/menu-select.wav");
127+
getApp()->getAudioSystem().playSound2D(menuMusic, 0.5f, 1.0f, true);
122128
}
123129

124130
void onDraw(double deltaTime) override {
@@ -181,8 +187,7 @@ class Menustate : public our::State {
181187

182188
// Play hover sound only on the frame the mouse first enters a button (edge detection)
183189
if (currentlyHovered != -1 && currentlyHovered != hoveredButton) {
184-
getApp()->getAudioSystem().playSound2D(
185-
our::audio_utils::loadWAV("assets/sounds/menu-select.wav"), 1.0, 1.0, false);
190+
getApp()->getAudioSystem().playSound2D(menuSelectSound, 1.0, 1.0, false);
186191
}
187192
hoveredButton = currentlyHovered;
188193

src/states/play-state.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Playstate : public our::State {
7676
if (keyboard.justPressed(GLFW_KEY_R)) {
7777
if (!getApp()->getAudioSystem().isPlaying(reloadSource)) { // TODO: remove when implementing a proper weapon/player system
7878
reloadSource = getApp()->getAudioSystem().playSound2D(
79-
our::audio_utils::loadWAV("assets/sounds/gun_reload.wav"), 1.0f, 1.0f, false);
79+
our::AssetLoader<our::AudioBuffer>::get("gun-reload"), 1.0f, 1.0f, false);
8080
}
8181
}
8282
// clang-format on

0 commit comments

Comments
 (0)