From 8c2f457de9086a9fe8ca8452ceefdbc26d766d71 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 4 Apr 2021 18:38:12 +1000 Subject: [PATCH] Reduce the weight of ma_sound by allocating some data on the heap. --- research/miniaudio_engine.h | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/research/miniaudio_engine.h b/research/miniaudio_engine.h index 8baf3983..b6bba8f3 100644 --- a/research/miniaudio_engine.h +++ b/research/miniaudio_engine.h @@ -1691,7 +1691,7 @@ struct ma_sound sound via the resource manager, which I *think* will be the most common scenario. */ #ifndef MA_NO_RESOURCE_MANAGER - ma_resource_manager_data_source resourceManagerDataSource; + ma_resource_manager_data_source* pResourceManagerDataSource; #endif }; @@ -11145,10 +11145,15 @@ MA_API ma_result ma_sound_init_from_file_internal(ma_engine* pEngine, const ma_s */ flags = pConfig->flags | MA_DATA_SOURCE_FLAG_WAIT_INIT; + pSound->pResourceManagerDataSource = (ma_resource_manager_data_source*)ma_malloc(sizeof(*pSound->pResourceManagerDataSource), &pEngine->allocationCallbacks); + if (pSound->pResourceManagerDataSource == NULL) { + return MA_OUT_OF_MEMORY; + } + if (pConfig->pFilePath != NULL) { - result = ma_resource_manager_data_source_init(pEngine->pResourceManager, pConfig->pFilePath, flags, NULL, &pSound->resourceManagerDataSource); + result = ma_resource_manager_data_source_init(pEngine->pResourceManager, pConfig->pFilePath, flags, NULL, pSound->pResourceManagerDataSource); } else { - result = ma_resource_manager_data_source_init_w(pEngine->pResourceManager, pConfig->pFilePathW, flags, NULL, &pSound->resourceManagerDataSource); + result = ma_resource_manager_data_source_init_w(pEngine->pResourceManager, pConfig->pFilePathW, flags, NULL, pSound->pResourceManagerDataSource); } if (result != MA_SUCCESS) { @@ -11161,11 +11166,12 @@ MA_API ma_result ma_sound_init_from_file_internal(ma_engine* pEngine, const ma_s config = *pConfig; config.pFilePath = NULL; config.pFilePathW = NULL; - config.pDataSource = &pSound->resourceManagerDataSource; + config.pDataSource = pSound->pResourceManagerDataSource; result = ma_sound_init_from_data_source_internal(pEngine, &config, pSound); if (result != MA_SUCCESS) { - ma_resource_manager_data_source_uninit(&pSound->resourceManagerDataSource); + ma_resource_manager_data_source_uninit(pSound->pResourceManagerDataSource); + ma_free(pSound->pResourceManagerDataSource, &pEngine->allocationCallbacks); MA_ZERO_OBJECT(pSound); return result; } @@ -11246,7 +11252,8 @@ MA_API void ma_sound_uninit(ma_sound* pSound) /* Once the sound is detached from the group we can guarantee that it won't be referenced by the mixer thread which means it's safe for us to destroy the data source. */ #ifndef MA_NO_RESOURCE_MANAGER if (pSound->ownsDataSource) { - ma_resource_manager_data_source_uninit(&pSound->resourceManagerDataSource); + ma_resource_manager_data_source_uninit(pSound->pResourceManagerDataSource); + ma_free(pSound->pResourceManagerDataSource, &pSound->engineNode.pEngine->allocationCallbacks); pSound->pDataSource = NULL; } #else @@ -11710,8 +11717,8 @@ MA_API ma_result ma_sound_set_looping(ma_sound* pSound, ma_bool8 isLooping) generically. */ #ifndef MA_NO_RESOURCE_MANAGER - if (pSound->pDataSource == &pSound->resourceManagerDataSource) { - ma_resource_manager_data_source_set_looping(&pSound->resourceManagerDataSource, isLooping); + if (pSound->pDataSource == pSound->pResourceManagerDataSource) { + ma_resource_manager_data_source_set_looping(pSound->pResourceManagerDataSource, isLooping); } #endif @@ -11762,8 +11769,8 @@ MA_API ma_result ma_sound_seek_to_pcm_frame(ma_sound* pSound, ma_uint64 frameInd thread safe as well so in that case we'll need to get the mixing thread to seek for us to ensure we don't try seeking at the same time as reading. */ #ifndef MA_NO_RESOURCE_MANAGER - if (pSound->pDataSource == &pSound->resourceManagerDataSource) { - ma_result result = ma_resource_manager_data_source_seek_to_pcm_frame(&pSound->resourceManagerDataSource, frameIndex); + if (pSound->pDataSource == pSound->pResourceManagerDataSource) { + ma_result result = ma_resource_manager_data_source_seek_to_pcm_frame(pSound->pResourceManagerDataSource, frameIndex); if (result != MA_SUCCESS) { return result; }