OpenAL Fixes

This commit is contained in:
TheBrokenRail 2022-07-08 15:00:01 -04:00
parent 3c1bce876c
commit 006243d02f
3 changed files with 61 additions and 11 deletions

View File

@ -8,6 +8,7 @@
#include "file.h" #include "file.h"
#include "engine.h" #include "engine.h"
#include "api.h"
// Store Audio Sources // Store Audio Sources
static std::vector<ALuint> &get_sources() { static std::vector<ALuint> &get_sources() {
@ -15,6 +16,13 @@ static std::vector<ALuint> &get_sources() {
return sources; return sources;
} }
// Store Idle Audio Sources
#define MAX_IDLE_SOURCES 50
static std::vector<ALuint> &get_idle_sources() {
static std::vector<ALuint> sources;
return sources;
}
// Error Checking // Error Checking
#define AL_ERROR_CHECK() AL_ERROR_CHECK_MANUAL(alGetError()) #define AL_ERROR_CHECK() AL_ERROR_CHECK_MANUAL(alGetError())
#define AL_ERROR_CHECK_MANUAL(val) \ #define AL_ERROR_CHECK_MANUAL(val) \
@ -25,6 +33,22 @@ static std::vector<ALuint> &get_sources() {
} \ } \
} }
// Delete Sources
void _media_audio_delete_sources() {
if (_media_audio_is_loaded()) {
for (ALuint source : get_idle_sources()) {
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
for (ALuint source : get_sources()) {
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
}
get_idle_sources().clear();
get_sources().clear();
}
// Update Listener // Update Listener
void media_audio_update(float volume, float x, float y, float z, float yaw) { void media_audio_update(float volume, float x, float y, float z, float yaw) {
// Check // Check
@ -57,8 +81,12 @@ void media_audio_update(float volume, float x, float y, float z, float yaw) {
if (source_state != AL_PLAYING) { if (source_state != AL_PLAYING) {
// Finished Playing // Finished Playing
remove = true; remove = true;
alDeleteSources(1, &source); if (get_idle_sources().size() < MAX_IDLE_SOURCES) {
AL_ERROR_CHECK(); get_idle_sources().push_back(source);
} else {
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
} }
} else { } else {
// Not A Source // Not A Source
@ -81,16 +109,23 @@ void media_audio_play(const char *source, const char *name, float x, float y, fl
// Load Sound // Load Sound
ALuint buffer = _media_audio_get_buffer(source, name); ALuint buffer = _media_audio_get_buffer(source, name);
if (volume > 0.0f && buffer) { if (volume > 0.0f && buffer) {
// Create Source // Get Source
ALuint al_source; ALuint al_source;
alGenSources(1, &al_source); if (get_idle_sources().size() > 0) {
// Special Out-Of-Memory Handling // Use Idle Source
{ al_source = get_idle_sources().back();
ALenum err = alGetError(); get_idle_sources().pop_back();
if (err == AL_OUT_OF_MEMORY) { } else {
return; // Create Source
} else { alGenSources(1, &al_source);
AL_ERROR_CHECK_MANUAL(err); // Special Out-Of-Memory Handling
{
ALenum err = alGetError();
if (err == AL_OUT_OF_MEMORY) {
return;
} else {
AL_ERROR_CHECK_MANUAL(err);
}
} }
} }

View File

@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
__attribute__((visibility("internal"))) void _media_audio_delete_sources();
#ifdef __cplusplus
}
#endif

View File

@ -6,6 +6,7 @@
#include "engine.h" #include "engine.h"
#include "file.h" #include "file.h"
#include "api.h"
// Store Device // Store Device
static ALCdevice *device = NULL; static ALCdevice *device = NULL;
@ -55,6 +56,9 @@ void _media_audio_init() {
// De-Init // De-Init
void _media_audio_cleanup() { void _media_audio_cleanup() {
if (_media_audio_is_loaded()) { if (_media_audio_is_loaded()) {
// Delete Audio Sources
_media_audio_delete_sources();
// Delete Audio Buffers // Delete Audio Buffers
_media_audio_delete_buffers(); _media_audio_delete_buffers();