From f5258f2d414a30a5afa9e1fee54bd70421ec16d6 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 21 May 2022 21:20:02 -0700 Subject: [PATCH] audio_stream_load_url() cleanup --- autogen/lua_definitions/functions.lua | 2 +- docs/lua/functions-4.md | 6 +++--- docs/lua/functions.md | 2 +- src/pc/lua/smlua_functions_autogen.c | 6 +++--- src/pc/lua/utils/smlua_audio_utils.c | 28 +++++++++++++++++---------- src/pc/lua/utils/smlua_audio_utils.h | 3 +-- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index 683001cc2..959cb991d 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -7213,7 +7213,7 @@ end --- @param url string --- @return BassAudio -function audio_stream_loadURL(url) +function audio_stream_load_url(url) -- ... end diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index ee9e3091b..6263d0970 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -4767,10 +4767,10 @@
-## [audio_stream_loadURL](#audio_stream_loadURL) +## [audio_stream_load_url](#audio_stream_load_url) ### Lua Example -`local BassAudioValue = audio_stream_loadURL(url)` +`local BassAudioValue = audio_stream_load_url(url)` ### Parameters | Field | Type | @@ -4781,7 +4781,7 @@ [BassAudio](structs.md#BassAudio) ### C Prototype -`struct BassAudio* audio_stream_loadURL(const char* url);` +`struct BassAudio* audio_stream_load_url(const char* url);` [:arrow_up_small:](#) diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 3ae990722..256e90c71 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1344,7 +1344,7 @@ - [audio_stream_get_tempo](functions-4.md#audio_stream_get_tempo) - [audio_stream_get_volume](functions-4.md#audio_stream_get_volume) - [audio_stream_load](functions-4.md#audio_stream_load) - - [audio_stream_loadURL](functions-4.md#audio_stream_loadURL) + - [audio_stream_load_url](functions-4.md#audio_stream_load_url) - [audio_stream_pause](functions-4.md#audio_stream_pause) - [audio_stream_play](functions-4.md#audio_stream_play) - [audio_stream_set_frequency](functions-4.md#audio_stream_set_frequency) diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index f4d25699e..3b6437a73 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -14857,13 +14857,13 @@ int smlua_func_audio_stream_load(lua_State* L) { return 1; } -int smlua_func_audio_stream_loadURL(lua_State* L) { +int smlua_func_audio_stream_load_url(lua_State* L) { if(!smlua_functions_valid_param_count(L, 1)) { return 0; } const char* url = smlua_to_string(L, 1); if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } - smlua_push_object(L, LOT_BASSAUDIO, audio_stream_loadURL(url)); + smlua_push_object(L, LOT_BASSAUDIO, audio_stream_load_url(url)); return 1; } @@ -17576,7 +17576,7 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "audio_stream_get_tempo", smlua_func_audio_stream_get_tempo); smlua_bind_function(L, "audio_stream_get_volume", smlua_func_audio_stream_get_volume); smlua_bind_function(L, "audio_stream_load", smlua_func_audio_stream_load); - smlua_bind_function(L, "audio_stream_loadURL", smlua_func_audio_stream_loadURL); + smlua_bind_function(L, "audio_stream_load_url", smlua_func_audio_stream_load_url); smlua_bind_function(L, "audio_stream_pause", smlua_func_audio_stream_pause); smlua_bind_function(L, "audio_stream_play", smlua_func_audio_stream_play); smlua_bind_function(L, "audio_stream_set_frequency", smlua_func_audio_stream_set_frequency); diff --git a/src/pc/lua/utils/smlua_audio_utils.c b/src/pc/lua/utils/smlua_audio_utils.c index b80e8de59..63cc1ff1f 100644 --- a/src/pc/lua/utils/smlua_audio_utils.c +++ b/src/pc/lua/utils/smlua_audio_utils.c @@ -250,6 +250,18 @@ struct BassAudio* audio_stream_load(const char* filename) { return audio_load_internal(filename, true); } +struct BassAudio* audio_stream_load_url(const char* url) { + if (url == NULL || strlen(url) == 0) { + LOG_LUA_LINE("Failed to load url"); + return NULL; + } + HSTREAM stream = BASS_StreamCreateURL(url, 0, 0, NULL, NULL); + struct BassAudio* res = malloc(sizeof(struct BassAudio)); + res->handle = stream; + res->rawData = NULL; + return res; +} + void audio_stream_destroy(struct BassAudio* audio) { if (!audio_sanity_check(audio, true, "destroy")) { return; @@ -258,7 +270,9 @@ void audio_stream_destroy(struct BassAudio* audio) { bassh_free_stream(audio->handle); audio->handle = 0; audio->loaded = false; - free(audio->rawData); + if (audio->rawData != NULL) { + free(audio->rawData); + } audio->rawData = NULL; } @@ -377,7 +391,9 @@ void audio_sample_destroy(struct BassAudio* audio) { bassh_free_stream(audio->handle); audio->handle = 0; audio->loaded = false; - free(audio->rawData); + if (audio->rawData) { + free(audio->rawData); + } audio->rawData = NULL; } @@ -423,11 +439,3 @@ void audio_custom_shutdown(void) { } sBassAudioCount = 0; } - -struct BassAudio* audio_stream_loadURL(const char* url) { - HSTREAM stream = BASS_StreamCreateURL(url, 0, 0, NULL, NULL); - struct BassAudio* res = malloc(sizeof(struct BassAudio)); - res->handle = stream; - res->rawData = NULL; - return res; -} diff --git a/src/pc/lua/utils/smlua_audio_utils.h b/src/pc/lua/utils/smlua_audio_utils.h index 3b9a15fd2..e5c7e18e7 100644 --- a/src/pc/lua/utils/smlua_audio_utils.h +++ b/src/pc/lua/utils/smlua_audio_utils.h @@ -18,6 +18,7 @@ struct BassAudio { }; struct BassAudio* audio_stream_load(const char* filename); +struct BassAudio* audio_stream_load_url(const char* url); void audio_stream_destroy(struct BassAudio* audio); void audio_stream_play(struct BassAudio* audio, bool restart, f32 volume); void audio_stream_pause(struct BassAudio* audio); @@ -33,8 +34,6 @@ void audio_stream_set_tempo(struct BassAudio* audio, f32 tempo); f32 audio_stream_get_volume(struct BassAudio* audio); void audio_stream_set_volume(struct BassAudio* audio, f32 volume); void audio_stream_set_speed(struct BassAudio* audio, f32 initial_freq, f32 speed, bool pitch); -struct BassAudio* audio_stream_loadURL(const char* url); - struct BassAudio* audio_sample_load(const char* filename); void audio_sample_destroy(struct BassAudio* audio);