src/audio/SDL_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 08 Jan 2017 14:18:03 -0500
changeset 10790 92726dd20acd
parent 10772 ffb8f194db6e
child 10792 98447b8fda53
permissions -rw-r--r--
audio: libsamplerate loading now happens once at init time.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* Allow access to a raw mixing buffer */
slouken@0
    24
slouken@0
    25
#include "SDL.h"
slouken@2984
    26
#include "SDL_audio.h"
slouken@0
    27
#include "SDL_audio_c.h"
slouken@0
    28
#include "SDL_sysaudio.h"
icculus@10146
    29
#include "../thread/SDL_systhread.h"
slouken@0
    30
aschiffler@4865
    31
#define _THIS SDL_AudioDevice *_this
icculus@2049
    32
icculus@2049
    33
static SDL_AudioDriver current_audio;
icculus@2049
    34
static SDL_AudioDevice *open_devices[16];
icculus@2049
    35
slouken@0
    36
/* Available audio drivers */
slouken@3162
    37
static const AudioBootStrap *const bootstrap[] = {
icculus@2939
    38
#if SDL_AUDIO_DRIVER_PULSEAUDIO
icculus@2939
    39
    &PULSEAUDIO_bootstrap,
slouken@0
    40
#endif
slouken@1361
    41
#if SDL_AUDIO_DRIVER_ALSA
slouken@1895
    42
    &ALSA_bootstrap,
slouken@0
    43
#endif
icculus@7367
    44
#if SDL_AUDIO_DRIVER_SNDIO
icculus@7367
    45
    &SNDIO_bootstrap,
icculus@7367
    46
#endif
slouken@4548
    47
#if SDL_AUDIO_DRIVER_BSD
slouken@4548
    48
    &BSD_AUDIO_bootstrap,
slouken@4548
    49
#endif
icculus@2939
    50
#if SDL_AUDIO_DRIVER_OSS
icculus@2939
    51
    &DSP_bootstrap,
icculus@2271
    52
#endif
slouken@3099
    53
#if SDL_AUDIO_DRIVER_QSA
slouken@3099
    54
    &QSAAUDIO_bootstrap,
slouken@663
    55
#endif
slouken@1361
    56
#if SDL_AUDIO_DRIVER_SUNAUDIO
slouken@1895
    57
    &SUNAUDIO_bootstrap,
slouken@148
    58
#endif
slouken@1361
    59
#if SDL_AUDIO_DRIVER_ARTS
slouken@1895
    60
    &ARTS_bootstrap,
slouken@0
    61
#endif
slouken@1361
    62
#if SDL_AUDIO_DRIVER_ESD
slouken@1895
    63
    &ESD_bootstrap,
slouken@0
    64
#endif
gabomdq@8833
    65
#if SDL_AUDIO_DRIVER_NACL
icculus@10281
    66
    &NACLAUDIO_bootstrap,
gabomdq@8833
    67
#endif
slouken@1361
    68
#if SDL_AUDIO_DRIVER_NAS
slouken@1895
    69
    &NAS_bootstrap,
slouken@0
    70
#endif
icculus@5592
    71
#if SDL_AUDIO_DRIVER_XAUDIO2
icculus@5592
    72
    &XAUDIO2_bootstrap,
icculus@5592
    73
#endif
slouken@1361
    74
#if SDL_AUDIO_DRIVER_DSOUND
slouken@1895
    75
    &DSOUND_bootstrap,
slouken@0
    76
#endif
icculus@5588
    77
#if SDL_AUDIO_DRIVER_WINMM
icculus@5588
    78
    &WINMM_bootstrap,
slouken@0
    79
#endif
icculus@2049
    80
#if SDL_AUDIO_DRIVER_PAUDIO
icculus@2049
    81
    &PAUDIO_bootstrap,
slouken@1361
    82
#endif
icculus@7981
    83
#if SDL_AUDIO_DRIVER_HAIKU
icculus@7981
    84
    &HAIKUAUDIO_bootstrap,
slouken@0
    85
#endif
slouken@1361
    86
#if SDL_AUDIO_DRIVER_COREAUDIO
slouken@1895
    87
    &COREAUDIO_bootstrap,
slouken@935
    88
#endif
slouken@1361
    89
#if SDL_AUDIO_DRIVER_DISK
icculus@10281
    90
    &DISKAUDIO_bootstrap,
slouken@68
    91
#endif
icculus@1532
    92
#if SDL_AUDIO_DRIVER_DUMMY
icculus@10281
    93
    &DUMMYAUDIO_bootstrap,
icculus@1532
    94
#endif
slouken@2947
    95
#if SDL_AUDIO_DRIVER_FUSIONSOUND
slouken@2947
    96
    &FUSIONSOUND_bootstrap,
slouken@2947
    97
#endif
paul@4718
    98
#if SDL_AUDIO_DRIVER_ANDROID
icculus@10281
    99
    &ANDROIDAUDIO_bootstrap,
paul@4718
   100
#endif
kimonline@7009
   101
#if SDL_AUDIO_DRIVER_PSP
icculus@10281
   102
    &PSPAUDIO_bootstrap,
kimonline@7009
   103
#endif
icculus@9278
   104
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
icculus@10281
   105
    &EMSCRIPTENAUDIO_bootstrap,
icculus@9278
   106
#endif
slouken@1895
   107
    NULL
slouken@0
   108
};
slouken@0
   109
icculus@10790
   110
icculus@10790
   111
#ifdef HAVE_LIBSAMPLERATE_H
icculus@10790
   112
#ifdef SDL_LIBSAMPLERATE_DYNAMIC
icculus@10790
   113
static void *SRC_lib = NULL;
icculus@10790
   114
#endif
icculus@10790
   115
SDL_bool SRC_available = SDL_FALSE;
icculus@10790
   116
SRC_STATE* (*SRC_src_new)(int converter_type, int channels, int *error) = NULL;
icculus@10790
   117
int (*SRC_src_process)(SRC_STATE *state, SRC_DATA *data) = NULL;
icculus@10790
   118
int (*SRC_src_reset)(SRC_STATE *state) = NULL;
icculus@10790
   119
SRC_STATE* (*SRC_src_delete)(SRC_STATE *state) = NULL;
icculus@10790
   120
const char* (*SRC_src_strerror)(int error) = NULL;
icculus@10790
   121
icculus@10790
   122
static SDL_bool
icculus@10790
   123
LoadLibSampleRate(void)
icculus@10790
   124
{
icculus@10790
   125
    SRC_available = SDL_FALSE;
icculus@10790
   126
icculus@10790
   127
    if (!SDL_GetHintBoolean("SDL_AUDIO_ALLOW_LIBRESAMPLE", SDL_TRUE)) {
icculus@10790
   128
        return SDL_FALSE;
icculus@10790
   129
    }
icculus@10790
   130
icculus@10790
   131
#ifdef SDL_LIBSAMPLERATE_DYNAMIC
icculus@10790
   132
    SDL_assert(SRC_lib == NULL);
icculus@10790
   133
    SRC_lib = SDL_LoadObject(SDL_LIBSAMPLERATE_DYNAMIC);
icculus@10790
   134
    if (!SRC_lib) {
icculus@10790
   135
        return SDL_FALSE;
icculus@10790
   136
    }
icculus@10790
   137
#endif
icculus@10790
   138
icculus@10790
   139
    SRC_src_new = (SRC_STATE* (*)(int converter_type, int channels, int *error))SDL_LoadFunction(state->SRC_lib, "src_new");
icculus@10790
   140
    SRC_src_process = (int (*)(SRC_STATE *state, SRC_DATA *data))SDL_LoadFunction(state->SRC_lib, "src_process");
icculus@10790
   141
    SRC_src_reset = (int(*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_reset");
icculus@10790
   142
    SRC_src_delete = (SRC_STATE* (*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_delete");
icculus@10790
   143
    SRC_src_strerror = (const char* (*)(int error))SDL_LoadFunction(state->SRC_lib, "src_strerror");
icculus@10790
   144
icculus@10790
   145
    if (!SRC_src_new || !SRC_src_process || !SRC_src_reset || !SRC_src_delete || !SRC_src_strerror) {
icculus@10790
   146
        #ifdef SDL_LIBSAMPLERATE_DYNAMIC
icculus@10790
   147
        SDL_UnloadObject(SRC_lib);
icculus@10790
   148
        SRC_lib = NULL;
icculus@10790
   149
        #endif
icculus@10790
   150
        return SDL_FALSE;
icculus@10790
   151
    }
icculus@10790
   152
icculus@10790
   153
    SRC_available = SDL_TRUE;
icculus@10790
   154
    return SDL_TRUE;
icculus@10790
   155
}
icculus@10790
   156
icculus@10790
   157
static void
icculus@10790
   158
UnloadLibSampleRate(void)
icculus@10790
   159
{
icculus@10790
   160
#ifdef SDL_LIBSAMPLERATE_DYNAMIC
icculus@10790
   161
    if (SRC_lib != NULL) {
icculus@10790
   162
        SDL_UnloadObject(SRC_lib);
icculus@10790
   163
    }
icculus@10790
   164
    SRC_lib = NULL;
icculus@10790
   165
#endif
icculus@10790
   166
icculus@10790
   167
    SRC_available = SDL_FALSE;
icculus@10790
   168
    SRC_src_new = NULL;
icculus@10790
   169
    SRC_src_process = NULL;
icculus@10790
   170
    SRC_src_reset = NULL;
icculus@10790
   171
    SRC_src_delete = NULL;
icculus@10790
   172
    SRC_src_strerror = NULL;
icculus@10790
   173
}
icculus@10790
   174
#endif
icculus@10790
   175
slouken@2060
   176
static SDL_AudioDevice *
slouken@2060
   177
get_audio_device(SDL_AudioDeviceID id)
icculus@2049
   178
{
icculus@2049
   179
    id--;
slouken@2060
   180
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
icculus@2049
   181
        SDL_SetError("Invalid audio device ID");
icculus@2049
   182
        return NULL;
icculus@2049
   183
    }
slouken@0
   184
icculus@2049
   185
    return open_devices[id];
icculus@2049
   186
}
icculus@2049
   187
icculus@2049
   188
icculus@2049
   189
/* stubs for audio drivers that don't need a specific entry point... */
icculus@5593
   190
static void
icculus@9394
   191
SDL_AudioDetectDevices_Default(void)
icculus@9394
   192
{
icculus@9394
   193
    /* you have to write your own implementation if these assertions fail. */
icculus@9394
   194
    SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice);
icculus@10258
   195
    SDL_assert(current_audio.impl.OnlyHasDefaultCaptureDevice || !current_audio.impl.HasCaptureSupport);
icculus@9394
   196
icculus@9394
   197
    SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) ((size_t) 0x1));
icculus@9394
   198
    if (current_audio.impl.HasCaptureSupport) {
icculus@9394
   199
        SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) ((size_t) 0x2));
icculus@9394
   200
    }
slouken@2060
   201
}
slouken@2735
   202
slouken@2060
   203
static void
slouken@2060
   204
SDL_AudioThreadInit_Default(_THIS)
slouken@2060
   205
{                               /* no-op. */
slouken@2060
   206
}
slouken@2735
   207
slouken@2060
   208
static void
slouken@2060
   209
SDL_AudioWaitDevice_Default(_THIS)
slouken@2060
   210
{                               /* no-op. */
slouken@2060
   211
}
slouken@2735
   212
slouken@2060
   213
static void
slouken@2060
   214
SDL_AudioPlayDevice_Default(_THIS)
slouken@2060
   215
{                               /* no-op. */
slouken@2060
   216
}
slouken@2735
   217
icculus@9031
   218
static int
icculus@9031
   219
SDL_AudioGetPendingBytes_Default(_THIS)
icculus@9031
   220
{
icculus@9031
   221
    return 0;
icculus@9031
   222
}
icculus@9031
   223
slouken@2060
   224
static Uint8 *
slouken@2060
   225
SDL_AudioGetDeviceBuf_Default(_THIS)
slouken@2060
   226
{
slouken@2060
   227
    return NULL;
slouken@2060
   228
}
slouken@2735
   229
icculus@10239
   230
static int
icculus@10239
   231
SDL_AudioCaptureFromDevice_Default(_THIS, void *buffer, int buflen)
icculus@10239
   232
{
icculus@10239
   233
    return -1;  /* just fail immediately. */
icculus@10239
   234
}
icculus@10239
   235
icculus@10239
   236
static void
icculus@10239
   237
SDL_AudioFlushCapture_Default(_THIS)
icculus@10239
   238
{                               /* no-op. */
icculus@10239
   239
}
icculus@10239
   240
slouken@2060
   241
static void
icculus@10473
   242
SDL_AudioPrepareToClose_Default(_THIS)
icculus@10473
   243
{                               /* no-op. */
icculus@10473
   244
}
icculus@10473
   245
icculus@10473
   246
static void
slouken@2060
   247
SDL_AudioCloseDevice_Default(_THIS)
slouken@2060
   248
{                               /* no-op. */
slouken@2060
   249
}
slouken@2735
   250
slouken@2060
   251
static void
slouken@2060
   252
SDL_AudioDeinitialize_Default(void)
slouken@2060
   253
{                               /* no-op. */
slouken@2060
   254
}
icculus@2049
   255
icculus@9394
   256
static void
icculus@9394
   257
SDL_AudioFreeDeviceHandle_Default(void *handle)
icculus@9394
   258
{                               /* no-op. */
icculus@9394
   259
}
icculus@9394
   260
icculus@9394
   261
icculus@2049
   262
static int
icculus@9394
   263
SDL_AudioOpenDevice_Default(_THIS, void *handle, const char *devname, int iscapture)
icculus@2049
   264
{
icculus@9394
   265
    return SDL_Unsupported();
icculus@2049
   266
}
icculus@2049
   267
icculus@9010
   268
static SDL_INLINE SDL_bool
icculus@9010
   269
is_in_audio_device_thread(SDL_AudioDevice * device)
icculus@9010
   270
{
icculus@9010
   271
    /* The device thread locks the same mutex, but not through the public API.
icculus@9010
   272
       This check is in case the application, in the audio callback,
icculus@9010
   273
       tries to lock the thread that we've already locked from the
icculus@9010
   274
       device thread...just in case we only have non-recursive mutexes. */
icculus@9010
   275
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@9010
   276
        return SDL_TRUE;
icculus@9010
   277
    }
icculus@9010
   278
icculus@9010
   279
    return SDL_FALSE;
icculus@9010
   280
}
icculus@9010
   281
icculus@2049
   282
static void
icculus@2049
   283
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   284
{
icculus@9010
   285
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   286
        SDL_LockMutex(device->mixer_lock);
icculus@2049
   287
    }
icculus@2049
   288
}
icculus@2049
   289
icculus@2049
   290
static void
icculus@2049
   291
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   292
{
icculus@9010
   293
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   294
        SDL_UnlockMutex(device->mixer_lock);
icculus@2049
   295
    }
icculus@2049
   296
}
icculus@2049
   297
icculus@10471
   298
static void
icculus@10471
   299
SDL_AudioLockOrUnlockDeviceWithNoMixerLock(SDL_AudioDevice * device)
icculus@10471
   300
{
icculus@10471
   301
}
icculus@2049
   302
slouken@2060
   303
static void
icculus@10471
   304
finish_audio_entry_points_init(void)
icculus@2049
   305
{
icculus@2049
   306
    /*
icculus@2049
   307
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   308
     *  blindly call them without having to check for validity first.
icculus@2049
   309
     */
icculus@2049
   310
icculus@10471
   311
    if (current_audio.impl.SkipMixerLock) {
icculus@10471
   312
        if (current_audio.impl.LockDevice == NULL) {
icculus@10471
   313
            current_audio.impl.LockDevice = SDL_AudioLockOrUnlockDeviceWithNoMixerLock;
icculus@10471
   314
        }
icculus@10471
   315
        if (current_audio.impl.UnlockDevice == NULL) {
icculus@10471
   316
            current_audio.impl.UnlockDevice = SDL_AudioLockOrUnlockDeviceWithNoMixerLock;
icculus@10471
   317
        }
icculus@10471
   318
    }
icculus@10471
   319
slouken@2060
   320
#define FILL_STUB(x) \
icculus@2049
   321
        if (current_audio.impl.x == NULL) { \
icculus@2049
   322
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   323
        }
icculus@2049
   324
    FILL_STUB(DetectDevices);
icculus@2049
   325
    FILL_STUB(OpenDevice);
icculus@2049
   326
    FILL_STUB(ThreadInit);
icculus@2049
   327
    FILL_STUB(WaitDevice);
icculus@2049
   328
    FILL_STUB(PlayDevice);
icculus@9031
   329
    FILL_STUB(GetPendingBytes);
icculus@2049
   330
    FILL_STUB(GetDeviceBuf);
icculus@10239
   331
    FILL_STUB(CaptureFromDevice);
icculus@10239
   332
    FILL_STUB(FlushCapture);
icculus@10473
   333
    FILL_STUB(PrepareToClose);
icculus@2049
   334
    FILL_STUB(CloseDevice);
icculus@2049
   335
    FILL_STUB(LockDevice);
icculus@2049
   336
    FILL_STUB(UnlockDevice);
icculus@9394
   337
    FILL_STUB(FreeDeviceHandle);
icculus@2049
   338
    FILL_STUB(Deinitialize);
slouken@2060
   339
#undef FILL_STUB
icculus@2049
   340
}
icculus@2049
   341
slouken@2716
   342
icculus@9393
   343
/* device hotplug support... */
icculus@9393
   344
icculus@9393
   345
static int
icculus@9394
   346
add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
icculus@9393
   347
{
icculus@9393
   348
    int retval = -1;
icculus@9394
   349
    const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1;
icculus@9394
   350
    SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size);
icculus@9394
   351
    if (item == NULL) {
icculus@9394
   352
        return -1;
icculus@9394
   353
    }
icculus@9393
   354
icculus@9410
   355
    SDL_assert(handle != NULL);  /* we reserve NULL, audio backends can't use it. */
icculus@9394
   356
icculus@9394
   357
    item->handle = handle;
icculus@9394
   358
    SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem));
icculus@9394
   359
icculus@9394
   360
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   361
    item->next = *devices;
icculus@9394
   362
    *devices = item;
icculus@9394
   363
    retval = (*devCount)++;
icculus@9394
   364
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   365
icculus@9393
   366
    return retval;
icculus@9393
   367
}
icculus@9393
   368
icculus@9394
   369
static SDL_INLINE int
icculus@9394
   370
add_capture_device(const char *name, void *handle)
icculus@9393
   371
{
icculus@10231
   372
    SDL_assert(current_audio.impl.HasCaptureSupport);
icculus@9394
   373
    return add_audio_device(name, handle, &current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9393
   374
}
icculus@9393
   375
icculus@9394
   376
static SDL_INLINE int
icculus@9394
   377
add_output_device(const char *name, void *handle)
icculus@9393
   378
{
icculus@9394
   379
    return add_audio_device(name, handle, &current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9393
   380
}
icculus@9393
   381
icculus@9393
   382
static void
icculus@9394
   383
free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
icculus@9393
   384
{
icculus@9394
   385
    SDL_AudioDeviceItem *item, *next;
icculus@9394
   386
    for (item = *devices; item != NULL; item = next) {
icculus@9394
   387
        next = item->next;
icculus@9394
   388
        if (item->handle != NULL) {
icculus@9394
   389
            current_audio.impl.FreeDeviceHandle(item->handle);
icculus@9393
   390
        }
icculus@9394
   391
        SDL_free(item);
icculus@9393
   392
    }
icculus@9393
   393
    *devices = NULL;
icculus@9393
   394
    *devCount = 0;
icculus@9393
   395
}
icculus@9393
   396
icculus@9393
   397
icculus@9393
   398
/* The audio backends call this when a new device is plugged in. */
icculus@9393
   399
void
icculus@9394
   400
SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
icculus@9393
   401
{
icculus@9394
   402
    const int device_index = iscapture ? add_capture_device(name, handle) : add_output_device(name, handle);
icculus@9393
   403
    if (device_index != -1) {
icculus@9393
   404
        /* Post the event, if desired */
icculus@9393
   405
        if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) {
icculus@9393
   406
            SDL_Event event;
icculus@9401
   407
            SDL_zero(event);
icculus@9393
   408
            event.adevice.type = SDL_AUDIODEVICEADDED;
icculus@9393
   409
            event.adevice.which = device_index;
icculus@9393
   410
            event.adevice.iscapture = iscapture;
icculus@9393
   411
            SDL_PushEvent(&event);
icculus@9393
   412
        }
icculus@9393
   413
    }
icculus@9393
   414
}
icculus@9393
   415
icculus@9394
   416
/* The audio backends call this when a currently-opened device is lost. */
icculus@9394
   417
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
icculus@9393
   418
{
icculus@9394
   419
    SDL_assert(get_audio_device(device->id) == device);
icculus@9396
   420
icculus@10238
   421
    if (!SDL_AtomicGet(&device->enabled)) {
icculus@9396
   422
        return;
icculus@9396
   423
    }
icculus@9393
   424
icculus@9394
   425
    /* Ends the audio callback and mark the device as STOPPED, but the
icculus@9394
   426
       app still needs to close the device to free resources. */
icculus@9394
   427
    current_audio.impl.LockDevice(device);
icculus@10238
   428
    SDL_AtomicSet(&device->enabled, 0);
icculus@9394
   429
    current_audio.impl.UnlockDevice(device);
icculus@9393
   430
icculus@9394
   431
    /* Post the event, if desired */
icculus@9394
   432
    if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
icculus@9394
   433
        SDL_Event event;
icculus@9401
   434
        SDL_zero(event);
icculus@9394
   435
        event.adevice.type = SDL_AUDIODEVICEREMOVED;
icculus@9394
   436
        event.adevice.which = device->id;
icculus@9394
   437
        event.adevice.iscapture = device->iscapture ? 1 : 0;
icculus@9394
   438
        SDL_PushEvent(&event);
icculus@9394
   439
    }
icculus@9394
   440
}
icculus@9394
   441
icculus@9394
   442
static void
icculus@9394
   443
mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag)
icculus@9394
   444
{
icculus@9394
   445
    SDL_AudioDeviceItem *item;
icculus@9394
   446
    SDL_assert(handle != NULL);
icculus@9394
   447
    for (item = devices; item != NULL; item = item->next) {
icculus@9394
   448
        if (item->handle == handle) {
icculus@9394
   449
            item->handle = NULL;
icculus@9394
   450
            *removedFlag = SDL_TRUE;
icculus@9394
   451
            return;
icculus@9393
   452
        }
icculus@9393
   453
    }
icculus@9394
   454
}
icculus@9393
   455
icculus@9394
   456
/* The audio backends call this when a device is removed from the system. */
icculus@9394
   457
void
icculus@9399
   458
SDL_RemoveAudioDevice(const int iscapture, void *handle)
icculus@9394
   459
{
slouken@10467
   460
    int device_index;
slouken@10467
   461
    SDL_AudioDevice *device = NULL;
slouken@10467
   462
icculus@9394
   463
    SDL_LockMutex(current_audio.detectionLock);
icculus@9399
   464
    if (iscapture) {
icculus@9399
   465
        mark_device_removed(handle, current_audio.inputDevices, &current_audio.captureDevicesRemoved);
icculus@9399
   466
    } else {
icculus@9399
   467
        mark_device_removed(handle, current_audio.outputDevices, &current_audio.outputDevicesRemoved);
icculus@9399
   468
    }
slouken@10467
   469
    for (device_index = 0; device_index < SDL_arraysize(open_devices); device_index++)
slouken@10467
   470
    {
slouken@10467
   471
        device = open_devices[device_index];
slouken@10467
   472
        if (device != NULL && device->handle == handle)
slouken@10467
   473
        {
slouken@10467
   474
            SDL_OpenedAudioDeviceDisconnected(device);
slouken@10467
   475
            break;
slouken@10467
   476
        }
slouken@10467
   477
    }
icculus@9394
   478
    SDL_UnlockMutex(current_audio.detectionLock);
slouken@10467
   479
icculus@9394
   480
    current_audio.impl.FreeDeviceHandle(handle);
icculus@9393
   481
}
icculus@9393
   482
icculus@9393
   483
icculus@9012
   484
icculus@9012
   485
/* buffer queueing support... */
icculus@9012
   486
icculus@10262
   487
static void SDLCALL
icculus@10262
   488
SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int len)
icculus@10262
   489
{
icculus@10262
   490
    /* this function always holds the mixer lock before being called. */
icculus@10262
   491
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
icculus@10681
   492
    size_t dequeued;
icculus@10262
   493
icculus@10262
   494
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
icculus@10262
   495
    SDL_assert(!device->iscapture);  /* this shouldn't ever happen, right?! */
icculus@10262
   496
    SDL_assert(len >= 0);  /* this shouldn't ever happen, right?! */
icculus@10262
   497
icculus@10681
   498
    dequeued = SDL_ReadFromDataQueue(device->buffer_queue, stream, len);
icculus@10681
   499
    stream += dequeued;
icculus@10681
   500
    len -= (int) dequeued;
icculus@10262
   501
icculus@10262
   502
    if (len > 0) {  /* fill any remaining space in the stream with silence. */
icculus@10681
   503
        SDL_assert(SDL_CountDataQueue(device->buffer_queue) == 0);
icculus@10262
   504
        SDL_memset(stream, device->spec.silence, len);
icculus@10262
   505
    }
icculus@10262
   506
}
icculus@10262
   507
icculus@10262
   508
static void SDLCALL
icculus@10262
   509
SDL_BufferQueueFillCallback(void *userdata, Uint8 *stream, int len)
icculus@10262
   510
{
icculus@10262
   511
    /* this function always holds the mixer lock before being called. */
icculus@10262
   512
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
icculus@10262
   513
icculus@10262
   514
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
icculus@10262
   515
    SDL_assert(device->iscapture);  /* this shouldn't ever happen, right?! */
icculus@10262
   516
    SDL_assert(len >= 0);  /* this shouldn't ever happen, right?! */
icculus@10262
   517
icculus@10262
   518
    /* note that if this needs to allocate more space and run out of memory,
icculus@10262
   519
       we have no choice but to quietly drop the data and hope it works out
icculus@10262
   520
       later, but you probably have bigger problems in this case anyhow. */
icculus@10681
   521
    SDL_WriteToDataQueue(device->buffer_queue, stream, len);
icculus@10262
   522
}
icculus@10262
   523
icculus@10262
   524
int
icculus@10262
   525
SDL_QueueAudio(SDL_AudioDeviceID devid, const void *data, Uint32 len)
icculus@10262
   526
{
icculus@10262
   527
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@10262
   528
    int rc = 0;
icculus@10262
   529
icculus@10262
   530
    if (!device) {
icculus@10262
   531
        return -1;  /* get_audio_device() will have set the error state */
icculus@10262
   532
    } else if (device->iscapture) {
icculus@10262
   533
        return SDL_SetError("This is a capture device, queueing not allowed");
icculus@10262
   534
    } else if (device->spec.callback != SDL_BufferQueueDrainCallback) {
icculus@10262
   535
        return SDL_SetError("Audio device has a callback, queueing not allowed");
icculus@10262
   536
    }
icculus@10262
   537
icculus@10262
   538
    if (len > 0) {
icculus@10262
   539
        current_audio.impl.LockDevice(device);
icculus@10681
   540
        rc = SDL_WriteToDataQueue(device->buffer_queue, data, len);
icculus@10262
   541
        current_audio.impl.UnlockDevice(device);
icculus@10262
   542
    }
icculus@10262
   543
icculus@10262
   544
    return rc;
icculus@10262
   545
}
icculus@10262
   546
icculus@10262
   547
Uint32
icculus@10262
   548
SDL_DequeueAudio(SDL_AudioDeviceID devid, void *data, Uint32 len)
icculus@10262
   549
{
icculus@10262
   550
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@10262
   551
    Uint32 rc;
icculus@10262
   552
icculus@10262
   553
    if ( (len == 0) ||  /* nothing to do? */
icculus@10262
   554
         (!device) ||  /* called with bogus device id */
icculus@10262
   555
         (!device->iscapture) ||  /* playback devices can't dequeue */
icculus@10262
   556
         (device->spec.callback != SDL_BufferQueueFillCallback) ) { /* not set for queueing */
icculus@10262
   557
        return 0;  /* just report zero bytes dequeued. */
icculus@10262
   558
    }
icculus@10262
   559
icculus@10262
   560
    current_audio.impl.LockDevice(device);
icculus@10681
   561
    rc = (Uint32) SDL_ReadFromDataQueue(device->buffer_queue, data, len);
icculus@9012
   562
    current_audio.impl.UnlockDevice(device);
icculus@10262
   563
    return rc;
icculus@9012
   564
}
icculus@9012
   565
icculus@9012
   566
Uint32
icculus@9012
   567
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
icculus@9012
   568
{
icculus@9012
   569
    Uint32 retval = 0;
icculus@9012
   570
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9032
   571
icculus@10262
   572
    if (!device) {
icculus@10262
   573
        return 0;
icculus@10262
   574
    }
icculus@10262
   575
icculus@9032
   576
    /* Nothing to do unless we're set up for queueing. */
icculus@10262
   577
    if (device->spec.callback == SDL_BufferQueueDrainCallback) {
icculus@9012
   578
        current_audio.impl.LockDevice(device);
icculus@10682
   579
        retval = ((Uint32) SDL_CountDataQueue(device->buffer_queue)) + current_audio.impl.GetPendingBytes(device);
icculus@9012
   580
        current_audio.impl.UnlockDevice(device);
icculus@10262
   581
    } else if (device->spec.callback == SDL_BufferQueueFillCallback) {
icculus@10262
   582
        current_audio.impl.LockDevice(device);
icculus@10682
   583
        retval = (Uint32) SDL_CountDataQueue(device->buffer_queue);
icculus@10262
   584
        current_audio.impl.UnlockDevice(device);
icculus@9012
   585
    }
icculus@9012
   586
icculus@9012
   587
    return retval;
icculus@9012
   588
}
icculus@9012
   589
icculus@9012
   590
void
icculus@9012
   591
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
icculus@9012
   592
{
icculus@9012
   593
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@10260
   594
icculus@9012
   595
    if (!device) {
icculus@9012
   596
        return;  /* nothing to do. */
icculus@9012
   597
    }
icculus@9012
   598
icculus@9012
   599
    /* Blank out the device and release the mutex. Free it afterwards. */
icculus@9012
   600
    current_audio.impl.LockDevice(device);
icculus@10260
   601
icculus@10260
   602
    /* Keep up to two packets in the pool to reduce future malloc pressure. */
icculus@10681
   603
    SDL_ClearDataQueue(device->buffer_queue, SDL_AUDIOBUFFERQUEUE_PACKETLEN * 2);
icculus@10260
   604
icculus@9012
   605
    current_audio.impl.UnlockDevice(device);
icculus@9012
   606
}
icculus@9012
   607
icculus@9012
   608
slouken@0
   609
/* The general mixing thread function */
icculus@10239
   610
static int SDLCALL
icculus@2049
   611
SDL_RunAudio(void *devicep)
slouken@0
   612
{
icculus@2049
   613
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@9398
   614
    const int silence = (int) device->spec.silence;
icculus@9398
   615
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
slouken@10772
   616
    const int data_len = device->callbackspec.size;
slouken@10772
   617
    Uint8 *data;
icculus@9398
   618
    void *udata = device->spec.userdata;
icculus@10757
   619
    SDL_AudioCallback callback = device->spec.callback;
icculus@10239
   620
icculus@10239
   621
    SDL_assert(!device->iscapture);
slouken@2716
   622
slouken@5509
   623
    /* The audio mixing is always a high priority thread */
slouken@5509
   624
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
slouken@5509
   625
slouken@1895
   626
    /* Perform any thread setup */
icculus@2049
   627
    device->threadid = SDL_ThreadID();
icculus@2049
   628
    current_audio.impl.ThreadInit(device);
slouken@0
   629
icculus@9398
   630
    /* Loop, filling the audio buffers */
icculus@10233
   631
    while (!SDL_AtomicGet(&device->shutdown)) {
icculus@9398
   632
        /* Fill the current buffer with sound */
icculus@10757
   633
        if (!device->stream && SDL_AtomicGet(&device->enabled)) {
slouken@10772
   634
            SDL_assert(data_len == device->spec.size);
slouken@10772
   635
            data = current_audio.impl.GetDeviceBuf(device);
icculus@9398
   636
        } else {
icculus@9398
   637
            /* if the device isn't enabled, we still write to the
icculus@10765
   638
               work_buffer, so the app's callback will fire with
icculus@9398
   639
               a regular frequency, in case they depend on that
icculus@9398
   640
               for timing or progress. They can use hotplug
icculus@10757
   641
               now to know if the device failed.
icculus@10765
   642
               Streaming playback uses work_buffer, too. */
slouken@10772
   643
            data = NULL;
icculus@9398
   644
        }
slouken@21
   645
slouken@10772
   646
        if (data == NULL) {
slouken@10772
   647
            data = device->work_buffer;
icculus@9398
   648
        }
slouken@2716
   649
slouken@10466
   650
        if ( SDL_AtomicGet(&device->enabled) ) {
icculus@10757
   651
            /* !!! FIXME: this should be LockDevice. */
slouken@10466
   652
            SDL_LockMutex(device->mixer_lock);
slouken@10466
   653
            if (SDL_AtomicGet(&device->paused)) {
slouken@10772
   654
                SDL_memset(data, silence, data_len);
slouken@10466
   655
            } else {
slouken@10772
   656
                callback(udata, data, data_len);
slouken@10466
   657
            }
slouken@10466
   658
            SDL_UnlockMutex(device->mixer_lock);
icculus@10757
   659
        } else {
slouken@10772
   660
            SDL_memset(data, silence, data_len);
icculus@9398
   661
        }
slouken@2716
   662
icculus@10757
   663
        if (device->stream) {
icculus@10757
   664
            /* Stream available audio to device, converting/resampling. */
icculus@10757
   665
            /* if this fails...oh well. We'll play silence here. */
slouken@10772
   666
            SDL_AudioStreamPut(device->stream, data, data_len);
icculus@10757
   667
icculus@10759
   668
            while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->spec.size)) {
slouken@10772
   669
                data = SDL_AtomicGet(&device->enabled) ? current_audio.impl.GetDeviceBuf(device) : NULL;
slouken@10772
   670
                if (data == NULL) {
icculus@10757
   671
                    SDL_AudioStreamClear(device->stream);
icculus@10757
   672
                    SDL_Delay(delay);
icculus@10757
   673
                    break;
icculus@10757
   674
                } else {
slouken@10772
   675
                    const int got = SDL_AudioStreamGet(device->stream, data, device->spec.size);
icculus@10757
   676
                    SDL_assert((got < 0) || (got == device->spec.size));
icculus@10757
   677
                    if (got != device->spec.size) {
slouken@10772
   678
                        SDL_memset(data, device->spec.silence, device->spec.size);
icculus@10757
   679
                    }
icculus@10757
   680
                    current_audio.impl.PlayDevice(device);
icculus@10757
   681
                    current_audio.impl.WaitDevice(device);
icculus@10757
   682
                }
slouken@1895
   683
            }
slouken@10772
   684
        } else if (data == device->work_buffer) {
icculus@10757
   685
            /* nothing to do; pause like we queued a buffer to play. */
icculus@9398
   686
            SDL_Delay(delay);
icculus@10757
   687
        } else {  /* writing directly to the device. */
icculus@10757
   688
            /* queue this buffer and wait for it to finish playing. */
icculus@9398
   689
            current_audio.impl.PlayDevice(device);
icculus@9398
   690
            current_audio.impl.WaitDevice(device);
slouken@1895
   691
        }
slouken@1895
   692
    }
slouken@1562
   693
icculus@10473
   694
    current_audio.impl.PrepareToClose(device);
icculus@10473
   695
icculus@9398
   696
    /* Wait for the audio to drain. */
icculus@10473
   697
    SDL_Delay(((device->spec.samples * 1000) / device->spec.freq) * 2);
slouken@21
   698
icculus@9382
   699
    return 0;
slouken@0
   700
}
slouken@0
   701
icculus@10239
   702
/* The general capture thread function */
icculus@10239
   703
static int SDLCALL
icculus@10239
   704
SDL_CaptureAudio(void *devicep)
icculus@10239
   705
{
icculus@10239
   706
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@10239
   707
    const int silence = (int) device->spec.silence;
icculus@10239
   708
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
slouken@10772
   709
    const int data_len = device->spec.size;
slouken@10772
   710
    Uint8 *data;
icculus@10239
   711
    void *udata = device->spec.userdata;
icculus@10757
   712
    SDL_AudioCallback callback = device->spec.callback;
icculus@10239
   713
icculus@10239
   714
    SDL_assert(device->iscapture);
icculus@10239
   715
icculus@10239
   716
    /* The audio mixing is always a high priority thread */
icculus@10239
   717
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
icculus@10239
   718
icculus@10239
   719
    /* Perform any thread setup */
icculus@10239
   720
    device->threadid = SDL_ThreadID();
icculus@10239
   721
    current_audio.impl.ThreadInit(device);
icculus@10239
   722
icculus@10239
   723
    /* Loop, filling the audio buffers */
icculus@10239
   724
    while (!SDL_AtomicGet(&device->shutdown)) {
icculus@10239
   725
        int still_need;
icculus@10239
   726
        Uint8 *ptr;
icculus@10239
   727
icculus@10239
   728
        if (!SDL_AtomicGet(&device->enabled) || SDL_AtomicGet(&device->paused)) {
icculus@10239
   729
            SDL_Delay(delay);  /* just so we don't cook the CPU. */
icculus@10757
   730
            if (device->stream) {
icculus@10757
   731
                SDL_AudioStreamClear(device->stream);
icculus@10757
   732
            }
icculus@10239
   733
            current_audio.impl.FlushCapture(device);  /* dump anything pending. */
icculus@10239
   734
            continue;
icculus@10239
   735
        }
icculus@10239
   736
icculus@10239
   737
        /* Fill the current buffer with sound */
slouken@10772
   738
        still_need = data_len;
icculus@10757
   739
icculus@10765
   740
        /* Use the work_buffer to hold data read from the device. */
slouken@10772
   741
        data = device->work_buffer;
slouken@10772
   742
        SDL_assert(data != NULL);
icculus@10757
   743
slouken@10772
   744
        ptr = data;
icculus@10239
   745
icculus@10239
   746
        /* We still read from the device when "paused" to keep the state sane,
icculus@10239
   747
           and block when there isn't data so this thread isn't eating CPU.
icculus@10239
   748
           But we don't process it further or call the app's callback. */
icculus@10239
   749
icculus@10239
   750
        while (still_need > 0) {
icculus@10239
   751
            const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need);
icculus@10239
   752
            SDL_assert(rc <= still_need);  /* device should not overflow buffer. :) */
icculus@10239
   753
            if (rc > 0) {
icculus@10239
   754
                still_need -= rc;
icculus@10239
   755
                ptr += rc;
icculus@10239
   756
            } else {  /* uhoh, device failed for some reason! */
icculus@10239
   757
                SDL_OpenedAudioDeviceDisconnected(device);
icculus@10239
   758
                break;
icculus@10239
   759
            }
icculus@10239
   760
        }
icculus@10239
   761
icculus@10239
   762
        if (still_need > 0) {
icculus@10239
   763
            /* Keep any data we already read, silence the rest. */
icculus@10239
   764
            SDL_memset(ptr, silence, still_need);
icculus@10239
   765
        }
icculus@10239
   766
icculus@10757
   767
        if (device->stream) {
icculus@10757
   768
            /* if this fails...oh well. */
slouken@10772
   769
            SDL_AudioStreamPut(device->stream, data, data_len);
icculus@10757
   770
icculus@10759
   771
            while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->callbackspec.size)) {
icculus@10765
   772
                const int got = SDL_AudioStreamGet(device->stream, device->work_buffer, device->callbackspec.size);
icculus@10757
   773
                SDL_assert((got < 0) || (got == device->callbackspec.size));
icculus@10757
   774
                if (got != device->callbackspec.size) {
icculus@10765
   775
                    SDL_memset(device->work_buffer, device->spec.silence, device->callbackspec.size);
icculus@10757
   776
                }
icculus@10757
   777
icculus@10757
   778
                /* !!! FIXME: this should be LockDevice. */
icculus@10757
   779
                SDL_LockMutex(device->mixer_lock);
icculus@10757
   780
                if (!SDL_AtomicGet(&device->paused)) {
icculus@10765
   781
                    callback(udata, device->work_buffer, device->callbackspec.size);
icculus@10757
   782
                }
icculus@10757
   783
                SDL_UnlockMutex(device->mixer_lock);
icculus@10757
   784
            }
icculus@10757
   785
        } else {  /* feeding user callback directly without streaming. */
icculus@10757
   786
            /* !!! FIXME: this should be LockDevice. */
icculus@10757
   787
            SDL_LockMutex(device->mixer_lock);
icculus@10757
   788
            if (!SDL_AtomicGet(&device->paused)) {
slouken@10772
   789
                callback(udata, data, device->callbackspec.size);
icculus@10757
   790
            }
icculus@10757
   791
            SDL_UnlockMutex(device->mixer_lock);
icculus@10239
   792
        }
icculus@10239
   793
    }
icculus@10239
   794
icculus@10239
   795
    current_audio.impl.FlushCapture(device);
icculus@10239
   796
icculus@10239
   797
    return 0;
icculus@10239
   798
}
icculus@10239
   799
slouken@322
   800
icculus@1982
   801
static SDL_AudioFormat
slouken@1895
   802
SDL_ParseAudioFormat(const char *string)
slouken@1794
   803
{
icculus@2076
   804
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   805
    CHECK_FMT_STRING(U8);
icculus@2049
   806
    CHECK_FMT_STRING(S8);
icculus@2049
   807
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   808
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   809
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   810
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   811
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   812
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   813
    CHECK_FMT_STRING(U16);
icculus@2049
   814
    CHECK_FMT_STRING(S16);
icculus@2049
   815
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   816
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   817
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   818
    CHECK_FMT_STRING(S32);
icculus@2049
   819
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   820
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   821
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   822
    CHECK_FMT_STRING(F32);
slouken@2060
   823
#undef CHECK_FMT_STRING
icculus@2049
   824
    return 0;
slouken@1794
   825
}
slouken@1794
   826
slouken@1895
   827
int
slouken@1895
   828
SDL_GetNumAudioDrivers(void)
slouken@0
   829
{
icculus@9382
   830
    return SDL_arraysize(bootstrap) - 1;
slouken@0
   831
}
slouken@0
   832
slouken@1895
   833
const char *
slouken@1895
   834
SDL_GetAudioDriver(int index)
slouken@0
   835
{
slouken@1895
   836
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
icculus@9382
   837
        return bootstrap[index]->name;
slouken@1895
   838
    }
icculus@9382
   839
    return NULL;
slouken@0
   840
}
slouken@0
   841
slouken@1895
   842
int
slouken@1895
   843
SDL_AudioInit(const char *driver_name)
slouken@0
   844
{
icculus@2049
   845
    int i = 0;
icculus@2049
   846
    int initialized = 0;
icculus@2049
   847
    int tried_to_init = 0;
slouken@0
   848
icculus@2049
   849
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   850
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   851
    }
slouken@0
   852
icculus@9392
   853
    SDL_zero(current_audio);
icculus@9392
   854
    SDL_zero(open_devices);
icculus@2049
   855
slouken@1895
   856
    /* Select the proper audio driver */
slouken@1909
   857
    if (driver_name == NULL) {
slouken@1909
   858
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   859
    }
slouken@262
   860
icculus@2049
   861
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   862
        /* make sure we should even try this driver before doing so... */
icculus@2049
   863
        const AudioBootStrap *backend = bootstrap[i];
slouken@6900
   864
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
slouken@6900
   865
            (!driver_name && backend->demand_only)) {
icculus@2049
   866
            continue;
slouken@1895
   867
        }
icculus@2049
   868
icculus@2049
   869
        tried_to_init = 1;
icculus@9392
   870
        SDL_zero(current_audio);
icculus@2049
   871
        current_audio.name = backend->name;
icculus@2049
   872
        current_audio.desc = backend->desc;
icculus@3699
   873
        initialized = backend->init(&current_audio.impl);
slouken@1895
   874
    }
icculus@2049
   875
icculus@2049
   876
    if (!initialized) {
icculus@2049
   877
        /* specific drivers will set the error message if they fail... */
icculus@2049
   878
        if (!tried_to_init) {
slouken@1895
   879
            if (driver_name) {
icculus@3699
   880
                SDL_SetError("Audio target '%s' not available", driver_name);
slouken@1895
   881
            } else {
slouken@1895
   882
                SDL_SetError("No available audio device");
slouken@1895
   883
            }
slouken@1895
   884
        }
icculus@2049
   885
icculus@9392
   886
        SDL_zero(current_audio);
icculus@9382
   887
        return -1;            /* No driver was available, so fail. */
slouken@1895
   888
    }
icculus@2049
   889
icculus@9394
   890
    current_audio.detectionLock = SDL_CreateMutex();
icculus@9393
   891
icculus@10471
   892
    finish_audio_entry_points_init();
icculus@2049
   893
icculus@9393
   894
    /* Make sure we have a list of devices available at startup. */
icculus@9394
   895
    current_audio.impl.DetectDevices();
icculus@9393
   896
icculus@10790
   897
#ifdef HAVE_LIBSAMPLERATE_H
icculus@10790
   898
    LoadLibSampleRate();
icculus@10790
   899
#endif
icculus@10790
   900
icculus@9382
   901
    return 0;
slouken@1895
   902
}
slouken@0
   903
slouken@1895
   904
/*
slouken@1895
   905
 * Get the current audio driver name
slouken@1895
   906
 */
slouken@1895
   907
const char *
slouken@1895
   908
SDL_GetCurrentAudioDriver()
slouken@1895
   909
{
icculus@2049
   910
    return current_audio.name;
slouken@1895
   911
}
slouken@1895
   912
icculus@9394
   913
/* Clean out devices that we've removed but had to keep around for stability. */
icculus@9394
   914
static void
icculus@9394
   915
clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
icculus@9394
   916
{
icculus@9394
   917
    SDL_AudioDeviceItem *item = *devices;
icculus@9394
   918
    SDL_AudioDeviceItem *prev = NULL;
icculus@9394
   919
    int total = 0;
icculus@9394
   920
icculus@9394
   921
    while (item) {
icculus@9394
   922
        SDL_AudioDeviceItem *next = item->next;
icculus@9394
   923
        if (item->handle != NULL) {
icculus@9394
   924
            total++;
icculus@9394
   925
            prev = item;
icculus@9394
   926
        } else {
icculus@9394
   927
            if (prev) {
icculus@9394
   928
                prev->next = next;
icculus@9394
   929
            } else {
icculus@9394
   930
                *devices = next;
icculus@9394
   931
            }
icculus@9394
   932
            SDL_free(item);
icculus@9394
   933
        }
icculus@9394
   934
        item = next;
icculus@9394
   935
    }
icculus@9394
   936
icculus@9394
   937
    *devCount = total;
icculus@9394
   938
    *removedFlag = SDL_FALSE;
icculus@9394
   939
}
icculus@9394
   940
icculus@9394
   941
slouken@1895
   942
int
icculus@2049
   943
SDL_GetNumAudioDevices(int iscapture)
slouken@1895
   944
{
icculus@5593
   945
    int retval = 0;
icculus@5593
   946
icculus@2049
   947
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   948
        return -1;
icculus@2049
   949
    }
icculus@5593
   950
icculus@9394
   951
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   952
    if (iscapture && current_audio.captureDevicesRemoved) {
icculus@9394
   953
        clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
icculus@2049
   954
    }
slouken@1895
   955
icculus@9394
   956
    if (!iscapture && current_audio.outputDevicesRemoved) {
icculus@9394
   957
        clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
icculus@9394
   958
        current_audio.outputDevicesRemoved = SDL_FALSE;
icculus@2049
   959
    }
icculus@2049
   960
icculus@9393
   961
    retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   962
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   963
icculus@5593
   964
    return retval;
icculus@2049
   965
}
icculus@2049
   966
icculus@2049
   967
icculus@2049
   968
const char *
icculus@2049
   969
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   970
{
icculus@9393
   971
    const char *retval = NULL;
icculus@9393
   972
icculus@2049
   973
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   974
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   975
        return NULL;
icculus@2049
   976
    }
icculus@2049
   977
icculus@2049
   978
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   979
        SDL_SetError("No capture support");
icculus@2049
   980
        return NULL;
icculus@2049
   981
    }
icculus@2049
   982
icculus@9394
   983
    if (index >= 0) {
icculus@9394
   984
        SDL_AudioDeviceItem *item;
icculus@9394
   985
        int i;
icculus@9394
   986
icculus@9394
   987
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   988
        item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
icculus@9394
   989
        i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   990
        if (index < i) {
icculus@9394
   991
            for (i--; i > index; i--, item = item->next) {
icculus@9394
   992
                SDL_assert(item != NULL);
icculus@9394
   993
            }
icculus@9394
   994
            SDL_assert(item != NULL);
icculus@9394
   995
            retval = item->name;
icculus@9394
   996
        }
icculus@9394
   997
        SDL_UnlockMutex(current_audio.detectionLock);
icculus@2049
   998
    }
icculus@2049
   999
icculus@9394
  1000
    if (retval == NULL) {
icculus@9394
  1001
        SDL_SetError("No such device");
icculus@2049
  1002
    }
icculus@2049
  1003
icculus@9394
  1004
    return retval;
icculus@2049
  1005
}
icculus@2049
  1006
icculus@2049
  1007
icculus@2049
  1008
static void
slouken@2060
  1009
close_audio_device(SDL_AudioDevice * device)
icculus@2049
  1010
{
icculus@10471
  1011
    if (!device) {
icculus@10471
  1012
        return;
slouken@10468
  1013
    }
icculus@9012
  1014
icculus@10471
  1015
    if (device->id > 0) {
icculus@10471
  1016
        SDL_AudioDevice *opendev = open_devices[device->id - 1];
icculus@10471
  1017
        SDL_assert((opendev == device) || (opendev == NULL));
icculus@10471
  1018
        if (opendev == device) {
icculus@10471
  1019
            open_devices[device->id - 1] = NULL;
icculus@10471
  1020
        }
icculus@10471
  1021
    }
icculus@10471
  1022
icculus@10480
  1023
    SDL_AtomicSet(&device->shutdown, 1);
icculus@10480
  1024
    SDL_AtomicSet(&device->enabled, 0);
icculus@10480
  1025
    if (device->thread != NULL) {
icculus@10480
  1026
        SDL_WaitThread(device->thread, NULL);
icculus@10480
  1027
    }
icculus@10480
  1028
    if (device->mixer_lock != NULL) {
icculus@10480
  1029
        SDL_DestroyMutex(device->mixer_lock);
icculus@10480
  1030
    }
icculus@10757
  1031
icculus@10765
  1032
    SDL_free(device->work_buffer);
icculus@10757
  1033
    SDL_FreeAudioStream(device->stream);
icculus@10757
  1034
icculus@10480
  1035
    if (device->hidden != NULL) {
icculus@10480
  1036
        current_audio.impl.CloseDevice(device);
icculus@10480
  1037
    }
icculus@10471
  1038
icculus@10681
  1039
    SDL_FreeDataQueue(device->buffer_queue);
icculus@10757
  1040
icculus@10480
  1041
    SDL_free(device);
icculus@2049
  1042
}
icculus@2049
  1043
icculus@2049
  1044
icculus@2049
  1045
/*
icculus@2049
  1046
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
  1047
 *  Fills in a sanitized copy in (prepared).
icculus@2049
  1048
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
  1049
 */
icculus@2049
  1050
static int
slouken@2060
  1051
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
  1052
{
slouken@2060
  1053
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
  1054
icculus@2049
  1055
    if (orig->freq == 0) {
icculus@2049
  1056
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
  1057
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
  1058
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
  1059
        }
slouken@1895
  1060
    }
slouken@1895
  1061
icculus@2049
  1062
    if (orig->format == 0) {
icculus@2049
  1063
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
  1064
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
  1065
            prepared->format = AUDIO_S16;       /* a reasonable default */
icculus@2049
  1066
        }
slouken@1895
  1067
    }
slouken@1895
  1068
icculus@2049
  1069
    switch (orig->channels) {
slouken@2060
  1070
    case 0:{
slouken@2060
  1071
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
  1072
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
  1073
                prepared->channels = 2; /* a reasonable default */
slouken@2060
  1074
            }
slouken@2060
  1075
            break;
slouken@1895
  1076
        }
slouken@1895
  1077
    case 1:                    /* Mono */
slouken@1895
  1078
    case 2:                    /* Stereo */
slouken@1895
  1079
    case 4:                    /* surround */
slouken@1895
  1080
    case 6:                    /* surround with center and lfe */
slouken@1895
  1081
        break;
slouken@1895
  1082
    default:
icculus@2049
  1083
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
  1084
        return 0;
slouken@1895
  1085
    }
icculus@2049
  1086
icculus@2049
  1087
    if (orig->samples == 0) {
icculus@2049
  1088
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
  1089
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
  1090
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
  1091
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
  1092
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
  1093
            int power2 = 1;
icculus@2049
  1094
            while (power2 < samples) {
icculus@2049
  1095
                power2 *= 2;
icculus@2049
  1096
            }
icculus@2049
  1097
            prepared->samples = power2;
slouken@1895
  1098
        }
slouken@1895
  1099
    }
slouken@0
  1100
slouken@1895
  1101
    /* Calculate the silence and size of the audio specification */
icculus@2049
  1102
    SDL_CalculateAudioSpec(prepared);
slouken@0
  1103
icculus@2049
  1104
    return 1;
icculus@2049
  1105
}
slouken@21
  1106
icculus@2049
  1107
static SDL_AudioDeviceID
icculus@2049
  1108
open_audio_device(const char *devname, int iscapture,
slouken@2866
  1109
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1110
                  int allowed_changes, int min_id)
icculus@2049
  1111
{
icculus@10270
  1112
    const SDL_bool is_internal_thread = (desired->callback != NULL);
icculus@2049
  1113
    SDL_AudioDeviceID id = 0;
slouken@2866
  1114
    SDL_AudioSpec _obtained;
icculus@2049
  1115
    SDL_AudioDevice *device;
icculus@10757
  1116
    SDL_bool build_stream;
icculus@9394
  1117
    void *handle = NULL;
icculus@2049
  1118
    int i = 0;
slouken@21
  1119
icculus@2049
  1120
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1121
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
  1122
        return 0;
slouken@1895
  1123
    }
slouken@21
  1124
icculus@2049
  1125
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
  1126
        SDL_SetError("No capture support");
icculus@2049
  1127
        return 0;
icculus@2049
  1128
    }
icculus@2049
  1129
icculus@10471
  1130
    /* !!! FIXME: there is a race condition here if two devices open from two threads at once. */
icculus@9393
  1131
    /* Find an available device ID... */
icculus@9393
  1132
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@9393
  1133
        if (open_devices[id] == NULL) {
icculus@9393
  1134
            break;
icculus@9393
  1135
        }
icculus@9393
  1136
    }
icculus@9393
  1137
icculus@9393
  1138
    if (id == SDL_arraysize(open_devices)) {
icculus@9393
  1139
        SDL_SetError("Too many open audio devices");
icculus@9393
  1140
        return 0;
icculus@9393
  1141
    }
icculus@9393
  1142
slouken@2866
  1143
    if (!obtained) {
slouken@2866
  1144
        obtained = &_obtained;
slouken@2866
  1145
    }
slouken@2866
  1146
    if (!prepare_audiospec(desired, obtained)) {
icculus@2049
  1147
        return 0;
icculus@2049
  1148
    }
icculus@2049
  1149
icculus@2049
  1150
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
  1151
    if (devname == NULL) {
icculus@2049
  1152
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
icculus@2049
  1153
    }
icculus@2049
  1154
icculus@2049
  1155
    /*
icculus@2049
  1156
     * Catch device names at the high level for the simple case...
icculus@2049
  1157
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
  1158
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
  1159
     *  always NULL when it hits the low level.
icculus@2049
  1160
     *
icculus@2049
  1161
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
  1162
     *  opens of the default system device.
icculus@2049
  1163
     */
icculus@2049
  1164
icculus@10258
  1165
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultCaptureDevice)) {
icculus@2049
  1166
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
  1167
            SDL_SetError("No such device");
icculus@2049
  1168
            return 0;
icculus@2049
  1169
        }
icculus@2049
  1170
        devname = NULL;
icculus@2049
  1171
icculus@2049
  1172
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1173
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
  1174
                SDL_SetError("Audio device already open");
icculus@2049
  1175
                return 0;
icculus@2049
  1176
            }
icculus@2049
  1177
        }
icculus@9394
  1178
    } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
  1179
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
  1180
            SDL_SetError("No such device");
icculus@2049
  1181
            return 0;
icculus@2049
  1182
        }
icculus@2049
  1183
        devname = NULL;
icculus@2049
  1184
icculus@2049
  1185
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1186
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
  1187
                SDL_SetError("Audio device already open");
icculus@2049
  1188
                return 0;
icculus@2049
  1189
            }
icculus@2049
  1190
        }
icculus@9394
  1191
    } else if (devname != NULL) {
icculus@9394
  1192
        /* if the app specifies an exact string, we can pass the backend
icculus@9394
  1193
           an actual device handle thingey, which saves them the effort of
icculus@9394
  1194
           figuring out what device this was (such as, reenumerating
icculus@9394
  1195
           everything again to find the matching human-readable name).
icculus@9394
  1196
           It might still need to open a device based on the string for,
icculus@9394
  1197
           say, a network audio server, but this optimizes some cases. */
icculus@9394
  1198
        SDL_AudioDeviceItem *item;
icculus@9394
  1199
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
  1200
        for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) {
icculus@9394
  1201
            if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) {
icculus@9394
  1202
                handle = item->handle;
icculus@9394
  1203
                break;
icculus@9394
  1204
            }
icculus@9394
  1205
        }
icculus@9394
  1206
        SDL_UnlockMutex(current_audio.detectionLock);
icculus@9394
  1207
    }
icculus@9394
  1208
icculus@9394
  1209
    if (!current_audio.impl.AllowsArbitraryDeviceNames) {
icculus@9394
  1210
        /* has to be in our device list, or the default device. */
icculus@9394
  1211
        if ((handle == NULL) && (devname != NULL)) {
icculus@9394
  1212
            SDL_SetError("No such device.");
icculus@9394
  1213
            return 0;
icculus@9394
  1214
        }
icculus@2049
  1215
    }
icculus@2049
  1216
icculus@10256
  1217
    device = (SDL_AudioDevice *) SDL_calloc(1, sizeof (SDL_AudioDevice));
icculus@2049
  1218
    if (device == NULL) {
icculus@2049
  1219
        SDL_OutOfMemory();
icculus@2049
  1220
        return 0;
icculus@2049
  1221
    }
icculus@9393
  1222
    device->id = id + 1;
slouken@2866
  1223
    device->spec = *obtained;
icculus@10235
  1224
    device->iscapture = iscapture ? SDL_TRUE : SDL_FALSE;
slouken@10467
  1225
    device->handle = handle;
icculus@2049
  1226
icculus@10238
  1227
    SDL_AtomicSet(&device->shutdown, 0);  /* just in case. */
icculus@10238
  1228
    SDL_AtomicSet(&device->paused, 1);
icculus@10238
  1229
    SDL_AtomicSet(&device->enabled, 1);
icculus@10238
  1230
icculus@9391
  1231
    /* Create a mutex for locking the sound buffers */
icculus@2049
  1232
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
  1233
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
  1234
        if (device->mixer_lock == NULL) {
icculus@2049
  1235
            close_audio_device(device);
icculus@2049
  1236
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
  1237
            return 0;
icculus@2049
  1238
        }
icculus@2049
  1239
    }
icculus@2049
  1240
icculus@9394
  1241
    if (current_audio.impl.OpenDevice(device, handle, devname, iscapture) < 0) {
icculus@2049
  1242
        close_audio_device(device);
icculus@2049
  1243
        return 0;
icculus@2049
  1244
    }
icculus@10255
  1245
icculus@10255
  1246
    /* if your target really doesn't need it, set it to 0x1 or something. */
icculus@10255
  1247
    /* otherwise, close_audio_device() won't call impl.CloseDevice(). */
icculus@10255
  1248
    SDL_assert(device->hidden != NULL);
slouken@0
  1249
slouken@1895
  1250
    /* See if we need to do any conversion */
icculus@10757
  1251
    build_stream = SDL_FALSE;
slouken@2866
  1252
    if (obtained->freq != device->spec.freq) {
slouken@2866
  1253
        if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
slouken@2866
  1254
            obtained->freq = device->spec.freq;
slouken@2866
  1255
        } else {
icculus@10757
  1256
            build_stream = SDL_TRUE;
slouken@2866
  1257
        }
slouken@2866
  1258
    }
slouken@2866
  1259
    if (obtained->format != device->spec.format) {
slouken@2866
  1260
        if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
slouken@2866
  1261
            obtained->format = device->spec.format;
slouken@2866
  1262
        } else {
icculus@10757
  1263
            build_stream = SDL_TRUE;
slouken@2866
  1264
        }
slouken@2866
  1265
    }
slouken@2866
  1266
    if (obtained->channels != device->spec.channels) {
slouken@2866
  1267
        if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
slouken@2866
  1268
            obtained->channels = device->spec.channels;
slouken@2866
  1269
        } else {
icculus@10757
  1270
            build_stream = SDL_TRUE;
slouken@2866
  1271
        }
slouken@2866
  1272
    }
slouken@7518
  1273
icculus@10757
  1274
    /* !!! FIXME in 2.1: add SDL_AUDIO_ALLOW_SAMPLES_CHANGE flag?
icculus@10757
  1275
       As of 2.0.6, we will build a stream to buffer the difference between
icculus@10757
  1276
       what the app wants to feed and the device wants to eat, so everyone
icculus@10757
  1277
       gets their way. In prior releases, SDL would force the callback to
icculus@10757
  1278
       feed at the rate the device requested, adjusted for resampling.
slouken@7518
  1279
     */
slouken@7518
  1280
    if (device->spec.samples != obtained->samples) {
icculus@10757
  1281
        build_stream = SDL_TRUE;
slouken@7518
  1282
    }
slouken@7518
  1283
icculus@10757
  1284
    SDL_CalculateAudioSpec(obtained);  /* recalc after possible changes. */
icculus@10757
  1285
icculus@10757
  1286
    device->callbackspec = *obtained;
icculus@10757
  1287
icculus@10757
  1288
    if (build_stream) {
icculus@10757
  1289
        if (iscapture) {
icculus@10757
  1290
            device->stream = SDL_NewAudioStream(device->spec.format,
icculus@10757
  1291
                                  device->spec.channels, device->spec.freq,
icculus@10757
  1292
                                  obtained->format, obtained->channels, obtained->freq);
icculus@10757
  1293
        } else {
icculus@10757
  1294
            device->stream = SDL_NewAudioStream(obtained->format, obtained->channels,
icculus@10757
  1295
                                  obtained->freq, device->spec.format,
icculus@10757
  1296
                                  device->spec.channels, device->spec.freq);
icculus@10757
  1297
        }
icculus@10757
  1298
icculus@10757
  1299
        if (!device->stream) {
icculus@2049
  1300
            close_audio_device(device);
icculus@2049
  1301
            return 0;
slouken@1895
  1302
        }
slouken@1895
  1303
    }
icculus@2049
  1304
icculus@9012
  1305
    if (device->spec.callback == NULL) {  /* use buffer queueing? */
icculus@9012
  1306
        /* pool a few packets to start. Enough for two callbacks. */
icculus@10757
  1307
        device->buffer_queue = SDL_NewDataQueue(SDL_AUDIOBUFFERQUEUE_PACKETLEN, obtained->size * 2);
icculus@10681
  1308
        if (!device->buffer_queue) {
icculus@10681
  1309
            close_audio_device(device);
icculus@10681
  1310
            SDL_SetError("Couldn't create audio buffer queue");
icculus@10681
  1311
            return 0;
icculus@9012
  1312
        }
icculus@10262
  1313
        device->spec.callback = iscapture ? SDL_BufferQueueFillCallback : SDL_BufferQueueDrainCallback;
icculus@9012
  1314
        device->spec.userdata = device;
icculus@9012
  1315
    }
icculus@9012
  1316
icculus@10760
  1317
    /* Allocate a scratch audio buffer */
icculus@10765
  1318
    device->work_buffer_len = build_stream ? device->callbackspec.size : 0;
icculus@10765
  1319
    if (device->spec.size > device->work_buffer_len) {
icculus@10765
  1320
        device->work_buffer_len = device->spec.size;
icculus@10760
  1321
    }
icculus@10765
  1322
    SDL_assert(device->work_buffer_len > 0);
icculus@10760
  1323
icculus@10765
  1324
    device->work_buffer = (Uint8 *) SDL_malloc(device->work_buffer_len);
icculus@10765
  1325
    if (device->work_buffer == NULL) {
icculus@10760
  1326
        close_audio_device(device);
icculus@10760
  1327
        SDL_OutOfMemory();
icculus@10760
  1328
        return 0;
icculus@10760
  1329
    }
icculus@10760
  1330
icculus@10757
  1331
    open_devices[id] = device;  /* add it to our list of open devices. */
icculus@2049
  1332
slouken@1895
  1333
    /* Start the audio thread if necessary */
icculus@2049
  1334
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
  1335
        /* Start the audio thread */
icculus@10277
  1336
        /* !!! FIXME: we don't force the audio thread stack size here if it calls into user code, but maybe we should? */
icculus@10277
  1337
        /* buffer queueing callback only needs a few bytes, so make the stack tiny. */
icculus@10277
  1338
        const size_t stacksize = is_internal_thread ? 64 * 1024 : 0;
icculus@10277
  1339
        char threadname[64];
icculus@10146
  1340
icculus@10277
  1341
        SDL_snprintf(threadname, sizeof (threadname), "SDLAudioDev%d", (int) device->id);
icculus@10277
  1342
        device->thread = SDL_CreateThreadInternal(iscapture ? SDL_CaptureAudio : SDL_RunAudio, threadname, stacksize, device);
icculus@10146
  1343
icculus@2049
  1344
        if (device->thread == NULL) {
icculus@10471
  1345
            close_audio_device(device);
slouken@1895
  1346
            SDL_SetError("Couldn't create audio thread");
icculus@2049
  1347
            return 0;
icculus@2049
  1348
        }
icculus@2049
  1349
    }
icculus@2049
  1350
icculus@9393
  1351
    return device->id;
icculus@2049
  1352
}
icculus@2049
  1353
icculus@2049
  1354
icculus@2049
  1355
int
slouken@2866
  1356
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
  1357
{
icculus@2049
  1358
    SDL_AudioDeviceID id = 0;
icculus@2049
  1359
icculus@2049
  1360
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
  1361
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1362
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
icculus@9382
  1363
            return -1;
slouken@1895
  1364
        }
icculus@2049
  1365
    }
slouken@0
  1366
icculus@2049
  1367
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
  1368
    if (open_devices[0] != NULL) {
icculus@2049
  1369
        SDL_SetError("Audio device is already opened");
icculus@9382
  1370
        return -1;
slouken@1895
  1371
    }
slouken@21
  1372
slouken@2866
  1373
    if (obtained) {
slouken@2866
  1374
        id = open_audio_device(NULL, 0, desired, obtained,
slouken@2866
  1375
                               SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
slouken@2866
  1376
    } else {
slouken@8919
  1377
        id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
slouken@2866
  1378
    }
slouken@21
  1379
icculus@5964
  1380
    SDL_assert((id == 0) || (id == 1));
icculus@9382
  1381
    return (id == 0) ? -1 : 0;
icculus@2049
  1382
}
icculus@2049
  1383
icculus@2049
  1384
SDL_AudioDeviceID
icculus@2049
  1385
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2866
  1386
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1387
                    int allowed_changes)
icculus@2049
  1388
{
slouken@2866
  1389
    return open_audio_device(device, iscapture, desired, obtained,
slouken@2866
  1390
                             allowed_changes, 2);
slouken@0
  1391
}
slouken@0
  1392
slouken@3537
  1393
SDL_AudioStatus
icculus@2049
  1394
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@0
  1395
{
icculus@2049
  1396
    SDL_AudioDevice *device = get_audio_device(devid);
slouken@3537
  1397
    SDL_AudioStatus status = SDL_AUDIO_STOPPED;
icculus@10238
  1398
    if (device && SDL_AtomicGet(&device->enabled)) {
icculus@10238
  1399
        if (SDL_AtomicGet(&device->paused)) {
slouken@1895
  1400
            status = SDL_AUDIO_PAUSED;
slouken@1895
  1401
        } else {
slouken@1895
  1402
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1403
        }
slouken@1895
  1404
    }
icculus@9382
  1405
    return status;
slouken@0
  1406
}
slouken@0
  1407
icculus@2049
  1408
slouken@3537
  1409
SDL_AudioStatus
icculus@2049
  1410
SDL_GetAudioStatus(void)
icculus@2049
  1411
{
icculus@2049
  1412
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1413
}
icculus@2049
  1414
icculus@2049
  1415
void
icculus@2049
  1416
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1417
{
icculus@2049
  1418
    SDL_AudioDevice *device = get_audio_device(devid);
gabomdq@9148
  1419
    if (device) {
gabomdq@9148
  1420
        current_audio.impl.LockDevice(device);
icculus@10238
  1421
        SDL_AtomicSet(&device->paused, pause_on ? 1 : 0);
gabomdq@9148
  1422
        current_audio.impl.UnlockDevice(device);
icculus@2049
  1423
    }
icculus@2049
  1424
}
icculus@2049
  1425
slouken@1895
  1426
void
slouken@1895
  1427
SDL_PauseAudio(int pause_on)
slouken@0
  1428
{
gabomdq@9148
  1429
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1430
}
slouken@0
  1431
icculus@2049
  1432
icculus@2049
  1433
void
icculus@2049
  1434
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1435
{
icculus@2049
  1436
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1437
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1438
    if (device) {
icculus@2049
  1439
        current_audio.impl.LockDevice(device);
slouken@1895
  1440
    }
slouken@0
  1441
}
slouken@0
  1442
slouken@1895
  1443
void
slouken@1895
  1444
SDL_LockAudio(void)
slouken@0
  1445
{
icculus@2049
  1446
    SDL_LockAudioDevice(1);
icculus@2049
  1447
}
slouken@0
  1448
icculus@2049
  1449
void
icculus@2049
  1450
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1451
{
slouken@1895
  1452
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1453
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1454
    if (device) {
icculus@2049
  1455
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1456
    }
slouken@0
  1457
}
slouken@0
  1458
slouken@1895
  1459
void
slouken@1895
  1460
SDL_UnlockAudio(void)
slouken@0
  1461
{
icculus@2049
  1462
    SDL_UnlockAudioDevice(1);
icculus@2049
  1463
}
slouken@0
  1464
icculus@2049
  1465
void
icculus@2049
  1466
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1467
{
icculus@10471
  1468
    close_audio_device(get_audio_device(devid));
slouken@0
  1469
}
slouken@0
  1470
slouken@1895
  1471
void
slouken@1895
  1472
SDL_CloseAudio(void)
slouken@0
  1473
{
icculus@2049
  1474
    SDL_CloseAudioDevice(1);
slouken@0
  1475
}
slouken@0
  1476
slouken@1895
  1477
void
slouken@1895
  1478
SDL_AudioQuit(void)
slouken@0
  1479
{
icculus@7348
  1480
    SDL_AudioDeviceID i;
icculus@7348
  1481
icculus@7345
  1482
    if (!current_audio.name) {  /* not initialized?! */
icculus@7345
  1483
        return;
icculus@7345
  1484
    }
icculus@7345
  1485
icculus@2049
  1486
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@10471
  1487
        close_audio_device(open_devices[i]);
icculus@2049
  1488
    }
slouken@0
  1489
icculus@9394
  1490
    free_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9394
  1491
    free_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9394
  1492
icculus@2049
  1493
    /* Free the driver data */
icculus@2049
  1494
    current_audio.impl.Deinitialize();
icculus@9393
  1495
icculus@9394
  1496
    SDL_DestroyMutex(current_audio.detectionLock);
icculus@9393
  1497
icculus@9393
  1498
    SDL_zero(current_audio);
icculus@9393
  1499
    SDL_zero(open_devices);
icculus@10790
  1500
icculus@10790
  1501
#ifdef HAVE_LIBSAMPLERATE_H
icculus@10790
  1502
    UnloadLibSampleRate();
icculus@10790
  1503
#endif
slouken@0
  1504
}
slouken@0
  1505
icculus@1982
  1506
#define NUM_FORMATS 10
slouken@0
  1507
static int format_idx;
slouken@0
  1508
static int format_idx_sub;
icculus@1982
  1509
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1510
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1511
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1512
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1513
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1514
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1515
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1516
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1517
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1518
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1519
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1520
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1521
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1522
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1523
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1524
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1525
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1526
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1527
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1528
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1529
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1530
};
slouken@0
  1531
icculus@1982
  1532
SDL_AudioFormat
icculus@1982
  1533
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1534
{
slouken@1895
  1535
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1536
        if (format_list[format_idx][0] == format) {
slouken@1895
  1537
            break;
slouken@1895
  1538
        }
slouken@1895
  1539
    }
slouken@1895
  1540
    format_idx_sub = 0;
icculus@9382
  1541
    return SDL_NextAudioFormat();
slouken@0
  1542
}
slouken@0
  1543
icculus@1982
  1544
SDL_AudioFormat
slouken@1895
  1545
SDL_NextAudioFormat(void)
slouken@0
  1546
{
slouken@1895
  1547
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
icculus@9382
  1548
        return 0;
slouken@1895
  1549
    }
icculus@9382
  1550
    return format_list[format_idx][format_idx_sub++];
slouken@0
  1551
}
slouken@0
  1552
slouken@1895
  1553
void
slouken@1895
  1554
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1555
{
slouken@1895
  1556
    switch (spec->format) {
slouken@1895
  1557
    case AUDIO_U8:
slouken@1895
  1558
        spec->silence = 0x80;
slouken@1895
  1559
        break;
slouken@1895
  1560
    default:
slouken@1895
  1561
        spec->silence = 0x00;
slouken@1895
  1562
        break;
slouken@1895
  1563
    }
icculus@2049
  1564
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1565
    spec->size *= spec->channels;
slouken@1895
  1566
    spec->size *= spec->samples;
slouken@0
  1567
}
slouken@1895
  1568
icculus@2049
  1569
icculus@2049
  1570
/*
icculus@2049
  1571
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1572
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1573
 */
icculus@2049
  1574
void
icculus@2049
  1575
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1576
{
icculus@2049
  1577
    /* Mix the user-level audio format */
icculus@2049
  1578
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1579
    if (device != NULL) {
icculus@10757
  1580
        SDL_MixAudioFormat(dst, src, device->callbackspec.format, len, volume);
icculus@2049
  1581
    }
icculus@2049
  1582
}
icculus@2049
  1583
slouken@1895
  1584
/* vi: set ts=4 sw=4 expandtab: */