Small mod audio refactor

This commit is contained in:
Agent X 2024-06-25 10:10:19 -04:00
parent 16d403c254
commit 3b6bc54c91
1 changed files with 38 additions and 36 deletions

View File

@ -163,11 +163,11 @@ void smlua_audio_utils_replace_sequence(u8 sequenceId, u8 bankId, u8 defaultVolu
#define MA_SOUND_STREAM_FLAGS (MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_STREAM) #define MA_SOUND_STREAM_FLAGS (MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_STREAM)
#define MA_SOUND_SAMPLE_FLAGS (MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_NO_PITCH | MA_SOUND_FLAG_DECODE) // No pitch, pre-decode audio samples #define MA_SOUND_SAMPLE_FLAGS (MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_NO_PITCH | MA_SOUND_FLAG_DECODE) // No pitch, pre-decode audio samples
ma_engine gModAudioEngine; static ma_engine sModAudioEngine;
static struct DynamicPool *sModAudio; static struct DynamicPool *sModAudioPool;
static struct ModAudio* find_mod_audio(struct ModFile* file) { static struct ModAudio* find_mod_audio(struct ModFile* file) {
struct DynamicPoolNode* node = sModAudio->tail; struct DynamicPoolNode* node = sModAudioPool->tail;
while (node) { while (node) {
struct DynamicPoolNode* prev = node->prev; struct DynamicPoolNode* prev = node->prev;
struct ModAudio* audio = node->ptr; struct ModAudio* audio = node->ptr;
@ -243,7 +243,7 @@ struct ModAudio* audio_load_internal(const char* filename, bool isStream) {
// allocate in ModAudio pool // allocate in ModAudio pool
if (audio == NULL) { if (audio == NULL) {
audio = dynamic_pool_alloc(sModAudio, sizeof(struct ModAudio)); audio = dynamic_pool_alloc(sModAudioPool, sizeof(struct ModAudio));
if (!audio) { if (!audio) {
LOG_LUA_LINE("Could not allocate space for new mod audio!"); LOG_LUA_LINE("Could not allocate space for new mod audio!");
return NULL; return NULL;
@ -294,7 +294,7 @@ struct ModAudio* audio_load_internal(const char* filename, bool isStream) {
} }
result = ma_sound_init_from_data_source( result = ma_sound_init_from_data_source(
&gModAudioEngine, decoder, &sModAudioEngine, decoder,
isStream ? MA_SOUND_STREAM_FLAGS : MA_SOUND_SAMPLE_FLAGS, isStream ? MA_SOUND_STREAM_FLAGS : MA_SOUND_SAMPLE_FLAGS,
NULL, &audio->sound NULL, &audio->sound
); );
@ -315,15 +315,16 @@ struct ModAudio* audio_stream_load(const char* filename) {
} }
void audio_stream_destroy(struct ModAudio* audio) { void audio_stream_destroy(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "destroy")) { if (!audio_sanity_check(audio, true, "destroy stream")) {
return; return;
} }
ma_sound_uninit(&audio->sound); ma_sound_uninit(&audio->sound);
dynamic_pool_free(sModAudioPool, audio);
} }
void audio_stream_play(struct ModAudio* audio, bool restart, f32 volume) { void audio_stream_play(struct ModAudio* audio, bool restart, f32 volume) {
if (!audio_sanity_check(audio, true, "play")) { if (!audio_sanity_check(audio, true, "play stream")) {
return; return;
} }
f32 masterVolume = (f32)configMasterVolume / 127.0f * (f32)gLuaVolumeMaster / 127.0f; f32 masterVolume = (f32)configMasterVolume / 127.0f * (f32)gLuaVolumeMaster / 127.0f;
@ -335,14 +336,14 @@ void audio_stream_play(struct ModAudio* audio, bool restart, f32 volume) {
} }
void audio_stream_pause(struct ModAudio* audio) { void audio_stream_pause(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "pause")) { if (!audio_sanity_check(audio, true, "pause stream")) {
return; return;
} }
ma_sound_stop(&audio->sound); ma_sound_stop(&audio->sound);
} }
void audio_stream_stop(struct ModAudio* audio) { void audio_stream_stop(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "stop")) { if (!audio_sanity_check(audio, true, "stop stream")) {
return; return;
} }
ma_sound_stop(&audio->sound); ma_sound_stop(&audio->sound);
@ -350,7 +351,7 @@ void audio_stream_stop(struct ModAudio* audio) {
} }
f32 audio_stream_get_position(struct ModAudio* audio) { f32 audio_stream_get_position(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "getpos")) { if (!audio_sanity_check(audio, true, "get stream position")) {
return 0; return 0;
} }
// ! This gets the time that the audio has been playing for, but is not reset when the stream loops // ! This gets the time that the audio has been playing for, but is not reset when the stream loops
@ -358,63 +359,63 @@ f32 audio_stream_get_position(struct ModAudio* audio) {
} }
void audio_stream_set_position(struct ModAudio* audio, f32 pos) { void audio_stream_set_position(struct ModAudio* audio, f32 pos) {
if (!audio_sanity_check(audio, true, "setpos")) { if (!audio_sanity_check(audio, true, "set stream position")) {
return; return;
} }
ma_sound_seek_to_pcm_frame(&audio->sound, pos * ma_engine_get_sample_rate(&gModAudioEngine)); ma_sound_seek_to_pcm_frame(&audio->sound, pos * ma_engine_get_sample_rate(&sModAudioEngine));
} }
bool audio_stream_get_looping(struct ModAudio* audio) { bool audio_stream_get_looping(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "getloop")) { if (!audio_sanity_check(audio, true, "get stream looping")) {
return false; return false;
} }
return ma_sound_is_looping(&audio->sound); return ma_sound_is_looping(&audio->sound);
} }
void audio_stream_set_looping(struct ModAudio* audio, bool looping) { void audio_stream_set_looping(struct ModAudio* audio, bool looping) {
if (!audio_sanity_check(audio, true, "setloop")) { if (!audio_sanity_check(audio, true, "set stream looping")) {
return; return;
} }
ma_sound_set_looping(&audio->sound, looping); ma_sound_set_looping(&audio->sound, looping);
} }
f32 audio_stream_get_frequency(struct ModAudio* audio) { f32 audio_stream_get_frequency(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "getfreq")) { if (!audio_sanity_check(audio, true, "get stream frequency")) {
return 0; return 0;
} }
return ma_sound_get_pitch(&audio->sound); return ma_sound_get_pitch(&audio->sound);
} }
void audio_stream_set_frequency(struct ModAudio* audio, f32 freq) { void audio_stream_set_frequency(struct ModAudio* audio, f32 freq) {
if (!audio_sanity_check(audio, true, "setfreq")) { if (!audio_sanity_check(audio, true, "set stream frequency")) {
return; return;
} }
ma_sound_set_pitch(&audio->sound, freq); ma_sound_set_pitch(&audio->sound, freq);
} }
// f32 audio_stream_get_tempo(struct ModAudio* audio) { // f32 audio_stream_get_tempo(struct ModAudio* audio) {
// if (!audio_sanity_check(audio, true, "gettempo")) { // if (!audio_sanity_check(audio, true, "get stream tempo")) {
// return 0; // return 0;
// } // }
// return bassh_get_tempo(audio->handle); // return bassh_get_tempo(audio->handle);
// } // }
// void audio_stream_set_tempo(struct ModAudio* audio, f32 tempo) { // void audio_stream_set_tempo(struct ModAudio* audio, f32 tempo) {
// if (!audio_sanity_check(audio, true, "settempo")) { // if (!audio_sanity_check(audio, true, "set stream tempo")) {
// return; // return;
// } // }
// bassh_set_tempo(audio->handle, tempo); // bassh_set_tempo(audio->handle, tempo);
// } // }
f32 audio_stream_get_volume(struct ModAudio* audio) { f32 audio_stream_get_volume(struct ModAudio* audio) {
if (!audio_sanity_check(audio, true, "getvol")) { if (!audio_sanity_check(audio, true, "get stream volume")) {
return 0; return 0;
} }
return audio->baseVolume; return audio->baseVolume;
} }
void audio_stream_set_volume(struct ModAudio* audio, f32 volume) { void audio_stream_set_volume(struct ModAudio* audio, f32 volume) {
if (!audio_sanity_check(audio, true, "setvol")) { if (!audio_sanity_check(audio, true, "set stream volume")) {
return; return;
} }
f32 masterVolume = (f32)configMasterVolume / 127.0f; f32 masterVolume = (f32)configMasterVolume / 127.0f;
@ -424,7 +425,7 @@ void audio_stream_set_volume(struct ModAudio* audio, f32 volume) {
} }
// void audio_stream_set_speed(struct ModAudio* audio, f32 initial_freq, f32 speed, bool pitch) { // void audio_stream_set_speed(struct ModAudio* audio, f32 initial_freq, f32 speed, bool pitch) {
// if (!audio_sanity_check(audio, true, "setspeed")) { // if (!audio_sanity_check(audio, true, "set stream speed")) {
// return; // return;
// } // }
// bassh_set_speed(audio->handle, initial_freq, speed, pitch); // bassh_set_speed(audio->handle, initial_freq, speed, pitch);
@ -496,7 +497,7 @@ struct ModAudio* audio_sample_load(const char* filename) {
} }
void audio_sample_destroy(struct ModAudio* audio) { void audio_sample_destroy(struct ModAudio* audio) {
if (!audio_sanity_check(audio, false, "destroy")) { if (!audio_sanity_check(audio, false, "destroy sample")) {
return; return;
} }
@ -504,10 +505,11 @@ void audio_sample_destroy(struct ModAudio* audio) {
audio_sample_destroy_copies(audio); audio_sample_destroy_copies(audio);
} }
ma_sound_uninit(&audio->sound); ma_sound_uninit(&audio->sound);
dynamic_pool_free(sModAudioPool, audio);
} }
void audio_sample_stop(struct ModAudio* audio) { void audio_sample_stop(struct ModAudio* audio) {
if (!audio_sanity_check(audio, false, "stop")) { if (!audio_sanity_check(audio, false, "stop sample")) {
return; return;
} }
if (audio->sampleCopiesTail) { if (audio->sampleCopiesTail) {
@ -518,14 +520,14 @@ void audio_sample_stop(struct ModAudio* audio) {
} }
void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) { void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) {
if (!audio_sanity_check(audio, false, "play")) { if (!audio_sanity_check(audio, false, "play sample")) {
return; return;
} }
ma_sound *sound = &audio->sound; ma_sound *sound = &audio->sound;
if (ma_sound_is_playing(sound)) { if (ma_sound_is_playing(sound)) {
struct ModAudioSampleCopies* copy = calloc(1, sizeof(struct ModAudioSampleCopies)); struct ModAudioSampleCopies* copy = calloc(1, sizeof(struct ModAudioSampleCopies));
ma_sound_init_copy(&gModAudioEngine, sound, MA_SOUND_SAMPLE_FLAGS, NULL, &copy->sound); ma_sound_init_copy(&sModAudioEngine, sound, MA_SOUND_SAMPLE_FLAGS, NULL, &copy->sound);
copy->parent = audio; copy->parent = audio;
if (!audio->sampleCopiesTail) { if (!audio->sampleCopiesTail) {
@ -565,7 +567,7 @@ void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) {
} }
void audio_custom_update_volume(void) { void audio_custom_update_volume(void) {
struct DynamicPoolNode* node = sModAudio->tail; struct DynamicPoolNode* node = sModAudioPool->tail;
while (node) { while (node) {
struct DynamicPoolNode* prev = node->prev; struct DynamicPoolNode* prev = node->prev;
struct ModAudio* audio = node->ptr; struct ModAudio* audio = node->ptr;
@ -579,8 +581,8 @@ void audio_custom_update_volume(void) {
} }
void audio_custom_shutdown(void) { void audio_custom_shutdown(void) {
if (!sModAudio) { return; } if (!sModAudioPool) { return; }
struct DynamicPoolNode* node = sModAudio->tail; struct DynamicPoolNode* node = sModAudioPool->tail;
while (node) { while (node) {
struct DynamicPoolNode* prev = node->prev; struct DynamicPoolNode* prev = node->prev;
struct ModAudio* audio = node->ptr; struct ModAudio* audio = node->ptr;
@ -591,23 +593,23 @@ void audio_custom_shutdown(void) {
} }
node = prev; node = prev;
} }
dynamic_pool_free_pool(sModAudio); dynamic_pool_free_pool(sModAudioPool);
} }
void smlua_audio_custom_init(void) { void smlua_audio_custom_init(void) {
sModAudio = dynamic_pool_init(); sModAudioPool = dynamic_pool_init();
ma_result result = ma_engine_init(NULL, &gModAudioEngine); ma_result result = ma_engine_init(NULL, &sModAudioEngine);
if (result != MA_SUCCESS) { if (result != MA_SUCCESS) {
LOG_ERROR("failed to init miniaudio: %d", result); LOG_ERROR("failed to init Miniaudio: %d", result);
} }
} }
void smlua_audio_custom_deinit(void) { void smlua_audio_custom_deinit(void) {
if (sModAudio) { if (sModAudioPool) {
audio_custom_shutdown(); audio_custom_shutdown();
free(sModAudio); free(sModAudioPool);
ma_engine_uninit(&gModAudioEngine); ma_engine_uninit(&sModAudioEngine);
sModAudio = NULL; sModAudioPool = NULL;
} }
} }