From 23993590894da047dbcd9f5ca7c427b32dfd754f Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 4 Jul 2021 17:03:46 +1000 Subject: [PATCH] API CHANGE: Update ma_decoder_get_length_in_pcm_frames(). This is now consistent with all other data sources: * Returns a result code * Length is returned via an output parameter --- miniaudio.h | 47 +++++++++++++++++++++---------------- research/miniaudio_engine.h | 11 +++++++-- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index 8c596d3c..adf86e13 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -6249,7 +6249,7 @@ For MP3's, this will decode the entire file. Do not call this in time critical s This function is not thread safe without your own synchronization. */ -MA_API ma_uint64 ma_decoder_get_length_in_pcm_frames(ma_decoder* pDecoder); +MA_API ma_result ma_decoder_get_length_in_pcm_frames(ma_decoder* pDecoder, ma_uint64* pLength); /* Reads PCM frames from the given decoder. @@ -49623,12 +49623,7 @@ static ma_result ma_decoder__data_source_on_get_length(ma_data_source* pDataSour { ma_decoder* pDecoder = (ma_decoder*)pDataSource; - *pLength = ma_decoder_get_length_in_pcm_frames(pDecoder); - if (*pLength == 0) { - return MA_NOT_IMPLEMENTED; - } - - return MA_SUCCESS; + return ma_decoder_get_length_in_pcm_frames(pDecoder, pLength); } static ma_data_source_vtable g_ma_decoder_data_source_vtable = @@ -50413,32 +50408,43 @@ MA_API ma_result ma_decoder_get_cursor_in_pcm_frames(ma_decoder* pDecoder, ma_ui return MA_SUCCESS; } -MA_API ma_uint64 ma_decoder_get_length_in_pcm_frames(ma_decoder* pDecoder) +MA_API ma_result ma_decoder_get_length_in_pcm_frames(ma_decoder* pDecoder, ma_uint64* pLength) { + if (pLength == NULL) { + return MA_INVALID_ARGS; + } + + *pLength = 0; + if (pDecoder == NULL) { - return 0; + return MA_INVALID_ARGS; } if (pDecoder->pBackend != NULL) { ma_result result; - ma_uint64 nativeLengthInPCMFrames; + ma_uint64 internalLengthInPCMFrames; ma_uint32 internalSampleRate; - ma_data_source_get_length_in_pcm_frames(pDecoder->pBackend, &nativeLengthInPCMFrames); + result = ma_data_source_get_length_in_pcm_frames(pDecoder->pBackend, &internalLengthInPCMFrames); + if (result != MA_SUCCESS) { + return result; /* Failed to retrieve the internal length. */ + } result = ma_data_source_get_data_format(pDecoder->pBackend, NULL, NULL, &internalSampleRate); if (result != MA_SUCCESS) { - return 0; /* Failed to retrieve the internal sample rate. */ + return result; /* Failed to retrieve the internal sample rate. */ } if (internalSampleRate == pDecoder->outputSampleRate) { - return nativeLengthInPCMFrames; + *pLength = internalLengthInPCMFrames; } else { - return ma_calculate_frame_count_after_resampling(pDecoder->outputSampleRate, internalSampleRate, nativeLengthInPCMFrames); + *pLength = ma_calculate_frame_count_after_resampling(pDecoder->outputSampleRate, internalSampleRate, internalLengthInPCMFrames); } - } - return 0; + return MA_SUCCESS; + } else { + return MA_NO_BACKEND; + } } MA_API ma_uint64 ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesOut, ma_uint64 frameCount) @@ -50571,6 +50577,7 @@ MA_API ma_result ma_decoder_seek_to_pcm_frame(ma_decoder* pDecoder, ma_uint64 fr MA_API ma_result ma_decoder_get_available_frames(ma_decoder* pDecoder, ma_uint64* pAvailableFrames) { + ma_result result; ma_uint64 totalFrameCount; if (pAvailableFrames == NULL) { @@ -50583,9 +50590,9 @@ MA_API ma_result ma_decoder_get_available_frames(ma_decoder* pDecoder, ma_uint64 return MA_INVALID_ARGS; } - totalFrameCount = ma_decoder_get_length_in_pcm_frames(pDecoder); - if (totalFrameCount == 0) { - return MA_NOT_IMPLEMENTED; + result = ma_decoder_get_length_in_pcm_frames(pDecoder, &totalFrameCount); + if (result != MA_SUCCESS) { + return result; } if (totalFrameCount <= pDecoder->readPointerInPCMFrames) { @@ -50594,7 +50601,7 @@ MA_API ma_result ma_decoder_get_available_frames(ma_decoder* pDecoder, ma_uint64 *pAvailableFrames = totalFrameCount - pDecoder->readPointerInPCMFrames; } - return MA_SUCCESS; /* No frames available. */ + return MA_SUCCESS; } diff --git a/research/miniaudio_engine.h b/research/miniaudio_engine.h index fe2d9de1..bb95cdb6 100644 --- a/research/miniaudio_engine.h +++ b/research/miniaudio_engine.h @@ -7736,7 +7736,11 @@ static ma_result ma_resource_manager_data_buffer_node_init_supply_decoded(ma_res allocated buffer, whereas a paged buffer is a linked list of paged-sized buffers. The latter is used when the length of a sound is unknown until a full decode has been performed. */ - totalFrameCount = ma_decoder_get_length_in_pcm_frames(pDecoder); + result = ma_decoder_get_length_in_pcm_frames(pDecoder, &totalFrameCount); + if (result != MA_SUCCESS) { + return result; + } + if (totalFrameCount > 0) { /* It's a known length. The data supply is a regular decoded buffer. */ ma_uint64 dataSizeInBytes; @@ -10200,7 +10204,10 @@ static ma_result ma_resource_manager_process_job__load_data_stream(ma_resource_m } /* Retrieve the total length of the file before marking the decoder are loaded. */ - pDataStream->totalLengthInPCMFrames = ma_decoder_get_length_in_pcm_frames(&pDataStream->decoder); + result = ma_decoder_get_length_in_pcm_frames(&pDataStream->decoder, &pDataStream->totalLengthInPCMFrames); + if (result != MA_SUCCESS) { + goto done; /* Failed to retrieve the length. */ + } /* Only mark the decoder as initialized when the length of the decoder has been retrieved because that can possibly require a scan over the whole file