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 "engine.h"
#include "api.h"
// Store Audio Sources
static std::vector<ALuint> &get_sources() {
@ -15,6 +16,13 @@ static std::vector<ALuint> &get_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
#define AL_ERROR_CHECK() AL_ERROR_CHECK_MANUAL(alGetError())
#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
void media_audio_update(float volume, float x, float y, float z, float yaw) {
// Check
@ -57,8 +81,12 @@ void media_audio_update(float volume, float x, float y, float z, float yaw) {
if (source_state != AL_PLAYING) {
// Finished Playing
remove = true;
alDeleteSources(1, &source);
AL_ERROR_CHECK();
if (get_idle_sources().size() < MAX_IDLE_SOURCES) {
get_idle_sources().push_back(source);
} else {
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
}
} else {
// Not A Source
@ -81,16 +109,23 @@ void media_audio_play(const char *source, const char *name, float x, float y, fl
// Load Sound
ALuint buffer = _media_audio_get_buffer(source, name);
if (volume > 0.0f && buffer) {
// Create Source
// Get Source
ALuint al_source;
alGenSources(1, &al_source);
// Special Out-Of-Memory Handling
{
ALenum err = alGetError();
if (err == AL_OUT_OF_MEMORY) {
return;
} else {
AL_ERROR_CHECK_MANUAL(err);
if (get_idle_sources().size() > 0) {
// Use Idle Source
al_source = get_idle_sources().back();
get_idle_sources().pop_back();
} else {
// Create Source
alGenSources(1, &al_source);
// 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 "file.h"
#include "api.h"
// Store Device
static ALCdevice *device = NULL;
@ -55,6 +56,9 @@ void _media_audio_init() {
// De-Init
void _media_audio_cleanup() {
if (_media_audio_is_loaded()) {
// Delete Audio Sources
_media_audio_delete_sources();
// Delete Audio Buffers
_media_audio_delete_buffers();