How to make Miniaudio play multiple sounds from the same data source more than once? #491
-
I am using miniaudio High level API with resource management disabled: #define MA_NO_RESOURCE_MANAGER
#include <miniaudio/miniaudio.h> I have a custom resource registry that loads audio files and stores the ma_decoder *decoder = new ma_decoder;
auto res = ma_decoder_init_file(filePath.string().c_str(), nullptr, decoder);
if (res != MA_SUCCESS) {
return Result::Error(...);
}
mRegistry.getAudios().addAsset({ decoder }); In my audio engine, I create sounds from the same decoder and play them. Once the sounds are done playing, they get uninitialized and deleted. Here is how I create, play, and uninitialize sounds. void *playSound(void *dataSource) {
auto *sound = new ma_sound;
auto initRes =
ma_sound_init_from_data_source(&mEngine, dataSource, 0, nullptr, sound);
LIQUID_ASSERT(initRes == MA_SUCCESS, "Cannot init sound");
// play the sound
auto startRes = ma_sound_start(sound);
return sound;
}
void destroySound(void *sound) {
ma_sound_uninit(static_cast<ma_sound *>(sound));
delete sound;
} This particular setup works for a single sound. If I have two separate assets (aka two decoders), I am able to play sound from each decoder once. However, if I try to create multiple sounds using the same decoder as the data source, the sound only gets played once. While debugging this issue I noticed that, on second play, the sound was initialized successfully, // new data source -- sound plays
void *sound = playSound(decoderBark);
ma_sound_is_playing(sound); // true
// 5 seconds later
{
// new data source -- sound plays
void *sound = playSound(decoderMeow);
ma_sound_is_playing(sound); // true
}
{
// data source already used -- sound does not play
void *sound = playSound(decoderBark);
ma_sound_is_playing(sound); // false
}
// 5 seconds later
{
// data source already used -- sound does not play
void *sound = playSound(decoderMeow);
ma_sound_is_playing(sound); // false
}
{
// data source already used -- sound does not play
void *sound = playSound(decoderBark);
ma_sound_is_playing(sound); // false
}
// ... none of the already loaded sounds ever play |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
That's not how miniaudio works. You create a separate data source for each sound you want to play. The resource manager is used for managing caching and loading sound data only once. |
Beta Was this translation helpful? Give feedback.
That's not how miniaudio works. You create a separate data source for each sound you want to play. The resource manager is used for managing caching and loading sound data only once.