21 #include "../SDL_internal.h"
29 #include "../thread/SDL_systhread.h"
31 #define _THIS SDL_AudioDevice *_this
38 #if SDL_AUDIO_DRIVER_PULSEAUDIO
41 #if SDL_AUDIO_DRIVER_ALSA
44 #if SDL_AUDIO_DRIVER_SNDIO
47 #if SDL_AUDIO_DRIVER_NETBSD
50 #if SDL_AUDIO_DRIVER_OSS
53 #if SDL_AUDIO_DRIVER_QSA
56 #if SDL_AUDIO_DRIVER_SUNAUDIO
59 #if SDL_AUDIO_DRIVER_ARTS
62 #if SDL_AUDIO_DRIVER_ESD
65 #if SDL_AUDIO_DRIVER_NACL
68 #if SDL_AUDIO_DRIVER_NAS
71 #if SDL_AUDIO_DRIVER_WASAPI
74 #if SDL_AUDIO_DRIVER_DSOUND
77 #if SDL_AUDIO_DRIVER_WINMM
80 #if SDL_AUDIO_DRIVER_PAUDIO
83 #if SDL_AUDIO_DRIVER_HAIKU
86 #if SDL_AUDIO_DRIVER_COREAUDIO
89 #if SDL_AUDIO_DRIVER_FUSIONSOUND
92 #if SDL_AUDIO_DRIVER_ANDROID
95 #if SDL_AUDIO_DRIVER_PSP
98 #if SDL_AUDIO_DRIVER_EMSCRIPTEN
101 #if SDL_AUDIO_DRIVER_JACK
104 #if SDL_AUDIO_DRIVER_DISK
107 #if SDL_AUDIO_DRIVER_DUMMY
114 #ifdef HAVE_LIBSAMPLERATE_H
115 #ifdef SDL_LIBSAMPLERATE_DYNAMIC
116 static void *SRC_lib =
NULL;
119 int SRC_converter = 0;
120 SRC_STATE* (*SRC_src_new)(
int converter_type,
int channels,
int *error) =
NULL;
121 int (*SRC_src_process)(SRC_STATE *
state, SRC_DATA *
data) =
NULL;
122 int (*SRC_src_reset)(SRC_STATE *
state) =
NULL;
123 SRC_STATE* (*SRC_src_delete)(SRC_STATE *
state) =
NULL;
124 const char* (*SRC_src_strerror)(
int error) =
NULL;
127 LoadLibSampleRate(
void)
134 if (!hint || *hint ==
'0' ||
SDL_strcasecmp(hint,
"default") == 0) {
137 SRC_converter = SRC_SINC_FASTEST;
139 SRC_converter = SRC_SINC_MEDIUM_QUALITY;
141 SRC_converter = SRC_SINC_BEST_QUALITY;
146 #ifdef SDL_LIBSAMPLERATE_DYNAMIC
154 SRC_src_new = (SRC_STATE* (*)(
int converter_type,
int channels,
int *error))
SDL_LoadFunction(SRC_lib,
"src_new");
158 SRC_src_strerror = (
const char* (*)(
int error))
SDL_LoadFunction(SRC_lib,
"src_strerror");
160 if (!SRC_src_new || !SRC_src_process || !SRC_src_reset || !SRC_src_delete || !SRC_src_strerror) {
166 SRC_src_new = src_new;
167 SRC_src_process = src_process;
168 SRC_src_reset = src_reset;
169 SRC_src_delete = src_delete;
170 SRC_src_strerror = src_strerror;
178 UnloadLibSampleRate(
void)
180 #ifdef SDL_LIBSAMPLERATE_DYNAMIC
181 if (SRC_lib !=
NULL) {
189 SRC_src_process =
NULL;
190 SRC_src_reset =
NULL;
191 SRC_src_delete =
NULL;
192 SRC_src_strerror =
NULL;
205 return open_devices[
id];
350 #define FILL_STUB(x) \
351 if (current_audio.impl.x == NULL) { \
352 current_audio.impl.x = SDL_Audio##x##_Default; \
405 for (i = *devices; i !=
NULL; i = i->
next) {
423 SDL_snprintf(replacement, len,
"%s (%d)", name, dupenum + 1);
425 item->
name = replacement;
430 retval = (*devCount)++;
454 for (item = *devices; item !=
NULL; item = next) {
476 if (device_index != -1) {
482 event.adevice.which = device_index;
483 event.adevice.iscapture = iscapture;
513 event.adevice.which = device->
id;
514 event.adevice.iscapture = device->
iscapture ? 1 : 0;
524 for (item = devices; item !=
NULL; item = item->
next) {
525 if (item->
handle == handle) {
546 for (device_index = 0; device_index <
SDL_arraysize(open_devices); device_index++)
548 device = open_devices[device_index];
549 if (device !=
NULL && device->
handle == handle)
577 len -= (int) dequeued;
610 return SDL_SetError(
"This is a capture device, queueing not allowed");
612 return SDL_SetError(
"Audio device has a callback, queueing not allowed");
788 const int data_len = device->
spec.
size;
819 still_need = data_len;
834 while (still_need > 0) {
847 if (still_need > 0) {
893 #define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
912 #undef CHECK_FMT_STRING
936 int tried_to_init = 0;
946 if (driver_name ==
NULL) {
950 for (i = 0; (!initialized) && (bootstrap[i]); ++
i) {
962 initialized = backend->
init(¤t_audio.
impl);
967 if (!tried_to_init) {
969 SDL_SetError(
"Audio target '%s' not available", driver_name);
986 #ifdef HAVE_LIBSAMPLERATE_H
999 return current_audio.
name;
1084 for (i--; i >
index; i--, item = item->
next) {
1088 retval = item->name;
1093 if (retval ==
NULL) {
1126 if (device->
id > 0) {
1129 if (opendev == device) {
1130 open_devices[device->
id - 1] =
NULL;
1154 if (orig->
freq == 0) {
1155 const char *env =
SDL_getenv(
"SDL_AUDIO_FREQUENCY");
1156 if ((!env) || ((prepared->
freq =
SDL_atoi(env)) == 0)) {
1157 prepared->
freq = 22050;
1162 const char *env =
SDL_getenv(
"SDL_AUDIO_FORMAT");
1170 const char *env =
SDL_getenv(
"SDL_AUDIO_CHANNELS");
1188 const char *env =
SDL_getenv(
"SDL_AUDIO_SAMPLES");
1194 while (power2 < samples) {
1210 int allowed_changes,
int min_id)
1232 for (
id = min_id - 1;
id <
SDL_arraysize(open_devices);
id++) {
1233 if (open_devices[
id] ==
NULL) {
1244 obtained = &_obtained;
1251 if (devname ==
NULL) {
1252 devname =
SDL_getenv(
"SDL_AUDIO_DEVICE_NAME");
1273 if ((open_devices[i]) && (open_devices[
i]->
iscapture)) {
1286 if ((open_devices[i]) && (!open_devices[
i]->
iscapture)) {
1291 }
else if (devname !=
NULL) {
1311 if ((handle ==
NULL) && (devname !=
NULL)) {
1318 if (device ==
NULL) {
1322 device->
id =
id + 1;
1323 device->
spec = *obtained;
1341 if (current_audio.
impl.
OpenDevice(device, handle, devname, iscapture) < 0) {
1435 const size_t stacksize = is_internal_thread ? 64 * 1024 : 0;
1436 char threadname[64];
1438 SDL_snprintf(threadname,
sizeof (threadname),
"SDLAudio%c%d", (iscapture) ?
'C' :
'P', (
int) device->
id);
1465 if (open_devices[0] !=
NULL) {
1485 return (
id == 0) ? -1 : 0;
1491 int allowed_changes)
1494 allowed_changes, 2);
1586 if (!current_audio.
name) {
1605 #ifdef HAVE_LIBSAMPLERATE_H
1606 UnloadLibSampleRate();
1612 #define NUM_FORMATS 10
1642 if (format_list[format_idx][0] == format) {
1656 return format_list[
format_idx][format_idx_sub++];
1685 if (device !=
NULL) {