diff --git a/examples/simple_capture.c b/examples/simple_capture.c index f355164c..0fef18c0 100644 --- a/examples/simple_capture.c +++ b/examples/simple_capture.c @@ -37,7 +37,7 @@ int main(int argc, char** argv) return -1; } - encoderConfig = ma_encoder_config_init(ma_resource_format_wav, ma_format_f32, 2, 44100); + encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100); if (ma_encoder_init_file(argv[1], &encoderConfig, &encoder) != MA_SUCCESS) { printf("Failed to initialize output file.\n"); diff --git a/examples/simple_loopback.c b/examples/simple_loopback.c index 084cf866..7b24f45f 100644 --- a/examples/simple_loopback.c +++ b/examples/simple_loopback.c @@ -44,7 +44,7 @@ int main(int argc, char** argv) return -1; } - encoderConfig = ma_encoder_config_init(ma_resource_format_wav, ma_format_f32, 2, 44100); + encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100); if (ma_encoder_init_file(argv[1], &encoderConfig, &encoder) != MA_SUCCESS) { printf("Failed to initialize output file.\n"); diff --git a/examples/simple_mixing.c b/examples/simple_mixing.c index eb8dafda..5d846048 100644 --- a/examples/simple_mixing.c +++ b/examples/simple_mixing.c @@ -61,7 +61,7 @@ ma_uint32 read_and_mix_pcm_frames_f32(ma_decoder* pDecoder, float* pOutputF32, m framesToReadThisIteration = totalFramesRemaining; } - framesReadThisIteration = (ma_uint32)ma_decoder_read_pcm_frames(pDecoder, temp, framesToReadThisIteration); + framesReadThisIteration = (ma_uint32)ma_decoder_read_pcm_frames(pDecoder, temp, framesToReadThisIteration, NULL); if (framesReadThisIteration == 0) { break; } diff --git a/examples/simple_playback.c b/examples/simple_playback.c index d0a9ea93..491b4c26 100644 --- a/examples/simple_playback.c +++ b/examples/simple_playback.c @@ -22,7 +22,7 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin return; } - ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount); + ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount, NULL); (void)pInput; } diff --git a/miniaudio.h b/miniaudio.h index adf86e13..af967279 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -6256,7 +6256,7 @@ Reads PCM frames from the given decoder. This is not thread safe without your own synchronization. */ -MA_API ma_uint64 ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesOut, ma_uint64 frameCount); +MA_API ma_result ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead); /* Seeks to a PCM frame based on it's absolute index. @@ -49583,17 +49583,7 @@ static ma_result ma_decoder__init_allocation_callbacks(const ma_decoder_config* static ma_result ma_decoder__data_source_on_read(ma_data_source* pDataSource, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead) { - ma_uint64 framesRead = ma_decoder_read_pcm_frames((ma_decoder*)pDataSource, pFramesOut, frameCount); - - if (pFramesRead != NULL) { - *pFramesRead = framesRead; - } - - if (framesRead == 0) { - return MA_AT_END; - } - - return MA_SUCCESS; + return ma_decoder_read_pcm_frames((ma_decoder*)pDataSource, pFramesOut, frameCount, pFramesRead); } static ma_result ma_decoder__data_source_on_seek(ma_data_source* pDataSource, ma_uint64 frameIndex) @@ -49614,16 +49604,12 @@ static ma_result ma_decoder__data_source_on_get_data_format(ma_data_source* pDat static ma_result ma_decoder__data_source_on_get_cursor(ma_data_source* pDataSource, ma_uint64* pCursor) { - ma_decoder* pDecoder = (ma_decoder*)pDataSource; - - return ma_decoder_get_cursor_in_pcm_frames(pDecoder, pCursor); + return ma_decoder_get_cursor_in_pcm_frames((ma_decoder*)pDataSource, pCursor); } static ma_result ma_decoder__data_source_on_get_length(ma_data_source* pDataSource, ma_uint64* pLength) { - ma_decoder* pDecoder = (ma_decoder*)pDataSource; - - return ma_decoder_get_length_in_pcm_frames(pDecoder, pLength); + return ma_decoder_get_length_in_pcm_frames((ma_decoder*)pDataSource, pLength); } static ma_data_source_vtable g_ma_decoder_data_source_vtable = @@ -50447,19 +50433,23 @@ MA_API ma_result ma_decoder_get_length_in_pcm_frames(ma_decoder* pDecoder, ma_ui } } -MA_API ma_uint64 ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesOut, ma_uint64 frameCount) +MA_API ma_result ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead) { - ma_result result; + ma_result result = MA_SUCCESS; ma_uint64 totalFramesReadOut; ma_uint64 totalFramesReadIn; void* pRunningFramesOut; + if (pFramesRead != NULL) { + *pFramesRead = 0; /* Safety. */ + } + if (pDecoder == NULL) { - return 0; + return MA_INVALID_ARGS; } if (pDecoder->pBackend == NULL) { - return 0; + return MA_INVALID_OPERATION; } /* Fast path. */ @@ -50483,7 +50473,7 @@ MA_API ma_uint64 ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesO result = ma_data_source_get_data_format(pDecoder->pBackend, &internalFormat, &internalChannels, NULL); if (result != MA_SUCCESS) { - return 0; /* Failed to retrieve the internal format and channel count. */ + return result; /* Failed to retrieve the internal format and channel count. */ } while (totalFramesReadOut < frameCount) { @@ -50538,7 +50528,11 @@ MA_API ma_uint64 ma_decoder_read_pcm_frames(ma_decoder* pDecoder, void* pFramesO pDecoder->readPointerInPCMFrames += totalFramesReadOut; - return totalFramesReadOut; + if (pFramesRead != NULL) { + *pFramesRead = totalFramesReadOut; + } + + return result; } MA_API ma_result ma_decoder_seek_to_pcm_frame(ma_decoder* pDecoder, ma_uint64 frameIndex) @@ -50607,6 +50601,7 @@ MA_API ma_result ma_decoder_get_available_frames(ma_decoder* pDecoder, ma_uint64 static ma_result ma_decoder__full_decode_and_uninit(ma_decoder* pDecoder, ma_decoder_config* pConfigOut, ma_uint64* pFrameCountOut, void** ppPCMFramesOut) { + ma_result result; ma_uint64 totalFrameCount; ma_uint64 bpf; ma_uint64 dataCapInFrames; @@ -50652,9 +50647,13 @@ static ma_result ma_decoder__full_decode_and_uninit(ma_decoder* pDecoder, ma_dec frameCountToTryReading = dataCapInFrames - totalFrameCount; MA_ASSERT(frameCountToTryReading > 0); - framesJustRead = ma_decoder_read_pcm_frames(pDecoder, (ma_uint8*)pPCMFramesOut + (totalFrameCount * bpf), frameCountToTryReading); + result = ma_decoder_read_pcm_frames(pDecoder, (ma_uint8*)pPCMFramesOut + (totalFrameCount * bpf), frameCountToTryReading, &framesJustRead); totalFrameCount += framesJustRead; + if (result != MA_SUCCESS) { + break; + } + if (framesJustRead < frameCountToTryReading) { break; } diff --git a/research/miniaudio_engine.c b/research/miniaudio_engine.c index 4ea2f248..bdb1f092 100644 --- a/research/miniaudio_engine.c +++ b/research/miniaudio_engine.c @@ -79,18 +79,18 @@ int main(int argc, char** argv) loadNotification.cb.onSignal = on_sound_loaded; loadNotification.pSound = &sound; - result = ma_sound_init_from_file(&engine, argv[1], MA_DATA_SOURCE_FLAG_DECODE | MA_DATA_SOURCE_FLAG_ASYNC /*| MA_DATA_SOURCE_FLAG_STREAM*/, &group, NULL, &baseSound); + result = ma_sound_init_from_file(&engine, argv[1], MA_DATA_SOURCE_FLAG_DECODE | MA_DATA_SOURCE_FLAG_ASYNC | MA_DATA_SOURCE_FLAG_STREAM, &group, NULL, &sound); if (result != MA_SUCCESS) { printf("Failed to load sound: %s\n", argv[1]); ma_engine_uninit(&engine); return -1; } - result = ma_sound_init_copy(&engine, &baseSound, 0, &group, &sound); + /*result = ma_sound_init_copy(&engine, &baseSound, 0, &group, &sound); if (result != MA_SUCCESS) { printf("Failed to copy sound.\n"); return -1; - } + }*/ #if 0 result = ma_sound_init_from_file(&engine, argv[1], MA_DATA_SOURCE_FLAG_DECODE /*| MA_DATA_SOURCE_FLAG_ASYNC | MA_DATA_SOURCE_FLAG_STREAM*/, NULL, &sound2); diff --git a/research/miniaudio_engine.h b/research/miniaudio_engine.h index bb95cdb6..5b8e2e27 100644 --- a/research/miniaudio_engine.h +++ b/research/miniaudio_engine.h @@ -7832,7 +7832,7 @@ static ma_result ma_resource_manager_data_buffer_node_decode_next_page(ma_resour ); MA_ASSERT(pDst != NULL); - framesRead = ma_decoder_read_pcm_frames(pDecoder, pDst, framesToTryReading); + result = ma_decoder_read_pcm_frames(pDecoder, pDst, framesToTryReading, &framesRead); if (framesRead > 0) { pDataBufferNode->data.decoded.decodedFrameCount += framesRead; } @@ -7848,7 +7848,7 @@ static ma_result ma_resource_manager_data_buffer_node_decode_next_page(ma_resour return result; } - framesRead = ma_decoder_read_pcm_frames(pDecoder, pPage->pAudioData, framesToTryReading); + result = ma_decoder_read_pcm_frames(pDecoder, pPage->pAudioData, framesToTryReading, &framesRead); if (framesRead > 0) { pPage->sizeInFrames = framesRead; @@ -9113,6 +9113,7 @@ static void* ma_resource_manager_data_stream_get_page_data_pointer(ma_resource_m static void ma_resource_manager_data_stream_fill_page(ma_resource_manager_data_stream* pDataStream, ma_uint32 pageIndex) { + ma_result result = MA_SUCCESS; ma_bool32 isLooping; ma_uint64 pageSizeInFrames; ma_uint64 totalFramesReadForThisPage = 0; @@ -9128,23 +9129,28 @@ static void ma_resource_manager_data_stream_fill_page(ma_resource_manager_data_s ma_uint64 framesRead; framesRemaining = pageSizeInFrames - totalFramesReadForThisPage; - framesRead = ma_decoder_read_pcm_frames(&pDataStream->decoder, ma_offset_pcm_frames_ptr(pPageData, totalFramesReadForThisPage, pDataStream->decoder.outputFormat, pDataStream->decoder.outputChannels), framesRemaining); + result = ma_decoder_read_pcm_frames(&pDataStream->decoder, ma_offset_pcm_frames_ptr(pPageData, totalFramesReadForThisPage, pDataStream->decoder.outputFormat, pDataStream->decoder.outputChannels), framesRemaining, &framesRead); totalFramesReadForThisPage += framesRead; /* Loop back to the start if we reached the end. We'll also have a known length at this point as well. */ - if (framesRead < framesRemaining) { + if (result == MA_AT_END || framesRead < framesRemaining) { if (pDataStream->totalLengthInPCMFrames == 0) { ma_decoder_get_cursor_in_pcm_frames(&pDataStream->decoder, &pDataStream->totalLengthInPCMFrames); } ma_decoder_seek_to_pcm_frame(&pDataStream->decoder, 0); + result = MA_SUCCESS; /* Clear the AT_END result so we don't incorrectly mark this looping stream as at the end and then have it stopped. */ + } + + if (result != MA_SUCCESS && result != MA_AT_END) { + break; } } } else { - totalFramesReadForThisPage = ma_decoder_read_pcm_frames(&pDataStream->decoder, pPageData, pageSizeInFrames); + result = ma_decoder_read_pcm_frames(&pDataStream->decoder, pPageData, pageSizeInFrames, &totalFramesReadForThisPage); } - if (totalFramesReadForThisPage < pageSizeInFrames) { + if (result == MA_AT_END || totalFramesReadForThisPage < pageSizeInFrames) { c89atomic_exchange_32(&pDataStream->isDecoderAtEnd, MA_TRUE); }