From 373234255ab7ffc0a1e3610ea717ec5830951e75 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sat, 13 Jun 2020 09:15:47 +1000 Subject: [PATCH] Add ma_decode_from_vfs(). --- miniaudio.h | 22 +++++++++++--------- research/ma_engine.h | 48 +++++--------------------------------------- 2 files changed, 18 insertions(+), 52 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index 631c6030..07b79acf 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -5571,6 +5571,7 @@ MA_API ma_result ma_decoder_seek_to_pcm_frame(ma_decoder* pDecoder, ma_uint64 fr Helper for opening and decoding a file into a heap allocated block of memory. Free the returned pointer with ma_free(). On input, pConfig should be set to what you want. On output it will be set to what you got. */ +MA_API ma_result ma_decode_from_vfs(ma_vfs* pVFS, const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut); MA_API ma_result ma_decode_file(const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut); MA_API ma_result ma_decode_memory(const void* pData, size_t dataSize, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut); @@ -43308,11 +43309,11 @@ static ma_result ma_decoder__full_decode_and_uninit(ma_decoder* pDecoder, ma_dec return MA_SUCCESS; } -MA_API ma_result ma_decode_file(const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut) +MA_API ma_result ma_decode_from_vfs(ma_vfs* pVFS, const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut) { + ma_result result; ma_decoder_config config; ma_decoder decoder; - ma_result result; if (pFrameCountOut != NULL) { *pFrameCountOut = 0; @@ -43321,18 +43322,21 @@ MA_API ma_result ma_decode_file(const char* pFilePath, ma_decoder_config* pConfi *ppPCMFramesOut = NULL; } - if (pFilePath == NULL) { - return MA_INVALID_ARGS; - } - config = ma_decoder_config_init_copy(pConfig); - - result = ma_decoder_init_file(pFilePath, &config, &decoder); + + result = ma_decoder_init_vfs(pVFS, pFilePath, &config, &decoder); if (result != MA_SUCCESS) { return result; } - return ma_decoder__full_decode_and_uninit(&decoder, pConfig, pFrameCountOut, ppPCMFramesOut); + result = ma_decoder__full_decode_and_uninit(&decoder, pConfig, pFrameCountOut, ppPCMFramesOut); + + return result; +} + +MA_API ma_result ma_decode_file(const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut) +{ + return ma_decode_from_vfs(NULL, pFilePath, pConfig, pFrameCountOut, ppPCMFramesOut); } MA_API ma_result ma_decode_memory(const void* pData, size_t dataSize, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut) diff --git a/research/ma_engine.h b/research/ma_engine.h index 185bb9b1..3bd54fee 100644 --- a/research/ma_engine.h +++ b/research/ma_engine.h @@ -67,12 +67,6 @@ Whenever a sound is played, it should usually be loaded into one of the in-memor extern "C" { #endif -/* -Fully decodes a file from a VFS. -*/ -MA_API ma_result ma_decode_from_vfs(ma_vfs* pVFS, const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut); - - /* Memory Allocation Types ======================= @@ -240,12 +234,6 @@ struct ma_resource_manager_data_source ma_result result; /* Result from asynchronous loading. When loading set to MA_BUSY. When fully loaded set to MA_SUCCESS. When deleting set to MA_UNAVAILABLE. */ ma_uint64 frameCursor; ma_bool32 seekToCursorOnNextRead : 1; /* On the next read we need to seek to the frame cursor. */ - /*ma_uint32 holdCount;*/ /* For preventing the backend from being uninitialized from under the data source while it's in the middle of performing an operation. */ - - union - { - int _unused; - } streaming; union { ma_decoder decoder; @@ -281,7 +269,7 @@ struct ma_resource_manager MA_API ma_result ma_resource_manager_init(const ma_resource_manager_config* pConfig, ma_resource_manager* pResourceManager); MA_API void ma_resource_manager_uninit(ma_resource_manager* pResourceManager); -/* Data Nodes. */ +/* Data Buffers. */ MA_API ma_result ma_resource_manager_create_data_buffer(ma_resource_manager* pResourceManager, const char* pFilePath, ma_resource_manager_data_buffer_type type, ma_event* pEvent, ma_resource_manager_data_buffer** ppDataBuffer); MA_API ma_result ma_resource_manager_delete_data_buffer(ma_resource_manager* pResourceManager, ma_resource_manager_data_buffer* pDataBuffer); MA_API ma_result ma_resource_manager_data_buffer_result(ma_resource_manager* pResourceManager, const ma_resource_manager_data_buffer* pDataBuffer); @@ -451,10 +439,10 @@ struct ma_sound_group ma_sound_group* pFirstChild; ma_sound_group* pPrevSibling; ma_sound_group* pNextSibling; - ma_sound* pFirstSoundInGroup; /* Marked as volatile because we need to be very explicit with when we make copies of this and we can't have the compiler optimize it out. */ + ma_sound* pFirstSoundInGroup; /* Marked as volatile because we need to be very explicit with when we make copies of this and we can't have the compiler optimize it out. */ ma_mixer mixer; - ma_mutex lock; /* Only used by ma_engine_create_sound_*() and ma_engine_delete_sound(). Not used in the mixing thread. */ - ma_bool32 isPlaying; /* True by default. Sound groups can be stopped with ma_engine_sound_stop() and resumed with ma_engine_sound_start(). Also affects children. */ + ma_mutex lock; /* Only used by ma_engine_sound_init_*() and ma_engine_sound_uninit(). Not used in the mixing thread. */ + ma_bool32 isPlaying; /* True by default. Sound groups can be stopped with ma_engine_sound_stop() and resumed with ma_engine_sound_start(). Also affects children. */ }; struct ma_listener @@ -661,32 +649,6 @@ static ma_uint32 ma_hash_string_32(const char* str) -MA_API ma_result ma_decode_from_vfs(ma_vfs* pVFS, const char* pFilePath, ma_decoder_config* pConfig, ma_uint64* pFrameCountOut, void** ppPCMFramesOut) -{ - ma_result result; - ma_decoder_config config; - ma_decoder decoder; - - if (pFrameCountOut != NULL) { - *pFrameCountOut = 0; - } - if (ppPCMFramesOut != NULL) { - *ppPCMFramesOut = NULL; - } - - config = ma_decoder_config_init_copy(pConfig); - - result = ma_decoder_init_vfs(pVFS, pFilePath, &config, &decoder); - if (result != MA_SUCCESS) { - return result; - } - - result = ma_decoder__full_decode_and_uninit(&decoder, pConfig, pFrameCountOut, ppPCMFramesOut); - - return result; -} - - MA_API ma_resource_manager_message ma_resource_manager_message_init(ma_uint32 code) { ma_resource_manager_message message; @@ -2855,7 +2817,7 @@ static void ma_engine_sound_mix_wait(ma_sound* pSound) /* Just do a basic spin wait. */ while (pSound->isMixing) { - continue; /* Do nothing - just keep waiting for the mixer thread. */ + ma_yield(); } }