How does high-level reference-counting, sound channel spawning, and loading from memory work exactly? #821
-
Hi, I'm trying to use high-level interface, and really confused about some things.
If I understand correctly, when you load a sound with e.g., ma_sound_init_from_file_w, loading another instance of the same file shouldn't load it again as long as at least one instance of the sound is present, effectively making what some libraries would call a sound channel/voice. If that were the case, I'd expect the load time to be greatly decreased to 1 or 2-digit ms numbers, but although the second instance loads about twice as fast, it still doesn't seem to exhibit sound channel spawning-like behavior.
Looking at some other posts on here, I ran into a reply that helped me find a way to do this with ma_resource_manager_register_encoded_data_w and using the file label/ID with ma_sound_init_from_file_w, however the memory buffer/data doesn't seem to be copied, making it necessary for me to keep it allocated and manually free it (not a huge issue, but if there is a way to let Miniaudio handle that for me, it would be nice). In all of the cases with ma_sound, I use malloc to allocate it before using. E.g., I'd really appreciate some clarification/help as I'm quite lost/stuck at the moment. |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 1 reply
-
Your understanding of ma_sound and it's instancing is correct. Loading a second instance should be fast so it's a bit strange that would be slow for you. What flags are you passing into
The buffers you pass into the registration functions in the resource manager are indeed self-managed - miniaudio will not take a copy of that data for you, and there are no functions to do so. Just some general comments, unrelated to your performance issue. miniaudio is different to other audio libraries like FMOD so you might need to alter your mental model a bit. You might be used to the concept of having a "sound", and then from the sound spawning an instance of that sound. In miniaudio it's a bit different. Instead you have a data source ( |
Beta Was this translation helpful? Give feedback.
-
@mackron Thank you so much for the response and clarifications.
Timing load/play for each sound usually shows that the second instance of the sound loads faster (if I run the compiled executable first time after a while, for a 60 mb ogg file (huge for non-streaming, but just for testing purposes), it first takes ~210 ms for the first instance, then ~110 for the other one. Running the second time seems to bring it down to first=~110, second=~90, but that's as good as it seems to get).
|
Beta Was this translation helpful? Give feedback.
-
If you load, say, 50 of your sounds in a loop, does your memory usage explode? That would be an obvious indication that the reference counting system is being bypassed for some reason. There's also the trip hazard in section 6.2.2 of the documentation (scroll down a bit), though looking at your code you shouldn't be hitting that at all. I'll have to run some tests on my end and try to replicate it. What are you using for your Vorbis decoding by the way, just so I can replicate your set up? |
Beta Was this translation helpful? Give feedback.
-
@mackron I appreciate your help very much. |
Beta Was this translation helpful? Give feedback.
-
OK, that is a big clue and although I've not verified it, I think I know what's going on. Some background. When not streaming ( As an experiment, is it possible to modify miniaudio_libvorbis.h and wrap your timer around the Regarding timing of your |
Beta Was this translation helpful? Give feedback.
-
@mackron Thank you for the insight/suggestions. |
Beta Was this translation helpful? Give feedback.
-
I was just going through my outstanding tickets and just realised something - do you think this might be causing the slowness? #765 As an experiment, what happens if you use |
Beta Was this translation helpful? Give feedback.
-
@mackron That's an excellent find! |
Beta Was this translation helpful? Give feedback.
-
That sounds good! |
Beta Was this translation helpful? Give feedback.
-
I've implemented a solution for this. It's in the dev-0.12 which means it won't be released for a while. See my comment here for more details: #765 (comment) |
Beta Was this translation helpful? Give feedback.
-
@mackron Thank you so much! |
Beta Was this translation helpful? Give feedback.
I was just going through my outstanding tickets and just realised something - do you think this might be causing the slowness? #765
As an experiment, what happens if you use
MA_NO_MP3
?