Commit 887bbeee authored by David Reid's avatar David Reid

Add support for custom backends.

This commit includes a few changes required for better supporting this:

  * Extra members have been added to ma_device_info which are going to
    eventually replace the min and max channels and sample rates. The
    new system is going to provide a list of supported data formats,
    groups by format/channels/rate and some flags. The only flag used
    at the moment is whether or not the format is usable in exclusive
    mode. The custom backend is the only backend currently using these
    new device info properties, and a backwards-compatibility layer has
    been implemented to fill out the old properties. Built-in backends
    will be migrated over to the new system in time.

  * A new set of backend callbacks have been implemented. Only the
    custom backend is using these at the moment. Built-in backends will
    be migrated over to these new backends soon.

  * A new public API called ma_device_get_state() has been added which
    returns the current state of the device (whether or not it's
    started or stopped). This is necessary for some custom backends.

  * A new public API called ma_device_handle_backend_data_callback()
    has been added. This is required for custom backends who use the
    callback paradigm for data delivery.

  * A new type of ring buffer has been created called ma_duplex_rb.
    This is used as an intermediary buffer for duplex devices running
    on backends that use the callback paradigm. It's used internally by
    ma_device_handle_backend_data_callback(). In the future it's
    planned to expand ma_duplex_rb to handle desyncs by dynamically
    resampling to get both sides back in sync. This is not implemented
    as of this commit.

Future work will involve converting existing built-in backends to be
consistent with the new ideas introduced with custom backend support.
parent 63aab332
/*
This example show how a custom backend can be implemented.
This implements a full-featured SDL2 backend. It's intentionally built using the same paradigms as the built-in backends in order to make
it suitable as a solid basis for a custom implementation. The SDL2 backend can be disabled with MA_NO_SDL, exactly like the build-in
backends. It supports both runtime and compile-time linking and respects the MA_NO_RUNTIME_LINKING option. It also works on Emscripten.
There may be times where you want to support more than one custom backend. This example has been designed to make it easy to plug-in extra
custom backends without needing to modify any of the base miniaudio initialization code. A custom context structure is declared called
`ma_context_ex`. The first member of this structure is a `ma_context` object which allows it to be cast between the two. The same is done
for devices, which is called `ma_device_ex`. In these structures there is a section for each custom backend, which in this example is just
SDL. These are only enabled at compile time if `MA_SUPPORT_SDL` is defined, which it always is in this example (you may want to have some
logic which more intelligently enables or disables SDL support).
To use a custom backend, at a minimum you must set the `custom.onContextInit()` callback in the context config. You do not need to set the
other callbacks, but if you don't, you must set them in the implementation of the `onContextInit()` callback which is done via an output
parameter. This is the approach taken by this example because it's the simplest way to support multiple custom backends. The idea is that
the `onContextInit()` callback is set to a generic "loader", which then calls out to a backend-specific implementation which then sets the
remaining callbacks if it is successfully initialized.
Custom backends are identified with the `ma_backend_custom` backend type. For the purpose of demonstration, this example only uses the
`ma_backend_custom` backend type because otherwise the built-in backends would always get chosen first and none of the code for the custom
backends would actually get hit. By default, the `ma_backend_custom` backend is the lowest priority backend, except for `ma_backend_null`.
*/
#define MINIAUDIO_IMPLEMENTATION
#include "../miniaudio.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
void main_loop__em()
{
}
#endif
/* Support SDL on everything. */
#define MA_SUPPORT_SDL
/* Only enable SDL if it's hasn't been explicitly disabled (MA_NO_SDL) and it's supported at compile time (MA_SUPPORT_SDL). */
#if !defined(MA_NO_SDL) && defined(MA_SUPPORT_SDL)
#define MA_ENABLE_SDL
#endif
typedef struct
{
ma_context context; /* Make this the first member so we can cast between ma_context and ma_context_ex. */
#if defined(MA_SUPPORT_SDL)
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;
} sdl;
#endif
} ma_context_ex;
typedef struct
{
ma_device device; /* Make this the first member so we can cast between ma_device and ma_device_ex. */
#if defined(MA_SUPPORT_SDL)
struct
{
int deviceIDPlayback;
int deviceIDCapture;
} sdl;
#endif
} ma_device_ex;
#if defined(MA_ENABLE_SDL)
#define MA_HAS_SDL
/* SDL headers are necessary if using compile-time linking. */
#ifdef MA_NO_RUNTIME_LINKING
#ifdef __has_include
#ifdef MA_EMSCRIPTEN
#if !__has_include(<SDL/SDL_audio.h>)
#undef MA_HAS_SDL
#endif
#else
#if !__has_include(<SDL2/SDL_audio.h>)
#undef MA_HAS_SDL
#endif
#endif
#endif
#endif
#endif
#if defined(MA_HAS_SDL)
#define MA_SDL_INIT_AUDIO 0x00000010
#define MA_AUDIO_U8 0x0008
#define MA_AUDIO_S16 0x8010
#define MA_AUDIO_S32 0x8020
#define MA_AUDIO_F32 0x8120
#define MA_SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
#define MA_SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
#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)
/* 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
#ifdef MA_EMSCRIPTEN
#include <SDL/SDL.h>
#else
#include <SDL2/SDL.h>
#endif
typedef SDL_AudioCallback MA_SDL_AudioCallback;
typedef SDL_AudioSpec MA_SDL_AudioSpec;
typedef SDL_AudioFormat MA_SDL_AudioFormat;
typedef SDL_AudioDeviceID MA_SDL_AudioDeviceID;
#else
typedef void (* MA_SDL_AudioCallback)(void* userdata, ma_uint8* stream, int len);
typedef ma_uint16 MA_SDL_AudioFormat;
typedef ma_uint32 MA_SDL_AudioDeviceID;
typedef struct MA_SDL_AudioSpec
{
int freq;
MA_SDL_AudioFormat format;
ma_uint8 channels;
ma_uint8 silence;
ma_uint16 samples;
ma_uint16 padding;
ma_uint32 size;
MA_SDL_AudioCallback callback;
void* userdata;
} MA_SDL_AudioSpec;
#endif
typedef int (* MA_PFN_SDL_InitSubSystem)(ma_uint32 flags);
typedef void (* MA_PFN_SDL_QuitSubSystem)(ma_uint32 flags);
typedef int (* MA_PFN_SDL_GetNumAudioDevices)(int iscapture);
typedef const char* (* MA_PFN_SDL_GetAudioDeviceName)(int index, int iscapture);
typedef void (* MA_PFN_SDL_CloseAudioDevice)(MA_SDL_AudioDeviceID dev);
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);
MA_SDL_AudioFormat ma_format_to_sdl(ma_format format)
{
switch (format)
{
case ma_format_unknown: return 0;
case ma_format_u8: return MA_AUDIO_U8;
case ma_format_s16: return MA_AUDIO_S16;
case ma_format_s24: return MA_AUDIO_S32; /* Closest match. */
case ma_format_s32: return MA_AUDIO_S32;
case ma_format_f32: return MA_AUDIO_F32;
default: return 0;
}
}
ma_format ma_format_from_sdl(MA_SDL_AudioFormat format)
{
switch (format)
{
case MA_AUDIO_U8: return ma_format_u8;
case MA_AUDIO_S16: return ma_format_s16;
case MA_AUDIO_S32: return ma_format_s32;
case MA_AUDIO_F32: return ma_format_f32;
default: return ma_format_unknown;
}
}
static ma_result ma_context_enumerate_devices__sdl(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{
ma_context_ex* pContextEx = (ma_context_ex*)pContext;
ma_bool32 isTerminated = MA_FALSE;
ma_bool32 cbResult;
int iDevice;
MA_ASSERT(pContext != NULL);
MA_ASSERT(callback != NULL);
/* Playback */
if (!isTerminated) {
int deviceCount = ((MA_PFN_SDL_GetNumAudioDevices)pContextEx->sdl.SDL_GetNumAudioDevices)(0);
for (iDevice = 0; iDevice < deviceCount; ++iDevice) {
ma_device_info deviceInfo;
MA_ZERO_OBJECT(&deviceInfo);
deviceInfo.id.custom.i = iDevice;
ma_strncpy_s(deviceInfo.name, sizeof(deviceInfo.name), ((MA_PFN_SDL_GetAudioDeviceName)pContextEx->sdl.SDL_GetAudioDeviceName)(iDevice, 0), (size_t)-1);
if (iDevice == 0) {
deviceInfo.isDefault = MA_TRUE;
}
cbResult = callback(pContext, ma_device_type_playback, &deviceInfo, pUserData);
if (cbResult == MA_FALSE) {
isTerminated = MA_TRUE;
break;
}
}
}
/* Capture */
if (!isTerminated) {
int deviceCount = ((MA_PFN_SDL_GetNumAudioDevices)pContextEx->sdl.SDL_GetNumAudioDevices)(1);
for (iDevice = 0; iDevice < deviceCount; ++iDevice) {
ma_device_info deviceInfo;
MA_ZERO_OBJECT(&deviceInfo);
deviceInfo.id.custom.i = iDevice;
ma_strncpy_s(deviceInfo.name, sizeof(deviceInfo.name), ((MA_PFN_SDL_GetAudioDeviceName)pContextEx->sdl.SDL_GetAudioDeviceName)(iDevice, 1), (size_t)-1);
if (iDevice == 0) {
deviceInfo.isDefault = MA_TRUE;
}
cbResult = callback(pContext, ma_device_type_capture, &deviceInfo, pUserData);
if (cbResult == MA_FALSE) {
isTerminated = MA_TRUE;
break;
}
}
}
return MA_SUCCESS;
}
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_ex* pContextEx = (ma_context_ex*)pContext;
MA_SDL_AudioSpec desiredSpec;
MA_SDL_AudioSpec obtainedSpec;
MA_SDL_AudioDeviceID tempDeviceID;
const char* pDeviceName;
MA_ASSERT(pContext != NULL);
if (pDeviceID == NULL) {
if (deviceType == ma_device_type_playback) {
pDeviceInfo->id.custom.i = 0;
ma_strncpy_s(pDeviceInfo->name, sizeof(pDeviceInfo->name), MA_DEFAULT_PLAYBACK_DEVICE_NAME, (size_t)-1);
} else {
pDeviceInfo->id.custom.i = 0;
ma_strncpy_s(pDeviceInfo->name, sizeof(pDeviceInfo->name), MA_DEFAULT_CAPTURE_DEVICE_NAME, (size_t)-1);
}
} else {
pDeviceInfo->id.custom.i = pDeviceID->custom.i;
ma_strncpy_s(pDeviceInfo->name, sizeof(pDeviceInfo->name), ((MA_PFN_SDL_GetAudioDeviceName)pContextEx->sdl.SDL_GetAudioDeviceName)(pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1), (size_t)-1);
}
if (pDeviceInfo->id.custom.i == 0) {
pDeviceInfo->isDefault = MA_TRUE;
}
/*
To get an accurate idea on the backend's native format we need to open the device. Not ideal, but it's the only way. An
alternative to this is to report all channel counts, sample rates and formats, but that doesn't offer a good representation
of the device's _actual_ ideal format.
Note: With Emscripten, it looks like non-zero values need to be specified for desiredSpec. Whatever is specified in
desiredSpec will be used by SDL since it uses it just does it's own format conversion internally. Therefore, from what
I can tell, there's no real way to know the device's actual format which means I'm just going to fall back to the full
range of channels and sample rates on Emscripten builds.
*/
#if defined(__EMSCRIPTEN__)
/* Good practice to prioritize the best format first so that the application can use the first data format as their chosen one if desired. */
pDeviceInfo->nativeDataFormatCount = 3;
pDeviceInfo->nativeDataFormats[0].format = ma_format_s16;
pDeviceInfo->nativeDataFormats[0].channels = 0; /* All channel counts supported. */
pDeviceInfo->nativeDataFormats[0].sampleRate = 0; /* All sample rates supported. */
pDeviceInfo->nativeDataFormats[1].format = ma_format_s32;
pDeviceInfo->nativeDataFormats[1].channels = 0; /* All channel counts supported. */
pDeviceInfo->nativeDataFormats[1].sampleRate = 0; /* All sample rates supported. */
pDeviceInfo->nativeDataFormats[2].format = ma_format_u8;
pDeviceInfo->nativeDataFormats[2].channels = 0; /* All channel counts supported. */
pDeviceInfo->nativeDataFormats[2].sampleRate = 0; /* All sample rates supported. */
#else
MA_ZERO_MEMORY(&desiredSpec, sizeof(desiredSpec));
pDeviceName = NULL;
if (pDeviceID != NULL) {
pDeviceName = ((MA_PFN_SDL_GetAudioDeviceName)pContextEx->sdl.SDL_GetAudioDeviceName)(pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1);
}
tempDeviceID = ((MA_PFN_SDL_OpenAudioDevice)pContextEx->sdl.SDL_OpenAudioDevice)(pDeviceName, (deviceType == ma_device_type_playback) ? 0 : 1, &desiredSpec, &obtainedSpec, MA_SDL_AUDIO_ALLOW_ANY_CHANGE);
if (tempDeviceID == 0) {
return ma_context_post_error(pContext, NULL, MA_LOG_LEVEL_ERROR, "Failed to open SDL device.", MA_FAILED_TO_OPEN_BACKEND_DEVICE);
}
((MA_PFN_SDL_CloseAudioDevice)pContextEx->sdl.SDL_CloseAudioDevice)(tempDeviceID);
/* Only reporting a single native data format. It'll be whatever SDL decides is the best. */
pDeviceInfo->nativeDataFormatCount = 1;
pDeviceInfo->nativeDataFormats[0].format = ma_format_from_sdl(obtainedSpec.format);
pDeviceInfo->nativeDataFormats[0].channels = obtainedSpec.channels;
pDeviceInfo->nativeDataFormats[0].sampleRate = obtainedSpec.freq;
pDeviceInfo->nativeDataFormats[0].flags = 0;
/* If miniaudio does not support the format, just use f32 as the native format (SDL will do the necessary conversions for us). */
if (pDeviceInfo->nativeDataFormats[0].format == ma_format_unknown) {
pDeviceInfo->nativeDataFormats[0].format = ma_format_f32;
}
#endif /* __EMSCRIPTEN__ */
return MA_SUCCESS;
}
void ma_audio_callback_capture__sdl(void* pUserData, ma_uint8* pBuffer, int bufferSizeInBytes)
{
ma_device_ex* pDeviceEx = (ma_device_ex*)pUserData;
MA_ASSERT(pDeviceEx != NULL);
ma_device_handle_backend_data_callback((ma_device*)pDeviceEx, NULL, pBuffer, (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(pDeviceEx->device.capture.internalFormat, pDeviceEx->device.capture.internalChannels));
}
void ma_audio_callback_playback__sdl(void* pUserData, ma_uint8* pBuffer, int bufferSizeInBytes)
{
ma_device_ex* pDeviceEx = (ma_device_ex*)pUserData;
MA_ASSERT(pDeviceEx != NULL);
ma_device_handle_backend_data_callback((ma_device*)pDeviceEx, pBuffer, NULL, (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(pDeviceEx->device.capture.internalFormat, pDeviceEx->device.capture.internalChannels));
}
static ma_result ma_device_init_internal__sdl(ma_device_ex* pDeviceEx, ma_device_type deviceType, ma_device_descriptor* pDescriptor)
{
ma_context_ex* pContextEx = (ma_context_ex*)pDeviceEx->device.pContext;
MA_SDL_AudioSpec desiredSpec;
MA_SDL_AudioSpec obtainedSpec;
const char* pDeviceName;
int deviceID;
MA_ASSERT(pDeviceEx != NULL);
MA_ASSERT(pDescriptor != NULL);
/*
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
the sample rate just set to 0, which indicates that the native sample rate should be used. There's no practical way to calculate this
that I can think of right now so I'm just using MA_DEFAULT_SAMPLE_RATE.
*/
if (pDescriptor->sampleRate == 0) {
pDescriptor->sampleRate = MA_DEFAULT_SAMPLE_RATE;
}
if (pDescriptor->periodSizeInFrames == 0) {
pDescriptor->periodSizeInFrames = ma_calculate_buffer_size_in_frames_from_milliseconds(pDescriptor->periodSizeInMilliseconds, pDescriptor->sampleRate);
}
/* SDL wants the buffer size to be a power of 2 for some reason. */
if (pDescriptor->periodSizeInFrames > 32768) {
pDescriptor->periodSizeInFrames = 32768;
} else {
pDescriptor->periodSizeInFrames = ma_next_power_of_2(pDescriptor->periodSizeInFrames);
}
/* We now have enough information to set up the device. */
MA_ZERO_OBJECT(&desiredSpec);
desiredSpec.freq = (int)pDescriptor->sampleRate;
desiredSpec.format = ma_format_to_sdl(pDescriptor->format);
desiredSpec.channels = (ma_uint8)pDescriptor->channels;
desiredSpec.samples = (ma_uint16)pDescriptor->periodSizeInFrames;
desiredSpec.callback = (deviceType == ma_device_type_capture) ? ma_audio_callback_capture__sdl : ma_audio_callback_playback__sdl;
desiredSpec.userdata = pDeviceEx;
/* We'll fall back to f32 if we don't have an appropriate mapping between SDL and miniaudio. */
if (desiredSpec.format == 0) {
desiredSpec.format = MA_AUDIO_F32;
}
pDeviceName = NULL;
if (pDescriptor->pDeviceID != NULL) {
pDeviceName = ((MA_PFN_SDL_GetAudioDeviceName)pContextEx->sdl.SDL_GetAudioDeviceName)(pDescriptor->pDeviceID->custom.i, (deviceType == ma_device_type_playback) ? 0 : 1);
}
deviceID = ((MA_PFN_SDL_OpenAudioDevice)pContextEx->sdl.SDL_OpenAudioDevice)(pDeviceName, (deviceType == ma_device_type_playback) ? 0 : 1, &desiredSpec, &obtainedSpec, MA_SDL_AUDIO_ALLOW_ANY_CHANGE);
if (deviceID == 0) {
return ma_post_error((ma_device*)pDeviceEx, MA_LOG_LEVEL_ERROR, "Failed to open SDL2 device.", MA_FAILED_TO_OPEN_BACKEND_DEVICE);
}
if (deviceType == ma_device_type_playback) {
pDeviceEx->sdl.deviceIDPlayback = deviceID;
} else {
pDeviceEx->sdl.deviceIDCapture = deviceID;
}
/* The descriptor needs to be updated with our actual settings. */
pDescriptor->format = ma_format_from_sdl(obtainedSpec.format);
pDescriptor->channels = obtainedSpec.channels;
pDescriptor->sampleRate = (ma_uint32)obtainedSpec.freq;
ma_get_standard_channel_map(ma_standard_channel_map_default, pDescriptor->channels, pDescriptor->channelMap);
pDescriptor->periodSizeInFrames = obtainedSpec.samples;
pDescriptor->periodCount = 1; /* SDL doesn't use the notion of period counts, so just set to 1. */
return MA_SUCCESS;
}
static ma_result ma_device_init__sdl(ma_device* pDevice, ma_device_type deviceType, ma_device_descriptor* pDescriptorPlayback, ma_device_descriptor* pDescriptorCapture)
{
ma_device_ex* pDeviceEx = (ma_device_ex*)pDevice;
ma_context_ex* pContextEx = (ma_context_ex*)pDevice->pContext;
ma_result result;
MA_ASSERT(pDevice != NULL);
/* SDL does not support loopback mode, so must return MA_DEVICE_TYPE_NOT_SUPPORTED if it's requested. */
if (deviceType == ma_device_type_loopback) {
return MA_DEVICE_TYPE_NOT_SUPPORTED;
}
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
result = ma_device_init_internal__sdl(pDeviceEx, ma_device_type_capture, pDescriptorCapture);
if (result != MA_SUCCESS) {
return result;
}
}
if (deviceType == ma_device_type_playback || deviceType == ma_device_type_duplex) {
result = ma_device_init_internal__sdl(pDeviceEx, ma_device_type_playback, pDescriptorPlayback);
if (result != MA_SUCCESS) {
if (deviceType == ma_device_type_duplex) {
((MA_PFN_SDL_CloseAudioDevice)pContextEx->sdl.SDL_CloseAudioDevice)(pDeviceEx->sdl.deviceIDCapture);
}
return result;
}
}
return MA_SUCCESS;
}
static ma_result ma_device_uninit__sdl(ma_device* pDevice)
{
ma_device_ex* pDeviceEx = (ma_device_ex*)pDevice;
ma_context_ex* pContextEx = (ma_context_ex*)pDevice->pContext;
MA_ASSERT(pDevice != NULL);
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_CloseAudioDevice)pContextEx->sdl.SDL_CloseAudioDevice)(pDeviceEx->sdl.deviceIDCapture);
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_CloseAudioDevice)pContextEx->sdl.SDL_CloseAudioDevice)(pDeviceEx->sdl.deviceIDCapture);
}
return MA_SUCCESS;
}
static ma_result ma_device_start__sdl(ma_device* pDevice)
{
ma_device_ex* pDeviceEx = (ma_device_ex*)pDevice;
ma_context_ex* pContextEx = (ma_context_ex*)pDevice->pContext;
MA_ASSERT(pDevice != NULL);
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextEx->sdl.SDL_PauseAudioDevice)(pDeviceEx->sdl.deviceIDCapture, 0);
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextEx->sdl.SDL_PauseAudioDevice)(pDeviceEx->sdl.deviceIDPlayback, 0);
}
return MA_SUCCESS;
}
static ma_result ma_device_stop__sdl(ma_device* pDevice)
{
ma_device_ex* pDeviceEx = (ma_device_ex*)pDevice;
ma_context_ex* pContextEx = (ma_context_ex*)pDevice->pContext;
MA_ASSERT(pDevice != NULL);
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextEx->sdl.SDL_PauseAudioDevice)(pDeviceEx->sdl.deviceIDCapture, 1);
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
((MA_PFN_SDL_PauseAudioDevice)pContextEx->sdl.SDL_PauseAudioDevice)(pDeviceEx->sdl.deviceIDPlayback, 1);
}
return MA_SUCCESS;
}
static ma_result ma_context_uninit__sdl(ma_context* pContext)
{
ma_context_ex* pContextEx = (ma_context_ex*)pContext;
MA_ASSERT(pContext != NULL);
((MA_PFN_SDL_QuitSubSystem)pContextEx->sdl.SDL_QuitSubSystem)(MA_SDL_INIT_AUDIO);
/* Close the handle to the SDL shared object last. */
ma_dlclose(pContext, pContextEx->sdl.hSDL);
pContextEx->sdl.hSDL = NULL;
return MA_SUCCESS;
}
static ma_result ma_context_init__sdl(ma_context* pContext, ma_backend_callbacks* pCallbacks)
{
ma_context_ex* pContextEx = (ma_context_ex*)pContext;
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
};
MA_ASSERT(pContext != NULL);
/* 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) {
pContextEx->sdl.hSDL = ma_dlopen(pContext, pSDLNames[iName]);
if (pContextEx->sdl.hSDL != NULL) {
break;
}
}
if (pContextEx->sdl.hSDL == NULL) {
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. */
pContextEx->sdl.SDL_InitSubSystem = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_InitSubSystem");
pContextEx->sdl.SDL_QuitSubSystem = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_QuitSubSystem");
pContextEx->sdl.SDL_GetNumAudioDevices = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_GetNumAudioDevices");
pContextEx->sdl.SDL_GetAudioDeviceName = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_GetAudioDeviceName");
pContextEx->sdl.SDL_CloseAudioDevice = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_CloseAudioDevice");
pContextEx->sdl.SDL_OpenAudioDevice = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_OpenAudioDevice");
pContextEx->sdl.SDL_PauseAudioDevice = ma_dlsym(pContext, pContextEx->sdl.hSDL, "SDL_PauseAudioDevice");
#else
pContextEx->sdl.SDL_InitSubSystem = (ma_proc)SDL_InitSubSystem;
pContextEx->sdl.SDL_QuitSubSystem = (ma_proc)SDL_QuitSubSystem;
pContextEx->sdl.SDL_GetNumAudioDevices = (ma_proc)SDL_GetNumAudioDevices;
pContextEx->sdl.SDL_GetAudioDeviceName = (ma_proc)SDL_GetAudioDeviceName;
pContextEx->sdl.SDL_CloseAudioDevice = (ma_proc)SDL_CloseAudioDevice;
pContextEx->sdl.SDL_OpenAudioDevice = (ma_proc)SDL_OpenAudioDevice;
pContextEx->sdl.SDL_PauseAudioDevice = (ma_proc)SDL_PauseAudioDevice;
#endif /* MA_NO_RUNTIME_LINKING */
resultSDL = ((MA_PFN_SDL_InitSubSystem)pContextEx->sdl.SDL_InitSubSystem)(MA_SDL_INIT_AUDIO);
if (resultSDL != 0) {
ma_dlclose(pContext, pContextEx->sdl.hSDL);
return MA_ERROR;
}
/*
The last step is to make sure the callbacks are set properly in `pCallbacks`. Internally, miniaudio will copy these callbacks into the
context object and then use them for then on for calling into our custom backend.
*/
pCallbacks->onContextInit = ma_context_init__sdl;
pCallbacks->onContextUninit = ma_context_uninit__sdl;
pCallbacks->onContextEnumerateDevices = ma_context_enumerate_devices__sdl;
pCallbacks->onContextGetDeviceInfo = ma_context_get_device_info__sdl;
pCallbacks->onDeviceInit = ma_device_init__sdl;
pCallbacks->onDeviceUninit = ma_device_uninit__sdl;
pCallbacks->onDeviceStart = ma_device_start__sdl;
pCallbacks->onDeviceStop = ma_device_stop__sdl;
return MA_SUCCESS;
}
#endif /* MA_HAS_SDL */
/*
This is our custom backend "loader". All this does is attempts to initialize our custom backends in the order they are listed. The first
one to successfully initialize is the one that's chosen. In this example we're just listing them statically, but you can use whatever logic
you want to handle backend selection.
This is used as the onContextInit() callback in the context config.
*/
static ma_result ma_context_init__custom_loader(ma_context* pContext, ma_backend_callbacks* pCallbacks)
{
ma_result result = MA_NO_BACKEND;
/* Silence some unused parameter warnings just in case no custom backends are enabled. */
(void)pContext;
(void)pCallbacks;
/* SDL. */
#if !defined(MA_NO_SDL)
if (result != MA_SUCCESS) {
result = ma_context_init__sdl(pContext, pCallbacks);
}
#endif
/* ... plug in any other custom backends here ... */
/* If we have a success result we have initialized a backend. Otherwise we need to tell miniaudio about the error so it can skip over our custom backends. */
return result;
}
/*
Main program starts here.
*/
#define DEVICE_FORMAT ma_format_f32
#define DEVICE_CHANNELS 2
#define DEVICE_SAMPLE_RATE 48000
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->playback.channels == DEVICE_CHANNELS);
if (pDevice->type == ma_device_type_playback) {
ma_waveform* pSineWave;
pSineWave = (ma_waveform*)pDevice->pUserData;
MA_ASSERT(pSineWave != NULL);
ma_waveform_read_pcm_frames(pSineWave, pOutput, frameCount);
}
if (pDevice->type == ma_device_type_duplex) {
ma_copy_pcm_frames(pOutput, pInput, frameCount, pDevice->playback.format, pDevice->playback.channels);
}
}
int main(int argc, char** argv)
{
ma_result result;
ma_context_config contextConfig;
ma_context_ex context;
ma_device_config deviceConfig;
ma_device_ex device;
ma_waveform_config sineWaveConfig;
ma_waveform sineWave;
/*
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
};
/*
To implement a custom backend you need to implement the callbacks in the "custom" member of the context config. The only mandatory
callback required at this point is the onContextInit() callback. If you do not set the other callbacks, you must set them in
onContextInit() by setting them on the `pCallbacks` parameter.
The way we're doing it in this example enables us to easily plug in multiple custom backends. What we do is set the onContextInit()
callback to a generic "loader" function (ma_context_init__custom_loader() in this example), which then calls out to backend-specific
context initialization routines, one of which will be for SDL. That way, if for example we wanted to add support for another backend,
we don't need to touch this part of the code. Instead we add logic to ma_context_init__custom_loader() to choose the most appropriate
custom backend. That will then fill out the other callbacks appropriately.
*/
contextConfig = ma_context_config_init();
contextConfig.custom.onContextInit = ma_context_init__custom_loader;
result = ma_context_init(backends, sizeof(backends)/sizeof(backends[0]), &contextConfig, (ma_context*)&context);
if (result != MA_SUCCESS) {
return -1;
}
/* In playback mode we're just going to play a sine wave. */
sineWaveConfig = ma_waveform_config_init(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, ma_waveform_type_sine, 0.2, 220);
ma_waveform_init(&sineWaveConfig, &sineWave);
/* The device is created exactly as per normal. */
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &sineWave;
result = ma_device_init((ma_context*)&context, &deviceConfig, (ma_device*)&device);
if (result != MA_SUCCESS) {
ma_context_uninit((ma_context*)&context);
return -1;
}
printf("Device Name: %s\n", ((ma_device*)&device)->playback.name);
if (ma_device_start((ma_device*)&device) != MA_SUCCESS) {
ma_device_uninit((ma_device*)&device);
ma_context_uninit((ma_context*)&context);
return -5;
}
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(main_loop__em, 0, 1);
#else
printf("Press Enter to quit...\n");
getchar();
#endif
ma_device_uninit((ma_device*)&device);
ma_context_uninit((ma_context*)&context);
(void)argc;
(void)argv;
return 0;
}
\ No newline at end of file
...@@ -2760,6 +2760,25 @@ MA_API ma_uint32 ma_pcm_rb_get_subbuffer_offset(ma_pcm_rb* pRB, ma_uint32 subbuf ...@@ -2760,6 +2760,25 @@ MA_API ma_uint32 ma_pcm_rb_get_subbuffer_offset(ma_pcm_rb* pRB, ma_uint32 subbuf
MA_API void* ma_pcm_rb_get_subbuffer_ptr(ma_pcm_rb* pRB, ma_uint32 subbufferIndex, void* pBuffer); MA_API void* ma_pcm_rb_get_subbuffer_ptr(ma_pcm_rb* pRB, ma_uint32 subbufferIndex, void* pBuffer);
/*
The idea of the duplex ring buffer is to act as the intermediary buffer when running two asynchronous devices in a duplex set up. The
capture device writes to it, and then a playback device reads from it.
At the moment this is just a simple naive implementation, but in the future I want to implement some dynamic resampling to seamlessly
handle desyncs. Note that the API is work in progress and may change at any time in any version.
The size of the buffer is based on the capture side since that's what'll be written to the buffer. It is based on the capture period size
in frames. The internal sample rate of the capture device is also needed in order to calculate the size.
*/
typedef struct
{
ma_pcm_rb rb;
} ma_duplex_rb;
MA_API ma_result ma_duplex_rb_init(ma_uint32 inputSampleRate, ma_format captureFormat, ma_uint32 captureChannels, ma_uint32 captureSampleRate, ma_uint32 capturePeriodSizeInFrames, const ma_allocation_callbacks* pAllocationCallbacks, ma_duplex_rb* pRB);
MA_API ma_result ma_duplex_rb_uninit(ma_duplex_rb* pRB);
/************************************************************************************************************************************************************ /************************************************************************************************************************************************************
Miscellaneous Helpers Miscellaneous Helpers
...@@ -2874,6 +2893,9 @@ This section contains the APIs for device playback and capture. Here is where yo ...@@ -2874,6 +2893,9 @@ This section contains the APIs for device playback and capture. Here is where yo
#define MA_SUPPORT_WEBAUDIO #define MA_SUPPORT_WEBAUDIO
#endif #endif
/* All platforms should support custom backends. */
#define MA_SUPPORT_CUSTOM
/* Explicitly disable the Null backend for Emscripten because it uses a background thread which is not properly supported right now. */ /* Explicitly disable the Null backend for Emscripten because it uses a background thread which is not properly supported right now. */
#if !defined(MA_EMSCRIPTEN) #if !defined(MA_EMSCRIPTEN)
#define MA_SUPPORT_NULL #define MA_SUPPORT_NULL
...@@ -2919,10 +2941,19 @@ This section contains the APIs for device playback and capture. Here is where yo ...@@ -2919,10 +2941,19 @@ This section contains the APIs for device playback and capture. Here is where yo
#if !defined(MA_NO_WEBAUDIO) && defined(MA_SUPPORT_WEBAUDIO) #if !defined(MA_NO_WEBAUDIO) && defined(MA_SUPPORT_WEBAUDIO)
#define MA_ENABLE_WEBAUDIO #define MA_ENABLE_WEBAUDIO
#endif #endif
#if !defined(MA_NO_CUSTOM) && defined(MA_SUPPORT_CUSTOM)
#define MA_ENABLE_CUSTOM
#endif
#if !defined(MA_NO_NULL) && defined(MA_SUPPORT_NULL) #if !defined(MA_NO_NULL) && defined(MA_SUPPORT_NULL)
#define MA_ENABLE_NULL #define MA_ENABLE_NULL
#endif #endif
#define MA_STATE_UNINITIALIZED 0
#define MA_STATE_STOPPED 1 /* The device's default state after initialization. */
#define MA_STATE_STARTED 2 /* The device is started and is requesting and/or delivering audio data. */
#define MA_STATE_STARTING 3 /* Transitioning from a stopped state to started. */
#define MA_STATE_STOPPING 4 /* Transitioning from a started state to stopped. */
#ifdef MA_SUPPORT_WASAPI #ifdef MA_SUPPORT_WASAPI
/* We need a IMMNotificationClient object for WASAPI. */ /* We need a IMMNotificationClient object for WASAPI. */
typedef struct typedef struct
...@@ -2949,7 +2980,8 @@ typedef enum ...@@ -2949,7 +2980,8 @@ typedef enum
ma_backend_aaudio, ma_backend_aaudio,
ma_backend_opensl, ma_backend_opensl,
ma_backend_webaudio, ma_backend_webaudio,
ma_backend_null /* <-- Must always be the last item. Lowest priority, and used as the terminator for backend enumeration. */ ma_backend_custom, /* <-- Custom backend, with callbacks defined by the context config. */
ma_backend_null /* <-- Must always be the last item. Lowest priority, and used as the terminator for backend enumeration. */
} ma_backend; } ma_backend;
#define MA_BACKEND_COUNT (ma_backend_null+1) #define MA_BACKEND_COUNT (ma_backend_null+1)
...@@ -3031,14 +3063,14 @@ pDevice (in) ...@@ -3031,14 +3063,14 @@ pDevice (in)
logLevel (in) logLevel (in)
The log level. This can be one of the following: The log level. This can be one of the following:
|----------------------| +----------------------+
| Log Level | | Log Level |
|----------------------| +----------------------+
| MA_LOG_LEVEL_VERBOSE | | MA_LOG_LEVEL_VERBOSE |
| MA_LOG_LEVEL_INFO | | MA_LOG_LEVEL_INFO |
| MA_LOG_LEVEL_WARNING | | MA_LOG_LEVEL_WARNING |
| MA_LOG_LEVEL_ERROR | | MA_LOG_LEVEL_ERROR |
|----------------------| +----------------------+
message (in) message (in)
The log message. The log message.
...@@ -3110,9 +3142,22 @@ typedef union ...@@ -3110,9 +3142,22 @@ typedef union
ma_int32 aaudio; /* AAudio uses a 32-bit integer for identification. */ ma_int32 aaudio; /* AAudio uses a 32-bit integer for identification. */
ma_uint32 opensl; /* OpenSL|ES uses a 32-bit unsigned integer for identification. */ ma_uint32 opensl; /* OpenSL|ES uses a 32-bit unsigned integer for identification. */
char webaudio[32]; /* Web Audio always uses default devices for now, but if this changes it'll be a GUID. */ char webaudio[32]; /* Web Audio always uses default devices for now, but if this changes it'll be a GUID. */
union
{
int i;
char s[256];
void* p;
} custom; /* The custom backend could be anything. Give them a few options. */
int nullbackend; /* The null backend uses an integer for device IDs. */ int nullbackend; /* The null backend uses an integer for device IDs. */
} ma_device_id; } ma_device_id;
typedef struct ma_context_config ma_context_config;
typedef struct ma_device_config ma_device_config;
typedef struct ma_backend_callbacks ma_backend_callbacks;
#define MA_DATA_FORMAT_FLAG_EXCLUSIVE_MODE (1U << 1); /* If set, this is supported in exclusive mode. Otherwise not natively supported by exclusive mode. */
typedef struct typedef struct
{ {
/* Basic info. This is the only information guaranteed to be filled in during device enumeration. */ /* Basic info. This is the only information guaranteed to be filled in during device enumeration. */
...@@ -3134,9 +3179,20 @@ typedef struct ...@@ -3134,9 +3179,20 @@ typedef struct
ma_uint32 maxChannels; ma_uint32 maxChannels;
ma_uint32 minSampleRate; ma_uint32 minSampleRate;
ma_uint32 maxSampleRate; ma_uint32 maxSampleRate;
/* Experimental. Don't use these right now. */
ma_uint32 nativeDataFormatCount;
struct
{
ma_format format; /* Sample format. If set to ma_format_unknown, all sample formats are supported. */
ma_uint32 channels; /* If set to 0, all channels are supported. */
ma_uint32 sampleRate; /* If set to 0, all sample rates are supported. */
ma_uint32 flags;
} nativeDataFormats[64];
} ma_device_info; } ma_device_info;
typedef struct struct ma_device_config
{ {
ma_device_type deviceType; ma_device_type deviceType;
ma_uint32 sampleRate; ma_uint32 sampleRate;
...@@ -3201,9 +3257,131 @@ typedef struct ...@@ -3201,9 +3257,131 @@ typedef struct
{ {
ma_bool32 allowNominalSampleRateChange; /* Desktop only. When enabled, allows changing of the sample rate at the operating system level. */ ma_bool32 allowNominalSampleRateChange; /* Desktop only. When enabled, allows changing of the sample rate at the operating system level. */
} coreaudio; } coreaudio;
} ma_device_config; };
/*
The callback for handling device enumeration. This is fired from `ma_context_enumerated_devices()`.
Parameters
----------
pContext (in)
A pointer to the context performing the enumeration.
deviceType (in)
The type of the device being enumerated. This will always be either `ma_device_type_playback` or `ma_device_type_capture`.
pInfo (in)
A pointer to a `ma_device_info` containing the ID and name of the enumerated device. Note that this will not include detailed information about the device,
only basic information (ID and name). The reason for this is that it would otherwise require opening the backend device to probe for the information which
is too inefficient.
pUserData (in)
The user data pointer passed into `ma_context_enumerate_devices()`.
*/
typedef ma_bool32 (* ma_enum_devices_callback_proc)(ma_context* pContext, ma_device_type deviceType, const ma_device_info* pInfo, void* pUserData);
/*
Describes some basic details about a playback or capture device.
*/
typedef struct typedef struct
{
const ma_device_id* pDeviceID;
ma_share_mode shareMode;
ma_format format;
ma_uint32 channels;
ma_uint32 sampleRate;
ma_channel channelMap[MA_MAX_CHANNELS];
ma_uint32 periodSizeInFrames;
ma_uint32 periodSizeInMilliseconds;
ma_uint32 periodCount;
} ma_device_descriptor;
/*
These are the callbacks required to be implemented for a backend. These callbacks are grouped into two parts: context and device. There is one context
to many devices. A device is created from a context.
The general flow goes like this:
1) A context is created with `onContextInit()`
1a) Available devices can be enumerated with `onContextEnumerateDevices()` if required.
1b) Detailed information about a device can be queried with `onContextGetDeviceInfo()` if required.
2) A device is created from the context that was created in the first step using `onDeviceInit()`, and optionally a device ID that was
selected from device enumeration via `onContextEnumerateDevices()`.
3) A device is started or stopped with `onDeviceStart()` / `onDeviceStop()`
4) Data is delivered to and from the device by the backend. This is always done based on the native format returned by the prior call
to `onDeviceInit()`. Conversion between the device's native format and the format requested by the application will be handled by
miniaudio internally.
Initialization of the context is quite simple. You need to do any necessary initialization of internal objects and then output the
callbacks defined in this structure.
Once the context has been initialized you can initialize a device. Before doing so, however, the application may want to know which
physical devices are available. This is where `onContextEnumerateDevices()` comes in. This is fairly simple. For each device, fire the
given callback with, at a minimum, the basic information filled out in `ma_device_info`. When the callback returns `MA_FALSE`, enumeration
needs to stop and the `onContextEnumerateDevices()` function return with a success code.
Detailed device information can be retrieved from a device ID using `onContextGetDeviceInfo()`. This takes as input the device type and ID,
and on output returns detailed information about the device in `ma_device_info`. The `onContextGetDeviceInfo()` callback must handle the
case when the device ID is NULL, in which case information about the default device needs to be retrieved.
Once the context has been created and the device ID retrieved (if using anything other than the default device), the device can be created.
This is a little bit more complicated than initialization of the context due to it's more complicated configuration. When initializing a
device, a duplex device may be requested. This means a separate data format needs to be specified for both playback and capture. On input,
the data format is set to what the application wants. On output it's set to the native format which should match as closely as possible to
the requested format. The conversion between the format requested by the application and the device's native format will be handled
internally by miniaudio.
On input, if the sample format is set to `ma_format_unknown`, the backend is free to use whatever sample format it desires, so long as it's
supported by miniaudio. When the channel count is set to 0, the backend should use the device's native channel count. The same applies for
sample rate. For the channel map, the default should be used when `ma_channel_map_blank()` returns true (all channels set to
`MA_CHANNEL_NONE`). On input, the `periodSizeInFrames` or `periodSizeInMilliseconds` option should always be set. The backend should
inspect both of these variables. If `periodSizeInFrames` is set, it should take priority, otherwise it needs to be derived from the period
size in milliseconds (`periodSizeInMilliseconds`) and the sample rate, keeping in mind that the sample rate may be 0, in which case the
sample rate will need to be determined before calculating the period size in frames. On output, all members of the `ma_device_data_format`
object should be set to a valid value, except for `periodSizeInMilliseconds` which is optional (`periodSizeInFrames` *must* be set).
Starting and stopping of the device is done with `onDeviceStart()` and `onDeviceStop()` and should be self-explanatory. If the backend uses
asynchronous reading and writing, `onDeviceStart()` is optional, so long as the device is automatically started in `onDeviceWrite()`.
The handling of data delivery between the application and the device is the most complicated part of the process. To make this a bit
easier, some helper callbacks are available. If the backend uses a blocking read/write style of API, the `onDeviceRead()` and
`onDeviceWrite()` callbacks can optionally be implemented. These are blocking and work just like reading and writing from a file. If the
backend uses a callback for data delivery, that callback must call `ma_device_handle_backend_data_callback()` from within it's callback.
This allows miniaudio to then process any necessary data conversion and then pass it to the miniaudio data callback.
If the backend requires absolute flexibility with it's data delivery, it can optionally implement the `onDeviceWorkerThread()` callback
which will allow it to implement the logic that will run on the audio thread. This is much more advanced and is completely optional.
The audio thread follows this general flow:
1) Start the device before entering the main loop.
2) Run data delivery logic in a loop while `ma_device_get_state() == MA_STATE_STARTED` and no errors have been encounted.
3) Stop thd device after leaving the main loop.
The invocation of the `onDeviceAudioThread()` callback will be handled by miniaudio. When you start the device, miniaudio will fire this
callback. When the device is stopped, the `ma_device_get_state() == MA_STATE_STARTED` condition will fail and the loop will be terminated
which will then fall through to the part that stops the device. For an example on how to implement the `onDeviceAudioThread()` callback,
look at `ma_device_audio_thread__default_read_write()`.
*/
struct ma_backend_callbacks
{
ma_result (* onContextInit)(ma_context* pContext, ma_backend_callbacks* pCallbacks);
ma_result (* onContextUninit)(ma_context* pContext);
ma_result (* onContextEnumerateDevices)(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData);
ma_result (* onContextGetDeviceInfo)(ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, ma_device_info* pDeviceInfo);
ma_result (* onDeviceInit)(ma_device* pDevice, ma_device_type deviceType, ma_device_descriptor* pDescriptorPlayback, ma_device_descriptor* pDescriptorCapture);
ma_result (* onDeviceUninit)(ma_device* pDevice);
ma_result (* onDeviceStart)(ma_device* pDevice);
ma_result (* onDeviceStop)(ma_device* pDevice);
ma_result (* onDeviceRead)(ma_device* pDevice, void* pFrames, ma_uint32 frameCount, ma_uint32* pFramesRead);
ma_result (* onDeviceWrite)(ma_device* pDevice, const void* pFrames, ma_uint32 frameCount, ma_uint32* pFramesWritten);
ma_result (* onDeviceAudioThread)(ma_device* pDevice);
};
struct ma_context_config
{ {
ma_log_proc logCallback; ma_log_proc logCallback;
ma_thread_priority threadPriority; ma_thread_priority threadPriority;
...@@ -3232,29 +3410,8 @@ typedef struct ...@@ -3232,29 +3410,8 @@ typedef struct
const char* pClientName; const char* pClientName;
ma_bool32 tryStartServer; ma_bool32 tryStartServer;
} jack; } jack;
} ma_context_config; ma_backend_callbacks custom;
};
/*
The callback for handling device enumeration. This is fired from `ma_context_enumerated_devices()`.
Parameters
----------
pContext (in)
A pointer to the context performing the enumeration.
deviceType (in)
The type of the device being enumerated. This will always be either `ma_device_type_playback` or `ma_device_type_capture`.
pInfo (in)
A pointer to a `ma_device_info` containing the ID and name of the enumerated device. Note that this will not include detailed information about the device,
only basic information (ID and name). The reason for this is that it would otherwise require opening the backend device to probe for the information which
is too inefficient.
pUserData (in)
The user data pointer passed into `ma_context_enumerate_devices()`.
*/
typedef ma_bool32 (* ma_enum_devices_callback_proc)(ma_context* pContext, ma_device_type deviceType, const ma_device_info* pInfo, void* pUserData);
struct ma_context struct ma_context
{ {
...@@ -3273,7 +3430,6 @@ struct ma_context ...@@ -3273,7 +3430,6 @@ struct ma_context
ma_bool32 isBackendAsynchronous : 1; /* Set when the context is initialized. Set to 1 for asynchronous backends such as Core Audio and JACK. Do not modify. */ ma_bool32 isBackendAsynchronous : 1; /* Set when the context is initialized. Set to 1 for asynchronous backends such as Core Audio and JACK. Do not modify. */
ma_result (* onUninit )(ma_context* pContext); ma_result (* onUninit )(ma_context* pContext);
ma_bool32 (* onDeviceIDEqual )(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1);
ma_result (* onEnumDevices )(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData); /* Return false from the callback to stop enumeration. */ ma_result (* onEnumDevices )(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData); /* Return false from the callback to stop enumeration. */
ma_result (* onGetDeviceInfo )(ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, ma_share_mode shareMode, ma_device_info* pDeviceInfo); ma_result (* onGetDeviceInfo )(ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, ma_share_mode shareMode, ma_device_info* pDeviceInfo);
ma_result (* onDeviceInit )(ma_context* pContext, const ma_device_config* pConfig, ma_device* pDevice); ma_result (* onDeviceInit )(ma_context* pContext, const ma_device_config* pConfig, ma_device* pDevice);
...@@ -3597,6 +3753,9 @@ struct ma_context ...@@ -3597,6 +3753,9 @@ struct ma_context
int _unused; int _unused;
} webaudio; } webaudio;
#endif #endif
#ifdef MA_SUPPORT_CUSTOM
ma_backend_callbacks custom;
#endif
#ifdef MA_SUPPORT_NULL #ifdef MA_SUPPORT_NULL
struct struct
{ {
...@@ -3675,6 +3834,7 @@ struct ma_device ...@@ -3675,6 +3834,7 @@ struct ma_device
ma_bool32 noPreZeroedOutputBuffer : 1; ma_bool32 noPreZeroedOutputBuffer : 1;
ma_bool32 noClip : 1; ma_bool32 noClip : 1;
volatile float masterVolumeFactor; /* Volatile so we can use some thread safety when applying volume to periods. */ volatile float masterVolumeFactor; /* Volatile so we can use some thread safety when applying volume to periods. */
ma_duplex_rb duplexRB; /* Intermediary buffer for duplex device on asynchronous backends. */
struct struct
{ {
ma_resample_algorithm algorithm; ma_resample_algorithm algorithm;
...@@ -4925,7 +5085,63 @@ See Also ...@@ -4925,7 +5085,63 @@ See Also
ma_device_start() ma_device_start()
ma_device_stop() ma_device_stop()
*/ */
MA_API ma_bool32 ma_device_is_started(ma_device* pDevice); MA_API ma_bool32 ma_device_is_started(const ma_device* pDevice);
/*
Retrieves the state of the device.
Parameters
----------
pDevice (in)
A pointer to the device whose state is being retrieved.
Return Value
------------
The current state of the device. The return value will be one of the following:
+------------------------+------------------------------------------------------------------------------+
| MA_STATE_UNINITIALIZED | Will only be returned if the device is in the middle of initialization. |
+------------------------+------------------------------------------------------------------------------+
| MA_STATE_STOPPED | The device is stopped. The initial state of the device after initialization. |
+------------------------+------------------------------------------------------------------------------+
| MA_STATE_STARTED | The device started and requesting and/or delivering audio data. |
+------------------------+------------------------------------------------------------------------------+
| MA_STATE_STARTING | The device is in the process of starting. |
+------------------------+------------------------------------------------------------------------------+
| MA_STATE_STOPPING | The device is in the process of stopping. |
+------------------------+------------------------------------------------------------------------------+
Thread Safety
-------------
Safe. This is implemented as a simple accessor. Note that if the device is started or stopped at the same time as this function is called,
there's a possibility the return value could be out of sync. See remarks.
Callback Safety
---------------
Safe. This is implemented as a simple accessor.
Remarks
-------
The general flow of a devices state goes like this:
```
ma_device_init() -> MA_STATE_UNINITIALIZED -> MA_STATE_STOPPED
ma_device_start() -> MA_STATE_STARTING -> MA_STATE_STARTED
ma_device_stop() -> MA_STATE_STOPPING -> MA_STATE_STOPPED
```
When the state of the device is changed with `ma_device_start()` or `ma_device_stop()` at this same time as this function is called, the
value returned by this function could potentially be out of sync. If this is significant to your program you need to implement your own
synchronization.
*/
MA_API ma_uint32 ma_device_get_state(const ma_device* pDevice);
/* /*
Sets the master volume factor for the device. Sets the master volume factor for the device.
...@@ -5109,6 +5325,56 @@ ma_device_get_master_volume() ...@@ -5109,6 +5325,56 @@ ma_device_get_master_volume()
MA_API ma_result ma_device_get_master_gain_db(ma_device* pDevice, float* pGainDB); MA_API ma_result ma_device_get_master_gain_db(ma_device* pDevice, float* pGainDB);
/*
Called from the data callback of asynchronous backends to allow miniaudio to process the data and fire the miniaudio data callback.
Parameters
----------
pDevice (in)
A pointer to device whose processing the data callback.
pOutput (out)
A pointer to the buffer that will receive the output PCM frame data. On a playback device this must not be NULL. On a duplex device
this can be NULL, in which case pInput must not be NULL.
pInput (in)
A pointer to the buffer containing input PCM frame data. On a capture device this must not be NULL. On a duplex device this can be
NULL, in which case `pOutput` must not be NULL.
frameCount (in)
The number of frames being processed.
Return Value
------------
MA_SUCCESS if successful; any other result code otherwise.
Thread Safety
-------------
This function should only ever be called from the internal data callback of the backend. It is safe to call this simultaneously between a
playback and capture device in duplex setups.
Callback Safety
---------------
Do not call this from the miniaudio data callback. It should only ever be called from the internal data callback of the backend.
Remarks
-------
If both `pOutput` and `pInput` are NULL, and error will be returned. In duplex scenarios, both `pOutput` and `pInput` can be non-NULL, in
which case `pInput` will be processed first, followed by `pOutput`.
If you are implementing a custom backend, and that backend uses a callback for data delivery, you'll need to call this from inside that
callback.
*/
MA_API ma_result ma_device_handle_backend_data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);
/* /*
Retrieves a friendly name for a backend. Retrieves a friendly name for a backend.
*/ */
...@@ -5125,14 +5391,14 @@ Retrieves compile-time enabled backends. ...@@ -5125,14 +5391,14 @@ Retrieves compile-time enabled backends.
Parameters Parameters
---------- ----------
pBackends(out, optional) pBackends (out, optional)
A pointer to the buffer that will receive the enabled backends. Set to NULL to retrieve the backend count. Setting A pointer to the buffer that will receive the enabled backends. Set to NULL to retrieve the backend count. Setting
the capacity of the buffer to `MA_BUFFER_COUNT` will guarantee it's large enough for all backends. the capacity of the buffer to `MA_BUFFER_COUNT` will guarantee it's large enough for all backends.
backendCap(in) backendCap (in)
The capacity of the `pBackends` buffer. The capacity of the `pBackends` buffer.
pBackendCount(out) pBackendCount (out)
A pointer to the variable that will receive the enabled backend count. A pointer to the variable that will receive the enabled backend count.
...@@ -9340,6 +9606,7 @@ MA_API ma_result ma_semaphore_release(ma_semaphore* pSemaphore) ...@@ -9340,6 +9606,7 @@ MA_API ma_result ma_semaphore_release(ma_semaphore* pSemaphore)
#endif /* MA_NO_THREADING */ #endif /* MA_NO_THREADING */
/************************************************************************************************************************************************************ /************************************************************************************************************************************************************
************************************************************************************************************************************************************* *************************************************************************************************************************************************************
...@@ -9445,6 +9712,9 @@ certain unused functions and variables can be excluded from the build to avoid w ...@@ -9445,6 +9712,9 @@ certain unused functions and variables can be excluded from the build to avoid w
#ifdef MA_ENABLE_WEBAUDIO #ifdef MA_ENABLE_WEBAUDIO
#define MA_HAS_WEBAUDIO #define MA_HAS_WEBAUDIO
#endif #endif
#ifdef MA_ENABLE_CUSTOM
#define MA_HAS_CUSTOM
#endif
#ifdef MA_ENABLE_NULL #ifdef MA_ENABLE_NULL
#define MA_HAS_NULL /* Everything supports the null backend. */ #define MA_HAS_NULL /* Everything supports the null backend. */
#endif #endif
...@@ -9466,6 +9736,7 @@ MA_API const char* ma_get_backend_name(ma_backend backend) ...@@ -9466,6 +9736,7 @@ MA_API const char* ma_get_backend_name(ma_backend backend)
case ma_backend_aaudio: return "AAudio"; case ma_backend_aaudio: return "AAudio";
case ma_backend_opensl: return "OpenSL|ES"; case ma_backend_opensl: return "OpenSL|ES";
case ma_backend_webaudio: return "Web Audio"; case ma_backend_webaudio: return "Web Audio";
case ma_backend_custom: return "Custom";
case ma_backend_null: return "Null"; case ma_backend_null: return "Null";
default: return "Unknown"; default: return "Unknown";
} }
...@@ -9557,6 +9828,12 @@ MA_API ma_bool32 ma_is_backend_enabled(ma_backend backend) ...@@ -9557,6 +9828,12 @@ MA_API ma_bool32 ma_is_backend_enabled(ma_backend backend)
#else #else
return MA_FALSE; return MA_FALSE;
#endif #endif
case ma_backend_custom:
#if defined(MA_HAS_CUSTOM)
return MA_TRUE;
#else
return MA_FALSE;
#endif
case ma_backend_null: case ma_backend_null:
#if defined(MA_HAS_NULL) #if defined(MA_HAS_NULL)
return MA_TRUE; return MA_TRUE;
...@@ -9619,6 +9896,7 @@ MA_API ma_bool32 ma_is_loopback_supported(ma_backend backend) ...@@ -9619,6 +9896,7 @@ MA_API ma_bool32 ma_is_loopback_supported(ma_backend backend)
case ma_backend_aaudio: return MA_FALSE; case ma_backend_aaudio: return MA_FALSE;
case ma_backend_opensl: return MA_FALSE; case ma_backend_opensl: return MA_FALSE;
case ma_backend_webaudio: return MA_FALSE; case ma_backend_webaudio: return MA_FALSE;
case ma_backend_custom: return MA_FALSE; /* <-- Will depend on the implementation of the backend. */
case ma_backend_null: return MA_FALSE; case ma_backend_null: return MA_FALSE;
default: return MA_FALSE; default: return MA_FALSE;
} }
...@@ -9803,12 +10081,6 @@ typedef LONG (WINAPI * MA_PFN_RegQueryValueExA)(HKEY hKey, LPCSTR lpValueName, L ...@@ -9803,12 +10081,6 @@ typedef LONG (WINAPI * MA_PFN_RegQueryValueExA)(HKEY hKey, LPCSTR lpValueName, L
#endif #endif
#define MA_STATE_UNINITIALIZED 0
#define MA_STATE_STOPPED 1 /* The device's default state after initialization. */
#define MA_STATE_STARTED 2 /* The worker thread is in it's main loop waiting for the driver to request or deliver audio data. */
#define MA_STATE_STARTING 3 /* Transitioning from a stopped state to started. */
#define MA_STATE_STOPPING 4 /* Transitioning from a started state to stopped. */
#define MA_DEFAULT_PLAYBACK_DEVICE_NAME "Default Playback Device" #define MA_DEFAULT_PLAYBACK_DEVICE_NAME "Default Playback Device"
#define MA_DEFAULT_CAPTURE_DEVICE_NAME "Default Capture Device" #define MA_DEFAULT_CAPTURE_DEVICE_NAME "Default Capture Device"
...@@ -10542,12 +10814,6 @@ static MA_INLINE void ma_device__set_state(ma_device* pDevice, ma_uint32 newStat ...@@ -10542,12 +10814,6 @@ static MA_INLINE void ma_device__set_state(ma_device* pDevice, ma_uint32 newStat
c89atomic_exchange_32(&pDevice->state, newState); c89atomic_exchange_32(&pDevice->state, newState);
} }
/* A helper for getting the state of the device. */
static MA_INLINE ma_uint32 ma_device__get_state(ma_device* pDevice)
{
return pDevice->state;
}
#ifdef MA_WIN32 #ifdef MA_WIN32
GUID MA_GUID_KSDATAFORMAT_SUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}; GUID MA_GUID_KSDATAFORMAT_SUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
...@@ -10557,79 +10823,500 @@ static MA_INLINE ma_uint32 ma_device__get_state(ma_device* pDevice) ...@@ -10557,79 +10823,500 @@ static MA_INLINE ma_uint32 ma_device__get_state(ma_device* pDevice)
#endif #endif
typedef struct
MA_API ma_uint32 ma_get_format_priority_index(ma_format format) /* Lower = better. */
{ {
ma_device_type deviceType; ma_uint32 i;
const ma_device_id* pDeviceID; for (i = 0; i < ma_countof(g_maFormatPriorities); ++i) {
char* pName; if (g_maFormatPriorities[i] == format) {
size_t nameBufferSize; return i;
ma_bool32 foundDevice; }
} ma_context__try_get_device_name_by_id__enum_callback_data; }
/* Getting here means the format could not be found or is equal to ma_format_unknown. */
return (ma_uint32)-1;
}
static ma_result ma_device__post_init_setup(ma_device* pDevice, ma_device_type deviceType);
static ma_bool32 ma_context__try_get_device_name_by_id__enum_callback(ma_context* pContext, ma_device_type deviceType, const ma_device_info* pDeviceInfo, void* pUserData) static ma_bool32 ma_device_descriptor_is_valid(const ma_device_descriptor* pDeviceDescriptor)
{ {
ma_context__try_get_device_name_by_id__enum_callback_data* pData = (ma_context__try_get_device_name_by_id__enum_callback_data*)pUserData; if (pDeviceDescriptor == NULL) {
MA_ASSERT(pData != NULL); return MA_FALSE;
}
if (pData->deviceType == deviceType) { if (pDeviceDescriptor->format == ma_format_unknown) {
if (pContext->onDeviceIDEqual(pContext, pData->pDeviceID, &pDeviceInfo->id)) { return MA_FALSE;
ma_strncpy_s(pData->pName, pData->nameBufferSize, pDeviceInfo->name, (size_t)-1); }
pData->foundDevice = MA_TRUE;
if (pDeviceDescriptor->channels < MA_MIN_CHANNELS || pDeviceDescriptor->channels > MA_MAX_CHANNELS) {
return MA_FALSE;
}
if (pDeviceDescriptor->sampleRate == 0) {
return MA_FALSE;
}
return MA_TRUE;
}
/* TODO: Remove the pCallbacks parameter when we move all backends to the new callbacks system, at which time we can just reference the context directly. */
static ma_result ma_device_audio_thread__default_read_write(ma_device* pDevice, ma_backend_callbacks* pCallbacks)
{
ma_result result = MA_SUCCESS;
ma_bool32 exitLoop = MA_FALSE;
ma_uint8 capturedDeviceData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE];
ma_uint8 playbackDeviceData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE];
ma_uint32 capturedDeviceDataCapInFrames = sizeof(capturedDeviceData) / ma_get_bytes_per_frame(pDevice->capture.internalFormat, pDevice->capture.internalChannels);
ma_uint32 playbackDeviceDataCapInFrames = sizeof(playbackDeviceData) / ma_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels);
MA_ASSERT(pDevice != NULL);
/* Just some quick validation on the device type and the available callbacks. */
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex || pDevice->type == ma_device_type_loopback) {
if (pCallbacks->onDeviceRead == NULL) {
return MA_NOT_IMPLEMENTED;
} }
} }
return !pData->foundDevice; if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex) {
if (pCallbacks->onDeviceWrite == NULL) {
return MA_NOT_IMPLEMENTED;
}
}
/* The device needs to be started immediately. */
if (pCallbacks->onDeviceStart != NULL) {
result = pCallbacks->onDeviceStart(pDevice);
if (result != MA_SUCCESS) {
return result;
}
} else {
/* Getting here means no start callback is defined. This is OK, as the backend may auto-start the device when reading or writing data. */
}
while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type) {
case ma_device_type_duplex:
{
/* The process is: onDeviceRead() -> convert -> callback -> convert -> onDeviceWrite() */
ma_uint32 totalCapturedDeviceFramesProcessed = 0;
ma_uint32 capturedDevicePeriodSizeInFrames = ma_min(pDevice->capture.internalPeriodSizeInFrames, pDevice->playback.internalPeriodSizeInFrames);
while (totalCapturedDeviceFramesProcessed < capturedDevicePeriodSizeInFrames) {
ma_uint32 capturedDeviceFramesRemaining;
ma_uint32 capturedDeviceFramesProcessed;
ma_uint32 capturedDeviceFramesToProcess;
ma_uint32 capturedDeviceFramesToTryProcessing = capturedDevicePeriodSizeInFrames - totalCapturedDeviceFramesProcessed;
if (capturedDeviceFramesToTryProcessing > capturedDeviceDataCapInFrames) {
capturedDeviceFramesToTryProcessing = capturedDeviceDataCapInFrames;
}
result = pCallbacks->onDeviceRead(pDevice, capturedDeviceData, capturedDeviceFramesToTryProcessing, &capturedDeviceFramesToProcess);
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
capturedDeviceFramesRemaining = capturedDeviceFramesToProcess;
capturedDeviceFramesProcessed = 0;
/* At this point we have our captured data in device format and we now need to convert it to client format. */
for (;;) {
ma_uint8 capturedClientData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE];
ma_uint8 playbackClientData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE];
ma_uint32 capturedClientDataCapInFrames = sizeof(capturedClientData) / ma_get_bytes_per_frame(pDevice->capture.format, pDevice->capture.channels);
ma_uint32 playbackClientDataCapInFrames = sizeof(playbackClientData) / ma_get_bytes_per_frame(pDevice->playback.format, pDevice->playback.channels);
ma_uint64 capturedClientFramesToProcessThisIteration = ma_min(capturedClientDataCapInFrames, playbackClientDataCapInFrames);
ma_uint64 capturedDeviceFramesToProcessThisIteration = capturedDeviceFramesRemaining;
ma_uint8* pRunningCapturedDeviceFrames = ma_offset_ptr(capturedDeviceData, capturedDeviceFramesProcessed * ma_get_bytes_per_frame(pDevice->capture.internalFormat, pDevice->capture.internalChannels));
/* Convert capture data from device format to client format. */
result = ma_data_converter_process_pcm_frames(&pDevice->capture.converter, pRunningCapturedDeviceFrames, &capturedDeviceFramesToProcessThisIteration, capturedClientData, &capturedClientFramesToProcessThisIteration);
if (result != MA_SUCCESS) {
break;
}
/*
If we weren't able to generate any output frames it must mean we've exhaused all of our input. The only time this would not be the case is if capturedClientData was too small
which should never be the case when it's of the size MA_DATA_CONVERTER_STACK_BUFFER_SIZE.
*/
if (capturedClientFramesToProcessThisIteration == 0) {
break;
}
ma_device__on_data(pDevice, playbackClientData, capturedClientData, (ma_uint32)capturedClientFramesToProcessThisIteration); /* Safe cast .*/
capturedDeviceFramesProcessed += (ma_uint32)capturedDeviceFramesToProcessThisIteration; /* Safe cast. */
capturedDeviceFramesRemaining -= (ma_uint32)capturedDeviceFramesToProcessThisIteration; /* Safe cast. */
/* At this point the playbackClientData buffer should be holding data that needs to be written to the device. */
for (;;) {
ma_uint64 convertedClientFrameCount = capturedClientFramesToProcessThisIteration;
ma_uint64 convertedDeviceFrameCount = playbackDeviceDataCapInFrames;
result = ma_data_converter_process_pcm_frames(&pDevice->playback.converter, playbackClientData, &convertedClientFrameCount, playbackDeviceData, &convertedDeviceFrameCount);
if (result != MA_SUCCESS) {
break;
}
result = pCallbacks->onDeviceWrite(pDevice, playbackDeviceData, (ma_uint32)convertedDeviceFrameCount, NULL); /* Safe cast. */
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
capturedClientFramesToProcessThisIteration -= (ma_uint32)convertedClientFrameCount; /* Safe cast. */
if (capturedClientFramesToProcessThisIteration == 0) {
break;
}
}
/* In case an error happened from ma_device_write__null()... */
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
}
totalCapturedDeviceFramesProcessed += capturedDeviceFramesProcessed;
}
} break;
case ma_device_type_capture:
case ma_device_type_loopback:
{
ma_uint32 periodSizeInFrames = pDevice->capture.internalPeriodSizeInFrames;
ma_uint32 framesReadThisPeriod = 0;
while (framesReadThisPeriod < periodSizeInFrames) {
ma_uint32 framesRemainingInPeriod = periodSizeInFrames - framesReadThisPeriod;
ma_uint32 framesProcessed;
ma_uint32 framesToReadThisIteration = framesRemainingInPeriod;
if (framesToReadThisIteration > capturedDeviceDataCapInFrames) {
framesToReadThisIteration = capturedDeviceDataCapInFrames;
}
result = pCallbacks->onDeviceRead(pDevice, capturedDeviceData, framesToReadThisIteration, &framesProcessed);
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
ma_device__send_frames_to_client(pDevice, framesProcessed, capturedDeviceData);
framesReadThisPeriod += framesProcessed;
}
} break;
case ma_device_type_playback:
{
/* We write in chunks of the period size, but use a stack allocated buffer for the intermediary. */
ma_uint32 periodSizeInFrames = pDevice->playback.internalPeriodSizeInFrames;
ma_uint32 framesWrittenThisPeriod = 0;
while (framesWrittenThisPeriod < periodSizeInFrames) {
ma_uint32 framesRemainingInPeriod = periodSizeInFrames - framesWrittenThisPeriod;
ma_uint32 framesProcessed;
ma_uint32 framesToWriteThisIteration = framesRemainingInPeriod;
if (framesToWriteThisIteration > playbackDeviceDataCapInFrames) {
framesToWriteThisIteration = playbackDeviceDataCapInFrames;
}
ma_device__read_frames_from_client(pDevice, framesToWriteThisIteration, playbackDeviceData);
result = pCallbacks->onDeviceWrite(pDevice, playbackDeviceData, framesToWriteThisIteration, &framesProcessed);
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
framesWrittenThisPeriod += framesProcessed;
}
} break;
/* Should never get here. */
default: break;
}
}
/* We've exited the loop so we'll need to stop the device. */
if (pCallbacks->onDeviceStop != NULL) {
pCallbacks->onDeviceStop(pDevice);
}
return result;
} }
/*
Generic function for retrieving the name of a device by it's ID.
This function simply enumerates every device and then retrieves the name of the first device that has the same ID. /*******************************************************************************
*/
static ma_result ma_context__try_get_device_name_by_id(ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, char* pName, size_t nameBufferSize) Custom Backend
*******************************************************************************/
#ifdef MA_HAS_CUSTOM
static ma_result ma_context_enumerate_devices__custom(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(callback != NULL);
if (pContext->custom.onContextEnumerateDevices == NULL) {
return MA_FALSE;
}
return pContext->custom.onContextEnumerateDevices(pContext, callback, pUserData);
}
static ma_result ma_context_get_device_info__custom(ma_context* pContext, ma_device_type deviceType, const ma_device_id* pDeviceID, ma_share_mode shareMode, ma_device_info* pDeviceInfo)
{
MA_ASSERT(pContext != NULL);
(void)shareMode; /* Not using the share mode. This is something I want to remove from device info queries. */
if (pContext->custom.onContextGetDeviceInfo == NULL) {
return MA_NOT_IMPLEMENTED;
}
/* Be as safe as possible and zero out the device info just in case the backend doesn't fill it out properly. */
MA_ZERO_OBJECT(pDeviceInfo);
return pContext->custom.onContextGetDeviceInfo(pContext, deviceType, pDeviceID, pDeviceInfo);
}
static ma_result ma_context_uninit__custom(ma_context* pContext)
{
MA_ASSERT(pContext != NULL);
if (pContext->custom.onContextUninit == NULL) {
return MA_NOT_IMPLEMENTED;
}
return pContext->custom.onContextUninit(pContext);
}
static ma_result ma_device_uninit__custom(ma_device* pDevice)
{
MA_ASSERT(pDevice != NULL);
if (pDevice->pContext->custom.onDeviceUninit == NULL) {
return MA_NOT_IMPLEMENTED;
}
return pDevice->pContext->custom.onDeviceUninit(pDevice);
}
static ma_result ma_device_init__custom(ma_context* pContext, const ma_device_config* pConfig, ma_device* pDevice)
{ {
ma_result result; ma_result result;
ma_context__try_get_device_name_by_id__enum_callback_data data; ma_device_descriptor descriptorPlayback;
ma_device_descriptor descriptorCapture;
MA_ASSERT(pContext != NULL); MA_ASSERT(pContext != NULL);
MA_ASSERT(pName != NULL); MA_ASSERT(pConfig != NULL);
MA_ASSERT(pDevice != NULL);
if (pDeviceID == NULL) { if (pContext->custom.onDeviceInit == NULL) {
return MA_NO_DEVICE; return MA_NOT_IMPLEMENTED;
} }
data.deviceType = deviceType; MA_ZERO_OBJECT(&descriptorPlayback);
data.pDeviceID = pDeviceID; descriptorPlayback.pDeviceID = pConfig->playback.pDeviceID;
data.pName = pName; descriptorPlayback.shareMode = pConfig->playback.shareMode;
data.nameBufferSize = nameBufferSize; descriptorPlayback.format = pConfig->playback.format;
data.foundDevice = MA_FALSE; descriptorPlayback.channels = pConfig->playback.channels;
result = ma_context_enumerate_devices(pContext, ma_context__try_get_device_name_by_id__enum_callback, &data); descriptorPlayback.sampleRate = pConfig->sampleRate;
ma_channel_map_copy(descriptorPlayback.channelMap, pConfig->playback.channelMap, pConfig->playback.channels);
descriptorPlayback.periodSizeInFrames = pConfig->periodSizeInFrames;
descriptorPlayback.periodSizeInMilliseconds = pConfig->periodSizeInMilliseconds;
descriptorPlayback.periodCount = pConfig->periods;
MA_ZERO_OBJECT(&descriptorCapture);
descriptorCapture.pDeviceID = pConfig->capture.pDeviceID;
descriptorCapture.shareMode = pConfig->capture.shareMode;
descriptorCapture.format = pConfig->capture.format;
descriptorCapture.channels = pConfig->capture.channels;
descriptorCapture.sampleRate = pConfig->sampleRate;
ma_channel_map_copy(descriptorCapture.channelMap, pConfig->capture.channelMap, pConfig->capture.channels);
descriptorCapture.periodSizeInFrames = pConfig->periodSizeInFrames;
descriptorCapture.periodSizeInMilliseconds = pConfig->periodSizeInMilliseconds;
descriptorCapture.periodCount = pConfig->periods;
result = pContext->custom.onDeviceInit(pDevice, pConfig->deviceType, &descriptorPlayback, &descriptorCapture);
if (result != MA_SUCCESS) { if (result != MA_SUCCESS) {
return result; return result; /* Failed to initialize the device. */
} }
if (!data.foundDevice) { /*
return MA_NO_DEVICE; On output the descriptors will contain the *actual* data format of the device. We need this to know how to convert the data between
} else { the requested format and the internal format.
return MA_SUCCESS; */
if (pConfig->deviceType == ma_device_type_capture || pConfig->deviceType == ma_device_type_duplex || pConfig->deviceType == ma_device_type_loopback) {
ma_device_info deviceInfo; /* For retrieving the name. */
if (!ma_device_descriptor_is_valid(&descriptorCapture)) {
ma_device_uninit__custom(pDevice);
return MA_INVALID_ARGS;
}
pDevice->capture.internalFormat = descriptorCapture.format;
pDevice->capture.internalChannels = descriptorCapture.channels;
pDevice->capture.internalSampleRate = descriptorCapture.sampleRate;
ma_channel_map_copy(pDevice->capture.internalChannelMap, descriptorCapture.channelMap, descriptorCapture.channels);
pDevice->capture.internalPeriodSizeInFrames = descriptorCapture.periodSizeInFrames;
pDevice->capture.internalPeriods = descriptorCapture.periodCount;
if (pDevice->capture.internalPeriodSizeInFrames == 0) {
pDevice->capture.internalPeriodSizeInFrames = ma_calculate_buffer_size_in_frames_from_milliseconds(descriptorCapture.periodSizeInMilliseconds, descriptorCapture.sampleRate);
}
/* The name of the device can be retrieved from device info. This may be temporary and replaced with a `ma_device_get_info()` instead. */
result = ma_context_get_device_info(pContext, ma_device_type_capture, descriptorCapture.pDeviceID, descriptorCapture.shareMode, &deviceInfo);
if (result != MA_SUCCESS) {
/* We failed to retrieve the device info. Fall back to a default name. */
if (descriptorCapture.pDeviceID == NULL) {
ma_strncpy_s(pDevice->capture.name, sizeof(pDevice->capture.name), MA_DEFAULT_CAPTURE_DEVICE_NAME, (size_t)-1);
} else {
ma_strncpy_s(pDevice->capture.name, sizeof(pDevice->capture.name), "Capture Device", (size_t)-1);
}
}
}
if (pConfig->deviceType == ma_device_type_playback || pConfig->deviceType == ma_device_type_duplex) {
ma_device_info deviceInfo; /* For retrieving the name. */
if (!ma_device_descriptor_is_valid(&descriptorPlayback)) {
ma_device_uninit__custom(pDevice);
return MA_INVALID_ARGS;
}
pDevice->playback.internalFormat = descriptorPlayback.format;
pDevice->playback.internalChannels = descriptorPlayback.channels;
pDevice->playback.internalSampleRate = descriptorPlayback.sampleRate;
ma_channel_map_copy(pDevice->playback.internalChannelMap, descriptorPlayback.channelMap, descriptorPlayback.channels);
pDevice->playback.internalPeriodSizeInFrames = descriptorPlayback.periodSizeInFrames;
pDevice->playback.internalPeriods = descriptorPlayback.periodCount;
if (pDevice->playback.internalPeriodSizeInFrames == 0) {
pDevice->playback.internalPeriodSizeInFrames = ma_calculate_buffer_size_in_frames_from_milliseconds(descriptorPlayback.periodSizeInMilliseconds, descriptorPlayback.sampleRate);
}
/* The name of the device can be retrieved from device info. This may be temporary and replaced with a `ma_device_get_info(pDevice, deviceType)` instead. */
result = ma_context_get_device_info(pContext, ma_device_type_playback, descriptorPlayback.pDeviceID, descriptorPlayback.shareMode, &deviceInfo);
if (result != MA_SUCCESS) {
/* We failed to retrieve the device info. Fall back to a default name. */
if (descriptorPlayback.pDeviceID == NULL) {
ma_strncpy_s(pDevice->playback.name, sizeof(pDevice->playback.name), MA_DEFAULT_PLAYBACK_DEVICE_NAME, (size_t)-1);
} else {
ma_strncpy_s(pDevice->playback.name, sizeof(pDevice->playback.name), "Playback Device", (size_t)-1);
}
}
} }
/* If the backend is asynchronous and the device is duplex, we'll need an intermediary ring buffer. */
if (pConfig->deviceType == ma_device_type_duplex) {
if (pContext->custom.onDeviceRead == NULL && pContext->custom.onDeviceWrite == NULL && pContext->custom.onDeviceAudioThread == NULL) {
result = ma_duplex_rb_init(pDevice->sampleRate, pDevice->capture.internalFormat, pDevice->capture.internalChannels, pDevice->capture.internalSampleRate, pDevice->capture.internalPeriodSizeInFrames, &pDevice->pContext->allocationCallbacks, &pDevice->duplexRB);
if (result != MA_SUCCESS) {
ma_device_uninit__custom(pDevice);
return result;
}
}
}
return MA_SUCCESS;
} }
static ma_result ma_device_start__custom(ma_device* pDevice)
{
MA_ASSERT(pDevice != NULL);
if (pDevice->pContext->custom.onDeviceStart == NULL) {
return MA_NOT_IMPLEMENTED;
}
MA_API ma_uint32 ma_get_format_priority_index(ma_format format) /* Lower = better. */ return pDevice->pContext->custom.onDeviceStart(pDevice);
}
static ma_result ma_device_stop__custom(ma_device* pDevice)
{ {
ma_uint32 i; MA_ASSERT(pDevice != NULL);
for (i = 0; i < ma_countof(g_maFormatPriorities); ++i) {
if (g_maFormatPriorities[i] == format) { if (pDevice->pContext->custom.onDeviceStop == NULL) {
return i; return MA_NOT_IMPLEMENTED;
}
return pDevice->pContext->custom.onDeviceStop(pDevice);
}
static ma_result ma_device_audio_thread__custom(ma_device* pDevice)
{
MA_ASSERT(pDevice != NULL);
if (pDevice->pContext->custom.onDeviceAudioThread == NULL) {
return MA_NOT_IMPLEMENTED; /* Should never happen, but check anyway. */
}
return pDevice->pContext->custom.onDeviceAudioThread(pDevice);
}
static ma_result ma_device_audio_thread__read_write__custom(ma_device* pDevice)
{
return ma_device_audio_thread__default_read_write(pDevice, &pDevice->pContext->custom);
}
static ma_result ma_context_init__custom(const ma_context_config* pConfig, ma_context* pContext)
{
ma_result result;
MA_ASSERT(pContext != NULL);
MA_ASSERT(pConfig != NULL);
/* We need to ensure we have the necessary callbacks. */
if (pConfig->custom.onContextInit == NULL) {
return MA_NO_BACKEND; /* Need a context initialization callback. When set to NULL it means a custom backend is not defined. */
}
/* Set the custom callbacks before firing the backend's context initialization routine just in case the backend wants to reference them for whatever reason. */
pContext->custom = pConfig->custom;
/* Initialize the context first. If this fails we need to abort. */
result = pConfig->custom.onContextInit(pContext, &pContext->custom);
if (result != MA_SUCCESS) {
return result;
}
/* At this point the context should be initialized. We can now set up the callbacks for miniaudio's use internally. */
pContext->onUninit = ma_context_uninit__custom;
pContext->onEnumDevices = ma_context_enumerate_devices__custom;
pContext->onGetDeviceInfo = ma_context_get_device_info__custom;
pContext->onDeviceInit = ma_device_init__custom;
pContext->onDeviceUninit = ma_device_uninit__custom;
pContext->onDeviceStart = ma_device_start__custom;
pContext->onDeviceStop = ma_device_stop__custom;
/*
For now the context needs to be marked as asynchronous. This is required so that miniaudio knows how to handle data delivery and thread
management for the device, but the requirement for the backend itself to set this property will probably be removed in the future.
*/
if (pContext->custom.onDeviceRead == NULL && pContext->custom.onDeviceWrite == NULL) {
if (pContext->custom.onDeviceAudioThread == NULL) {
pContext->onDeviceMainLoop = NULL; /* Backend is asynchronous and expected to call ma_device_handle_backend_data_callback() from within their data callback. */
pContext->isBackendAsynchronous = MA_TRUE;
} else {
pContext->onDeviceMainLoop = ma_device_audio_thread__custom; /* Backend is doing a custom main loop. */
pContext->isBackendAsynchronous = MA_FALSE;
} }
} else {
pContext->isBackendAsynchronous = MA_FALSE;
pContext->onDeviceMainLoop = ma_device_audio_thread__read_write__custom; /* Backend is using blocking read/write calls. */
} }
/* Getting here means the format could not be found or is equal to ma_format_unknown. */ return MA_SUCCESS;
return (ma_uint32)-1;
} }
static ma_result ma_device__post_init_setup(ma_device* pDevice, ma_device_type deviceType); #endif /* MA_HAS_CUSTOM */
/******************************************************************************* /*******************************************************************************
...@@ -10734,16 +11421,6 @@ static ma_uint64 ma_device_get_total_run_time_in_frames__null(ma_device* pDevice ...@@ -10734,16 +11421,6 @@ static ma_uint64 ma_device_get_total_run_time_in_frames__null(ma_device* pDevice
return (ma_uint64)((pDevice->null_device.priorRunTime + ma_timer_get_time_in_seconds(&pDevice->null_device.timer)) * internalSampleRate); return (ma_uint64)((pDevice->null_device.priorRunTime + ma_timer_get_time_in_seconds(&pDevice->null_device.timer)) * internalSampleRate);
} }
static ma_bool32 ma_context_is_device_id_equal__null(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return pID0->nullbackend == pID1->nullbackend;
}
static ma_result ma_context_enumerate_devices__null(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__null(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
ma_bool32 cbResult = MA_TRUE; ma_bool32 cbResult = MA_TRUE;
...@@ -11046,171 +11723,12 @@ static ma_result ma_device_read__null(ma_device* pDevice, void* pPCMFrames, ma_u ...@@ -11046,171 +11723,12 @@ static ma_result ma_device_read__null(ma_device* pDevice, void* pPCMFrames, ma_u
static ma_result ma_device_main_loop__null(ma_device* pDevice) static ma_result ma_device_main_loop__null(ma_device* pDevice)
{ {
ma_result result = MA_SUCCESS; ma_backend_callbacks callbacks;
ma_bool32 exitLoop = MA_FALSE; callbacks.onDeviceStart = ma_device_start__null;
ma_uint8 capturedDeviceData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE]; callbacks.onDeviceStop = ma_device_stop__null;
ma_uint8 playbackDeviceData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE]; callbacks.onDeviceRead = ma_device_read__null;
ma_uint32 capturedDeviceDataCapInFrames = sizeof(capturedDeviceData) / ma_get_bytes_per_frame(pDevice->capture.internalFormat, pDevice->capture.internalChannels); callbacks.onDeviceWrite = ma_device_write__null;
ma_uint32 playbackDeviceDataCapInFrames = sizeof(playbackDeviceData) / ma_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels); return ma_device_audio_thread__default_read_write(pDevice, &callbacks);
MA_ASSERT(pDevice != NULL);
/* The capture device needs to be started immediately. */
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
result = ma_device_start__null(pDevice);
if (result != MA_SUCCESS) {
return result;
}
}
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type)
{
case ma_device_type_duplex:
{
/* The process is: device_read -> convert -> callback -> convert -> device_write */
ma_uint32 totalCapturedDeviceFramesProcessed = 0;
ma_uint32 capturedDevicePeriodSizeInFrames = ma_min(pDevice->capture.internalPeriodSizeInFrames, pDevice->playback.internalPeriodSizeInFrames);
while (totalCapturedDeviceFramesProcessed < capturedDevicePeriodSizeInFrames) {
ma_uint32 capturedDeviceFramesRemaining;
ma_uint32 capturedDeviceFramesProcessed;
ma_uint32 capturedDeviceFramesToProcess;
ma_uint32 capturedDeviceFramesToTryProcessing = capturedDevicePeriodSizeInFrames - totalCapturedDeviceFramesProcessed;
if (capturedDeviceFramesToTryProcessing > capturedDeviceDataCapInFrames) {
capturedDeviceFramesToTryProcessing = capturedDeviceDataCapInFrames;
}
result = ma_device_read__null(pDevice, capturedDeviceData, capturedDeviceFramesToTryProcessing, &capturedDeviceFramesToProcess);
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
capturedDeviceFramesRemaining = capturedDeviceFramesToProcess;
capturedDeviceFramesProcessed = 0;
/* At this point we have our captured data in device format and we now need to convert it to client format. */
for (;;) {
ma_uint8 capturedClientData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE];
ma_uint8 playbackClientData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE];
ma_uint32 capturedClientDataCapInFrames = sizeof(capturedClientData) / ma_get_bytes_per_frame(pDevice->capture.format, pDevice->capture.channels);
ma_uint32 playbackClientDataCapInFrames = sizeof(playbackClientData) / ma_get_bytes_per_frame(pDevice->playback.format, pDevice->playback.channels);
ma_uint64 capturedClientFramesToProcessThisIteration = ma_min(capturedClientDataCapInFrames, playbackClientDataCapInFrames);
ma_uint64 capturedDeviceFramesToProcessThisIteration = capturedDeviceFramesRemaining;
ma_uint8* pRunningCapturedDeviceFrames = ma_offset_ptr(capturedDeviceData, capturedDeviceFramesProcessed * ma_get_bytes_per_frame(pDevice->capture.internalFormat, pDevice->capture.internalChannels));
/* Convert capture data from device format to client format. */
result = ma_data_converter_process_pcm_frames(&pDevice->capture.converter, pRunningCapturedDeviceFrames, &capturedDeviceFramesToProcessThisIteration, capturedClientData, &capturedClientFramesToProcessThisIteration);
if (result != MA_SUCCESS) {
break;
}
/*
If we weren't able to generate any output frames it must mean we've exhaused all of our input. The only time this would not be the case is if capturedClientData was too small
which should never be the case when it's of the size MA_DATA_CONVERTER_STACK_BUFFER_SIZE.
*/
if (capturedClientFramesToProcessThisIteration == 0) {
break;
}
ma_device__on_data(pDevice, playbackClientData, capturedClientData, (ma_uint32)capturedClientFramesToProcessThisIteration); /* Safe cast .*/
capturedDeviceFramesProcessed += (ma_uint32)capturedDeviceFramesToProcessThisIteration; /* Safe cast. */
capturedDeviceFramesRemaining -= (ma_uint32)capturedDeviceFramesToProcessThisIteration; /* Safe cast. */
/* At this point the playbackClientData buffer should be holding data that needs to be written to the device. */
for (;;) {
ma_uint64 convertedClientFrameCount = capturedClientFramesToProcessThisIteration;
ma_uint64 convertedDeviceFrameCount = playbackDeviceDataCapInFrames;
result = ma_data_converter_process_pcm_frames(&pDevice->playback.converter, playbackClientData, &convertedClientFrameCount, playbackDeviceData, &convertedDeviceFrameCount);
if (result != MA_SUCCESS) {
break;
}
result = ma_device_write__null(pDevice, playbackDeviceData, (ma_uint32)convertedDeviceFrameCount, NULL); /* Safe cast. */
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
capturedClientFramesToProcessThisIteration -= (ma_uint32)convertedClientFrameCount; /* Safe cast. */
if (capturedClientFramesToProcessThisIteration == 0) {
break;
}
}
/* In case an error happened from ma_device_write__null()... */
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
}
totalCapturedDeviceFramesProcessed += capturedDeviceFramesProcessed;
}
} break;
case ma_device_type_capture:
{
ma_uint32 periodSizeInFrames = pDevice->capture.internalPeriodSizeInFrames;
ma_uint32 framesReadThisPeriod = 0;
while (framesReadThisPeriod < periodSizeInFrames) {
ma_uint32 framesRemainingInPeriod = periodSizeInFrames - framesReadThisPeriod;
ma_uint32 framesProcessed;
ma_uint32 framesToReadThisIteration = framesRemainingInPeriod;
if (framesToReadThisIteration > capturedDeviceDataCapInFrames) {
framesToReadThisIteration = capturedDeviceDataCapInFrames;
}
result = ma_device_read__null(pDevice, capturedDeviceData, framesToReadThisIteration, &framesProcessed);
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
ma_device__send_frames_to_client(pDevice, framesProcessed, capturedDeviceData);
framesReadThisPeriod += framesProcessed;
}
} break;
case ma_device_type_playback:
{
/* We write in chunks of the period size, but use a stack allocated buffer for the intermediary. */
ma_uint32 periodSizeInFrames = pDevice->playback.internalPeriodSizeInFrames;
ma_uint32 framesWrittenThisPeriod = 0;
while (framesWrittenThisPeriod < periodSizeInFrames) {
ma_uint32 framesRemainingInPeriod = periodSizeInFrames - framesWrittenThisPeriod;
ma_uint32 framesProcessed;
ma_uint32 framesToWriteThisIteration = framesRemainingInPeriod;
if (framesToWriteThisIteration > playbackDeviceDataCapInFrames) {
framesToWriteThisIteration = playbackDeviceDataCapInFrames;
}
ma_device__read_frames_from_client(pDevice, framesToWriteThisIteration, playbackDeviceData);
result = ma_device_write__null(pDevice, playbackDeviceData, framesToWriteThisIteration, &framesProcessed);
if (result != MA_SUCCESS) {
exitLoop = MA_TRUE;
break;
}
framesWrittenThisPeriod += framesProcessed;
}
} break;
/* To silence a warning. Will never hit this. */
case ma_device_type_loopback:
default: break;
}
}
/* Here is where the device is started. */
ma_device_stop__null(pDevice);
return result;
} }
static ma_result ma_context_uninit__null(ma_context* pContext) static ma_result ma_context_uninit__null(ma_context* pContext)
...@@ -11229,7 +11747,6 @@ static ma_result ma_context_init__null(const ma_context_config* pConfig, ma_cont ...@@ -11229,7 +11747,6 @@ static ma_result ma_context_init__null(const ma_context_config* pConfig, ma_cont
(void)pConfig; (void)pConfig;
pContext->onUninit = ma_context_uninit__null; pContext->onUninit = ma_context_uninit__null;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__null;
pContext->onEnumDevices = ma_context_enumerate_devices__null; pContext->onEnumDevices = ma_context_enumerate_devices__null;
pContext->onGetDeviceInfo = ma_context_get_device_info__null; pContext->onGetDeviceInfo = ma_context_get_device_info__null;
pContext->onDeviceInit = ma_device_init__null; pContext->onDeviceInit = ma_device_init__null;
...@@ -12295,17 +12812,6 @@ typedef ma_IUnknown ma_WASAPIDeviceInterface; ...@@ -12295,17 +12812,6 @@ typedef ma_IUnknown ma_WASAPIDeviceInterface;
#endif #endif
static ma_bool32 ma_context_is_device_id_equal__wasapi(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return memcmp(pID0->wasapi, pID1->wasapi, sizeof(pID0->wasapi)) == 0;
}
static void ma_set_device_info_from_WAVEFORMATEX(const WAVEFORMATEX* pWF, ma_device_info* pInfo) static void ma_set_device_info_from_WAVEFORMATEX(const WAVEFORMATEX* pWF, ma_device_info* pInfo)
{ {
MA_ASSERT(pWF != NULL); MA_ASSERT(pWF != NULL);
...@@ -13851,7 +14357,7 @@ static ma_result ma_device_main_loop__wasapi(ma_device* pDevice) ...@@ -13851,7 +14357,7 @@ static ma_result ma_device_main_loop__wasapi(ma_device* pDevice)
c89atomic_exchange_32(&pDevice->wasapi.isStartedCapture, MA_TRUE); c89atomic_exchange_32(&pDevice->wasapi.isStartedCapture, MA_TRUE);
} }
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
/* We may need to reroute the device. */ /* We may need to reroute the device. */
if (ma_device_is_reroute_required__wasapi(pDevice, ma_device_type_playback)) { if (ma_device_is_reroute_required__wasapi(pDevice, ma_device_type_playback)) {
result = ma_device_reroute__wasapi(pDevice, ma_device_type_playback); result = ma_device_reroute__wasapi(pDevice, ma_device_type_playback);
...@@ -14487,7 +14993,6 @@ static ma_result ma_context_init__wasapi(const ma_context_config* pConfig, ma_co ...@@ -14487,7 +14993,6 @@ static ma_result ma_context_init__wasapi(const ma_context_config* pConfig, ma_co
} }
pContext->onUninit = ma_context_uninit__wasapi; pContext->onUninit = ma_context_uninit__wasapi;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__wasapi;
pContext->onEnumDevices = ma_context_enumerate_devices__wasapi; pContext->onEnumDevices = ma_context_enumerate_devices__wasapi;
pContext->onGetDeviceInfo = ma_context_get_device_info__wasapi; pContext->onGetDeviceInfo = ma_context_get_device_info__wasapi;
pContext->onDeviceInit = ma_device_init__wasapi; pContext->onDeviceInit = ma_device_init__wasapi;
...@@ -15060,16 +15565,6 @@ static ma_result ma_context_get_format_info_for_IDirectSoundCapture__dsound(ma_c ...@@ -15060,16 +15565,6 @@ static ma_result ma_context_get_format_info_for_IDirectSoundCapture__dsound(ma_c
return MA_SUCCESS; return MA_SUCCESS;
} }
static ma_bool32 ma_context_is_device_id_equal__dsound(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return memcmp(pID0->dsound, pID1->dsound, sizeof(pID0->dsound)) == 0;
}
typedef struct typedef struct
{ {
...@@ -15681,7 +16176,7 @@ static ma_result ma_device_main_loop__dsound(ma_device* pDevice) ...@@ -15681,7 +16176,7 @@ static ma_result ma_device_main_loop__dsound(ma_device* pDevice)
} }
} }
while (ma_device__get_state(pDevice) == MA_STATE_STARTED) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED) {
switch (pDevice->type) switch (pDevice->type)
{ {
case ma_device_type_duplex: case ma_device_type_duplex:
...@@ -16198,7 +16693,6 @@ static ma_result ma_context_init__dsound(const ma_context_config* pConfig, ma_co ...@@ -16198,7 +16693,6 @@ static ma_result ma_context_init__dsound(const ma_context_config* pConfig, ma_co
pContext->dsound.DirectSoundCaptureEnumerateA = ma_dlsym(pContext, pContext->dsound.hDSoundDLL, "DirectSoundCaptureEnumerateA"); pContext->dsound.DirectSoundCaptureEnumerateA = ma_dlsym(pContext, pContext->dsound.hDSoundDLL, "DirectSoundCaptureEnumerateA");
pContext->onUninit = ma_context_uninit__dsound; pContext->onUninit = ma_context_uninit__dsound;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__dsound;
pContext->onEnumDevices = ma_context_enumerate_devices__dsound; pContext->onEnumDevices = ma_context_enumerate_devices__dsound;
pContext->onGetDeviceInfo = ma_context_get_device_info__dsound; pContext->onGetDeviceInfo = ma_context_get_device_info__dsound;
pContext->onDeviceInit = ma_device_init__dsound; pContext->onDeviceInit = ma_device_init__dsound;
...@@ -16608,16 +17102,6 @@ static ma_result ma_context_get_device_info_from_WAVEINCAPS2(ma_context* pContex ...@@ -16608,16 +17102,6 @@ static ma_result ma_context_get_device_info_from_WAVEINCAPS2(ma_context* pContex
} }
static ma_bool32 ma_context_is_device_id_equal__winmm(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return pID0->winmm == pID1->winmm;
}
static ma_result ma_context_enumerate_devices__winmm(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__winmm(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
UINT playbackDeviceCount; UINT playbackDeviceCount;
...@@ -17112,7 +17596,7 @@ static ma_result ma_device_write__winmm(ma_device* pDevice, const void* pPCMFram ...@@ -17112,7 +17596,7 @@ static ma_result ma_device_write__winmm(ma_device* pDevice, const void* pPCMFram
} }
/* If the device has been stopped we need to break. */ /* If the device has been stopped we need to break. */
if (ma_device__get_state(pDevice) != MA_STATE_STARTED) { if (ma_device_get_state(pDevice) != MA_STATE_STARTED) {
break; break;
} }
} }
...@@ -17201,7 +17685,7 @@ static ma_result ma_device_read__winmm(ma_device* pDevice, void* pPCMFrames, ma_ ...@@ -17201,7 +17685,7 @@ static ma_result ma_device_read__winmm(ma_device* pDevice, void* pPCMFrames, ma_
} }
/* If the device has been stopped we need to break. */ /* If the device has been stopped we need to break. */
if (ma_device__get_state(pDevice) != MA_STATE_STARTED) { if (ma_device_get_state(pDevice) != MA_STATE_STARTED) {
break; break;
} }
} }
...@@ -17254,7 +17738,7 @@ static ma_result ma_device_main_loop__winmm(ma_device* pDevice) ...@@ -17254,7 +17738,7 @@ static ma_result ma_device_main_loop__winmm(ma_device* pDevice)
} }
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type) switch (pDevice->type)
{ {
case ma_device_type_duplex: case ma_device_type_duplex:
...@@ -17443,7 +17927,6 @@ static ma_result ma_context_init__winmm(const ma_context_config* pConfig, ma_con ...@@ -17443,7 +17927,6 @@ static ma_result ma_context_init__winmm(const ma_context_config* pConfig, ma_con
pContext->winmm.waveInReset = ma_dlsym(pContext, pContext->winmm.hWinMM, "waveInReset"); pContext->winmm.waveInReset = ma_dlsym(pContext, pContext->winmm.hWinMM, "waveInReset");
pContext->onUninit = ma_context_uninit__winmm; pContext->onUninit = ma_context_uninit__winmm;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__winmm;
pContext->onEnumDevices = ma_context_enumerate_devices__winmm; pContext->onEnumDevices = ma_context_enumerate_devices__winmm;
pContext->onGetDeviceInfo = ma_context_get_device_info__winmm; pContext->onGetDeviceInfo = ma_context_get_device_info__winmm;
pContext->onDeviceInit = ma_device_init__winmm; pContext->onDeviceInit = ma_device_init__winmm;
...@@ -18181,16 +18664,6 @@ static ma_result ma_context_open_pcm__alsa(ma_context* pContext, ma_share_mode s ...@@ -18181,16 +18664,6 @@ static ma_result ma_context_open_pcm__alsa(ma_context* pContext, ma_share_mode s
} }
static ma_bool32 ma_context_is_device_id_equal__alsa(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return ma_strcmp(pID0->alsa, pID1->alsa) == 0;
}
static ma_result ma_context_enumerate_devices__alsa(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__alsa(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
int resultALSA; int resultALSA;
...@@ -18550,7 +19023,7 @@ static ma_uint32 ma_device__wait_for_frames__alsa(ma_device* pDevice, ma_bool32* ...@@ -18550,7 +19023,7 @@ static ma_uint32 ma_device__wait_for_frames__alsa(ma_device* pDevice, ma_bool32*
static ma_bool32 ma_device_read_from_client_and_write__alsa(ma_device* pDevice) static ma_bool32 ma_device_read_from_client_and_write__alsa(ma_device* pDevice)
{ {
MA_ASSERT(pDevice != NULL); MA_ASSERT(pDevice != NULL);
if (!ma_device_is_started(pDevice) && ma_device__get_state(pDevice) != MA_STATE_STARTING) { if (!ma_device_is_started(pDevice) && ma_device_get_state(pDevice) != MA_STATE_STARTING) {
return MA_FALSE; return MA_FALSE;
} }
if (pDevice->alsa.breakFromMainLoop) { if (pDevice->alsa.breakFromMainLoop) {
...@@ -19354,7 +19827,7 @@ static ma_result ma_device_main_loop__alsa(ma_device* pDevice) ...@@ -19354,7 +19827,7 @@ static ma_result ma_device_main_loop__alsa(ma_device* pDevice)
} }
} }
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type) switch (pDevice->type)
{ {
case ma_device_type_duplex: case ma_device_type_duplex:
...@@ -19769,7 +20242,6 @@ static ma_result ma_context_init__alsa(const ma_context_config* pConfig, ma_cont ...@@ -19769,7 +20242,6 @@ static ma_result ma_context_init__alsa(const ma_context_config* pConfig, ma_cont
} }
pContext->onUninit = ma_context_uninit__alsa; pContext->onUninit = ma_context_uninit__alsa;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__alsa;
pContext->onEnumDevices = ma_context_enumerate_devices__alsa; pContext->onEnumDevices = ma_context_enumerate_devices__alsa;
pContext->onGetDeviceInfo = ma_context_get_device_info__alsa; pContext->onGetDeviceInfo = ma_context_get_device_info__alsa;
pContext->onDeviceInit = ma_device_init__alsa; pContext->onDeviceInit = ma_device_init__alsa;
...@@ -20755,17 +21227,6 @@ static ma_result ma_context_wait_for_pa_stream_to_connect__pulse(ma_context* pCo ...@@ -20755,17 +21227,6 @@ static ma_result ma_context_wait_for_pa_stream_to_connect__pulse(ma_context* pCo
} }
static ma_bool32 ma_context_is_device_id_equal__pulse(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return ma_strcmp(pID0->pulse, pID1->pulse) == 0;
}
static void ma_device_sink_info_callback(ma_pa_context* pPulseContext, const ma_pa_sink_info* pInfo, int endOfList, void* pUserData) static void ma_device_sink_info_callback(ma_pa_context* pPulseContext, const ma_pa_sink_info* pInfo, int endOfList, void* pUserData)
{ {
ma_pa_sink_info* pInfoOut; ma_pa_sink_info* pInfoOut;
...@@ -21207,7 +21668,7 @@ static void ma_device_on_read__pulse(ma_pa_stream* pStream, size_t byteCount, vo ...@@ -21207,7 +21668,7 @@ static void ma_device_on_read__pulse(ma_pa_stream* pStream, size_t byteCount, vo
frameCount = byteCount / bpf; frameCount = byteCount / bpf;
framesProcessed = 0; framesProcessed = 0;
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && framesProcessed < frameCount) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && framesProcessed < frameCount) {
const void* pMappedPCMFrames; const void* pMappedPCMFrames;
size_t bytesMapped; size_t bytesMapped;
ma_uint64 framesMapped; ma_uint64 framesMapped;
...@@ -21260,7 +21721,7 @@ static ma_result ma_device_write_to_stream__pulse(ma_device* pDevice, ma_pa_stre ...@@ -21260,7 +21721,7 @@ static ma_result ma_device_write_to_stream__pulse(ma_device* pDevice, ma_pa_stre
bpf = ma_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels); bpf = ma_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels);
MA_ASSERT(bpf > 0); MA_ASSERT(bpf > 0);
deviceState = ma_device__get_state(pDevice); deviceState = ma_device_get_state(pDevice);
bytesMapped = ((ma_pa_stream_writable_size_proc)pDevice->pContext->pulse.pa_stream_writable_size)(pStream); bytesMapped = ((ma_pa_stream_writable_size_proc)pDevice->pContext->pulse.pa_stream_writable_size)(pStream);
if (bytesMapped != (size_t)-1) { if (bytesMapped != (size_t)-1) {
...@@ -21330,7 +21791,7 @@ static void ma_device_on_write__pulse(ma_pa_stream* pStream, size_t byteCount, v ...@@ -21330,7 +21791,7 @@ static void ma_device_on_write__pulse(ma_pa_stream* pStream, size_t byteCount, v
ma_uint32 deviceState; ma_uint32 deviceState;
/* Don't keep trying to process frames if the device isn't started. */ /* Don't keep trying to process frames if the device isn't started. */
deviceState = ma_device__get_state(pDevice); deviceState = ma_device_get_state(pDevice);
if (deviceState != MA_STATE_STARTING && deviceState != MA_STATE_STARTED) { if (deviceState != MA_STATE_STARTING && deviceState != MA_STATE_STARTED) {
break; break;
} }
...@@ -21936,7 +22397,6 @@ static ma_result ma_context_init__pulse(const ma_context_config* pConfig, ma_con ...@@ -21936,7 +22397,6 @@ static ma_result ma_context_init__pulse(const ma_context_config* pConfig, ma_con
pContext->isBackendAsynchronous = MA_TRUE; /* We are using PulseAudio in asynchronous mode. */ pContext->isBackendAsynchronous = MA_TRUE; /* We are using PulseAudio in asynchronous mode. */
pContext->onUninit = ma_context_uninit__pulse; pContext->onUninit = ma_context_uninit__pulse;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__pulse;
pContext->onEnumDevices = ma_context_enumerate_devices__pulse; pContext->onEnumDevices = ma_context_enumerate_devices__pulse;
pContext->onGetDeviceInfo = ma_context_get_device_info__pulse; pContext->onGetDeviceInfo = ma_context_get_device_info__pulse;
pContext->onDeviceInit = ma_device_init__pulse; pContext->onDeviceInit = ma_device_init__pulse;
...@@ -22089,15 +22549,6 @@ static ma_result ma_context_open_client__jack(ma_context* pContext, ma_jack_clie ...@@ -22089,15 +22549,6 @@ static ma_result ma_context_open_client__jack(ma_context* pContext, ma_jack_clie
return MA_SUCCESS; return MA_SUCCESS;
} }
static ma_bool32 ma_context_is_device_id_equal__jack(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return pID0->jack == pID1->jack;
}
static ma_result ma_context_enumerate_devices__jack(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__jack(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
...@@ -22644,7 +23095,6 @@ static ma_result ma_context_init__jack(const ma_context_config* pConfig, ma_cont ...@@ -22644,7 +23095,6 @@ static ma_result ma_context_init__jack(const ma_context_config* pConfig, ma_cont
pContext->isBackendAsynchronous = MA_TRUE; pContext->isBackendAsynchronous = MA_TRUE;
pContext->onUninit = ma_context_uninit__jack; pContext->onUninit = ma_context_uninit__jack;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__jack;
pContext->onEnumDevices = ma_context_enumerate_devices__jack; pContext->onEnumDevices = ma_context_enumerate_devices__jack;
pContext->onGetDeviceInfo = ma_context_get_device_info__jack; pContext->onGetDeviceInfo = ma_context_get_device_info__jack;
pContext->onDeviceInit = ma_device_init__jack; pContext->onDeviceInit = ma_device_init__jack;
...@@ -23872,15 +24322,6 @@ static ma_result ma_get_AudioUnit_channel_map(ma_context* pContext, AudioUnit au ...@@ -23872,15 +24322,6 @@ static ma_result ma_get_AudioUnit_channel_map(ma_context* pContext, AudioUnit au
} }
#endif /* MA_APPLE_DESKTOP */ #endif /* MA_APPLE_DESKTOP */
static ma_bool32 ma_context_is_device_id_equal__coreaudio(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return strcmp(pID0->coreaudio, pID1->coreaudio) == 0;
}
#if !defined(MA_APPLE_DESKTOP) #if !defined(MA_APPLE_DESKTOP)
static void ma_AVAudioSessionPortDescription_to_device_info(AVAudioSessionPortDescription* pPortDesc, ma_device_info* pInfo) static void ma_AVAudioSessionPortDescription_to_device_info(AVAudioSessionPortDescription* pPortDesc, ma_device_info* pInfo)
...@@ -24420,7 +24861,7 @@ static void on_start_stop__coreaudio(void* pUserData, AudioUnit audioUnit, Audio ...@@ -24420,7 +24861,7 @@ static void on_start_stop__coreaudio(void* pUserData, AudioUnit audioUnit, Audio
can try waiting on the same lock. I'm going to try working around this by not calling any Core can try waiting on the same lock. I'm going to try working around this by not calling any Core
Audio APIs in the callback when the device has been stopped or uninitialized. Audio APIs in the callback when the device has been stopped or uninitialized.
*/ */
if (ma_device__get_state(pDevice) == MA_STATE_UNINITIALIZED || ma_device__get_state(pDevice) == MA_STATE_STOPPING || ma_device__get_state(pDevice) == MA_STATE_STOPPED) { if (ma_device_get_state(pDevice) == MA_STATE_UNINITIALIZED || ma_device_get_state(pDevice) == MA_STATE_STOPPING || ma_device_get_state(pDevice) == MA_STATE_STOPPED) {
ma_stop_proc onStop = pDevice->onStop; ma_stop_proc onStop = pDevice->onStop;
if (onStop) { if (onStop) {
onStop(pDevice); onStop(pDevice);
...@@ -24528,7 +24969,7 @@ static OSStatus ma_default_device_changed__coreaudio(AudioObjectID objectID, UIn ...@@ -24528,7 +24969,7 @@ static OSStatus ma_default_device_changed__coreaudio(AudioObjectID objectID, UIn
ma_device__post_init_setup(pDevice, deviceType); ma_device__post_init_setup(pDevice, deviceType);
/* Restart the device if required. If this fails we need to stop the device entirely. */ /* Restart the device if required. If this fails we need to stop the device entirely. */
if (ma_device__get_state(pDevice) == MA_STATE_STARTED) { if (ma_device_get_state(pDevice) == MA_STATE_STARTED) {
OSStatus status; OSStatus status;
if (deviceType == ma_device_type_playback) { if (deviceType == ma_device_type_playback) {
status = ((ma_AudioOutputUnitStart_proc)pDevice->pContext->coreaudio.AudioOutputUnitStart)((AudioUnit)pDevice->coreaudio.audioUnitPlayback); status = ((ma_AudioOutputUnitStart_proc)pDevice->pContext->coreaudio.AudioOutputUnitStart)((AudioUnit)pDevice->coreaudio.audioUnitPlayback);
...@@ -24793,7 +25234,7 @@ static ma_result ma_device__untrack__coreaudio(ma_device* pDevice) ...@@ -24793,7 +25234,7 @@ static ma_result ma_device__untrack__coreaudio(ma_device* pDevice)
static void ma_device_uninit__coreaudio(ma_device* pDevice) static void ma_device_uninit__coreaudio(ma_device* pDevice)
{ {
MA_ASSERT(pDevice != NULL); MA_ASSERT(pDevice != NULL);
MA_ASSERT(ma_device__get_state(pDevice) == MA_STATE_UNINITIALIZED); MA_ASSERT(ma_device_get_state(pDevice) == MA_STATE_UNINITIALIZED);
#if defined(MA_APPLE_DESKTOP) #if defined(MA_APPLE_DESKTOP)
/* /*
...@@ -25772,7 +26213,6 @@ static ma_result ma_context_init__coreaudio(const ma_context_config* pConfig, ma ...@@ -25772,7 +26213,6 @@ static ma_result ma_context_init__coreaudio(const ma_context_config* pConfig, ma
pContext->isBackendAsynchronous = MA_TRUE; pContext->isBackendAsynchronous = MA_TRUE;
pContext->onUninit = ma_context_uninit__coreaudio; pContext->onUninit = ma_context_uninit__coreaudio;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__coreaudio;
pContext->onEnumDevices = ma_context_enumerate_devices__coreaudio; pContext->onEnumDevices = ma_context_enumerate_devices__coreaudio;
pContext->onGetDeviceInfo = ma_context_get_device_info__coreaudio; pContext->onGetDeviceInfo = ma_context_get_device_info__coreaudio;
pContext->onDeviceInit = ma_device_init__coreaudio; pContext->onDeviceInit = ma_device_init__coreaudio;
...@@ -26162,16 +26602,6 @@ static ma_uint32 ma_find_best_sample_rate_from_sio_cap__sndio(struct ma_sio_cap* ...@@ -26162,16 +26602,6 @@ static ma_uint32 ma_find_best_sample_rate_from_sio_cap__sndio(struct ma_sio_cap*
} }
static ma_bool32 ma_context_is_device_id_equal__sndio(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return ma_strcmp(pID0->sndio, pID1->sndio) == 0;
}
static ma_result ma_context_enumerate_devices__sndio(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__sndio(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
ma_bool32 isTerminating = MA_FALSE; ma_bool32 isTerminating = MA_FALSE;
...@@ -26640,7 +27070,7 @@ static ma_result ma_device_main_loop__sndio(ma_device* pDevice) ...@@ -26640,7 +27070,7 @@ static ma_result ma_device_main_loop__sndio(ma_device* pDevice)
((ma_sio_start_proc)pDevice->pContext->sndio.sio_start)((struct ma_sio_hdl*)pDevice->sndio.handlePlayback); /* <-- Doesn't actually playback until data is written. */ ((ma_sio_start_proc)pDevice->pContext->sndio.sio_start)((struct ma_sio_hdl*)pDevice->sndio.handlePlayback); /* <-- Doesn't actually playback until data is written. */
} }
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type) switch (pDevice->type)
{ {
case ma_device_type_duplex: case ma_device_type_duplex:
...@@ -26850,7 +27280,6 @@ static ma_result ma_context_init__sndio(const ma_context_config* pConfig, ma_con ...@@ -26850,7 +27280,6 @@ static ma_result ma_context_init__sndio(const ma_context_config* pConfig, ma_con
#endif #endif
pContext->onUninit = ma_context_uninit__sndio; pContext->onUninit = ma_context_uninit__sndio;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__sndio;
pContext->onEnumDevices = ma_context_enumerate_devices__sndio; pContext->onEnumDevices = ma_context_enumerate_devices__sndio;
pContext->onGetDeviceInfo = ma_context_get_device_info__sndio; pContext->onGetDeviceInfo = ma_context_get_device_info__sndio;
pContext->onDeviceInit = ma_device_init__sndio; pContext->onDeviceInit = ma_device_init__sndio;
...@@ -26934,15 +27363,6 @@ static ma_result ma_extract_device_index_from_id__audio4(const char* id, const c ...@@ -26934,15 +27363,6 @@ static ma_result ma_extract_device_index_from_id__audio4(const char* id, const c
return MA_SUCCESS; return MA_SUCCESS;
} }
static ma_bool32 ma_context_is_device_id_equal__audio4(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return ma_strcmp(pID0->audio4, pID1->audio4) == 0;
}
#if !defined(MA_AUDIO4_USE_NEW_API) /* Old API */ #if !defined(MA_AUDIO4_USE_NEW_API) /* Old API */
static ma_format ma_format_from_encoding__audio4(unsigned int encoding, unsigned int precision) static ma_format ma_format_from_encoding__audio4(unsigned int encoding, unsigned int precision)
...@@ -27612,7 +28032,7 @@ static ma_result ma_device_main_loop__audio4(ma_device* pDevice) ...@@ -27612,7 +28032,7 @@ static ma_result ma_device_main_loop__audio4(ma_device* pDevice)
/* No need to explicitly start the device like the other backends. */ /* No need to explicitly start the device like the other backends. */
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type) switch (pDevice->type)
{ {
case ma_device_type_duplex: case ma_device_type_duplex:
...@@ -27786,7 +28206,6 @@ static ma_result ma_context_init__audio4(const ma_context_config* pConfig, ma_co ...@@ -27786,7 +28206,6 @@ static ma_result ma_context_init__audio4(const ma_context_config* pConfig, ma_co
(void)pConfig; (void)pConfig;
pContext->onUninit = ma_context_uninit__audio4; pContext->onUninit = ma_context_uninit__audio4;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__audio4;
pContext->onEnumDevices = ma_context_enumerate_devices__audio4; pContext->onEnumDevices = ma_context_enumerate_devices__audio4;
pContext->onGetDeviceInfo = ma_context_get_device_info__audio4; pContext->onGetDeviceInfo = ma_context_get_device_info__audio4;
pContext->onDeviceInit = ma_device_init__audio4; pContext->onDeviceInit = ma_device_init__audio4;
...@@ -27862,16 +28281,6 @@ static ma_result ma_context_open_device__oss(ma_context* pContext, ma_device_typ ...@@ -27862,16 +28281,6 @@ static ma_result ma_context_open_device__oss(ma_context* pContext, ma_device_typ
return MA_SUCCESS; return MA_SUCCESS;
} }
static ma_bool32 ma_context_is_device_id_equal__oss(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return ma_strcmp(pID0->oss, pID1->oss) == 0;
}
static ma_result ma_context_enumerate_devices__oss(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__oss(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
int fd; int fd;
...@@ -28333,7 +28742,7 @@ static ma_result ma_device_main_loop__oss(ma_device* pDevice) ...@@ -28333,7 +28742,7 @@ static ma_result ma_device_main_loop__oss(ma_device* pDevice)
/* No need to explicitly start the device like the other backends. */ /* No need to explicitly start the device like the other backends. */
while (ma_device__get_state(pDevice) == MA_STATE_STARTED && !exitLoop) { while (ma_device_get_state(pDevice) == MA_STATE_STARTED && !exitLoop) {
switch (pDevice->type) switch (pDevice->type)
{ {
case ma_device_type_duplex: case ma_device_type_duplex:
...@@ -28528,7 +28937,6 @@ static ma_result ma_context_init__oss(const ma_context_config* pConfig, ma_conte ...@@ -28528,7 +28937,6 @@ static ma_result ma_context_init__oss(const ma_context_config* pConfig, ma_conte
pContext->oss.versionMinor = ((ossVersion & 0x00FF00) >> 8); pContext->oss.versionMinor = ((ossVersion & 0x00FF00) >> 8);
pContext->onUninit = ma_context_uninit__oss; pContext->onUninit = ma_context_uninit__oss;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__oss;
pContext->onEnumDevices = ma_context_enumerate_devices__oss; pContext->onEnumDevices = ma_context_enumerate_devices__oss;
pContext->onGetDeviceInfo = ma_context_get_device_info__oss; pContext->onGetDeviceInfo = ma_context_get_device_info__oss;
pContext->onDeviceInit = ma_device_init__oss; pContext->onDeviceInit = ma_device_init__oss;
...@@ -28806,16 +29214,6 @@ static ma_result ma_wait_for_simple_state_transition__aaudio(ma_context* pContex ...@@ -28806,16 +29214,6 @@ static ma_result ma_wait_for_simple_state_transition__aaudio(ma_context* pContex
} }
static ma_bool32 ma_context_is_device_id_equal__aaudio(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return pID0->aaudio == pID1->aaudio;
}
static ma_result ma_context_enumerate_devices__aaudio(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__aaudio(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
ma_bool32 cbResult = MA_TRUE; ma_bool32 cbResult = MA_TRUE;
...@@ -29200,7 +29598,6 @@ static ma_result ma_context_init__aaudio(const ma_context_config* pConfig, ma_co ...@@ -29200,7 +29598,6 @@ static ma_result ma_context_init__aaudio(const ma_context_config* pConfig, ma_co
pContext->isBackendAsynchronous = MA_TRUE; pContext->isBackendAsynchronous = MA_TRUE;
pContext->onUninit = ma_context_uninit__aaudio; pContext->onUninit = ma_context_uninit__aaudio;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__aaudio;
pContext->onEnumDevices = ma_context_enumerate_devices__aaudio; pContext->onEnumDevices = ma_context_enumerate_devices__aaudio;
pContext->onGetDeviceInfo = ma_context_get_device_info__aaudio; pContext->onGetDeviceInfo = ma_context_get_device_info__aaudio;
pContext->onDeviceInit = ma_device_init__aaudio; pContext->onDeviceInit = ma_device_init__aaudio;
...@@ -29413,16 +29810,6 @@ static SLuint32 ma_round_to_standard_sample_rate__opensl(SLuint32 samplesPerSec) ...@@ -29413,16 +29810,6 @@ static SLuint32 ma_round_to_standard_sample_rate__opensl(SLuint32 samplesPerSec)
} }
static ma_bool32 ma_context_is_device_id_equal__opensl(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return pID0->opensl == pID1->opensl;
}
static ma_result ma_context_enumerate_devices__opensl(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__opensl(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
ma_bool32 cbResult; ma_bool32 cbResult;
...@@ -30362,7 +30749,6 @@ static ma_result ma_context_init__opensl(const ma_context_config* pConfig, ma_co ...@@ -30362,7 +30749,6 @@ static ma_result ma_context_init__opensl(const ma_context_config* pConfig, ma_co
pContext->isBackendAsynchronous = MA_TRUE; pContext->isBackendAsynchronous = MA_TRUE;
pContext->onUninit = ma_context_uninit__opensl; pContext->onUninit = ma_context_uninit__opensl;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__opensl;
pContext->onEnumDevices = ma_context_enumerate_devices__opensl; pContext->onEnumDevices = ma_context_enumerate_devices__opensl;
pContext->onGetDeviceInfo = ma_context_get_device_info__opensl; pContext->onGetDeviceInfo = ma_context_get_device_info__opensl;
pContext->onDeviceInit = ma_device_init__opensl; pContext->onDeviceInit = ma_device_init__opensl;
...@@ -30414,16 +30800,6 @@ void EMSCRIPTEN_KEEPALIVE ma_device_process_pcm_frames_playback__webaudio(ma_dev ...@@ -30414,16 +30800,6 @@ void EMSCRIPTEN_KEEPALIVE ma_device_process_pcm_frames_playback__webaudio(ma_dev
} }
#endif #endif
static ma_bool32 ma_context_is_device_id_equal__webaudio(ma_context* pContext, const ma_device_id* pID0, const ma_device_id* pID1)
{
MA_ASSERT(pContext != NULL);
MA_ASSERT(pID0 != NULL);
MA_ASSERT(pID1 != NULL);
(void)pContext;
return ma_strcmp(pID0->webaudio, pID1->webaudio) == 0;
}
static ma_result ma_context_enumerate_devices__webaudio(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) static ma_result ma_context_enumerate_devices__webaudio(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{ {
ma_bool32 cbResult = MA_TRUE; ma_bool32 cbResult = MA_TRUE;
...@@ -30984,14 +31360,13 @@ static ma_result ma_context_init__webaudio(const ma_context_config* pConfig, ma_ ...@@ -30984,14 +31360,13 @@ static ma_result ma_context_init__webaudio(const ma_context_config* pConfig, ma_
pContext->isBackendAsynchronous = MA_TRUE; pContext->isBackendAsynchronous = MA_TRUE;
pContext->onUninit = ma_context_uninit__webaudio; pContext->onUninit = ma_context_uninit__webaudio;
pContext->onDeviceIDEqual = ma_context_is_device_id_equal__webaudio; pContext->onEnumDevices = ma_context_enumerate_devices__webaudio;
pContext->onEnumDevices = ma_context_enumerate_devices__webaudio; pContext->onGetDeviceInfo = ma_context_get_device_info__webaudio;
pContext->onGetDeviceInfo = ma_context_get_device_info__webaudio; pContext->onDeviceInit = ma_device_init__webaudio;
pContext->onDeviceInit = ma_device_init__webaudio; pContext->onDeviceUninit = ma_device_uninit__webaudio;
pContext->onDeviceUninit = ma_device_uninit__webaudio; pContext->onDeviceStart = ma_device_start__webaudio;
pContext->onDeviceStart = ma_device_start__webaudio; pContext->onDeviceStop = ma_device_stop__webaudio;
pContext->onDeviceStop = ma_device_stop__webaudio;
(void)pConfig; /* Unused. */ (void)pConfig; /* Unused. */
return MA_SUCCESS; return MA_SUCCESS;
...@@ -31150,7 +31525,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData) ...@@ -31150,7 +31525,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData)
pDevice->workResult = MA_SUCCESS; pDevice->workResult = MA_SUCCESS;
/* If the reason for the wake up is that we are terminating, just break from the loop. */ /* If the reason for the wake up is that we are terminating, just break from the loop. */
if (ma_device__get_state(pDevice) == MA_STATE_UNINITIALIZED) { if (ma_device_get_state(pDevice) == MA_STATE_UNINITIALIZED) {
break; break;
} }
...@@ -31159,7 +31534,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData) ...@@ -31159,7 +31534,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData)
be started will be waiting on an event (pDevice->startEvent) which means we need to make sure we signal the event be started will be waiting on an event (pDevice->startEvent) which means we need to make sure we signal the event
in both the success and error case. It's important that the state of the device is set _before_ signaling the event. in both the success and error case. It's important that the state of the device is set _before_ signaling the event.
*/ */
MA_ASSERT(ma_device__get_state(pDevice) == MA_STATE_STARTING); MA_ASSERT(ma_device_get_state(pDevice) == MA_STATE_STARTING);
/* Make sure the state is set appropriately. */ /* Make sure the state is set appropriately. */
ma_device__set_state(pDevice, MA_STATE_STARTED); ma_device__set_state(pDevice, MA_STATE_STARTED);
...@@ -31176,7 +31551,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData) ...@@ -31176,7 +31551,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData)
may have actually already happened above if the device was lost and miniaudio has attempted to re-initialize the device. In this case we may have actually already happened above if the device was lost and miniaudio has attempted to re-initialize the device. In this case we
don't want to be doing this a second time. don't want to be doing this a second time.
*/ */
if (ma_device__get_state(pDevice) != MA_STATE_UNINITIALIZED) { if (ma_device_get_state(pDevice) != MA_STATE_UNINITIALIZED) {
if (pDevice->pContext->onDeviceStop) { if (pDevice->pContext->onDeviceStop) {
pDevice->pContext->onDeviceStop(pDevice); pDevice->pContext->onDeviceStop(pDevice);
} }
...@@ -31193,7 +31568,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData) ...@@ -31193,7 +31568,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData)
it's possible that the device has been uninitialized which means we need to _not_ change the status to stopped. We cannot go from an it's possible that the device has been uninitialized which means we need to _not_ change the status to stopped. We cannot go from an
uninitialized state to stopped state. uninitialized state to stopped state.
*/ */
if (ma_device__get_state(pDevice) != MA_STATE_UNINITIALIZED) { if (ma_device_get_state(pDevice) != MA_STATE_UNINITIALIZED) {
ma_device__set_state(pDevice, MA_STATE_STOPPED); ma_device__set_state(pDevice, MA_STATE_STOPPED);
ma_event_signal(&pDevice->stopEvent); ma_event_signal(&pDevice->stopEvent);
} }
...@@ -31217,7 +31592,7 @@ static ma_bool32 ma_device__is_initialized(ma_device* pDevice) ...@@ -31217,7 +31592,7 @@ static ma_bool32 ma_device__is_initialized(ma_device* pDevice)
return MA_FALSE; return MA_FALSE;
} }
return ma_device__get_state(pDevice) != MA_STATE_UNINITIALIZED; return ma_device_get_state(pDevice) != MA_STATE_UNINITIALIZED;
} }
...@@ -31519,6 +31894,12 @@ MA_API ma_result ma_context_init(const ma_backend backends[], ma_uint32 backendC ...@@ -31519,6 +31894,12 @@ MA_API ma_result ma_context_init(const ma_backend backends[], ma_uint32 backendC
result = ma_context_init__webaudio(&config, pContext); result = ma_context_init__webaudio(&config, pContext);
} break; } break;
#endif #endif
#ifdef MA_HAS_CUSTOM
case ma_backend_custom:
{
result = ma_context_init__custom(&config, pContext);
} break;
#endif
#ifdef MA_HAS_NULL #ifdef MA_HAS_NULL
case ma_backend_null: case ma_backend_null:
{ {
...@@ -31709,7 +32090,6 @@ MA_API ma_result ma_context_get_device_info(ma_context* pContext, ma_device_type ...@@ -31709,7 +32090,6 @@ MA_API ma_result ma_context_get_device_info(ma_context* pContext, ma_device_type
MA_COPY_MEMORY(&deviceInfo.id, pDeviceID, sizeof(*pDeviceID)); MA_COPY_MEMORY(&deviceInfo.id, pDeviceID, sizeof(*pDeviceID));
} }
/* The backend may have an optimized device info retrieval function. If so, try that first. */
if (pContext->onGetDeviceInfo != NULL) { if (pContext->onGetDeviceInfo != NULL) {
ma_result result; ma_result result;
ma_mutex_lock(&pContext->deviceInfoLock); ma_mutex_lock(&pContext->deviceInfoLock);
...@@ -31718,6 +32098,75 @@ MA_API ma_result ma_context_get_device_info(ma_context* pContext, ma_device_type ...@@ -31718,6 +32098,75 @@ MA_API ma_result ma_context_get_device_info(ma_context* pContext, ma_device_type
} }
ma_mutex_unlock(&pContext->deviceInfoLock); ma_mutex_unlock(&pContext->deviceInfoLock);
/*
If the backend is using the new device info system, do a pass to fill out the old settings for backwards compatibility. This will be removed in
the future when all backends have implemented the new device info system.
*/
if (deviceInfo.nativeDataFormatCount > 0) {
ma_uint32 iNativeFormat;
ma_uint32 iSampleFormat;
deviceInfo.minChannels = 0xFFFFFFFF;
deviceInfo.maxChannels = 0;
deviceInfo.minSampleRate = 0xFFFFFFFF;
deviceInfo.maxSampleRate = 0;
for (iNativeFormat = 0; iNativeFormat < deviceInfo.nativeDataFormatCount; iNativeFormat += 1) {
/* Formats. */
if (deviceInfo.nativeDataFormats[iNativeFormat].format == ma_format_unknown) {
/* All formats are supported. */
deviceInfo.formats[0] = ma_format_u8;
deviceInfo.formats[1] = ma_format_s16;
deviceInfo.formats[2] = ma_format_s24;
deviceInfo.formats[3] = ma_format_s32;
deviceInfo.formats[4] = ma_format_f32;
deviceInfo.formatCount = ma_format_count;
} else {
/* Make sure the format isn't already in the list. If so, skip. */
ma_bool32 alreadyExists = MA_FALSE;
for (iSampleFormat = 0; iSampleFormat < deviceInfo.formatCount; iSampleFormat += 1) {
if (deviceInfo.formats[iSampleFormat] == deviceInfo.nativeDataFormats[iNativeFormat].format) {
alreadyExists = MA_TRUE;
break;
}
}
if (!alreadyExists) {
deviceInfo.formats[deviceInfo.formatCount++] = deviceInfo.nativeDataFormats[iNativeFormat].format;
}
}
/* Channels. */
if (deviceInfo.nativeDataFormats[iNativeFormat].channels == 0) {
/* All channels supported. */
deviceInfo.minChannels = MA_MIN_CHANNELS;
deviceInfo.maxChannels = MA_MAX_CHANNELS;
} else {
if (deviceInfo.minChannels > deviceInfo.nativeDataFormats[iNativeFormat].channels) {
deviceInfo.minChannels = deviceInfo.nativeDataFormats[iNativeFormat].channels;
}
if (deviceInfo.maxChannels < deviceInfo.nativeDataFormats[iNativeFormat].channels) {
deviceInfo.maxChannels = deviceInfo.nativeDataFormats[iNativeFormat].channels;
}
}
/* Sample rate. */
if (deviceInfo.nativeDataFormats[iNativeFormat].sampleRate == 0) {
/* All sample rates supported. */
deviceInfo.minSampleRate = MA_MIN_SAMPLE_RATE;
deviceInfo.maxSampleRate = MA_MAX_SAMPLE_RATE;
} else {
if (deviceInfo.minSampleRate > deviceInfo.nativeDataFormats[iNativeFormat].sampleRate) {
deviceInfo.minSampleRate = deviceInfo.nativeDataFormats[iNativeFormat].sampleRate;
}
if (deviceInfo.maxSampleRate < deviceInfo.nativeDataFormats[iNativeFormat].sampleRate) {
deviceInfo.maxSampleRate = deviceInfo.nativeDataFormats[iNativeFormat].sampleRate;
}
}
}
}
/* Clamp ranges. */ /* Clamp ranges. */
deviceInfo.minChannels = ma_max(deviceInfo.minChannels, MA_MIN_CHANNELS); deviceInfo.minChannels = ma_max(deviceInfo.minChannels, MA_MIN_CHANNELS);
deviceInfo.maxChannels = ma_min(deviceInfo.maxChannels, MA_MAX_CHANNELS); deviceInfo.maxChannels = ma_min(deviceInfo.maxChannels, MA_MAX_CHANNELS);
...@@ -31950,23 +32399,6 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC ...@@ -31950,23 +32399,6 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC
ma_device__post_init_setup(pDevice, pConfig->deviceType); ma_device__post_init_setup(pDevice, pConfig->deviceType);
/* If the backend did not fill out a name for the device, try a generic method. */
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_duplex) {
if (pDevice->capture.name[0] == '\0') {
if (ma_context__try_get_device_name_by_id(pContext, ma_device_type_capture, config.capture.pDeviceID, pDevice->capture.name, sizeof(pDevice->capture.name)) != MA_SUCCESS) {
ma_strncpy_s(pDevice->capture.name, sizeof(pDevice->capture.name), (config.capture.pDeviceID == NULL) ? MA_DEFAULT_CAPTURE_DEVICE_NAME : "Capture Device", (size_t)-1);
}
}
}
if (pDevice->type == ma_device_type_playback || pDevice->type == ma_device_type_duplex || pDevice->type == ma_device_type_loopback) {
if (pDevice->playback.name[0] == '\0') {
if (ma_context__try_get_device_name_by_id(pContext, ma_device_type_playback, config.playback.pDeviceID, pDevice->playback.name, sizeof(pDevice->playback.name)) != MA_SUCCESS) {
ma_strncpy_s(pDevice->playback.name, sizeof(pDevice->playback.name), (config.playback.pDeviceID == NULL) ? MA_DEFAULT_PLAYBACK_DEVICE_NAME : "Playback Device", (size_t)-1);
}
}
}
/* Some backends don't require the worker thread. */ /* Some backends don't require the worker thread. */
if (!ma_context_is_backend_asynchronous(pContext)) { if (!ma_context_is_backend_asynchronous(pContext)) {
/* The worker thread. */ /* The worker thread. */
...@@ -32011,7 +32443,7 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC ...@@ -32011,7 +32443,7 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC
ma_post_log_messagef(pContext, pDevice, MA_LOG_LEVEL_INFO, " Passthrough: %s", pDevice->playback.converter.isPassthrough ? "YES" : "NO"); ma_post_log_messagef(pContext, pDevice, MA_LOG_LEVEL_INFO, " Passthrough: %s", pDevice->playback.converter.isPassthrough ? "YES" : "NO");
} }
MA_ASSERT(ma_device__get_state(pDevice) == MA_STATE_STOPPED); MA_ASSERT(ma_device_get_state(pDevice) == MA_STATE_STOPPED);
return MA_SUCCESS; return MA_SUCCESS;
} }
...@@ -32123,11 +32555,11 @@ MA_API ma_result ma_device_start(ma_device* pDevice) ...@@ -32123,11 +32555,11 @@ MA_API ma_result ma_device_start(ma_device* pDevice)
return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_start() called with invalid arguments (pDevice == NULL).", MA_INVALID_ARGS); return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_start() called with invalid arguments (pDevice == NULL).", MA_INVALID_ARGS);
} }
if (ma_device__get_state(pDevice) == MA_STATE_UNINITIALIZED) { if (ma_device_get_state(pDevice) == MA_STATE_UNINITIALIZED) {
return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_start() called for an uninitialized device.", MA_DEVICE_NOT_INITIALIZED); return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_start() called for an uninitialized device.", MA_DEVICE_NOT_INITIALIZED);
} }
if (ma_device__get_state(pDevice) == MA_STATE_STARTED) { if (ma_device_get_state(pDevice) == MA_STATE_STARTED) {
return ma_post_error(pDevice, MA_LOG_LEVEL_WARNING, "ma_device_start() called when the device is already started.", MA_INVALID_OPERATION); /* Already started. Returning an error to let the application know because it probably means they're doing something wrong. */ return ma_post_error(pDevice, MA_LOG_LEVEL_WARNING, "ma_device_start() called when the device is already started.", MA_INVALID_OPERATION); /* Already started. Returning an error to let the application know because it probably means they're doing something wrong. */
} }
...@@ -32135,7 +32567,7 @@ MA_API ma_result ma_device_start(ma_device* pDevice) ...@@ -32135,7 +32567,7 @@ MA_API ma_result ma_device_start(ma_device* pDevice)
ma_mutex_lock(&pDevice->lock); ma_mutex_lock(&pDevice->lock);
{ {
/* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a stopped or paused state. */ /* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a stopped or paused state. */
MA_ASSERT(ma_device__get_state(pDevice) == MA_STATE_STOPPED); MA_ASSERT(ma_device_get_state(pDevice) == MA_STATE_STOPPED);
ma_device__set_state(pDevice, MA_STATE_STARTING); ma_device__set_state(pDevice, MA_STATE_STARTING);
...@@ -32173,11 +32605,11 @@ MA_API ma_result ma_device_stop(ma_device* pDevice) ...@@ -32173,11 +32605,11 @@ MA_API ma_result ma_device_stop(ma_device* pDevice)
return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_stop() called with invalid arguments (pDevice == NULL).", MA_INVALID_ARGS); return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_stop() called with invalid arguments (pDevice == NULL).", MA_INVALID_ARGS);
} }
if (ma_device__get_state(pDevice) == MA_STATE_UNINITIALIZED) { if (ma_device_get_state(pDevice) == MA_STATE_UNINITIALIZED) {
return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_stop() called for an uninitialized device.", MA_DEVICE_NOT_INITIALIZED); return ma_post_error(pDevice, MA_LOG_LEVEL_ERROR, "ma_device_stop() called for an uninitialized device.", MA_DEVICE_NOT_INITIALIZED);
} }
if (ma_device__get_state(pDevice) == MA_STATE_STOPPED) { if (ma_device_get_state(pDevice) == MA_STATE_STOPPED) {
return ma_post_error(pDevice, MA_LOG_LEVEL_WARNING, "ma_device_stop() called when the device is already stopped.", MA_INVALID_OPERATION); /* Already stopped. Returning an error to let the application know because it probably means they're doing something wrong. */ return ma_post_error(pDevice, MA_LOG_LEVEL_WARNING, "ma_device_stop() called when the device is already stopped.", MA_INVALID_OPERATION); /* Already stopped. Returning an error to let the application know because it probably means they're doing something wrong. */
} }
...@@ -32185,7 +32617,7 @@ MA_API ma_result ma_device_stop(ma_device* pDevice) ...@@ -32185,7 +32617,7 @@ MA_API ma_result ma_device_stop(ma_device* pDevice)
ma_mutex_lock(&pDevice->lock); ma_mutex_lock(&pDevice->lock);
{ {
/* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a started or paused state. */ /* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a started or paused state. */
MA_ASSERT(ma_device__get_state(pDevice) == MA_STATE_STARTED); MA_ASSERT(ma_device_get_state(pDevice) == MA_STATE_STARTED);
ma_device__set_state(pDevice, MA_STATE_STOPPING); ma_device__set_state(pDevice, MA_STATE_STOPPING);
...@@ -32215,13 +32647,18 @@ MA_API ma_result ma_device_stop(ma_device* pDevice) ...@@ -32215,13 +32647,18 @@ MA_API ma_result ma_device_stop(ma_device* pDevice)
return result; return result;
} }
MA_API ma_bool32 ma_device_is_started(ma_device* pDevice) MA_API ma_bool32 ma_device_is_started(const ma_device* pDevice)
{
return ma_device_get_state(pDevice) == MA_STATE_STARTED;
}
MA_API ma_uint32 ma_device_get_state(const ma_device* pDevice)
{ {
if (pDevice == NULL) { if (pDevice == NULL) {
return MA_FALSE; return MA_STATE_UNINITIALIZED;
} }
return ma_device__get_state(pDevice) == MA_STATE_STARTED; return pDevice->state;
} }
MA_API ma_result ma_device_set_master_volume(ma_device* pDevice, float volume) MA_API ma_result ma_device_set_master_volume(ma_device* pDevice, float volume)
...@@ -32283,6 +32720,46 @@ MA_API ma_result ma_device_get_master_gain_db(ma_device* pDevice, float* pGainDB ...@@ -32283,6 +32720,46 @@ MA_API ma_result ma_device_get_master_gain_db(ma_device* pDevice, float* pGainDB
return MA_SUCCESS; return MA_SUCCESS;
} }
MA_API ma_result ma_device_handle_backend_data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
if (pDevice == NULL) {
return MA_INVALID_ARGS;
}
if (pOutput == NULL && pInput == NULL) {
return MA_INVALID_ARGS;
}
if (pDevice->type == ma_device_type_duplex) {
if (pInput != NULL) {
ma_device__handle_duplex_callback_capture(pDevice, frameCount, pInput, &pDevice->duplexRB.rb);
}
if (pOutput != NULL) {
ma_device__handle_duplex_callback_playback(pDevice, frameCount, pOutput, &pDevice->duplexRB.rb);
}
} else {
if (pDevice->type == ma_device_type_capture || pDevice->type == ma_device_type_loopback) {
if (pInput == NULL) {
return MA_INVALID_ARGS;
}
ma_device__send_frames_to_client(pDevice, frameCount, pInput);
}
if (pDevice->type == ma_device_type_playback) {
if (pOutput == NULL) {
return MA_INVALID_ARGS;
}
ma_device__read_frames_from_client(pDevice, frameCount, pOutput);
}
}
return MA_SUCCESS;
}
#endif /* MA_NO_DEVICE_IO */ #endif /* MA_NO_DEVICE_IO */
...@@ -41140,6 +41617,7 @@ MA_API void* ma_rb_get_subbuffer_ptr(ma_rb* pRB, size_t subbufferIndex, void* pB ...@@ -41140,6 +41617,7 @@ MA_API void* ma_rb_get_subbuffer_ptr(ma_rb* pRB, size_t subbufferIndex, void* pB
} }
static MA_INLINE ma_uint32 ma_pcm_rb_get_bpf(ma_pcm_rb* pRB) static MA_INLINE ma_uint32 ma_pcm_rb_get_bpf(ma_pcm_rb* pRB)
{ {
MA_ASSERT(pRB != NULL); MA_ASSERT(pRB != NULL);
...@@ -41338,6 +41816,35 @@ MA_API void* ma_pcm_rb_get_subbuffer_ptr(ma_pcm_rb* pRB, ma_uint32 subbufferInde ...@@ -41338,6 +41816,35 @@ MA_API void* ma_pcm_rb_get_subbuffer_ptr(ma_pcm_rb* pRB, ma_uint32 subbufferInde
MA_API ma_result ma_duplex_rb_init(ma_uint32 inputSampleRate, ma_format captureFormat, ma_uint32 captureChannels, ma_uint32 captureSampleRate, ma_uint32 capturePeriodSizeInFrames, const ma_allocation_callbacks* pAllocationCallbacks, ma_duplex_rb* pRB)
{
ma_result result;
ma_uint32 sizeInFrames;
sizeInFrames = (ma_uint32)ma_calculate_frame_count_after_resampling(inputSampleRate, captureSampleRate, capturePeriodSizeInFrames * 5);
if (sizeInFrames == 0) {
return MA_INVALID_ARGS;
}
result = ma_pcm_rb_init(captureFormat, captureChannels, sizeInFrames, NULL, pAllocationCallbacks, &pRB->rb);
if (result != MA_SUCCESS) {
return result;
}
/* Seek forward a bit so we have a bit of a buffer in case of desyncs. */
ma_pcm_rb_seek_write((ma_pcm_rb*)pRB, capturePeriodSizeInFrames * 2);
return MA_SUCCESS;
}
MA_API ma_result ma_duplex_rb_uninit(ma_duplex_rb* pRB)
{
ma_pcm_rb_uninit((ma_pcm_rb*)pRB);
return MA_SUCCESS;
}
/************************************************************************************************************************************************************** /**************************************************************************************************************************************************************
Miscellaneous Helpers Miscellaneous Helpers
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment