Repository navigation
- 8000
|
Hi, I'm implementing dynamic sample rate adjustment in miniaudio and would appreciate feedback on my approach. I've written a function that reinitializes the device with a new sample rate: struct ma_context_collection {
ma_context context;
ma_device device;
ma_engine engine;
ma_resource_manager resourceManager;
ma_uint32 sampleRate;
ma_uint8 channels;
ma_format format;
};
MA_API ma_result ma_reinit_device(ma_context_collection* collection, ma_uint32 sampleRate)
{
if (collection == NULL)
return MA_INVALID_ARGS;
if (collection->device.sampleRate == sampleRate)
return MA_SUCCESS;
collection->sampleRate = sampleRate;
collection->engine.sampleRate = sampleRate;
ma_device_id deviceId;
memset(&deviceId, 0, sizeof(deviceId));
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.format = collection->device.playback.format;
deviceConfig.playback.channels = collection->device.playback.channels;
deviceConfig.sampleRate = sampleRate;
deviceConfig.dataCallback = collection->device.onData;
memcpy(&deviceId, &collection->device.playback.id, sizeof(collection->device.playback.id));
deviceConfig.playback.pDeviceID = &deviceId;
ma_result ret = ma_device_stop(&collection->device);
if (ret != MA_SUCCESS) {
return ret;
}
ma_engine_uninit(&collection->engine);
ma_device_uninit(&collection->device);
memset(&collection->device, 0, sizeof(collection->device));
ret = ma_device_init(&collection->context, &deviceConfig, &collection->device);
if (ret != MA_SUCCESS) {
return ret;
}
ma_engine_config engineConfig = ma_engine_config_init();
engineConfig.listenerCount = MA_ENGINE_MAX_LISTENERS;
engineConfig.pDevice = &collection->device;
engineConfig.pResourceManager = &collection->resourceManager;
ret = ma_engine_init(&engineConfig, &collection->engine);
if (ret != MA_SUCCESS) {
ma_device_uninit(&collection->device);
return ret;
}
collection->device.pUserData = &collection->engine;
ret = ma_device_start(&collection->device);
if (ret != MA_SUCCESS) {
ma_device_uninit(&collection->device);
return ret;
}
return MA_SUCCESS;
}I've noticed that after changing the sample rate, I need to reinitialize existing ma_sound instances. Here's how I'm handling that: ma_context_collection collection;
// ...
ma_sound sound;
ma_sound_init_from_file(&collection.engine, path, MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, NULL, NULL, &sound);
ma_uint32 audioSampleRate = 0;
ma_sound_get_data_format(&sound, NULL, NULL, &audioSampleRate, NULL, 0);
if (collection.sampleRate != audioSampleRate)
{
ma_result ret = ma_reinit_device(&collection, audioSampleRate);
if (ret == MA_SUCCESS)
{
ma_sound_uninit(&sound);
ma_sound_init_from_file(&collection.engine, path, MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, NULL, NULL, &sound);
}
}My questions:
My tests show this works locally, but I want to ensure it's a robust solution before using it in production. |
Answered by
mackron
Apr 1, 2026
Replies: 1 comment 5 replies
What is your use case for wanting to dynamically change your sample rate? Is it just a configuration thing that you user changes through the UI? |
5 replies
Answer selected by
lfeng1420
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is your use case for wanting to dynamically change your sample rate? Is it just a configuration thing that you user changes through the UI?