One Big Beautiful Commit with refactoring to the device backend system.

This includes API changes that affect custom backends.

`ma_backend_callbacks` has been renamed to `ma_device_backend_vtable`.
The reason for this change is to to be consistent with the naming
convention used in other parts of the library. In addition, using the
term "device backend" rather than just "backend" removes ambiguity with
decoding backends.

A change has been made to the way backends manage their internal state,
and some functions in the vtable have been updated to reflect this.
Previously internal state for stock backends were located directly in
the `ma_device` structure. This works fine if stock backends are the
only backends to be concerned about, but it falls apart when you need
to consider how to manage the internal state of custom backends since
they cannot modify the `ma_device` structure. In order to simplify and
unify state management between stock and custom backends, the decision
was made to change the backend system such that backends now manage
their own internal state.

When the context is initialized with `onContextInit`, the backend must
now allocate an internal state object and output a pointer to it via
an output parameter. Typically you would do something like this:

    ma_result custom_context_init(..., void** ppContextState)
    {
        ctx_state_t* state = malloc(...);

        ...

        *ppContextState = state;
        return MA_SUCCESS;
    }

miniaudio will store a pointer to the state object internally. When you
need to access this later in other backend callbacks, you can retrieve
it straight from the context with `ma_context_get_backend_state()`:

    state = (ctx_state_t*)ma_context_get_backend_state(pContext);

The same idea applies to devices and `onDeviceInit`. You can use
`ma_device_get_backend_state()` to get a pointer to the internal state
object.

When a context and device is initialized, backend-specific
configurations can be supplied. The way these configs are provided to
`onContextInit` and `onDeviceInit` has been changed. Previously, these
callbacks would take a `ma_context/device_config` object. These have
been replaced with a `const void*` which points to a backend-specific
config object which is defined by the backend. All stock backends have
their own backend-specific config object:

    struct ma_context_config_wasapi
    struct ma_context_config_pulseaudio
    etc.

    struct ma_device_config_wasapi
    struct ma_device_config_pulseaudio
    etc.

You can cast the config object inside the relevant callbacks:

    ma_result custom_context_init(..., const void* pBackendConfig, ...)
    {
        ctx_config_t* pCustomConfig = (ctx_config_t*)pBackendConfig;
    }

The backend itself defines whether or not a config is required. None of
the stock backends require a config. If the config is NULL, it'll use
defaults. It's recommended custom backends follow this convention.

In addition to the above, `onContextUninit` and `onDeviceUninit` have
been updated to return void instead of `ma_result`.

The last change to the backend vtable is a new callback called
`onBackendInfo`. This is used to fill the `ma_device_backend_info`
structure.

In addition to the backend vtable, some changes have been made to the
public API to make it much easier to support plugging in custom
backends.

Previously, plugging in more than one custom backend was a complete
mess. It was possible, but you had to use a stupid wrapper thing to
make it work, and you had no control over prioritization. The entire
thing was just aweful, so it's now been stripped out and replaced with
a brand new system.

When a context or device is initialized, it is done so with a config
which is standard across the entire library. A complication to this is
that backends can sometimes require their own backend-specific configs.
But since miniaudio cannot possibly know about custom backends, it
cannot put their config options inside `ma_context/device_config`. The
functions for initializing a context and device have been updated to
allow plugging in backend-specific configs.

When initializing a context, instead of passing in an array of
`ma_backend` enums, an array of `ma_device_backend_config` objects is
passed in instead. This object has two members: A pointer to a backend
vtable, and a pointer to a config object. It can be initialized
something like this:

    ma_context_config_custom customContextConfig;
    ... initialize the custom backend config if necessary ...

    ma_device_backend_config backends[] =
    {
        { ma_device_backend_custom,     &customContextConfig },
        { ma_device_backend_wasapi,     NULL },
        { ma_device_backend_pulseaudio, NULL }
    };

    ma_context_init(backends, backendCount, ...);

Here `ma_device_backend_custom` is our custom backend. You can see how
the config is mapped to the backend. For stock backends (WASAPI and
PulseAudio in this example), you can pass in NULL and just set the
relevant config options straight in `ma_context_config` exactly how it
was done before:

    ma_context_config contextConfig = ma_context_config_init();
    contextConfig.pulseaudio.pApplicationName = "My App";

Here we are just using the standard `ma_context_config` object for
configuring the stock PulseAudio backend. This is possible for all
stock backends, but for custom backends an explicit config object will
be required. You can still use a separate explicit config object for
stock backends if you prefer that style:

    ma_context_config_pulseaudio paContextConfig;
    paContextConfig = ma_context_config_pulseaudio_init();
    paContextConfig.pApplicationName = "My App";

    ma_device_backend_config backends[] =
    {
        { ma_device_backend_pulseaudio, &paContextConfig }
    };

Note that if you do not use custom backends, you can still pass in NULL
for the backends in which case defaults will be used like how it's
always worked in the past.

As with contexts, devices can also have their own backend-specific
configs associated with them. These work exactly the same way, except
these configs are passed into the main `ma_device_config` object. (A
future commit may make this consistent between contexts and devices).

    ma_device_backend_config deviceBackendConfigs[] =
    {
        { ma_device_backend_custom, &customDeviceConfig }
    };

    deviceConfig.pBackendConfigs    = deviceBackendConfigs;
    deviceConfig.backendConfigCount = backendCount;

    ma_device_init(&deviceConfig, &device);

This commit is just the start of many backend related changes. Future
commits will be cleaning up a lot of residual code from the old system,
such as removing `ma_backend`.
This commit is contained in:
David Reid
2025-07-14 18:05:55 +10:00
parent cdfd219377
commit 8890eac6aa
7 changed files with 7442 additions and 6038 deletions
+11 -26
View File
@@ -45,7 +45,6 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
int main(int argc, char** argv)
{
ma_result result;
ma_context_config contextConfig;
ma_context context;
ma_device_config deviceConfig;
ma_device device;
@@ -53,15 +52,6 @@ int main(int argc, char** argv)
ma_waveform sineWave;
char name[256];
/*
We're just using ma_backend_custom in this example for demonstration purposes, but a more realistic use case would probably want to include
other backends as well for robustness.
*/
ma_backend backends[] = {
ma_backend_custom
};
/*
Here is where we would set up the SDL-specific context-level config. The custom SDL backend allows this to be null, but we're
defining it here just for the sake of demonstration. Whether or not this is required depends on the backend. If you're not sure,
@@ -73,26 +63,18 @@ int main(int argc, char** argv)
/*
You must include an entry for each backend you're using, even if the config is NULL. This is how miniaudio knows about
your custom backend.
*/
ma_device_backend_spec pCustomContextConfigs[] = {
{ MA_DEVICE_BACKEND_VTABLE_SDL, &sdlContextConfig, NULL }
};
#if 0
For stock backends, you can just leave the config pointer as NULL and fill out any backend-specific config options in
the ma_context_config structure. Same with device configs.
*/
ma_device_backend_config backends[] =
{
{ ma_device_backend_sdl, &sdlContextConfig },
{ ma_device_backend_wasapi, NULL },
{ ma_device_backend_pulseaudio, NULL }
};
#endif
contextConfig = ma_context_config_init();
contextConfig.custom.pBackends = pCustomContextConfigs;
contextConfig.custom.count = (sizeof(pCustomContextConfigs) / sizeof(pCustomContextConfigs[0]));
result = ma_context_init(backends, sizeof(backends)/sizeof(backends[0]), &contextConfig, &context);
result = ma_context_init(backends, sizeof(backends)/sizeof(backends[0]), NULL, &context);
if (result != MA_SUCCESS) {
return -1;
}
@@ -113,8 +95,11 @@ int main(int argc, char** argv)
/*
Unlike with contexts, if your backend does not require a device-level config, you can just leave it out of this list entirely.
*/
ma_device_backend_spec pCustomDeviceConfigs[] = {
{ MA_DEVICE_BACKEND_VTABLE_SDL, &sdlDeviceConfig, NULL }
ma_device_backend_config pBackendDeviceConfigs[] =
{
{ ma_device_backend_sdl, &sdlDeviceConfig },
{ ma_device_backend_wasapi, NULL },
{ ma_device_backend_pulseaudio, NULL }
};
deviceConfig = ma_device_config_init(ma_device_type_playback);
@@ -125,8 +110,8 @@ int main(int argc, char** argv)
deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &sineWave;
deviceConfig.custom.pBackends = pCustomDeviceConfigs;
deviceConfig.custom.count = sizeof(pCustomDeviceConfigs) / sizeof(pCustomDeviceConfigs[0]);
deviceConfig.pBackendConfigs = pBackendDeviceConfigs;
deviceConfig.backendConfigCount = sizeof(pBackendDeviceConfigs) / sizeof(pBackendDeviceConfigs[0]);
result = ma_device_init(&context, &deviceConfig, &device);
if (result != MA_SUCCESS) {
+2 -2
View File
@@ -31,8 +31,8 @@ int main(int argc, char** argv)
ma_device device;
/* Loopback mode is currently only supported on WASAPI. */
ma_backend backends[] = {
ma_backend_wasapi
ma_device_backend_config backends[] = {
{ ma_device_backend_wasapi, NULL }
};
if (argc < 2) {
+232 -235
View File
@@ -15,6 +15,11 @@ which requires the `-s USE_SDL=2` option.
#include "backend_sdl.h"
#include <string.h> /* memset() */
#include <assert.h>
#ifndef MA_SDL_ASSERT
#define MA_SDL_ASSERT(cond) assert(cond)
#endif
/* Support SDL on everything. */
#define MA_SUPPORT_SDL
@@ -56,24 +61,6 @@ MA_ENABLE_ONLY_SPECIFIC_BACKENDS) and it's supported at compile time (MA_SUPPORT
#define MA_SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
#define MA_SDL_AUDIO_ALLOW_ANY_CHANGE (MA_SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | MA_SDL_AUDIO_ALLOW_FORMAT_CHANGE | MA_SDL_AUDIO_ALLOW_CHANNELS_CHANGE)
typedef struct
{
ma_handle hSDL; /* A handle to the SDL2 shared object. We dynamically load function pointers at runtime so we can avoid linking. */
ma_proc SDL_InitSubSystem;
ma_proc SDL_QuitSubSystem;
ma_proc SDL_GetNumAudioDevices;
ma_proc SDL_GetAudioDeviceName;
ma_proc SDL_CloseAudioDevice;
ma_proc SDL_OpenAudioDevice;
ma_proc SDL_PauseAudioDevice;
} ma_context_data_sdl;
typedef struct
{
int deviceIDPlayback;
int deviceIDCapture;
} ma_device_data_sdl;
/* If we are linking at compile time we'll just #include SDL.h. Otherwise we can just redeclare some stuff to avoid the need for development packages to be installed. */
#ifdef MA_NO_RUNTIME_LINKING
#define SDL_MAIN_HANDLED
@@ -114,6 +101,35 @@ typedef void (* MA_PFN_SDL_CloseAudioDevice)(MA_SDL_AudioDeviceI
typedef MA_SDL_AudioDeviceID (* MA_PFN_SDL_OpenAudioDevice)(const char* device, int iscapture, const MA_SDL_AudioSpec* desired, MA_SDL_AudioSpec* obtained, int allowed_changes);
typedef void (* MA_PFN_SDL_PauseAudioDevice)(MA_SDL_AudioDeviceID dev, int pause_on);
typedef struct
{
ma_handle hSDL; /* A handle to the SDL2 shared object. We dynamically load function pointers at runtime so we can avoid linking. */
MA_PFN_SDL_InitSubSystem SDL_InitSubSystem;
MA_PFN_SDL_QuitSubSystem SDL_QuitSubSystem;
MA_PFN_SDL_GetNumAudioDevices SDL_GetNumAudioDevices;
MA_PFN_SDL_GetAudioDeviceName SDL_GetAudioDeviceName;
MA_PFN_SDL_CloseAudioDevice SDL_CloseAudioDevice;
MA_PFN_SDL_OpenAudioDevice SDL_OpenAudioDevice;
MA_PFN_SDL_PauseAudioDevice SDL_PauseAudioDevice;
} ma_context_state_sdl;
typedef struct
{
struct
{
int deviceID;
ma_format format;
ma_uint32 channels;
} capture;
struct
{
int deviceID;
ma_format format;
ma_uint32 channels;
} playback;
} ma_device_state_sdl;
MA_SDL_AudioFormat ma_format_to_sdl(ma_format format)
{
switch (format)
@@ -140,26 +156,134 @@ ma_format ma_format_from_sdl(MA_SDL_AudioFormat format)
}
}
static ma_result ma_context_enumerate_devices__sdl(void* pUserData, ma_context* pContext, ma_enum_devices_callback_proc callback, void* pCallbackUserData)
static ma_context_state_sdl* ma_context_get_backend_state__sdl(ma_context* pContext)
{
ma_context_data_sdl* pContextDataSDL;
return (ma_context_state_sdl*)ma_context_get_backend_state(pContext);
}
static ma_device_state_sdl* ma_device_get_backend_state__sdl(ma_device* pDevice)
{
return (ma_device_state_sdl*)ma_device_get_backend_state(pDevice);
}
static void ma_backend_info__sdl(ma_device_backend_info* pBackendInfo)
{
MA_SDL_ASSERT(pBackendInfo != NULL);
pBackendInfo->pName = "SDL2";
}
static ma_result ma_context_init__sdl(ma_context* pContext, const void* pContextBackendConfig, void** ppContextState)
{
ma_context_state_sdl* pContextStateSDL;
const ma_context_config_sdl* pContextConfigSDL = (ma_context_config_sdl*)pContextBackendConfig;
ma_log* pLog = ma_context_get_log(pContext);
int resultSDL;
/* The context config is not currently being used for this backend. */
(void)pContextConfigSDL;
/* Allocate our SDL-specific context data. */
pContextStateSDL = (ma_context_state_sdl*)ma_calloc(sizeof(*pContextStateSDL), ma_context_get_allocation_callbacks(pContext));
if (pContextStateSDL == NULL) {
return MA_OUT_OF_MEMORY;
}
#ifndef MA_NO_RUNTIME_LINKING
{
/* We'll use a list of possible shared object names for easier extensibility. */
size_t iName;
const char* pSDLNames[] = {
#if defined(_WIN32)
"SDL2.dll"
#elif defined(__APPLE__)
"SDL2.framework/SDL2"
#else
"libSDL2-2.0.so.0"
#endif
};
/* Check if we have SDL2 installed somewhere. If not it's not usable and we need to abort. */
for (iName = 0; iName < ma_countof(pSDLNames); iName += 1) {
pContextStateSDL->hSDL = ma_dlopen(pLog, pSDLNames[iName]);
if (pContextStateSDL->hSDL != NULL) {
break;
}
}
if (pContextStateSDL->hSDL == NULL) {
ma_free(pContextStateSDL, ma_context_get_allocation_callbacks(pContext));
return MA_NO_BACKEND; /* SDL2 could not be loaded. */
}
/* Now that we have the handle to the shared object we can go ahead and load some function pointers. */
pContextStateSDL->SDL_InitSubSystem = (MA_PFN_SDL_InitSubSystem )ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_InitSubSystem");
pContextStateSDL->SDL_QuitSubSystem = (MA_PFN_SDL_QuitSubSystem )ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_QuitSubSystem");
pContextStateSDL->SDL_GetNumAudioDevices = (MA_PFN_SDL_GetNumAudioDevices)ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_GetNumAudioDevices");
pContextStateSDL->SDL_GetAudioDeviceName = (MA_PFN_SDL_GetAudioDeviceName)ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_GetAudioDeviceName");
pContextStateSDL->SDL_CloseAudioDevice = (MA_PFN_SDL_CloseAudioDevice )ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_CloseAudioDevice");
pContextStateSDL->SDL_OpenAudioDevice = (MA_PFN_SDL_OpenAudioDevice )ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_OpenAudioDevice");
pContextStateSDL->SDL_PauseAudioDevice = (MA_PFN_SDL_PauseAudioDevice )ma_dlsym(pLog, pContextStateSDL->hSDL, "SDL_PauseAudioDevice");
}
#else
{
pContextStateSDL->SDL_InitSubSystem = SDL_InitSubSystem;
pContextStateSDL->SDL_QuitSubSystem = SDL_QuitSubSystem;
pContextStateSDL->SDL_GetNumAudioDevices = SDL_GetNumAudioDevices;
pContextStateSDL->SDL_GetAudioDeviceName = SDL_GetAudioDeviceName;
pContextStateSDL->SDL_CloseAudioDevice = SDL_CloseAudioDevice;
pContextStateSDL->SDL_OpenAudioDevice = SDL_OpenAudioDevice;
pContextStateSDL->SDL_PauseAudioDevice = SDL_PauseAudioDevice;
}
#endif /* MA_NO_RUNTIME_LINKING */
resultSDL = pContextStateSDL->SDL_InitSubSystem(MA_SDL_INIT_AUDIO);
if (resultSDL != 0) {
ma_dlclose(pLog, pContextStateSDL->hSDL);
ma_free(pContextStateSDL, ma_context_get_allocation_callbacks(pContext));
return MA_ERROR;
}
*ppContextState = pContextStateSDL;
return MA_SUCCESS;
}
static void ma_context_uninit__sdl(ma_context* pContext)
{
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(pContext);
MA_SDL_ASSERT(pContextStateSDL != NULL);
pContextStateSDL->SDL_QuitSubSystem(MA_SDL_INIT_AUDIO);
/* Close the handle to the SDL shared object last. */
ma_dlclose(ma_context_get_log(pContext), pContextStateSDL->hSDL);
pContextStateSDL->hSDL = NULL;
ma_free(pContextStateSDL, ma_context_get_allocation_callbacks(pContext));
}
static ma_result ma_context_enumerate_devices__sdl(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pCallbackUserData)
{
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(pContext);
ma_bool32 isTerminated = MA_FALSE;
ma_bool32 cbResult;
int iDevice;
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pContext->pBackendData;
MA_SDL_ASSERT(pContextStateSDL != NULL);
/* Playback */
if (!isTerminated) {
int deviceCount = ((MA_PFN_SDL_GetNumAudioDevices)pContextDataSDL->SDL_GetNumAudioDevices)(0);
int deviceCount = pContextStateSDL->SDL_GetNumAudioDevices(0);
for (iDevice = 0; iDevice < deviceCount; ++iDevice) {
ma_device_info deviceInfo;
memset(&deviceInfo, 0, sizeof(deviceInfo));
deviceInfo.id.custom.i = iDevice;
ma_strncpy_s(deviceInfo.name, sizeof(deviceInfo.name), ((MA_PFN_SDL_GetAudioDeviceName)pContextDataSDL->SDL_GetAudioDeviceName)(iDevice, 0), (size_t)-1);
ma_strncpy_s(deviceInfo.name, sizeof(deviceInfo.name), pContextStateSDL->SDL_GetAudioDeviceName(iDevice, 0), (size_t)-1);
if (iDevice == 0) {
deviceInfo.isDefault = MA_TRUE;
@@ -175,13 +299,13 @@ static ma_result ma_context_enumerate_devices__sdl(void* pUserData, ma_context*
/* Capture */
if (!isTerminated) {
int deviceCount = ((MA_PFN_SDL_GetNumAudioDevices)pContextDataSDL->SDL_GetNumAudioDevices)(1);
int deviceCount = pContextStateSDL->SDL_GetNumAudioDevices(1);
for (iDevice = 0; iDevice < deviceCount; ++iDevice) {
ma_device_info deviceInfo;
memset(&deviceInfo, 0, sizeof(deviceInfo));
deviceInfo.id.custom.i = iDevice;
ma_strncpy_s(deviceInfo.name, sizeof(deviceInfo.name), ((MA_PFN_SDL_GetAudioDeviceName)pContextDataSDL->SDL_GetAudioDeviceName)(iDevice, 1), (size_t)-1);
ma_strncpy_s(deviceInfo.name, sizeof(deviceInfo.name), pContextStateSDL->SDL_GetAudioDeviceName(iDevice, 1), (size_t)-1);
if (iDevice == 0) {
deviceInfo.isDefault = MA_TRUE;
@@ -198,9 +322,9 @@ static ma_result ma_context_enumerate_devices__sdl(void* pUserData, ma_context*
return MA_SUCCESS;
}
static ma_result ma_context_get_device_info__sdl(void* pUserData, ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, ma_device_info* pDeviceInfo)
static ma_result ma_context_get_device_info__sdl(ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, ma_device_info* pDeviceInfo)
{
ma_context_data_sdl* pContextDataSDL;
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(pContext);
#if !defined(__EMSCRIPTEN__)
MA_SDL_AudioSpec desiredSpec;
@@ -209,10 +333,6 @@ static ma_result ma_context_get_device_info__sdl(void* pUserData, ma_context* pC
const char* pDeviceName;
#endif
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pContext->pBackendData;
if (pDeviceID == NULL) {
if (deviceType == ma_device_type_playback) {
pDeviceInfo->id.custom.i = 0;
@@ -223,7 +343,7 @@ static ma_result ma_context_get_device_info__sdl(void* pUserData, ma_context* pC
}
} else {
pDeviceInfo->id.custom.i = pDeviceID->custom.i;
ma_strncpy_s(pDeviceInfo->name, sizeof(pDeviceInfo->name), ((MA_PFN_SDL_GetAudioDeviceName)pContextDataSDL->SDL_GetAudioDeviceName)(pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1), (size_t)-1);
ma_strncpy_s(pDeviceInfo->name, sizeof(pDeviceInfo->name), pContextStateSDL->SDL_GetAudioDeviceName(pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1), (size_t)-1);
}
if (pDeviceInfo->id.custom.i == 0) {
@@ -260,16 +380,16 @@ static ma_result ma_context_get_device_info__sdl(void* pUserData, ma_context* pC
pDeviceName = NULL;
if (pDeviceID != NULL) {
pDeviceName = ((MA_PFN_SDL_GetAudioDeviceName)pContextDataSDL->SDL_GetAudioDeviceName)(pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1);
pDeviceName = pContextStateSDL->SDL_GetAudioDeviceName(pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1);
}
tempDeviceID = ((MA_PFN_SDL_OpenAudioDevice)pContextDataSDL->SDL_OpenAudioDevice)(pDeviceName, (deviceType == ma_device_type_playback) ? 0 : 1, &desiredSpec, &obtainedSpec, MA_SDL_AUDIO_ALLOW_ANY_CHANGE);
tempDeviceID = pContextStateSDL->SDL_OpenAudioDevice(pDeviceName, (deviceType == ma_device_type_playback) ? 0 : 1, &desiredSpec, &obtainedSpec, MA_SDL_AUDIO_ALLOW_ANY_CHANGE);
if (tempDeviceID == 0) {
ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_ERROR, "Failed to open SDL device.");
return MA_FAILED_TO_OPEN_BACKEND_DEVICE;
}
((MA_PFN_SDL_CloseAudioDevice)pContextDataSDL->SDL_CloseAudioDevice)(tempDeviceID);
pContextStateSDL->SDL_CloseAudioDevice(tempDeviceID);
/* Only reporting a single native data format. It'll be whatever SDL decides is the best. */
pDeviceInfo->nativeDataFormatCount = 1;
@@ -291,33 +411,26 @@ static ma_result ma_context_get_device_info__sdl(void* pUserData, ma_context* pC
void ma_audio_callback_capture__sdl(void* pUserData, ma_uint8* pBuffer, int bufferSizeInBytes)
{
ma_device* pDevice = (ma_device*)pUserData;
ma_device_handle_backend_data_callback(pDevice, NULL, pBuffer, (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(pDevice->capture.internalFormat, pDevice->capture.internalChannels));
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
ma_device_handle_backend_data_callback(pDevice, NULL, pBuffer, (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(pDeviceStateSDL->capture.format, pDeviceStateSDL->capture.channels));
}
void ma_audio_callback_playback__sdl(void* pUserData, ma_uint8* pBuffer, int bufferSizeInBytes)
{
ma_device* pDevice = (ma_device*)pUserData;
ma_device_handle_backend_data_callback(pDevice, pBuffer, NULL, (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels));
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
ma_device_handle_backend_data_callback(pDevice, pBuffer, NULL, (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(pDeviceStateSDL->playback.format, pDeviceStateSDL->playback.channels));
}
static ma_result ma_device_init_internal__sdl(ma_device* pDevice, const ma_device_config* pConfig, ma_device_descriptor* pDescriptor)
static ma_result ma_device_init_internal__sdl(ma_device* pDevice, ma_context_state_sdl* pContextStateSDL, ma_device_state_sdl* pDeviceStateSDL, const ma_device_config_sdl* pDeviceConfigSDL, ma_device_type deviceType, ma_device_descriptor* pDescriptor)
{
ma_context_data_sdl* pContextDataSDL;
ma_device_data_sdl* pDeviceDataSDL;
const ma_device_config_sdl* pDeviceConfigSDL;
MA_SDL_AudioSpec desiredSpec;
MA_SDL_AudioSpec obtainedSpec;
const char* pDeviceName;
int deviceID;
pContextDataSDL = (ma_context_data_sdl*)pDevice->pContext->pBackendData;
pDeviceDataSDL = (ma_device_data_sdl*)pDevice->pBackendData;
/* Grab the SDL backend config. This is not currently used. */
pDeviceConfigSDL = (const ma_device_config_sdl*)ma_device_config_find_custom_backend_config(pConfig, MA_DEVICE_BACKEND_VTABLE_SDL);
(void)pDeviceConfigSDL;
/*
SDL is a little bit awkward with specifying the buffer size, You need to specify the size of the buffer in frames, but since we may
have requested a period size in milliseconds we'll need to convert, which depends on the sample rate. But there's a possibility that
@@ -343,7 +456,7 @@ static ma_result ma_device_init_internal__sdl(ma_device* pDevice, const ma_devic
A helper function called ma_calculate_buffer_size_in_frames_from_descriptor() is available to do all of this for you which is what
we'll be using here.
*/
pDescriptor->periodSizeInFrames = ma_calculate_buffer_size_in_frames_from_descriptor(pDescriptor, pDescriptor->sampleRate, pConfig->performanceProfile);
pDescriptor->periodSizeInFrames = ma_calculate_buffer_size_in_frames_from_descriptor(pDescriptor, pDescriptor->sampleRate, ma_performance_profile_low_latency);
/* SDL wants the buffer size to be a power of 2 for some reason. */
if (pDescriptor->periodSizeInFrames > 32768) {
@@ -359,8 +472,8 @@ static ma_result ma_device_init_internal__sdl(ma_device* pDevice, const ma_devic
desiredSpec.format = ma_format_to_sdl(pDescriptor->format);
desiredSpec.channels = (ma_uint8)pDescriptor->channels;
desiredSpec.samples = (ma_uint16)pDescriptor->periodSizeInFrames;
desiredSpec.callback = (pConfig->deviceType == ma_device_type_capture) ? ma_audio_callback_capture__sdl : ma_audio_callback_playback__sdl;
desiredSpec.userdata = pDevice;
desiredSpec.callback = (deviceType == ma_device_type_capture) ? ma_audio_callback_capture__sdl : ma_audio_callback_playback__sdl;
desiredSpec.userdata = pDeviceStateSDL;
/* We'll fall back to f32 if we don't have an appropriate mapping between SDL and miniaudio. */
if (desiredSpec.format == 0) {
@@ -369,21 +482,15 @@ static ma_result ma_device_init_internal__sdl(ma_device* pDevice, const ma_devic
pDeviceName = NULL;
if (pDescriptor->pDeviceID != NULL) {
pDeviceName = ((MA_PFN_SDL_GetAudioDeviceName)pContextDataSDL->SDL_GetAudioDeviceName)(pDescriptor->pDeviceID->custom.i, (pConfig->deviceType == ma_device_type_playback) ? 0 : 1);
pDeviceName = pContextStateSDL->SDL_GetAudioDeviceName(pDescriptor->pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1);
}
deviceID = ((MA_PFN_SDL_OpenAudioDevice)pContextDataSDL->SDL_OpenAudioDevice)(pDeviceName, (pConfig->deviceType == ma_device_type_playback) ? 0 : 1, &desiredSpec, &obtainedSpec, MA_SDL_AUDIO_ALLOW_ANY_CHANGE);
deviceID = pContextStateSDL->SDL_OpenAudioDevice(pDeviceName, (deviceType == ma_device_type_playback) ? 0 : 1, &desiredSpec, &obtainedSpec, MA_SDL_AUDIO_ALLOW_ANY_CHANGE);
if (deviceID == 0) {
ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_ERROR, "Failed to open SDL2 device.");
return MA_FAILED_TO_OPEN_BACKEND_DEVICE;
}
if (pConfig->deviceType == ma_device_type_playback) {
pDeviceDataSDL->deviceIDPlayback = deviceID;
} else {
pDeviceDataSDL->deviceIDCapture = deviceID;
}
/* The descriptor needs to be updated with our actual settings. */
pDescriptor->format = ma_format_from_sdl(obtainedSpec.format);
pDescriptor->channels = obtainedSpec.channels;
@@ -392,210 +499,109 @@ static ma_result ma_device_init_internal__sdl(ma_device* pDevice, const ma_devic
pDescriptor->periodSizeInFrames = obtainedSpec.samples;
pDescriptor->periodCount = 1; /* SDL doesn't use the notion of period counts, so just set to 1. */
if (deviceType == ma_device_type_playback) {
pDeviceStateSDL->playback.deviceID = deviceID;
pDeviceStateSDL->playback.format = pDescriptor->format;
pDeviceStateSDL->playback.channels = pDescriptor->channels;
} else {
pDeviceStateSDL->capture.deviceID = deviceID;
pDeviceStateSDL->capture.format = pDescriptor->format;
pDeviceStateSDL->capture.channels = pDescriptor->channels;
}
return MA_SUCCESS;
}
static ma_result ma_device_init__sdl(void* pUserData, ma_device* pDevice, const ma_device_config* pConfig, ma_device_descriptor* pDescriptorPlayback, ma_device_descriptor* pDescriptorCapture)
static ma_result ma_device_init__sdl(ma_device* pDevice, const void* pDeviceBackendConfig, ma_device_descriptor* pDescriptorPlayback, ma_device_descriptor* pDescriptorCapture, void** ppDeviceState)
{
ma_context_data_sdl* pContextDataSDL;
ma_device_data_sdl* pDeviceDataSDL;
ma_device_state_sdl* pDeviceStateSDL;
ma_device_config_sdl* pDeviceConfigSDL = (ma_device_config_sdl*)pDeviceBackendConfig;
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(ma_device_get_context(pDevice));
ma_device_type deviceType = ma_device_get_type(pDevice);
ma_result result;
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pDevice->pContext->pBackendData;
/* SDL does not support loopback mode, so must return MA_DEVICE_TYPE_NOT_SUPPORTED if it's requested. */
if (pConfig->deviceType == ma_device_type_loopback) {
if (deviceType == ma_device_type_loopback) {
return MA_DEVICE_TYPE_NOT_SUPPORTED;
}
/* We need to allocate our backend-specific data. */
pDeviceDataSDL = (ma_device_data_sdl*)ma_calloc(sizeof(*pDeviceDataSDL), &pDevice->pContext->allocationCallbacks);
if (pDeviceDataSDL == NULL) {
pDeviceStateSDL = (ma_device_state_sdl*)ma_calloc(sizeof(*pDeviceStateSDL), ma_device_get_allocation_callbacks(pDevice));
if (pDeviceStateSDL == NULL) {
return MA_OUT_OF_MEMORY;
}
pDevice->pBackendData = pDeviceDataSDL;
if (pConfig->deviceType == ma_device_type_capture || pConfig->deviceType == ma_device_type_duplex) {
result = ma_device_init_internal__sdl(pDevice, pConfig, pDescriptorCapture);
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
result = ma_device_init_internal__sdl(pDevice, pContextStateSDL, pDeviceStateSDL, pDeviceConfigSDL, ma_device_type_capture, pDescriptorCapture);
if (result != MA_SUCCESS) {
ma_free(pDeviceDataSDL, &pDevice->pContext->allocationCallbacks);
ma_free(pDeviceStateSDL, ma_device_get_allocation_callbacks(pDevice));
return result;
}
}
if (pConfig->deviceType == ma_device_type_playback || pConfig->deviceType == ma_device_type_duplex) {
result = ma_device_init_internal__sdl(pDevice, pConfig, pDescriptorPlayback);
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
result = ma_device_init_internal__sdl(pDevice, pContextStateSDL, pDeviceStateSDL, pDeviceConfigSDL, ma_device_type_playback, pDescriptorPlayback);
if (result != MA_SUCCESS) {
if (pConfig->deviceType == ma_device_type_duplex) {
((MA_PFN_SDL_CloseAudioDevice)pContextDataSDL->SDL_CloseAudioDevice)(pDeviceDataSDL->deviceIDCapture);
if (deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_CloseAudioDevice(pDeviceStateSDL->capture.deviceID);
}
ma_free(pDeviceDataSDL, &pDevice->pContext->allocationCallbacks);
ma_free(pDeviceStateSDL, ma_device_get_allocation_callbacks(pDevice));
return result;
}
}
return MA_SUCCESS;
}
static ma_result ma_device_uninit__sdl(void* pUserData, ma_device* pDevice)
{
ma_context_data_sdl* pContextDataSDL;
ma_device_data_sdl* pDeviceDataSDL;
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pDevice->pContext->pBackendData;
pDeviceDataSDL = (ma_device_data_sdl*)pDevice->pBackendData;
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_CloseAudioDevice)pContextDataSDL->SDL_CloseAudioDevice)(pDeviceDataSDL->deviceIDCapture);
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_CloseAudioDevice)pContextDataSDL->SDL_CloseAudioDevice)(pDeviceDataSDL->deviceIDCapture);
}
ma_free(pDeviceDataSDL, &pDevice->pContext->allocationCallbacks);
*ppDeviceState = pDeviceStateSDL;
return MA_SUCCESS;
}
static ma_result ma_device_start__sdl(void* pUserData, ma_device* pDevice)
static void ma_device_uninit__sdl(ma_device* pDevice)
{
ma_context_data_sdl* pContextDataSDL;
ma_device_data_sdl* pDeviceDataSDL;
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(ma_device_get_context(pDevice));
ma_device_type deviceType = ma_device_get_type(pDevice);
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pDevice->pContext->pBackendData;
pDeviceDataSDL = (ma_device_data_sdl*)pDevice->pBackendData;
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextDataSDL->SDL_PauseAudioDevice)(pDeviceDataSDL->deviceIDCapture, 0);
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_CloseAudioDevice(pDeviceStateSDL->capture.deviceID);
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextDataSDL->SDL_PauseAudioDevice)(pDeviceDataSDL->deviceIDPlayback, 0);
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_CloseAudioDevice(pDeviceStateSDL->playback.deviceID);
}
ma_free(pDeviceStateSDL, ma_device_get_allocation_callbacks(pDevice));
}
static ma_result ma_device_start__sdl(ma_device* pDevice)
{
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(ma_device_get_context(pDevice));
ma_device_type deviceType = ma_device_get_type(pDevice);
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_PauseAudioDevice(pDeviceStateSDL->capture.deviceID, 0);
}
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_PauseAudioDevice(pDeviceStateSDL->playback.deviceID, 0);
}
return MA_SUCCESS;
}
static ma_result ma_device_stop__sdl(void* pUserData, ma_device* pDevice)
static ma_result ma_device_stop__sdl(ma_device* pDevice)
{
ma_context_data_sdl* pContextDataSDL;
ma_device_data_sdl* pDeviceDataSDL;
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
ma_context_state_sdl* pContextStateSDL = ma_context_get_backend_state__sdl(ma_device_get_context(pDevice));
ma_device_type deviceType = ma_device_get_type(pDevice);
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pDevice->pContext->pBackendData;
pDeviceDataSDL = (ma_device_data_sdl*)pDevice->pBackendData;
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextDataSDL->SDL_PauseAudioDevice)(pDeviceDataSDL->deviceIDCapture, 1);
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_PauseAudioDevice(pDeviceStateSDL->capture.deviceID, 1);
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextDataSDL->SDL_PauseAudioDevice)(pDeviceDataSDL->deviceIDPlayback, 1);
}
return MA_SUCCESS;
}
static ma_result ma_context_uninit__sdl(void* pUserData, ma_context* pContext)
{
ma_context_data_sdl* pContextDataSDL;
(void)pUserData;
pContextDataSDL = (ma_context_data_sdl*)pContext->pBackendData;
((MA_PFN_SDL_QuitSubSystem)pContextDataSDL->SDL_QuitSubSystem)(MA_SDL_INIT_AUDIO);
/* Close the handle to the SDL shared object last. */
ma_dlclose(ma_context_get_log(pContext), pContextDataSDL->hSDL);
pContextDataSDL->hSDL = NULL;
ma_free(pContextDataSDL, &pContext->allocationCallbacks);
return MA_SUCCESS;
}
static ma_result ma_context_init__sdl(void* pUserData, ma_context* pContext, const ma_context_config* pConfig)
{
ma_context_data_sdl* pContextDataSDL;
const ma_context_config_sdl* pContextConfigSDL;
int resultSDL;
#ifndef MA_NO_RUNTIME_LINKING
/* We'll use a list of possible shared object names for easier extensibility. */
size_t iName;
const char* pSDLNames[] = {
#if defined(_WIN32)
"SDL2.dll"
#elif defined(__APPLE__)
"SDL2.framework/SDL2"
#else
"libSDL2-2.0.so.0"
#endif
};
(void)pUserData;
/* Grab the config. This is not currently being used. */
pContextConfigSDL = (const ma_context_config_sdl*)ma_context_config_find_custom_backend_config(pConfig, MA_DEVICE_BACKEND_VTABLE_SDL);
(void)pContextConfigSDL;
/* Allocate our SDL-specific context data. */
pContextDataSDL = (ma_context_data_sdl*)ma_calloc(sizeof(*pContextDataSDL), &pContext->allocationCallbacks);
if (pContextDataSDL == NULL) {
return MA_OUT_OF_MEMORY;
}
pContext->pBackendData = pContextDataSDL;
/* Check if we have SDL2 installed somewhere. If not it's not usable and we need to abort. */
for (iName = 0; iName < ma_countof(pSDLNames); iName += 1) {
pContextDataSDL->hSDL = ma_dlopen(ma_context_get_log(pContext), pSDLNames[iName]);
if (pContextDataSDL->hSDL != NULL) {
break;
}
}
if (pContextDataSDL->hSDL == NULL) {
ma_free(pContextDataSDL, &pContext->allocationCallbacks);
return MA_NO_BACKEND; /* SDL2 could not be loaded. */
}
/* Now that we have the handle to the shared object we can go ahead and load some function pointers. */
pContextDataSDL->SDL_InitSubSystem = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_InitSubSystem");
pContextDataSDL->SDL_QuitSubSystem = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_QuitSubSystem");
pContextDataSDL->SDL_GetNumAudioDevices = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_GetNumAudioDevices");
pContextDataSDL->SDL_GetAudioDeviceName = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_GetAudioDeviceName");
pContextDataSDL->SDL_CloseAudioDevice = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_CloseAudioDevice");
pContextDataSDL->SDL_OpenAudioDevice = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_OpenAudioDevice");
pContextDataSDL->SDL_PauseAudioDevice = ma_dlsym(ma_context_get_log(pContext), pContextDataSDL->hSDL, "SDL_PauseAudioDevice");
#else
pContextDataSDL->SDL_InitSubSystem = (ma_proc)SDL_InitSubSystem;
pContextDataSDL->SDL_QuitSubSystem = (ma_proc)SDL_QuitSubSystem;
pContextDataSDL->SDL_GetNumAudioDevices = (ma_proc)SDL_GetNumAudioDevices;
pContextDataSDL->SDL_GetAudioDeviceName = (ma_proc)SDL_GetAudioDeviceName;
pContextDataSDL->SDL_CloseAudioDevice = (ma_proc)SDL_CloseAudioDevice;
pContextDataSDL->SDL_OpenAudioDevice = (ma_proc)SDL_OpenAudioDevice;
pContextDataSDL->SDL_PauseAudioDevice = (ma_proc)SDL_PauseAudioDevice;
#endif /* MA_NO_RUNTIME_LINKING */
resultSDL = ((MA_PFN_SDL_InitSubSystem)pContextDataSDL->SDL_InitSubSystem)(MA_SDL_INIT_AUDIO);
if (resultSDL != 0) {
ma_dlclose(ma_context_get_log(pContext), pContextDataSDL->hSDL);
ma_free(pContextDataSDL, &pContext->allocationCallbacks);
return MA_ERROR;
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
pContextStateSDL->SDL_PauseAudioDevice(pDeviceStateSDL->playback.deviceID, 1);
}
return MA_SUCCESS;
@@ -604,6 +610,7 @@ static ma_result ma_context_init__sdl(void* pUserData, ma_context* pContext, con
static ma_device_backend_vtable ma_gDeviceBackendVTable_SDL =
{
ma_backend_info__sdl,
ma_context_init__sdl,
ma_context_uninit__sdl,
ma_context_enumerate_devices__sdl,
@@ -614,23 +621,13 @@ static ma_device_backend_vtable ma_gDeviceBackendVTable_SDL =
ma_device_stop__sdl,
NULL, /* onDeviceRead */
NULL, /* onDeviceWrite */
NULL, /* onDeviceDataLoop */
NULL, /* onDeviceDataLoopWakeup */
NULL, /* onDeviceGetInfo */
/* Temp. */
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
NULL, /* onDeviceLoop */
NULL /* onDeviceWakeup */
};
const ma_device_backend_vtable* MA_DEVICE_BACKEND_VTABLE_SDL = &ma_gDeviceBackendVTable_SDL;
ma_device_backend_vtable* ma_device_backend_sdl = &ma_gDeviceBackendVTable_SDL;
#else
const ma_device_backend_vtable* MA_DEVICE_BACKEND_VTABLE_SDL = NULL;
ma_device_backend_vtable* ma_device_backend_sdl = NULL;
#endif /* MA_HAS_SDL */
+1 -1
View File
@@ -10,7 +10,7 @@ and device configs.
extern "C" {
#endif
extern const ma_device_backend_vtable* MA_DEVICE_BACKEND_VTABLE_SDL;
extern ma_device_backend_vtable* ma_device_backend_sdl;
typedef struct
+8511 -7095
View File
File diff suppressed because it is too large Load Diff
@@ -75,7 +75,7 @@ Java_io_miniaud_miniaudiotester_MainActivity_PlayAudio(JNIEnv *env, jobject, jlo
/* If we don't have a device, create one. */
if (!pAudioState->hasDevice) {
ma_context_config contextConfig = ma_context_config_init();
ma_backend pBackends[1];
ma_device_backend_config pBackends[1];
size_t backendCount;
if (backend == BACKEND_AUTO) {
@@ -83,9 +83,9 @@ Java_io_miniaud_miniaudiotester_MainActivity_PlayAudio(JNIEnv *env, jobject, jlo
} else {
backendCount = 1;
if (backend == BACKEND_AAUDIO) {
pBackends[0] = ma_backend_aaudio;
pBackends[0] = ma_device_backend_config_init(ma_device_backend_aaudio, nullptr);
} else if (backend == BACKEND_OPENSL) {
pBackends[0] = ma_backend_opensl;
pBackends[0] = ma_device_backend_config_init(ma_device_backend_opensl, nullptr);
} else {
backendCount = 0;
}
+37 -31
View File
@@ -112,7 +112,7 @@ ma_bool32 try_parse_mode(const char* arg, ma_device_type* pDeviceType)
return MA_FALSE;
}
ma_bool32 try_parse_backend(const char* arg, ma_backend* pBackends, ma_uint32 backendCap, ma_uint32* pBackendCount)
ma_bool32 try_parse_backend(const char* arg, ma_device_backend_config* pBackends, ma_uint32 backendCap, ma_uint32* pBackendCount)
{
ma_uint32 backendCount;
@@ -126,59 +126,59 @@ ma_bool32 try_parse_backend(const char* arg, ma_backend* pBackends, ma_uint32 ba
}
if (strcmp(arg, "wasapi") == 0) {
pBackends[backendCount++] = ma_backend_wasapi;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_wasapi, NULL);
goto done;
}
if (strcmp(arg, "dsound") == 0 || strcmp(arg, "directsound") == 0) {
pBackends[backendCount++] = ma_backend_dsound;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_dsound, NULL);
goto done;
}
if (strcmp(arg, "winmm") == 0) {
pBackends[backendCount++] = ma_backend_winmm;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_winmm, NULL);
goto done;
}
if (strcmp(arg, "coreaudio") == 0) {
pBackends[backendCount++] = ma_backend_coreaudio;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_coreaudio, NULL);
goto done;
}
if (strcmp(arg, "sndio") == 0) {
pBackends[backendCount++] = ma_backend_sndio;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_sndio, NULL);
goto done;
}
if (strcmp(arg, "audio4") == 0) {
pBackends[backendCount++] = ma_backend_audio4;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_audio4, NULL);
goto done;
}
if (strcmp(arg, "oss") == 0) {
pBackends[backendCount++] = ma_backend_oss;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_oss, NULL);
goto done;
}
if (strcmp(arg, "pulseaudio") == 0 || strcmp(arg, "pulse") == 0) {
pBackends[backendCount++] = ma_backend_pulseaudio;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_pulseaudio, NULL);
goto done;
}
if (strcmp(arg, "alsa") == 0) {
pBackends[backendCount++] = ma_backend_alsa;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_alsa, NULL);
goto done;
}
if (strcmp(arg, "jack") == 0) {
pBackends[backendCount++] = ma_backend_jack;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_jack, NULL);
goto done;
}
if (strcmp(arg, "aaudio") == 0) {
pBackends[backendCount++] = ma_backend_aaudio;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_aaudio, NULL);
goto done;
}
if (strcmp(arg, "opensl") == 0) {
pBackends[backendCount++] = ma_backend_opensl;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_opensl, NULL);
goto done;
}
if (strcmp(arg, "webaudio") == 0) {
pBackends[backendCount++] = ma_backend_webaudio;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_webaudio, NULL);
goto done;
}
if (strcmp(arg, "null") == 0) {
pBackends[backendCount++] = ma_backend_null;
pBackends[backendCount++] = ma_device_backend_config_init(ma_device_backend_null, NULL);
goto done;
}
@@ -237,6 +237,26 @@ ma_bool32 try_parse_noise(const char* arg, ma_noise_type* pNoiseType)
return MA_FALSE;
}
void print_enabled_backends()
{
const ma_device_backend_config pStockBackends[] = MA_STOCK_DEVICE_BACKENDS;
ma_uint32 stockBackendCount = ma_countof(pStockBackends);
ma_uint32 iEnabledStockBackend;
printf("Enabled Backends:\n");
for (iEnabledStockBackend = 0; iEnabledStockBackend < stockBackendCount; iEnabledStockBackend += 1) {
ma_device_backend_config backend = pStockBackends[iEnabledStockBackend];
if (backend.pVTable != NULL && backend.pVTable->onBackendInfo != NULL) {
ma_device_backend_info backendInfo;
backend.pVTable->onBackendInfo(&backendInfo);
printf(" %s\n", backendInfo.pName);
}
}
printf("\n");
}
ma_result print_device_info(ma_context* pContext, ma_device_type deviceType, const ma_device_info* pDeviceInfo)
{
ma_result result;
@@ -399,10 +419,7 @@ int main(int argc, char** argv)
{
int iarg;
ma_result result;
ma_backend enabledBackends[MA_BACKEND_COUNT];
size_t enabledBackendCount;
size_t iEnabledBackend;
ma_backend backends[MA_BACKEND_COUNT];
ma_device_backend_config backends[256];
ma_uint32 backendCount = 0;
ma_context_config contextConfig;
ma_device_type deviceType = ma_device_type_playback;
@@ -455,18 +472,7 @@ int main(int argc, char** argv)
}
/* Here we'll quickly print the available backends. */
printf("Enabled Backends:\n");
result = ma_get_enabled_backends(enabledBackends, ma_countof(enabledBackends), &enabledBackendCount);
if (result != MA_SUCCESS) {
printf("Failed to retrieve available backends.\n");
return -1;
}
for (iEnabledBackend = 0; iEnabledBackend < enabledBackendCount; iEnabledBackend += 1) {
printf(" %s\n", ma_get_backend_name(enabledBackends[iEnabledBackend]));
}
printf("\n");
print_enabled_backends();
/* Initialize the context first. If no backends were passed into the command line we just use defaults. */
contextConfig = ma_context_config_init();