mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-21 15:56:58 +02:00
8890eac6aa
This includes API changes that affect custom backends.
`ma_backend_callbacks` has been renamed to `ma_device_backend_vtable`.
The reason for this change is to to be consistent with the naming
convention used in other parts of the library. In addition, using the
term "device backend" rather than just "backend" removes ambiguity with
decoding backends.
A change has been made to the way backends manage their internal state,
and some functions in the vtable have been updated to reflect this.
Previously internal state for stock backends were located directly in
the `ma_device` structure. This works fine if stock backends are the
only backends to be concerned about, but it falls apart when you need
to consider how to manage the internal state of custom backends since
they cannot modify the `ma_device` structure. In order to simplify and
unify state management between stock and custom backends, the decision
was made to change the backend system such that backends now manage
their own internal state.
When the context is initialized with `onContextInit`, the backend must
now allocate an internal state object and output a pointer to it via
an output parameter. Typically you would do something like this:
ma_result custom_context_init(..., void** ppContextState)
{
ctx_state_t* state = malloc(...);
...
*ppContextState = state;
return MA_SUCCESS;
}
miniaudio will store a pointer to the state object internally. When you
need to access this later in other backend callbacks, you can retrieve
it straight from the context with `ma_context_get_backend_state()`:
state = (ctx_state_t*)ma_context_get_backend_state(pContext);
The same idea applies to devices and `onDeviceInit`. You can use
`ma_device_get_backend_state()` to get a pointer to the internal state
object.
When a context and device is initialized, backend-specific
configurations can be supplied. The way these configs are provided to
`onContextInit` and `onDeviceInit` has been changed. Previously, these
callbacks would take a `ma_context/device_config` object. These have
been replaced with a `const void*` which points to a backend-specific
config object which is defined by the backend. All stock backends have
their own backend-specific config object:
struct ma_context_config_wasapi
struct ma_context_config_pulseaudio
etc.
struct ma_device_config_wasapi
struct ma_device_config_pulseaudio
etc.
You can cast the config object inside the relevant callbacks:
ma_result custom_context_init(..., const void* pBackendConfig, ...)
{
ctx_config_t* pCustomConfig = (ctx_config_t*)pBackendConfig;
}
The backend itself defines whether or not a config is required. None of
the stock backends require a config. If the config is NULL, it'll use
defaults. It's recommended custom backends follow this convention.
In addition to the above, `onContextUninit` and `onDeviceUninit` have
been updated to return void instead of `ma_result`.
The last change to the backend vtable is a new callback called
`onBackendInfo`. This is used to fill the `ma_device_backend_info`
structure.
In addition to the backend vtable, some changes have been made to the
public API to make it much easier to support plugging in custom
backends.
Previously, plugging in more than one custom backend was a complete
mess. It was possible, but you had to use a stupid wrapper thing to
make it work, and you had no control over prioritization. The entire
thing was just aweful, so it's now been stripped out and replaced with
a brand new system.
When a context or device is initialized, it is done so with a config
which is standard across the entire library. A complication to this is
that backends can sometimes require their own backend-specific configs.
But since miniaudio cannot possibly know about custom backends, it
cannot put their config options inside `ma_context/device_config`. The
functions for initializing a context and device have been updated to
allow plugging in backend-specific configs.
When initializing a context, instead of passing in an array of
`ma_backend` enums, an array of `ma_device_backend_config` objects is
passed in instead. This object has two members: A pointer to a backend
vtable, and a pointer to a config object. It can be initialized
something like this:
ma_context_config_custom customContextConfig;
... initialize the custom backend config if necessary ...
ma_device_backend_config backends[] =
{
{ ma_device_backend_custom, &customContextConfig },
{ ma_device_backend_wasapi, NULL },
{ ma_device_backend_pulseaudio, NULL }
};
ma_context_init(backends, backendCount, ...);
Here `ma_device_backend_custom` is our custom backend. You can see how
the config is mapped to the backend. For stock backends (WASAPI and
PulseAudio in this example), you can pass in NULL and just set the
relevant config options straight in `ma_context_config` exactly how it
was done before:
ma_context_config contextConfig = ma_context_config_init();
contextConfig.pulseaudio.pApplicationName = "My App";
Here we are just using the standard `ma_context_config` object for
configuring the stock PulseAudio backend. This is possible for all
stock backends, but for custom backends an explicit config object will
be required. You can still use a separate explicit config object for
stock backends if you prefer that style:
ma_context_config_pulseaudio paContextConfig;
paContextConfig = ma_context_config_pulseaudio_init();
paContextConfig.pApplicationName = "My App";
ma_device_backend_config backends[] =
{
{ ma_device_backend_pulseaudio, &paContextConfig }
};
Note that if you do not use custom backends, you can still pass in NULL
for the backends in which case defaults will be used like how it's
always worked in the past.
As with contexts, devices can also have their own backend-specific
configs associated with them. These work exactly the same way, except
these configs are passed into the main `ma_device_config` object. (A
future commit may make this consistent between contexts and devices).
ma_device_backend_config deviceBackendConfigs[] =
{
{ ma_device_backend_custom, &customDeviceConfig }
};
deviceConfig.pBackendConfigs = deviceBackendConfigs;
deviceConfig.backendConfigCount = backendCount;
ma_device_init(&deviceConfig, &device);
This commit is just the start of many backend related changes. Future
commits will be cleaning up a lot of residual code from the old system,
such as removing `ma_backend`.
79 lines
2.6 KiB
C
79 lines
2.6 KiB
C
/*
|
|
Demonstrates how to implement loopback recording.
|
|
|
|
This example simply captures data from your default playback device until you press Enter. The output is saved to the
|
|
file specified on the command line.
|
|
|
|
Loopback mode is when you record audio that is played from a given speaker. It is only supported on WASAPI, but can be
|
|
used indirectly with PulseAudio by choosing the appropriate loopback device after enumeration.
|
|
|
|
To use loopback mode you just need to set the device type to ma_device_type_loopback and set the capture device config
|
|
properties. The output buffer in the callback will be null whereas the input buffer will be valid.
|
|
*/
|
|
#include "../miniaudio.c"
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
|
|
{
|
|
ma_encoder_write_pcm_frames((ma_encoder*)pDevice->pUserData, pInput, frameCount, NULL);
|
|
|
|
(void)pOutput;
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
ma_result result;
|
|
ma_encoder_config encoderConfig;
|
|
ma_encoder encoder;
|
|
ma_device_config deviceConfig;
|
|
ma_device device;
|
|
|
|
/* Loopback mode is currently only supported on WASAPI. */
|
|
ma_device_backend_config backends[] = {
|
|
{ ma_device_backend_wasapi, NULL }
|
|
};
|
|
|
|
if (argc < 2) {
|
|
printf("No output file.\n");
|
|
return -1;
|
|
}
|
|
|
|
encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100);
|
|
|
|
if (ma_encoder_init_file(argv[1], &encoderConfig, &encoder) != MA_SUCCESS) {
|
|
printf("Failed to initialize output file.\n");
|
|
return -1;
|
|
}
|
|
|
|
deviceConfig = ma_device_config_init(ma_device_type_loopback);
|
|
deviceConfig.capture.pDeviceID = NULL; /* Use default device for this example. Set this to the ID of a _playback_ device if you want to capture from a specific device. */
|
|
deviceConfig.capture.format = encoder.config.format;
|
|
deviceConfig.capture.channels = encoder.config.channels;
|
|
deviceConfig.sampleRate = encoder.config.sampleRate;
|
|
deviceConfig.dataCallback = data_callback;
|
|
deviceConfig.pUserData = &encoder;
|
|
|
|
result = ma_device_init_ex(backends, sizeof(backends)/sizeof(backends[0]), NULL, &deviceConfig, &device);
|
|
if (result != MA_SUCCESS) {
|
|
printf("Failed to initialize loopback device.\n");
|
|
return -2;
|
|
}
|
|
|
|
result = ma_device_start(&device);
|
|
if (result != MA_SUCCESS) {
|
|
ma_device_uninit(&device);
|
|
printf("Failed to start device.\n");
|
|
return -3;
|
|
}
|
|
|
|
printf("Press Enter to stop recording...\n");
|
|
getchar();
|
|
|
|
ma_device_uninit(&device);
|
|
ma_encoder_uninit(&encoder);
|
|
|
|
return 0;
|
|
}
|