From d1a7ddcb9f7ab1a357d3e27f9036289ffae6c62c Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 27 Oct 2021 19:05:49 -0700 Subject: [PATCH] null device checks --- src/FAudioGMS.c | 87 ++++++++++++++++++++++++++----------------------- src/FAudioGMS.h | 1 - 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/FAudioGMS.c b/src/FAudioGMS.c index a7d4b4a..107b957 100644 --- a/src/FAudioGMS.c +++ b/src/FAudioGMS.c @@ -306,6 +306,9 @@ typedef struct FAudioGMS_Device static FAudioGMS_Device* device = NULL; +/* Game Maker doesn't let us control execution order on clean up so we have this stupid macro to help us not crash on exit */ +#define RETURN_ON_NULL_DEVICE(x) if (device == NULL) { return x; } + static inline FAudioGMS_StaticSound* FAudioGMS_INTERNAL_LookupStaticSound(uint32_t id) { if (id >= 0 && id < device->staticSoundCount) @@ -516,6 +519,7 @@ static void SetPanMatrixCoefficients(FAudioGMS_SoundInstance *instance) double FAudioGMS_StaticSound_LoadWAV(char *filePath) { + RETURN_ON_NULL_DEVICE(-1.0) drwav_uint64 frameCount; FAudioGMS_StaticSound *sound = SDL_malloc(sizeof(FAudioGMS_StaticSound)); @@ -670,7 +674,6 @@ static void FAudioGMS_INTERNAL_SoundInstance_SetPitch(FAudioGMS_SoundInstance* i static void FAudioGMS_INTERNAL_SoundInstance_SetVolume(FAudioGMS_SoundInstance* instance, float volume) { - instance->adjustingVolumeOverTime = 0; instance->volume = volume; FAudioVoice_SetVolume(instance->handle, volume, 0); } @@ -884,6 +887,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_StreamingUpdate(FAudioGMS_SoundInst double FAudioGMS_StreamingSound_LoadOGG(char* filePath) { + RETURN_ON_NULL_DEVICE(-1.0) int error = 0; stb_vorbis *fileHandle = stb_vorbis_open_filename(filePath, &error, NULL); @@ -999,6 +1003,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_Play(FAudioGMS_SoundInstance* insta void FAudioGMS_StaticSound_PlayOneOff(double staticSoundID, double pan, double pitch, double volume) { + RETURN_ON_NULL_DEVICE() FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID); if (staticSound != NULL) @@ -1016,6 +1021,7 @@ void FAudioGMS_StaticSound_PlayOneOff(double staticSoundID, double pan, double p void FAudioGMS_StaticSound_PlayOneOffSpatial(double staticSoundID, double x, double y, double z, double pitch, double volume) { + RETURN_ON_NULL_DEVICE() FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID); if (staticSound != NULL) @@ -1034,6 +1040,7 @@ void FAudioGMS_StaticSound_PlayOneOffSpatial(double staticSoundID, double x, dou double FAudioGMS_StaticSound_Play(double staticSoundID, double pan, double pitch, double volume, double loop) { + RETURN_ON_NULL_DEVICE(-1.0) FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID); if (staticSound != NULL) @@ -1053,6 +1060,7 @@ double FAudioGMS_StaticSound_Play(double staticSoundID, double pan, double pitch double FAudioGMS_StaticSound_PlaySpatial(double staticSoundID, double x, double y, double z, double pitch, double volume, double loop) { + RETURN_ON_NULL_DEVICE(-1.0) FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID); if (staticSound != NULL) @@ -1073,6 +1081,7 @@ double FAudioGMS_StaticSound_PlaySpatial(double staticSoundID, double x, double void FAudioGMS_SoundInstance_Play(double soundInstanceID, double loop) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1084,13 +1093,14 @@ void FAudioGMS_SoundInstance_Play(double soundInstanceID, double loop) void FAudioGMS_SoundInstance_Pause(double soundInstanceID) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) { if (instance->soundState == SoundState_Playing) { - FAudioSourceVoice_Stop(instance->handle, 0, 0); + FAudioSourceVoice_Stop(instance->handle, 0, 0); /* this actually just pauses lol */ instance->soundState = SoundState_Paused; } } @@ -1104,13 +1114,14 @@ static void FAudioGMS_INTERNAL_SoundInstance_Stop(FAudioGMS_SoundInstance* insta { if (instance != NULL) { - if (instance->isStatic) + FAudioSourceVoice_Stop(instance->handle, 0, 0); + FAudioSourceVoice_FlushSourceBuffers(instance->handle); + + if (!instance->isStatic) { - FAudioSourceVoice_ExitLoop(instance->handle, 0); - } - else - { - FAudioSourceVoice_Stop(instance->handle, 0, 0); + FAudioGMS_INTERNAL_SoundInstance_ClearBuffers(instance); + stb_vorbis_seek_start(instance->soundData.streamingSound.fileHandle); /* back to the start */ + FAudioGMS_INTERNAL_SoundInstance_StreamingUpdate(instance); /* preload so we dont stutter on play */ } instance->soundState = SoundState_Stopped; @@ -1123,39 +1134,14 @@ static void FAudioGMS_INTERNAL_SoundInstance_Stop(FAudioGMS_SoundInstance* insta void FAudioGMS_SoundInstance_Stop(double soundInstanceID) { -FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); -FAudioGMS_INTERNAL_SoundInstance_Stop(instance); -} - -static void FAudioGMS_INTERNAL_SoundInstance_StopImmediate(FAudioGMS_SoundInstance* instance) -{ - if (instance != NULL) - { - FAudioSourceVoice_Stop(instance->handle, 0, 0); - FAudioSourceVoice_FlushSourceBuffers(instance->handle); - - if (!instance->isStatic) - { - FAudioGMS_INTERNAL_SoundInstance_ClearBuffers(instance); - FAudioGMS_INTERNAL_SoundInstance_StreamingUpdate(instance); - } - - instance->soundState = SoundState_Stopped; - } - else - { - Log("SoundInstance_Stop: Invalid sound instance ID! Did you destroy this instance?"); - } -} - -void FAudioGMS_SoundInstance_StopImmediate(double soundInstanceID) -{ + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); - FAudioGMS_INTERNAL_SoundInstance_StopImmediate(instance); + FAudioGMS_INTERNAL_SoundInstance_Stop(instance); } void FAudioGMS_SoundInstance_SetPan(double soundInstanceID, double pan) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1166,6 +1152,7 @@ void FAudioGMS_SoundInstance_SetPan(double soundInstanceID, double pan) void FAudioGMS_SoundInstance_SetPitch(double soundInstanceID, double pitch) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1176,8 +1163,10 @@ void FAudioGMS_SoundInstance_SetPitch(double soundInstanceID, double pitch) void FAudioGMS_SoundInstance_SetVolume(double soundInstanceID, double volume) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); + instance->adjustingVolumeOverTime = 0; /* override volume adjustment over time */ if (instance != NULL) { FAudioGMS_INTERNAL_SoundInstance_SetVolume(instance, volume); @@ -1186,6 +1175,7 @@ void FAudioGMS_SoundInstance_SetVolume(double soundInstanceID, double volume) void FAudioGMS_SoundInstance_Set3DPosition(double soundInstanceID, double x, double y, double z) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1208,6 +1198,7 @@ void FAudioGMS_SoundInstance_Set3DPosition(double soundInstanceID, double x, dou void FAudioGMS_SoundInstance_SetTrackPosition(double soundInstanceID, double trackPositionInSeconds) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1217,7 +1208,7 @@ void FAudioGMS_SoundInstance_SetTrackPosition(double soundInstanceID, double tra FAudioGMS_SoundState currentState = instance->soundState; if (currentState == SoundState_Playing) { - FAudioGMS_INTERNAL_SoundInstance_StopImmediate(instance); + FAudioGMS_INTERNAL_SoundInstance_Stop(instance); } if (instance->isStatic) @@ -1238,6 +1229,7 @@ void FAudioGMS_SoundInstance_SetTrackPosition(double soundInstanceID, double tra void FAudioGMS_SoundInstance_SetVolumeOverTime(double soundInstanceID, double volume, double milliseconds) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);\ if (instance != NULL) @@ -1250,6 +1242,7 @@ void FAudioGMS_SoundInstance_SetVolumeOverTime(double soundInstanceID, double vo double FAudioGMS_SoundInstance_GetPitch(double soundInstanceID) { + RETURN_ON_NULL_DEVICE(-1.0) FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1265,6 +1258,7 @@ double FAudioGMS_SoundInstance_GetPitch(double soundInstanceID) double FAudioGMS_SoundInstance_GetVolume(double soundInstanceID) { + RETURN_ON_NULL_DEVICE(-1.0) FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1280,6 +1274,7 @@ double FAudioGMS_SoundInstance_GetVolume(double soundInstanceID) double FAudioGMS_SoundInstance_GetTrackLengthInSeconds(double soundInstanceID) { + RETURN_ON_NULL_DEVICE(-1.0) FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1302,6 +1297,7 @@ double FAudioGMS_SoundInstance_GetTrackLengthInSeconds(double soundInstanceID) void FAudioGMS_SetListenerPosition(double x, double y, double z) { + RETURN_ON_NULL_DEVICE() device->listener.Velocity.x = x - device->listener.Position.x; device->listener.Velocity.y = y - device->listener.Position.y; device->listener.Velocity.z = z - device->listener.Position.z; @@ -1317,7 +1313,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_Destroy(FAudioGMS_SoundInstance* in { device->soundInstances[instance->id] = NULL; IdStack_Push(&device->soundInstanceIndexStack, instance->id); - FAudioGMS_INTERNAL_SoundInstance_StopImmediate(instance); + FAudioGMS_INTERNAL_SoundInstance_Stop(instance); FAudioVoice_DestroyVoice(instance->handle); if (!instance->isStatic) { @@ -1335,6 +1331,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_Destroy(FAudioGMS_SoundInstance* in void FAudioGMS_SoundInstance_Destroy(double soundInstanceID) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1345,6 +1342,7 @@ void FAudioGMS_SoundInstance_Destroy(double soundInstanceID) void FAudioGMS_SoundInstance_DestroyWhenFinished(double soundInstanceID) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); if (instance != NULL) @@ -1353,7 +1351,7 @@ void FAudioGMS_SoundInstance_DestroyWhenFinished(double soundInstanceID) } } -/* FIXME: this will die horribly if a sound is playing */ +/* NOTE: this will die horribly if a sound is playing */ static void FAudioGMS_INTERNAL_StaticSound_Destroy(FAudioGMS_StaticSound* sound) { if (sound != NULL) @@ -1367,12 +1365,14 @@ static void FAudioGMS_INTERNAL_StaticSound_Destroy(FAudioGMS_StaticSound* sound) void FAudioGMS_StaticSound_Destroy(double staticSoundID) { + RETURN_ON_NULL_DEVICE() FAudioGMS_StaticSound *sound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID); FAudioGMS_INTERNAL_StaticSound_Destroy(sound); } double FAudioGMS_EffectChain_Create() { + RETURN_ON_NULL_DEVICE(-1.0) FAudioGMS_EffectChain* effectChain = SDL_malloc(sizeof(FAudioGMS_EffectChain)); effectChain->fAudioEffectChain.EffectCount = 0; @@ -1420,6 +1420,7 @@ static void FAudioGMS_INTERNAL_EffectChain_AddReverb(FAudioGMS_EffectChain* effe void FAudioGMS_EffectChain_AddDefaultReverb(double effectChainID) { + RETURN_ON_NULL_DEVICE() FAudioGMS_EffectChain *effectChain = FAudioGMS_INTERNAL_LookupEffectChain((uint32_t)effectChainID); if (effectChain != NULL) @@ -1471,6 +1472,7 @@ void FAudioGMS_EffectChain_AddReverb( double density, double roomSize ) { + RETURN_ON_NULL_DEVICE() FAudioGMS_EffectChain* effectChain = FAudioGMS_INTERNAL_LookupEffectChain((uint32_t)effectChainID); if (effectChain != NULL) @@ -1506,6 +1508,7 @@ void FAudioGMS_EffectChain_AddReverb( void FAudioGMS_SoundInstance_SetEffectChain(double soundInstanceID, double effectChainID, double effectGain) { + RETURN_ON_NULL_DEVICE() FAudioGMS_SoundInstance *instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID); FAudioGMS_EffectChain *effectChain = FAudioGMS_INTERNAL_LookupEffectChain((uint32_t)effectChainID); uint32_t i; @@ -1612,6 +1615,7 @@ static void FAudioGMS_INTERNAL_EffectChain_Destroy(FAudioGMS_EffectChain *effect void FAudioGMS_EffectChain_Destroy(double effectChainID) { + RETURN_ON_NULL_DEVICE() FAudioGMS_EffectChain *effectChain = FAudioGMS_INTERNAL_LookupEffectChain((uint32_t)effectChainID); if (effectChain != NULL) @@ -1622,6 +1626,7 @@ void FAudioGMS_EffectChain_Destroy(double effectChainID) void FAudioGMS_Update() { + RETURN_ON_NULL_DEVICE() uint32_t i; for (i = 0; i < device->soundInstanceCount; i += 1) @@ -1684,16 +1689,18 @@ void FAudioGMS_Update() void FAudioGMS_StopAll() { + RETURN_ON_NULL_DEVICE() uint32_t i; for (i = 0; i < device->soundInstanceCount; i += 1) { - FAudioGMS_INTERNAL_SoundInstance_StopImmediate(device->soundInstances[i]); + FAudioGMS_INTERNAL_SoundInstance_Stop(device->soundInstances[i]); } } void FAudioGMS_Destroy() { + RETURN_ON_NULL_DEVICE() uint32_t i; for (i = 0; i < device->soundInstanceCount; i += 1) diff --git a/src/FAudioGMS.h b/src/FAudioGMS.h index 23dadf6..127a1d6 100644 --- a/src/FAudioGMS.h +++ b/src/FAudioGMS.h @@ -53,7 +53,6 @@ FAUDIOGMSAPI double FAudioGMS_StreamingSound_LoadOGG(char* filepath); /* returns FAUDIOGMSAPI void FAudioGMS_SoundInstance_Play(double soundInstanceID, double loop); FAUDIOGMSAPI void FAudioGMS_SoundInstance_Pause(double soundInstanceID); FAUDIOGMSAPI void FAudioGMS_SoundInstance_Stop(double soundInstanceID); -FAUDIOGMSAPI void FAudioGMS_SoundInstance_StopImmediate(double soundInstanceID); FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetPan(double soundInstanceID, double pan); FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetPitch(double soundInstanceID, double pitch);