[raudio] Remove usage of ma_data_converter_get_required_input_frame_count()#5568
Conversation
This function is being removed from miniaudio. To make this work with the current architecture of raylib it requires the use of a cache. This commit implements a generic solution that works across all AudioBuffer types (static, streams and callback based), but the static case could be optimized to avoid the cache by incorporating the functionality of ReadAudioBufferFramesInInternalFormat() into ReadAudioBufferFramesInMixingFormat(). It would be unpractical to avoid the cache with streams and callback-based AudioBuffers however so this commit sticks with a generic solution.
ma_data_converter_get_required_input_frame_count()
|
@mackron Thanks for the update! I added some questions, not sure if I'm understanding the need for the data cache... As per my understanding it stores the stream data converted, ready to be played, on a user side buffer. Was it previously done inside I also understand this improvement is mostly for streamed data and static data won't need to initialize that cache array, could it be just skipped if |
|
The cache is not for performance, it's to avoid glitching. So when you are resampling, you consume input frames and from that generate output frames. The complication is that the number of output frames you generate is different from the number of input frames you'll consume. Say, for example, you need to output 2 output frames, you will not be consuming exactly 2 input frames. Imagine you are going from 48000 to 24000. That's a ratio of 2:1. If you output 2 frames, you'll be consuming 4 input frames. The way audio works in raylib is the audio device requests a number of frames from a callback, and raylib's job is to fill the output buffer with audio data. If the audio device asks for 512 frames, which is the output frames, how does raylib know how many input frames will be required to fill that? It doesn't, so it continuously reads from the Previously, the function I'll change the I had anticipated you might have some reservations about the overhead of a cache which is why I mentioned the |
|
@mackron thank you very much for the detailed explanation, I understand now. I'll follow the progress. |
This affects pitch shifting. The output rate is being modified with ma_data_converter_set_rate(), but then that value is being used in the computation of the output rate the next time SetAudioBufferPitch() which results in a cascade. The correct way to do this is to use an anchored output rate as the basis for the calculation after pitch shifting. In this case, it's the device's sample rate that acts as the anchor.
This reduces the per-AudioBuffer conversion cache from 256 PCM frames down to 8.
|
I've gone ahead and optimized the code to use a different approach and have got the cache from 256 down to 8 frames. For a stereo sound that equates to 64 bytes. I have not yet done the I did some renaming of some variables to hopefully make it a bit clearer as to the purpose of the buffer. I renamed it from "cache" to "residual" which I think should be a bit clearer? Either way, happy to make any style changes you don't like. |
It's ok, it's small enough and I beter to avoid the code complexity of two different code paths. I also think the renaming is good. Thank you very much for all the improvements! Merging! |
|
@mackron @raysan5 It may be a coincidence but I started to hear audio artifacts after updating RayLib from master today. I believe this was the only PR that touched audio, but I may be wrong. The artifacts sound like 1-2 short clicks after the sound ends with delay approximately 0.1-0.3 seconds. The OS is Windows 7. The used method is PlaySound. It happens for many sounds but I attached an example in case you need to check wav properties. |
|
I hear it. Investigating. |
|
This should be fixed here: #5578 Would you be able to try that branch? Thanks for reporting this. |
|
@mackron I cannot test the branch, but incorporating changes into my branch fixes the issue. Thank you. btw, it's not related to this issues, but maybe you'll be interested in working on this in the future. This PR #3743 fixed the crash at the moment of changing default audio device, but the sound isn't restored. Switching back to the first audio device doesn't return sound either. Also if default audio device was changed, CloseAudioDevice() hangs inside ma_device_uninit(&AUDIO.System.device); It's not a big issue though because CloseAudioDevice() is only needed on exit, and if it's not called, app termination succeeds (doesn't hang or anything) even if default audio device was changed. I can easily imagine that this is specific to my outdated setup (Win 7) so I didn't create a ticket. But since you yourself is here, I'm reporting it just in case you are interested. |
|
@casperbear I think that might be addressed in the next version of miniaudio (0.12 - current version is 0.11.x). I have changed the way WASAPI works, and that COM error should not be relevant anymore. In any case, the code has changed so much now that I don't particularly have any interest in fixing that in the 0.11.x cycle. If it still happens when miniaudio 0.12 is released (any raylib is updated) I'd be very interested to hear about that. |
commit 45d451b Merge: 19f499f ad82393 Author: Lisha from Black-Windows10-HP <lishastudies.cs@gmail.com> Date: Wed Feb 25 23:12:18 2026 +0530 Updated to upstream commit ad82393 commit ad82393 Author: Borin Ouch <borin.ouch+github@gmail.com> Date: Wed Feb 25 03:35:34 2026 -0800 Fix reversed window blur/focus logic for web (raysan5#5590) commit 4ebe7d6 Author: Krzysztof Szenk <crisserpl2@gmail.com> Date: Tue Feb 24 12:56:16 2026 +0100 win32 clipboard: fix for BI_ALPHABITFIELDS narrow support (raysan5#5586) * win32 clipbaord: fix for BI_ALPHABITFIELDS narrow support * Define BI_ALPHABITFIELDS even if wingdi headers are already included since BI_ALPHABITFIELDS is not always defined there commit 29b5844 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 12:48:54 2026 +0100 REVIEWED: Comments and header commit aec6e85 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 12:40:17 2026 +0100 Update raygui.h commit 25e5215 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 12:33:00 2026 +0100 Update models_animation_blending.c commit d504e40 Merge: 406861a ace4d77 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 12:32:50 2026 +0100 Merge branch 'master' of https://github.com/raysan5/raylib commit 406861a Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 12:32:48 2026 +0100 Update raudio.c commit ace4d77 Author: BadRAM <dgamebillda@gmail.com> Date: Tue Feb 24 02:26:30 2026 -0800 New example: textures_magnifying_glass (raysan5#5587) commit 7f0cedb Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 01:31:40 2026 +0100 Update shaders_shadowmap_rendering.c commit 592cac5 Merge: ade8124 149062f Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 01:26:16 2026 +0100 Merge branch 'master' of https://github.com/raysan5/raylib commit ade8124 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 01:26:07 2026 +0100 REVIEWED: Right timing commit 149062f Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue Feb 24 00:19:18 2026 +0000 rlparser: update raylib_api.* by CI commit d4dc038 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 01:18:57 2026 +0100 WARNING: BREAKING: REDESIGNED: **Animation System** raysan5#4606 REVIEWED: Reorganized structures for a clearer distinction between "skeleton", "skin" and "skinning" data ADDED: New structures: `ModelSkeleton`, `ModelAnimPose` (alias `Transform*`) ADDED: Runtime data `currentPose` and `boneMatrices` to `Model` structure ADDED: Support animation frames-blending, for timing control ADDED: Support animations blending, between two animations REVIEWED: All models animation loading functions ADDED: `UpdateModelAnimationEx()` for two animations blending REMOVED: `UpdateModelAnimationBones*()`, simplified API REVIEWED: Shader attributes/uniforms names for animations, for consistency REVIEWED: Multiple tweaks on animations loading for consistency between formats ADDED: example: `models_animation_timing` ADDED: example: `models_animation_blending` REVIEWED: example: `models_animation_gpu_skinning` REVIEWED: example: `models_animation_blend_custom` REVIEWED: All animated models loading examples commit bee3dc6 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 00:51:39 2026 +0100 Update models_animation_blending.vcxproj commit 770677d Merge: 3f36c2d 73cb1d5 Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 00:51:22 2026 +0100 Merge branch 'master' of https://github.com/raysan5/raylib commit 3f36c2d Author: Ray <raysan5@gmail.com> Date: Tue Feb 24 00:51:20 2026 +0100 Update raygui.h commit 73cb1d5 Author: FinnDemonCat <94855390+FinnDemonCat@users.noreply.github.com> Date: Mon Feb 23 17:08:46 2026 -0300 Add Dart binding to BINDINGS.md (raysan5#5585) commit e4cbf6b Author: Thomas Anderson <5776225+CrackedPixel@users.noreply.github.com> Date: Mon Feb 23 14:07:42 2026 -0600 make glfw consistent (raysan5#5583) commit 43d8933 Author: Ray <raysan5@gmail.com> Date: Mon Feb 23 01:45:23 2026 +0100 Renamed skinning shader variables (new default naming) commit ae6d34a Author: Ray <raysan5@gmail.com> Date: Mon Feb 23 01:43:53 2026 +0100 Renamed some models examples for consistency commit 542333b Author: Ray <raysan5@gmail.com> Date: Mon Feb 23 01:41:16 2026 +0100 Update README.md commit cd17ed1 Author: Ray <raysan5@gmail.com> Date: Sun Feb 22 23:09:23 2026 +0100 Update models_animation_blending.c commit 7ba604e Author: Ray <raysan5@gmail.com> Date: Sun Feb 22 23:08:41 2026 +0100 REXM: RENAME: `models_animation_playing` --> `models_loading_iqm` commit fbd83ca Author: Thomas Anderson <5776225+CrackedPixel@users.noreply.github.com> Date: Sun Feb 22 15:21:09 2026 -0600 [Backend/RGFW] Update RGFW to 2.0.0 (raysan5#5582) * initial update * minor changes * added minigamepad * updates * updates - 2.0-dev * update char press * update gamepad * update gamepad * update (web) * update rename, spacing, etc * updates * updates for mac-arm64 * moved RGFW into folder * highdpi fixes * update windowPos (bug remaining somewhere) * temp stash, macos fixes for pixelratio * highdpi resize window fixes * remove unneeded makefiles * fix undef oopsie * update fullscreen/borderless * update macos defines for older macs and newer ones * update resizing window commit 8b181b1 Author: Krzysztof Szenk <crisserpl2@gmail.com> Date: Sun Feb 22 21:36:37 2026 +0100 Fix for PR raysan5#5579 (WebRGFW platform exposed to cmake) (raysan5#5581) * Expose PLATFORM_WEB_RGFW to cmake * Fix for WebRGFW platform exposed to cmake commit d2c4aa1 Author: Thomas Anderson <5776225+CrackedPixel@users.noreply.github.com> Date: Sun Feb 22 14:35:54 2026 -0600 update examples cmakelists (raysan5#5580) commit 7a42778 Author: Krzysztof Szenk <crisserpl2@gmail.com> Date: Sun Feb 22 10:40:53 2026 +0100 Expose PLATFORM_WEB_RGFW to cmake (raysan5#5579) commit 11e3e6e Author: Ray <raysan5@gmail.com> Date: Sat Feb 21 09:09:43 2026 +0100 REVIEWED: Formating, tested sound examples commit 0c91f23 Author: David Reid <mackron@gmail.com> Date: Sat Feb 21 17:04:32 2026 +1000 Audio: Fix a glitch at the end of a sound. (raysan5#5578) This is happening because the processing function keeps reading audio data from the AudioBuffer even after it has been marked as stopped. There is also an error in ReadAudioBufferFramesInInternalFormat() where if it is called on a stopped sound, it'll still return audio frames. This has also been addressed with this commit. commit 005ff74 Author: David Reid <mackron@gmail.com> Date: Sat Feb 21 17:02:40 2026 +1000 Audio: Improvements to device configuration (raysan5#5577) * Audio: Stop setting capture config options. Since the device is being configured as a playback device, all capture config options are unused and therefore need to not be set. * Audio: Stop pre-silencing the miniaudio output buffer. raylib already manually silences the output buffer prior to mixing so there is no reason to have miniaudio also do it. It can therefore be disabled via the device config to make data processing slightly more efficient. * Audio: Stop forcing fixed sized processing callbacks. There is no requirement for raylib to have guaranteed fixed sized audio processing. By disabling it, audio processing can be made more efficient by not having to run the data through an internal intermediary buffer. * Audio: Make the period size (latency) configurable. The default period size is 10ms, but this is inappropriate for certain platforms so it is useful to be able to allow those platforms to configure the period size as required. * Audio: Fix documentation for pan. The pan if -1..1, not 0..1. commit c519e9f Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 23:56:11 2026 +0100 REVIEWED: Simplified `char **` approach commit 09f22f3 Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 23:02:43 2026 +0100 REVIEWED: Avoid `const char **` usage (aligned with raylib) commit 29b9c05 Author: Thomas Anderson <5776225+CrackedPixel@users.noreply.github.com> Date: Fri Feb 20 13:18:28 2026 -0600 fix example (raysan5#5575) commit 0343cb6 Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 18:47:53 2026 +0100 Update rcore_desktop_sdl.c commit d148d95 Author: TheKodeToad <TheKodeToad@proton.me> Date: Fri Feb 20 17:44:34 2026 +0000 Fix text input on SDL3 (raysan5#5574) commit 19e6352 Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 18:16:49 2026 +0100 Update shapes_easings_testbed.c commit d03a59c Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 16:36:13 2026 +0100 Update core_directory_files.c commit 2454b3e Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 16:27:08 2026 +0100 REVIEWED: `TextReplace()` and `TextLength()`, avoid using `strcpy()` commit d996bf2 Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 16:06:59 2026 +0100 Update textures_screen_buffer.c commit f33823c Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 15:55:38 2026 +0100 Update textures_screen_buffer.c commit 0aacd33 Author: David Reid <mackron@gmail.com> Date: Fri Feb 20 22:46:41 2026 +1000 [raudio] Remove usage of `ma_data_converter_get_required_input_frame_count()` (raysan5#5568) * Audio: Remove use of ma_data_converter_get_required_input_frame_count(). This function is being removed from miniaudio. To make this work with the current architecture of raylib it requires the use of a cache. This commit implements a generic solution that works across all AudioBuffer types (static, streams and callback based), but the static case could be optimized to avoid the cache by incorporating the functionality of ReadAudioBufferFramesInInternalFormat() into ReadAudioBufferFramesInMixingFormat(). It would be unpractical to avoid the cache with streams and callback-based AudioBuffers however so this commit sticks with a generic solution. * Audio: Correct usage of miniaudio's dynamic rate adjustment. This affects pitch shifting. The output rate is being modified with ma_data_converter_set_rate(), but then that value is being used in the computation of the output rate the next time SetAudioBufferPitch() which results in a cascade. The correct way to do this is to use an anchored output rate as the basis for the calculation after pitch shifting. In this case, it's the device's sample rate that acts as the anchor. * Audio: Optimize memory usage for data conversion. This reduces the per-AudioBuffer conversion cache from 256 PCM frames down to 8. commit ce617cd Author: Ray <raysan5@gmail.com> Date: Fri Feb 20 11:46:46 2026 +0100 Update rlgl.h commit 4a3c49c Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:55:13 2026 +0100 REVIEWED: `rlLoadTextureDepth()`, address inconsistencies with WebGL 2.0 for sized depth formats raysan5#5500 commit 90dd9ae Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:40:22 2026 +0100 REVIEWED: `GenImageFontAtlas()`, no need for the conservative approach flag commit 4a89da3 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:22:45 2026 +0100 Update VS2022 examples solution commit 1a5e228 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:19:44 2026 +0100 REXM: Update examples collection commit 1f4e1bc Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:18:50 2026 +0100 REXM: Update examples collection commit dea67fa Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:15:26 2026 +0100 REXM: Update examples collection commit b9f16a2 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:13:20 2026 +0100 REXM: Update examples collection commit 0a7c756 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:11:32 2026 +0100 Update examples_list.txt commit 781c379 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:11:02 2026 +0100 Updated examples, removed others category processing commit d40ad48 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 17:01:27 2026 +0100 REVIEWED: examples: moved some examples out of others commit 98c7734 Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 16:46:48 2026 +0100 REVIEWED: `models_basic_voxel` example commit 71607db Author: Ray <raysan5@gmail.com> Date: Thu Feb 19 16:46:30 2026 +0100 Moved easings example to shapes commit 0e6cb09 Author: Kirandeep-Singh-Khehra <107160937+Kirandeep-Singh-Khehra@users.noreply.github.com> Date: Thu Feb 19 17:27:05 2026 +0530 [rmodels] Added implementation of `UpdateModelAnimationBonesWithBlending()` function (raysan5#4578) * [rmodels] Added implementation of `UpdateModelAnimationBonesWithBlending()` function Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Added example for animation blending and fixed wrap issue for blend factor Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Updated build information for animation blending example Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Fixed typos in anmation blending example Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Updated blend function signature and added function to update verts from bones Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Updated documentation Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * rlparser: update raylib_api.* by CI * rlparser: update raylib_api.* by CI --------- Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> Co-authored-by: Ray <raysan5@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
The function
ma_data_converter_get_required_input_frame_count()is being removed from miniaudio. This PR is prep work to ease the transition for when the time comes to update to version miniaudio v0.12.To make this work with the current architecture of raylib it requires the use of a cache. It should technically be possible to avoid the cache for a static AudioBuffer, but it would be unpractical to avoid it with streams and callback-based AudioBuffers so I've decided to keep it simple and use a generic solution. This might be a worthwhile optimization to keep in mind for later.
The size of the cache can be configured with a macro called
AUDIO_BUFFER_CONVERSION_CACHE_SIZE. Higher values use more memory, but have less overhead. There's diminishing returns the higher you go.I've tried to keep the coding style consistent. Happy to fix anything I've let slip.
I've run all audio_* examples on Linux. Could be worth someone having a second look at it however. In testing I did actually find a few issues with some examples, but these were also present prior to this PRs changes and are unrelated. I'll leave those issues for a separate discussion.