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