src/audio/SDL_audio.c
author Ryan C. Gordon
Fri, 20 Mar 2015 11:11:44 -0400
changeset 9410 e25530faeda0
parent 9405 22d4e4b5a5e5
child 9421 fd5dd91ec0ce
permissions -rw-r--r--
Audio hotplug fixes for winmm and XAudio2 backends.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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_audiomem.h"
slouken@0
    29
#include "SDL_sysaudio.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
gabomdq@8833
   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@9394
   168
    SDL_assert(current_audio.impl.OnlyHasDefaultInputDevice || !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
slouken@2060
   208
static void
slouken@2060
   209
SDL_AudioCloseDevice_Default(_THIS)
slouken@2060
   210
{                               /* no-op. */
slouken@2060
   211
}
slouken@2735
   212
slouken@2060
   213
static void
slouken@2060
   214
SDL_AudioDeinitialize_Default(void)
slouken@2060
   215
{                               /* no-op. */
slouken@2060
   216
}
icculus@2049
   217
icculus@9394
   218
static void
icculus@9394
   219
SDL_AudioFreeDeviceHandle_Default(void *handle)
icculus@9394
   220
{                               /* no-op. */
icculus@9394
   221
}
icculus@9394
   222
icculus@9394
   223
icculus@2049
   224
static int
icculus@9394
   225
SDL_AudioOpenDevice_Default(_THIS, void *handle, const char *devname, int iscapture)
icculus@2049
   226
{
icculus@9394
   227
    return SDL_Unsupported();
icculus@2049
   228
}
icculus@2049
   229
icculus@9010
   230
static SDL_INLINE SDL_bool
icculus@9010
   231
is_in_audio_device_thread(SDL_AudioDevice * device)
icculus@9010
   232
{
icculus@9010
   233
    /* The device thread locks the same mutex, but not through the public API.
icculus@9010
   234
       This check is in case the application, in the audio callback,
icculus@9010
   235
       tries to lock the thread that we've already locked from the
icculus@9010
   236
       device thread...just in case we only have non-recursive mutexes. */
icculus@9010
   237
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@9010
   238
        return SDL_TRUE;
icculus@9010
   239
    }
icculus@9010
   240
icculus@9010
   241
    return SDL_FALSE;
icculus@9010
   242
}
icculus@9010
   243
icculus@2049
   244
static void
icculus@2049
   245
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   246
{
icculus@9010
   247
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   248
        SDL_LockMutex(device->mixer_lock);
icculus@2049
   249
    }
icculus@2049
   250
}
icculus@2049
   251
icculus@2049
   252
static void
icculus@2049
   253
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   254
{
icculus@9010
   255
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   256
        SDL_UnlockMutex(device->mixer_lock);
icculus@2049
   257
    }
icculus@2049
   258
}
icculus@2049
   259
slouken@0
   260
slouken@2060
   261
static void
slouken@2060
   262
finalize_audio_entry_points(void)
icculus@2049
   263
{
icculus@2049
   264
    /*
icculus@2049
   265
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   266
     *  blindly call them without having to check for validity first.
icculus@2049
   267
     */
icculus@2049
   268
slouken@2060
   269
#define FILL_STUB(x) \
icculus@2049
   270
        if (current_audio.impl.x == NULL) { \
icculus@2049
   271
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   272
        }
icculus@2049
   273
    FILL_STUB(DetectDevices);
icculus@2049
   274
    FILL_STUB(OpenDevice);
icculus@2049
   275
    FILL_STUB(ThreadInit);
icculus@2049
   276
    FILL_STUB(WaitDevice);
icculus@2049
   277
    FILL_STUB(PlayDevice);
icculus@9031
   278
    FILL_STUB(GetPendingBytes);
icculus@2049
   279
    FILL_STUB(GetDeviceBuf);
icculus@2049
   280
    FILL_STUB(WaitDone);
icculus@2049
   281
    FILL_STUB(CloseDevice);
icculus@2049
   282
    FILL_STUB(LockDevice);
icculus@2049
   283
    FILL_STUB(UnlockDevice);
icculus@9394
   284
    FILL_STUB(FreeDeviceHandle);
icculus@2049
   285
    FILL_STUB(Deinitialize);
slouken@2060
   286
#undef FILL_STUB
icculus@2049
   287
}
icculus@2049
   288
slouken@2716
   289
icculus@9393
   290
/* device hotplug support... */
icculus@9393
   291
icculus@9393
   292
static int
icculus@9394
   293
add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
icculus@9393
   294
{
icculus@9393
   295
    int retval = -1;
icculus@9394
   296
    const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1;
icculus@9394
   297
    SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size);
icculus@9394
   298
    if (item == NULL) {
icculus@9394
   299
        return -1;
icculus@9394
   300
    }
icculus@9393
   301
icculus@9410
   302
    SDL_assert(handle != NULL);  /* we reserve NULL, audio backends can't use it. */
icculus@9394
   303
icculus@9394
   304
    item->handle = handle;
icculus@9394
   305
    SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem));
icculus@9394
   306
icculus@9394
   307
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   308
    item->next = *devices;
icculus@9394
   309
    *devices = item;
icculus@9394
   310
    retval = (*devCount)++;
icculus@9394
   311
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   312
icculus@9393
   313
    return retval;
icculus@9393
   314
}
icculus@9393
   315
icculus@9394
   316
static SDL_INLINE int
icculus@9394
   317
add_capture_device(const char *name, void *handle)
icculus@9393
   318
{
icculus@9393
   319
    /* !!! FIXME: add this later. SDL_assert(current_audio.impl.HasCaptureSupport);*/
icculus@9394
   320
    return add_audio_device(name, handle, &current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9393
   321
}
icculus@9393
   322
icculus@9394
   323
static SDL_INLINE int
icculus@9394
   324
add_output_device(const char *name, void *handle)
icculus@9393
   325
{
icculus@9394
   326
    return add_audio_device(name, handle, &current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9393
   327
}
icculus@9393
   328
icculus@9393
   329
static void
icculus@9394
   330
free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
icculus@9393
   331
{
icculus@9394
   332
    SDL_AudioDeviceItem *item, *next;
icculus@9394
   333
    for (item = *devices; item != NULL; item = next) {
icculus@9394
   334
        next = item->next;
icculus@9394
   335
        if (item->handle != NULL) {
icculus@9394
   336
            current_audio.impl.FreeDeviceHandle(item->handle);
icculus@9393
   337
        }
icculus@9394
   338
        SDL_free(item);
icculus@9393
   339
    }
icculus@9393
   340
    *devices = NULL;
icculus@9393
   341
    *devCount = 0;
icculus@9393
   342
}
icculus@9393
   343
icculus@9393
   344
icculus@9393
   345
/* The audio backends call this when a new device is plugged in. */
icculus@9393
   346
void
icculus@9394
   347
SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
icculus@9393
   348
{
icculus@9394
   349
    const int device_index = iscapture ? add_capture_device(name, handle) : add_output_device(name, handle);
icculus@9393
   350
    if (device_index != -1) {
icculus@9393
   351
        /* Post the event, if desired */
icculus@9393
   352
        if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) {
icculus@9393
   353
            SDL_Event event;
icculus@9401
   354
            SDL_zero(event);
icculus@9393
   355
            event.adevice.type = SDL_AUDIODEVICEADDED;
icculus@9393
   356
            event.adevice.which = device_index;
icculus@9393
   357
            event.adevice.iscapture = iscapture;
icculus@9393
   358
            SDL_PushEvent(&event);
icculus@9393
   359
        }
icculus@9393
   360
    }
icculus@9393
   361
}
icculus@9393
   362
icculus@9394
   363
/* The audio backends call this when a currently-opened device is lost. */
icculus@9394
   364
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
icculus@9393
   365
{
icculus@9394
   366
    SDL_assert(get_audio_device(device->id) == device);
icculus@9396
   367
icculus@9396
   368
    if (!device->enabled) {
icculus@9396
   369
        return;
icculus@9396
   370
    }
icculus@9394
   371
icculus@9394
   372
    /* Ends the audio callback and mark the device as STOPPED, but the
icculus@9394
   373
       app still needs to close the device to free resources. */
icculus@9394
   374
    current_audio.impl.LockDevice(device);
icculus@9394
   375
    device->enabled = 0;
icculus@9394
   376
    current_audio.impl.UnlockDevice(device);
icculus@9393
   377
icculus@9394
   378
    /* Post the event, if desired */
icculus@9394
   379
    if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
icculus@9394
   380
        SDL_Event event;
icculus@9401
   381
        SDL_zero(event);
icculus@9394
   382
        event.adevice.type = SDL_AUDIODEVICEREMOVED;
icculus@9394
   383
        event.adevice.which = device->id;
icculus@9394
   384
        event.adevice.iscapture = device->iscapture ? 1 : 0;
icculus@9394
   385
        SDL_PushEvent(&event);
icculus@9394
   386
    }
icculus@9394
   387
}
icculus@9393
   388
icculus@9394
   389
static void
icculus@9394
   390
mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag)
icculus@9394
   391
{
icculus@9394
   392
    SDL_AudioDeviceItem *item;
icculus@9394
   393
    SDL_assert(handle != NULL);
icculus@9394
   394
    for (item = devices; item != NULL; item = item->next) {
icculus@9394
   395
        if (item->handle == handle) {
icculus@9394
   396
            item->handle = NULL;
icculus@9394
   397
            *removedFlag = SDL_TRUE;
icculus@9394
   398
            return;
icculus@9393
   399
        }
icculus@9393
   400
    }
icculus@9394
   401
}
icculus@9393
   402
icculus@9394
   403
/* The audio backends call this when a device is removed from the system. */
icculus@9394
   404
void
icculus@9399
   405
SDL_RemoveAudioDevice(const int iscapture, void *handle)
icculus@9394
   406
{
icculus@9394
   407
    SDL_LockMutex(current_audio.detectionLock);
icculus@9399
   408
    if (iscapture) {
icculus@9399
   409
        mark_device_removed(handle, current_audio.inputDevices, &current_audio.captureDevicesRemoved);
icculus@9399
   410
    } else {
icculus@9399
   411
        mark_device_removed(handle, current_audio.outputDevices, &current_audio.outputDevicesRemoved);
icculus@9399
   412
    }
icculus@9394
   413
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9394
   414
    current_audio.impl.FreeDeviceHandle(handle);
icculus@9393
   415
}
icculus@9393
   416
icculus@9393
   417
icculus@9012
   418
icculus@9012
   419
/* buffer queueing support... */
icculus@9012
   420
icculus@9012
   421
/* this expects that you managed thread safety elsewhere. */
icculus@9012
   422
static void
icculus@9012
   423
free_audio_queue(SDL_AudioBufferQueue *buffer)
icculus@9012
   424
{
icculus@9012
   425
    while (buffer) {
icculus@9012
   426
        SDL_AudioBufferQueue *next = buffer->next;
icculus@9012
   427
        SDL_free(buffer);
icculus@9012
   428
        buffer = next;
icculus@9012
   429
    }
icculus@9012
   430
}
icculus@9012
   431
icculus@9012
   432
static void SDLCALL
icculus@9012
   433
SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int _len)
icculus@9012
   434
{
icculus@9012
   435
    /* this function always holds the mixer lock before being called. */
icculus@9012
   436
    Uint32 len = (Uint32) _len;
icculus@9012
   437
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
icculus@9012
   438
    SDL_AudioBufferQueue *buffer;
icculus@9012
   439
icculus@9012
   440
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
icculus@9012
   441
    SDL_assert(_len >= 0);  /* this shouldn't ever happen, right?! */
icculus@9012
   442
icculus@9012
   443
    while ((len > 0) && ((buffer = device->buffer_queue_head) != NULL)) {
icculus@9012
   444
        const Uint32 avail = buffer->datalen - buffer->startpos;
icculus@9012
   445
        const Uint32 cpy = SDL_min(len, avail);
icculus@9012
   446
        SDL_assert(device->queued_bytes >= avail);
icculus@9012
   447
icculus@9012
   448
        SDL_memcpy(stream, buffer->data + buffer->startpos, cpy);
icculus@9012
   449
        buffer->startpos += cpy;
icculus@9012
   450
        stream += cpy;
icculus@9012
   451
        device->queued_bytes -= cpy;
icculus@9012
   452
        len -= cpy;
icculus@9012
   453
icculus@9012
   454
        if (buffer->startpos == buffer->datalen) {  /* packet is done, put it in the pool. */
icculus@9012
   455
            device->buffer_queue_head = buffer->next;
icculus@9012
   456
            SDL_assert((buffer->next != NULL) || (buffer == device->buffer_queue_tail));
icculus@9012
   457
            buffer->next = device->buffer_queue_pool;
icculus@9012
   458
            device->buffer_queue_pool = buffer;
icculus@9012
   459
        }
icculus@9012
   460
    }
icculus@9012
   461
icculus@9012
   462
    SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
icculus@9012
   463
icculus@9012
   464
    if (len > 0) {  /* fill any remaining space in the stream with silence. */
icculus@9012
   465
        SDL_assert(device->buffer_queue_head == NULL);
icculus@9012
   466
        SDL_memset(stream, device->spec.silence, len);
icculus@9012
   467
    }
icculus@9012
   468
icculus@9012
   469
    if (device->buffer_queue_head == NULL) {
icculus@9012
   470
        device->buffer_queue_tail = NULL;  /* in case we drained the queue entirely. */
icculus@9012
   471
    }
icculus@9012
   472
}
icculus@9012
   473
icculus@9012
   474
int
icculus@9012
   475
SDL_QueueAudio(SDL_AudioDeviceID devid, const void *_data, Uint32 len)
icculus@9012
   476
{
icculus@9012
   477
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9012
   478
    const Uint8 *data = (const Uint8 *) _data;
icculus@9012
   479
    SDL_AudioBufferQueue *orighead;
icculus@9012
   480
    SDL_AudioBufferQueue *origtail;
icculus@9012
   481
    Uint32 origlen;
icculus@9012
   482
    Uint32 datalen;
icculus@9012
   483
icculus@9012
   484
    if (!device) {
icculus@9012
   485
        return -1;  /* get_audio_device() will have set the error state */
icculus@9012
   486
    }
icculus@9012
   487
icculus@9012
   488
    if (device->spec.callback != SDL_BufferQueueDrainCallback) {
icculus@9012
   489
        return SDL_SetError("Audio device has a callback, queueing not allowed");
icculus@9012
   490
    }
icculus@9012
   491
icculus@9012
   492
    current_audio.impl.LockDevice(device);
icculus@9012
   493
icculus@9012
   494
    orighead = device->buffer_queue_head;
icculus@9012
   495
    origtail = device->buffer_queue_tail;
icculus@9012
   496
    origlen = origtail ? origtail->datalen : 0;
icculus@9012
   497
icculus@9012
   498
    while (len > 0) {
icculus@9012
   499
        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
icculus@9012
   500
        SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
icculus@9012
   501
        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
icculus@9012
   502
            /* tail packet missing or completely full; we need a new packet. */
icculus@9012
   503
            packet = device->buffer_queue_pool;
icculus@9012
   504
            if (packet != NULL) {
icculus@9012
   505
                /* we have one available in the pool. */
icculus@9012
   506
                device->buffer_queue_pool = packet->next;
icculus@9012
   507
            } else {
icculus@9012
   508
                /* Have to allocate a new one! */
icculus@9012
   509
                packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
icculus@9012
   510
                if (packet == NULL) {
icculus@9012
   511
                    /* uhoh, reset so we've queued nothing new, free what we can. */
icculus@9012
   512
                    if (!origtail) {
icculus@9012
   513
                        packet = device->buffer_queue_head;  /* whole queue. */
icculus@9012
   514
                    } else {
icculus@9012
   515
                        packet = origtail->next;  /* what we added to existing queue. */
icculus@9012
   516
                        origtail->next = NULL;
icculus@9012
   517
                        origtail->datalen = origlen;
icculus@9012
   518
                    }
icculus@9012
   519
                    device->buffer_queue_head = orighead;
icculus@9012
   520
                    device->buffer_queue_tail = origtail;
icculus@9012
   521
                    device->buffer_queue_pool = NULL;
icculus@9012
   522
icculus@9012
   523
                    current_audio.impl.UnlockDevice(device);
icculus@9012
   524
icculus@9012
   525
                    free_audio_queue(packet);  /* give back what we can. */
icculus@9012
   526
icculus@9012
   527
                    return SDL_OutOfMemory();
icculus@9012
   528
                }
icculus@9012
   529
            }
icculus@9012
   530
            packet->datalen = 0;
icculus@9012
   531
            packet->startpos = 0;
icculus@9012
   532
            packet->next = NULL;
icculus@9012
   533
icculus@9012
   534
            SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
icculus@9012
   535
            if (device->buffer_queue_tail == NULL) {
icculus@9012
   536
                device->buffer_queue_head = packet;
icculus@9012
   537
            } else {
icculus@9012
   538
                device->buffer_queue_tail->next = packet;
icculus@9012
   539
            }
icculus@9012
   540
            device->buffer_queue_tail = packet;
icculus@9012
   541
        }
icculus@9012
   542
icculus@9012
   543
        datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
icculus@9012
   544
        SDL_memcpy(packet->data + packet->datalen, data, datalen);
icculus@9012
   545
        data += datalen;
icculus@9012
   546
        len -= datalen;
icculus@9012
   547
        packet->datalen += datalen;
icculus@9012
   548
        device->queued_bytes += datalen;
icculus@9012
   549
    }
icculus@9012
   550
icculus@9012
   551
    current_audio.impl.UnlockDevice(device);
icculus@9012
   552
icculus@9012
   553
    return 0;
icculus@9012
   554
}
icculus@9012
   555
icculus@9012
   556
Uint32
icculus@9012
   557
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
icculus@9012
   558
{
icculus@9012
   559
    Uint32 retval = 0;
icculus@9012
   560
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9032
   561
icculus@9032
   562
    /* Nothing to do unless we're set up for queueing. */
icculus@9032
   563
    if (device && (device->spec.callback == SDL_BufferQueueDrainCallback)) {
icculus@9012
   564
        current_audio.impl.LockDevice(device);
icculus@9031
   565
        retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
icculus@9012
   566
        current_audio.impl.UnlockDevice(device);
icculus@9012
   567
    }
icculus@9012
   568
icculus@9012
   569
    return retval;
icculus@9012
   570
}
icculus@9012
   571
icculus@9012
   572
void
icculus@9012
   573
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
icculus@9012
   574
{
icculus@9012
   575
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9012
   576
    SDL_AudioBufferQueue *buffer = NULL;
icculus@9012
   577
    if (!device) {
icculus@9012
   578
        return;  /* nothing to do. */
icculus@9012
   579
    }
icculus@9012
   580
icculus@9012
   581
    /* Blank out the device and release the mutex. Free it afterwards. */
icculus@9012
   582
    current_audio.impl.LockDevice(device);
icculus@9012
   583
    buffer = device->buffer_queue_head;
icculus@9012
   584
    device->buffer_queue_tail = NULL;
icculus@9012
   585
    device->buffer_queue_head = NULL;
icculus@9012
   586
    device->queued_bytes = 0;
icculus@9012
   587
    current_audio.impl.UnlockDevice(device);
icculus@9012
   588
icculus@9012
   589
    free_audio_queue(buffer);
icculus@9012
   590
}
icculus@9012
   591
icculus@9012
   592
dimitris@8761
   593
#if defined(__ANDROID__)
paul@4722
   594
#include <android/log.h>
aschiffler@4865
   595
#endif
paul@4722
   596
slouken@0
   597
/* The general mixing thread function */
slouken@1895
   598
int SDLCALL
icculus@2049
   599
SDL_RunAudio(void *devicep)
slouken@0
   600
{
icculus@2049
   601
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@9398
   602
    const int silence = (int) device->spec.silence;
icculus@9398
   603
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
icculus@9398
   604
    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
slouken@1895
   605
    Uint8 *stream;
icculus@9398
   606
    void *udata = device->spec.userdata;
icculus@9398
   607
    void (SDLCALL *fill) (void *, Uint8 *, int) = device->spec.callback;
slouken@2716
   608
slouken@5509
   609
    /* The audio mixing is always a high priority thread */
slouken@5509
   610
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
slouken@5509
   611
slouken@1895
   612
    /* Perform any thread setup */
icculus@2049
   613
    device->threadid = SDL_ThreadID();
icculus@2049
   614
    current_audio.impl.ThreadInit(device);
slouken@0
   615
icculus@9398
   616
    /* Loop, filling the audio buffers */
icculus@9398
   617
    while (!device->shutdown) {
icculus@9398
   618
        /* Fill the current buffer with sound */
icculus@9398
   619
        if (device->convert.needed) {
icculus@9398
   620
            stream = device->convert.buf;
icculus@9398
   621
        } else if (device->enabled) {
icculus@9398
   622
            stream = current_audio.impl.GetDeviceBuf(device);
icculus@9398
   623
        } else {
icculus@9398
   624
            /* if the device isn't enabled, we still write to the
icculus@9398
   625
               fake_stream, so the app's callback will fire with
icculus@9398
   626
               a regular frequency, in case they depend on that
icculus@9398
   627
               for timing or progress. They can use hotplug
icculus@9398
   628
               now to know if the device failed. */
icculus@9398
   629
            stream = NULL;
slouken@2716
   630
        }
slouken@3336
   631
icculus@9398
   632
        if (stream == NULL) {
icculus@9398
   633
            stream = device->fake_stream;
icculus@9398
   634
        }
slouken@2716
   635
icculus@9398
   636
        /* !!! FIXME: this should be LockDevice. */
icculus@9398
   637
        SDL_LockMutex(device->mixer_lock);
icculus@9398
   638
        if (device->paused) {
icculus@9398
   639
            SDL_memset(stream, silence, stream_len);
icculus@9398
   640
        } else {
icculus@9398
   641
            (*fill) (udata, stream, stream_len);
icculus@9398
   642
        }
icculus@9398
   643
        SDL_UnlockMutex(device->mixer_lock);
slouken@2716
   644
icculus@9398
   645
        /* Convert the audio if necessary */
icculus@9398
   646
        if (device->enabled && device->convert.needed) {
icculus@9398
   647
            SDL_ConvertAudio(&device->convert);
icculus@9398
   648
            stream = current_audio.impl.GetDeviceBuf(device);
icculus@9397
   649
            if (stream == NULL) {
icculus@9397
   650
                stream = device->fake_stream;
icculus@9398
   651
            } else {
icculus@9398
   652
                SDL_memcpy(stream, device->convert.buf,
icculus@9398
   653
                           device->convert.len_cvt);
slouken@1895
   654
            }
icculus@9398
   655
        }
slouken@1562
   656
icculus@9398
   657
        /* Ready current buffer for play and change current buffer */
icculus@9398
   658
        if (stream == device->fake_stream) {
icculus@9398
   659
            SDL_Delay(delay);
icculus@9398
   660
        } else {
icculus@9398
   661
            current_audio.impl.PlayDevice(device);
icculus@9398
   662
            current_audio.impl.WaitDevice(device);
slouken@1895
   663
        }
slouken@1895
   664
    }
slouken@1562
   665
icculus@9398
   666
    /* Wait for the audio to drain. */
icculus@2049
   667
    current_audio.impl.WaitDone(device);
slouken@21
   668
icculus@9382
   669
    return 0;
slouken@0
   670
}
slouken@0
   671
slouken@322
   672
icculus@1982
   673
static SDL_AudioFormat
slouken@1895
   674
SDL_ParseAudioFormat(const char *string)
slouken@1794
   675
{
icculus@2076
   676
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   677
    CHECK_FMT_STRING(U8);
icculus@2049
   678
    CHECK_FMT_STRING(S8);
icculus@2049
   679
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   680
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   681
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   682
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   683
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   684
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   685
    CHECK_FMT_STRING(U16);
icculus@2049
   686
    CHECK_FMT_STRING(S16);
icculus@2049
   687
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   688
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   689
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   690
    CHECK_FMT_STRING(S32);
icculus@2049
   691
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   692
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   693
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   694
    CHECK_FMT_STRING(F32);
slouken@2060
   695
#undef CHECK_FMT_STRING
icculus@2049
   696
    return 0;
slouken@1895
   697
}
slouken@1895
   698
slouken@1895
   699
int
slouken@1895
   700
SDL_GetNumAudioDrivers(void)
slouken@1895
   701
{
icculus@9382
   702
    return SDL_arraysize(bootstrap) - 1;
slouken@1895
   703
}
slouken@1895
   704
slouken@1895
   705
const char *
slouken@1895
   706
SDL_GetAudioDriver(int index)
slouken@1895
   707
{
slouken@1895
   708
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
icculus@9382
   709
        return bootstrap[index]->name;
slouken@1895
   710
    }
icculus@9382
   711
    return NULL;
slouken@1794
   712
}
slouken@1794
   713
slouken@1895
   714
int
slouken@1895
   715
SDL_AudioInit(const char *driver_name)
slouken@0
   716
{
icculus@2049
   717
    int i = 0;
icculus@2049
   718
    int initialized = 0;
icculus@2049
   719
    int tried_to_init = 0;
slouken@0
   720
icculus@2049
   721
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   722
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   723
    }
slouken@0
   724
icculus@9392
   725
    SDL_zero(current_audio);
icculus@9392
   726
    SDL_zero(open_devices);
icculus@2049
   727
slouken@1895
   728
    /* Select the proper audio driver */
slouken@1909
   729
    if (driver_name == NULL) {
slouken@1909
   730
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   731
    }
icculus@2049
   732
icculus@2049
   733
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   734
        /* make sure we should even try this driver before doing so... */
icculus@2049
   735
        const AudioBootStrap *backend = bootstrap[i];
slouken@6900
   736
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
slouken@6900
   737
            (!driver_name && backend->demand_only)) {
icculus@2049
   738
            continue;
icculus@2049
   739
        }
slouken@0
   740
icculus@2049
   741
        tried_to_init = 1;
icculus@9392
   742
        SDL_zero(current_audio);
icculus@2049
   743
        current_audio.name = backend->name;
icculus@2049
   744
        current_audio.desc = backend->desc;
icculus@3699
   745
        initialized = backend->init(&current_audio.impl);
slouken@1895
   746
    }
icculus@2049
   747
icculus@2049
   748
    if (!initialized) {
icculus@2049
   749
        /* specific drivers will set the error message if they fail... */
icculus@2049
   750
        if (!tried_to_init) {
slouken@1895
   751
            if (driver_name) {
icculus@3699
   752
                SDL_SetError("Audio target '%s' not available", driver_name);
slouken@1895
   753
            } else {
slouken@1895
   754
                SDL_SetError("No available audio device");
slouken@1895
   755
            }
slouken@1895
   756
        }
icculus@2049
   757
icculus@9392
   758
        SDL_zero(current_audio);
icculus@9382
   759
        return -1;            /* No driver was available, so fail. */
slouken@1895
   760
    }
icculus@2049
   761
icculus@9394
   762
    current_audio.detectionLock = SDL_CreateMutex();
icculus@9393
   763
icculus@2049
   764
    finalize_audio_entry_points();
icculus@2049
   765
icculus@9393
   766
    /* Make sure we have a list of devices available at startup. */
icculus@9394
   767
    current_audio.impl.DetectDevices();
icculus@9393
   768
icculus@9382
   769
    return 0;
slouken@0
   770
}
slouken@0
   771
slouken@1895
   772
/*
slouken@1895
   773
 * Get the current audio driver name
slouken@1895
   774
 */
slouken@1895
   775
const char *
slouken@1895
   776
SDL_GetCurrentAudioDriver()
slouken@0
   777
{
icculus@2049
   778
    return current_audio.name;
slouken@0
   779
}
slouken@0
   780
icculus@9394
   781
/* Clean out devices that we've removed but had to keep around for stability. */
icculus@9394
   782
static void
icculus@9394
   783
clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
icculus@9394
   784
{
icculus@9394
   785
    SDL_AudioDeviceItem *item = *devices;
icculus@9394
   786
    SDL_AudioDeviceItem *prev = NULL;
icculus@9394
   787
    int total = 0;
icculus@9394
   788
icculus@9394
   789
    while (item) {
icculus@9394
   790
        SDL_AudioDeviceItem *next = item->next;
icculus@9394
   791
        if (item->handle != NULL) {
icculus@9394
   792
            total++;
icculus@9394
   793
            prev = item;
icculus@9394
   794
        } else {
icculus@9394
   795
            if (prev) {
icculus@9394
   796
                prev->next = next;
icculus@9394
   797
            } else {
icculus@9394
   798
                *devices = next;
icculus@9394
   799
            }
icculus@9394
   800
            SDL_free(item);
icculus@9394
   801
        }
icculus@9394
   802
        item = next;
icculus@9394
   803
    }
icculus@9394
   804
icculus@9394
   805
    *devCount = total;
icculus@9394
   806
    *removedFlag = SDL_FALSE;
icculus@9394
   807
}
icculus@9394
   808
icculus@9394
   809
slouken@1895
   810
int
icculus@2049
   811
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   812
{
icculus@5593
   813
    int retval = 0;
icculus@5593
   814
icculus@2049
   815
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   816
        return -1;
icculus@2049
   817
    }
icculus@5593
   818
icculus@9394
   819
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   820
    if (iscapture && current_audio.captureDevicesRemoved) {
icculus@9394
   821
        clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
icculus@2049
   822
    }
icculus@2049
   823
icculus@9394
   824
    if (!iscapture && current_audio.outputDevicesRemoved) {
icculus@9394
   825
        clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
icculus@9394
   826
        current_audio.outputDevicesRemoved = SDL_FALSE;
icculus@5593
   827
    }
icculus@5593
   828
icculus@9393
   829
    retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   830
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   831
icculus@5593
   832
    return retval;
icculus@2049
   833
}
icculus@2049
   834
slouken@0
   835
icculus@2049
   836
const char *
icculus@2049
   837
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   838
{
icculus@9393
   839
    const char *retval = NULL;
icculus@9393
   840
icculus@2049
   841
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   842
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   843
        return NULL;
icculus@2049
   844
    }
icculus@2049
   845
icculus@2049
   846
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   847
        SDL_SetError("No capture support");
icculus@2049
   848
        return NULL;
slouken@1895
   849
    }
icculus@2049
   850
icculus@9394
   851
    if (index >= 0) {
icculus@9394
   852
        SDL_AudioDeviceItem *item;
icculus@9394
   853
        int i;
slouken@0
   854
icculus@9394
   855
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   856
        item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
icculus@9394
   857
        i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   858
        if (index < i) {
icculus@9394
   859
            for (i--; i > index; i--, item = item->next) {
icculus@9394
   860
                SDL_assert(item != NULL);
icculus@9394
   861
            }
icculus@9394
   862
            SDL_assert(item != NULL);
icculus@9394
   863
            retval = item->name;
slouken@7904
   864
        }
icculus@9394
   865
        SDL_UnlockMutex(current_audio.detectionLock);
slouken@1895
   866
    }
slouken@262
   867
icculus@9394
   868
    if (retval == NULL) {
icculus@9394
   869
        SDL_SetError("No such device");
icculus@5593
   870
    }
icculus@5593
   871
icculus@9394
   872
    return retval;
icculus@2049
   873
}
icculus@2049
   874
icculus@2049
   875
icculus@2049
   876
static void
slouken@2060
   877
close_audio_device(SDL_AudioDevice * device)
icculus@2049
   878
{
icculus@2049
   879
    device->enabled = 0;
icculus@9397
   880
    device->shutdown = 1;
icculus@2049
   881
    if (device->thread != NULL) {
icculus@2049
   882
        SDL_WaitThread(device->thread, NULL);
icculus@2049
   883
    }
icculus@2049
   884
    if (device->mixer_lock != NULL) {
icculus@2049
   885
        SDL_DestroyMutex(device->mixer_lock);
icculus@2049
   886
    }
slouken@7719
   887
    SDL_FreeAudioMem(device->fake_stream);
icculus@2049
   888
    if (device->convert.needed) {
icculus@2049
   889
        SDL_FreeAudioMem(device->convert.buf);
icculus@2049
   890
    }
icculus@2049
   891
    if (device->opened) {
icculus@2049
   892
        current_audio.impl.CloseDevice(device);
icculus@2049
   893
        device->opened = 0;
icculus@2049
   894
    }
icculus@9012
   895
icculus@9012
   896
    free_audio_queue(device->buffer_queue_head);
icculus@9012
   897
    free_audio_queue(device->buffer_queue_pool);
icculus@9012
   898
icculus@2049
   899
    SDL_FreeAudioMem(device);
icculus@2049
   900
}
icculus@2049
   901
icculus@2049
   902
icculus@2049
   903
/*
icculus@2049
   904
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   905
 *  Fills in a sanitized copy in (prepared).
icculus@2049
   906
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
   907
 */
icculus@2049
   908
static int
slouken@2060
   909
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
   910
{
slouken@2060
   911
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
   912
icculus@2049
   913
    if (orig->freq == 0) {
icculus@2049
   914
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
   915
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
   916
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
   917
        }
slouken@1895
   918
    }
icculus@2049
   919
icculus@2049
   920
    if (orig->format == 0) {
icculus@2049
   921
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
   922
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
   923
            prepared->format = AUDIO_S16;       /* a reasonable default */
slouken@1895
   924
        }
slouken@1895
   925
    }
icculus@2049
   926
icculus@2049
   927
    switch (orig->channels) {
slouken@2060
   928
    case 0:{
slouken@2060
   929
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
   930
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
   931
                prepared->channels = 2; /* a reasonable default */
slouken@2060
   932
            }
slouken@2060
   933
            break;
icculus@2049
   934
        }
slouken@1895
   935
    case 1:                    /* Mono */
slouken@1895
   936
    case 2:                    /* Stereo */
slouken@1895
   937
    case 4:                    /* surround */
slouken@1895
   938
    case 6:                    /* surround with center and lfe */
slouken@1895
   939
        break;
slouken@1895
   940
    default:
icculus@2049
   941
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
   942
        return 0;
slouken@1895
   943
    }
icculus@2049
   944
icculus@2049
   945
    if (orig->samples == 0) {
icculus@2049
   946
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
   947
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
   948
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
   949
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
   950
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
   951
            int power2 = 1;
icculus@2049
   952
            while (power2 < samples) {
icculus@2049
   953
                power2 *= 2;
icculus@2049
   954
            }
icculus@2049
   955
            prepared->samples = power2;
slouken@1895
   956
        }
slouken@1895
   957
    }
slouken@0
   958
slouken@1895
   959
    /* Calculate the silence and size of the audio specification */
icculus@2049
   960
    SDL_CalculateAudioSpec(prepared);
slouken@21
   961
icculus@2049
   962
    return 1;
icculus@2049
   963
}
slouken@1408
   964
icculus@2049
   965
static SDL_AudioDeviceID
icculus@2049
   966
open_audio_device(const char *devname, int iscapture,
slouken@2866
   967
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
   968
                  int allowed_changes, int min_id)
icculus@2049
   969
{
icculus@2049
   970
    SDL_AudioDeviceID id = 0;
slouken@2866
   971
    SDL_AudioSpec _obtained;
icculus@2049
   972
    SDL_AudioDevice *device;
slouken@2866
   973
    SDL_bool build_cvt;
icculus@9394
   974
    void *handle = NULL;
icculus@9404
   975
    Uint32 stream_len;
icculus@2049
   976
    int i = 0;
slouken@21
   977
icculus@2049
   978
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   979
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   980
        return 0;
icculus@2049
   981
    }
icculus@2049
   982
icculus@2049
   983
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   984
        SDL_SetError("No capture support");
icculus@2049
   985
        return 0;
icculus@2049
   986
    }
icculus@2049
   987
icculus@9393
   988
    /* Find an available device ID... */
icculus@9393
   989
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@9393
   990
        if (open_devices[id] == NULL) {
icculus@9393
   991
            break;
icculus@9393
   992
        }
icculus@9393
   993
    }
icculus@9393
   994
icculus@9393
   995
    if (id == SDL_arraysize(open_devices)) {
icculus@9393
   996
        SDL_SetError("Too many open audio devices");
icculus@9393
   997
        return 0;
icculus@9393
   998
    }
icculus@9393
   999
slouken@2866
  1000
    if (!obtained) {
slouken@2866
  1001
        obtained = &_obtained;
slouken@2866
  1002
    }
slouken@2866
  1003
    if (!prepare_audiospec(desired, obtained)) {
icculus@2049
  1004
        return 0;
icculus@2049
  1005
    }
icculus@2049
  1006
icculus@2049
  1007
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
  1008
    if (devname == NULL) {
icculus@2049
  1009
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
  1010
    }
slouken@21
  1011
icculus@2049
  1012
    /*
icculus@2049
  1013
     * Catch device names at the high level for the simple case...
icculus@2049
  1014
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
  1015
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
  1016
     *  always NULL when it hits the low level.
icculus@2049
  1017
     *
icculus@2049
  1018
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
  1019
     *  opens of the default system device.
icculus@2049
  1020
     */
icculus@2049
  1021
icculus@2049
  1022
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
  1023
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
  1024
            SDL_SetError("No such device");
icculus@2049
  1025
            return 0;
icculus@2049
  1026
        }
icculus@2049
  1027
        devname = NULL;
icculus@2049
  1028
icculus@2049
  1029
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1030
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
  1031
                SDL_SetError("Audio device already open");
icculus@2049
  1032
                return 0;
icculus@2049
  1033
            }
icculus@2049
  1034
        }
icculus@9394
  1035
    } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
  1036
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
  1037
            SDL_SetError("No such device");
icculus@2049
  1038
            return 0;
icculus@2049
  1039
        }
icculus@2049
  1040
        devname = NULL;
icculus@2049
  1041
icculus@2049
  1042
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1043
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
  1044
                SDL_SetError("Audio device already open");
icculus@2049
  1045
                return 0;
icculus@2049
  1046
            }
icculus@2049
  1047
        }
icculus@9394
  1048
    } else if (devname != NULL) {
icculus@9394
  1049
        /* if the app specifies an exact string, we can pass the backend
icculus@9394
  1050
           an actual device handle thingey, which saves them the effort of
icculus@9394
  1051
           figuring out what device this was (such as, reenumerating
icculus@9394
  1052
           everything again to find the matching human-readable name).
icculus@9394
  1053
           It might still need to open a device based on the string for,
icculus@9394
  1054
           say, a network audio server, but this optimizes some cases. */
icculus@9394
  1055
        SDL_AudioDeviceItem *item;
icculus@9394
  1056
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
  1057
        for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) {
icculus@9394
  1058
            if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) {
icculus@9394
  1059
                handle = item->handle;
icculus@9394
  1060
                break;
icculus@9394
  1061
            }
icculus@9394
  1062
        }
icculus@9394
  1063
        SDL_UnlockMutex(current_audio.detectionLock);
icculus@9394
  1064
    }
icculus@9394
  1065
icculus@9394
  1066
    if (!current_audio.impl.AllowsArbitraryDeviceNames) {
icculus@9394
  1067
        /* has to be in our device list, or the default device. */
icculus@9394
  1068
        if ((handle == NULL) && (devname != NULL)) {
icculus@9394
  1069
            SDL_SetError("No such device.");
icculus@9394
  1070
            return 0;
icculus@9394
  1071
        }
icculus@2049
  1072
    }
icculus@2049
  1073
slouken@2060
  1074
    device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof(SDL_AudioDevice));
icculus@2049
  1075
    if (device == NULL) {
icculus@2049
  1076
        SDL_OutOfMemory();
icculus@2049
  1077
        return 0;
icculus@2049
  1078
    }
icculus@9012
  1079
    SDL_zerop(device);
icculus@9393
  1080
    device->id = id + 1;
slouken@2866
  1081
    device->spec = *obtained;
icculus@2049
  1082
    device->enabled = 1;
icculus@2049
  1083
    device->paused = 1;
icculus@2049
  1084
    device->iscapture = iscapture;
icculus@2049
  1085
icculus@9391
  1086
    /* Create a mutex for locking the sound buffers */
icculus@2049
  1087
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
  1088
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
  1089
        if (device->mixer_lock == NULL) {
icculus@2049
  1090
            close_audio_device(device);
icculus@2049
  1091
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
  1092
            return 0;
icculus@2049
  1093
        }
icculus@2049
  1094
    }
icculus@2049
  1095
icculus@9394
  1096
    if (current_audio.impl.OpenDevice(device, handle, devname, iscapture) < 0) {
icculus@2049
  1097
        close_audio_device(device);
icculus@2049
  1098
        return 0;
icculus@2049
  1099
    }
icculus@2049
  1100
    device->opened = 1;
slouken@0
  1101
slouken@1895
  1102
    /* See if we need to do any conversion */
slouken@2866
  1103
    build_cvt = SDL_FALSE;
slouken@2866
  1104
    if (obtained->freq != device->spec.freq) {
slouken@2866
  1105
        if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
slouken@2866
  1106
            obtained->freq = device->spec.freq;
slouken@2866
  1107
        } else {
slouken@2866
  1108
            build_cvt = SDL_TRUE;
slouken@2866
  1109
        }
slouken@2866
  1110
    }
slouken@2866
  1111
    if (obtained->format != device->spec.format) {
slouken@2866
  1112
        if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
slouken@2866
  1113
            obtained->format = device->spec.format;
slouken@2866
  1114
        } else {
slouken@2866
  1115
            build_cvt = SDL_TRUE;
slouken@2866
  1116
        }
slouken@2866
  1117
    }
slouken@2866
  1118
    if (obtained->channels != device->spec.channels) {
slouken@2866
  1119
        if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
slouken@2866
  1120
            obtained->channels = device->spec.channels;
slouken@2866
  1121
        } else {
slouken@2866
  1122
            build_cvt = SDL_TRUE;
slouken@2866
  1123
        }
slouken@2866
  1124
    }
slouken@7518
  1125
slouken@7518
  1126
    /* If the audio driver changes the buffer size, accept it.
slouken@7518
  1127
       This needs to be done after the format is modified above,
slouken@7518
  1128
       otherwise it might not have the correct buffer size.
slouken@7518
  1129
     */
slouken@7518
  1130
    if (device->spec.samples != obtained->samples) {
slouken@7518
  1131
        obtained->samples = device->spec.samples;
slouken@7518
  1132
        SDL_CalculateAudioSpec(obtained);
slouken@7518
  1133
    }
slouken@7518
  1134
slouken@2866
  1135
    if (build_cvt) {
slouken@1895
  1136
        /* Build an audio conversion block */
icculus@2049
  1137
        if (SDL_BuildAudioCVT(&device->convert,
slouken@2866
  1138
                              obtained->format, obtained->channels,
slouken@2866
  1139
                              obtained->freq,
icculus@2049
  1140
                              device->spec.format, device->spec.channels,
icculus@2049
  1141
                              device->spec.freq) < 0) {
icculus@2049
  1142
            close_audio_device(device);
icculus@2049
  1143
            return 0;
slouken@1895
  1144
        }
icculus@2049
  1145
        if (device->convert.needed) {
slouken@7518
  1146
            device->convert.len = (int) (((double) device->spec.size) /
slouken@2060
  1147
                                         device->convert.len_ratio);
icculus@2053
  1148
icculus@2049
  1149
            device->convert.buf =
icculus@2049
  1150
                (Uint8 *) SDL_AllocAudioMem(device->convert.len *
icculus@2049
  1151
                                            device->convert.len_mult);
icculus@2049
  1152
            if (device->convert.buf == NULL) {
icculus@2049
  1153
                close_audio_device(device);
slouken@1895
  1154
                SDL_OutOfMemory();
icculus@2049
  1155
                return 0;
slouken@1895
  1156
            }
slouken@1895
  1157
        }
slouken@1895
  1158
    }
icculus@2049
  1159
icculus@9397
  1160
    /* Allocate a fake audio memory buffer */
icculus@9397
  1161
    stream_len = (device->convert.needed) ? device->convert.len_cvt : 0;
icculus@9397
  1162
    if (device->spec.size > stream_len) {
icculus@9397
  1163
        stream_len = device->spec.size;
icculus@9397
  1164
    }
icculus@9405
  1165
    SDL_assert(stream_len > 0);
icculus@9397
  1166
    device->fake_stream = (Uint8 *)SDL_AllocAudioMem(stream_len);
icculus@9397
  1167
    if (device->fake_stream == NULL) {
icculus@9397
  1168
        close_audio_device(device);
icculus@9397
  1169
        SDL_OutOfMemory();
icculus@9397
  1170
        return 0;
icculus@9397
  1171
    }
icculus@9397
  1172
icculus@9012
  1173
    if (device->spec.callback == NULL) {  /* use buffer queueing? */
icculus@9012
  1174
        /* pool a few packets to start. Enough for two callbacks. */
icculus@9012
  1175
        const int packetlen = SDL_AUDIOBUFFERQUEUE_PACKETLEN;
icculus@9012
  1176
        const int wantbytes = ((device->convert.needed) ? device->convert.len : device->spec.size) * 2;
icculus@9012
  1177
        const int wantpackets = (wantbytes / packetlen) + ((wantbytes % packetlen) ? packetlen : 0);
icculus@9012
  1178
        for (i = 0; i < wantpackets; i++) {
icculus@9012
  1179
            SDL_AudioBufferQueue *packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
icculus@9012
  1180
            if (packet) { /* don't care if this fails, we'll deal later. */
icculus@9012
  1181
                packet->datalen = 0;
icculus@9012
  1182
                packet->startpos = 0;
icculus@9012
  1183
                packet->next = device->buffer_queue_pool;
icculus@9012
  1184
                device->buffer_queue_pool = packet;
icculus@9012
  1185
            }
icculus@9012
  1186
        }
icculus@9012
  1187
icculus@9012
  1188
        device->spec.callback = SDL_BufferQueueDrainCallback;
icculus@9012
  1189
        device->spec.userdata = device;
icculus@9012
  1190
    }
icculus@9012
  1191
icculus@9393
  1192
    /* add it to our list of open devices. */
icculus@9393
  1193
    open_devices[id] = device;
icculus@2049
  1194
slouken@1895
  1195
    /* Start the audio thread if necessary */
icculus@2049
  1196
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
  1197
        /* Start the audio thread */
icculus@5969
  1198
        char name[64];
icculus@9393
  1199
        SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) device->id);
icculus@2049
  1200
/* !!! FIXME: this is nasty. */
icculus@6430
  1201
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@1330
  1202
#undef SDL_CreateThread
icculus@8094
  1203
#if SDL_DYNAMIC_API
icculus@8094
  1204
        device->thread = SDL_CreateThread_REAL(SDL_RunAudio, name, device, NULL, NULL);
icculus@8094
  1205
#else
icculus@5969
  1206
        device->thread = SDL_CreateThread(SDL_RunAudio, name, device, NULL, NULL);
icculus@8094
  1207
#endif
slouken@1330
  1208
#else
icculus@5969
  1209
        device->thread = SDL_CreateThread(SDL_RunAudio, name, device);
slouken@1330
  1210
#endif
icculus@2049
  1211
        if (device->thread == NULL) {
icculus@9393
  1212
            SDL_CloseAudioDevice(device->id);
slouken@1895
  1213
            SDL_SetError("Couldn't create audio thread");
icculus@2049
  1214
            return 0;
icculus@2049
  1215
        }
icculus@2049
  1216
    }
icculus@2049
  1217
icculus@9393
  1218
    return device->id;
icculus@2049
  1219
}
icculus@2049
  1220
icculus@2049
  1221
icculus@2049
  1222
int
slouken@2866
  1223
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
  1224
{
icculus@2049
  1225
    SDL_AudioDeviceID id = 0;
icculus@2049
  1226
icculus@2049
  1227
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
  1228
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1229
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
icculus@9382
  1230
            return -1;
slouken@1895
  1231
        }
icculus@2049
  1232
    }
slouken@0
  1233
icculus@2049
  1234
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
  1235
    if (open_devices[0] != NULL) {
icculus@2049
  1236
        SDL_SetError("Audio device is already opened");
icculus@9382
  1237
        return -1;
slouken@1895
  1238
    }
icculus@2049
  1239
slouken@2866
  1240
    if (obtained) {
slouken@2866
  1241
        id = open_audio_device(NULL, 0, desired, obtained,
slouken@2866
  1242
                               SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
slouken@2866
  1243
    } else {
slouken@8919
  1244
        id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
slouken@2866
  1245
    }
slouken@21
  1246
icculus@5964
  1247
    SDL_assert((id == 0) || (id == 1));
icculus@9382
  1248
    return (id == 0) ? -1 : 0;
icculus@2049
  1249
}
slouken@21
  1250
icculus@2049
  1251
SDL_AudioDeviceID
icculus@2049
  1252
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2866
  1253
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1254
                    int allowed_changes)
icculus@2049
  1255
{
slouken@2866
  1256
    return open_audio_device(device, iscapture, desired, obtained,
slouken@2866
  1257
                             allowed_changes, 2);
slouken@1895
  1258
}
slouken@1895
  1259
slouken@3537
  1260
SDL_AudioStatus
icculus@2049
  1261
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
  1262
{
icculus@2049
  1263
    SDL_AudioDevice *device = get_audio_device(devid);
slouken@3537
  1264
    SDL_AudioStatus status = SDL_AUDIO_STOPPED;
icculus@2049
  1265
    if (device && device->enabled) {
icculus@2049
  1266
        if (device->paused) {
slouken@1895
  1267
            status = SDL_AUDIO_PAUSED;
slouken@1895
  1268
        } else {
slouken@1895
  1269
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1270
        }
slouken@1895
  1271
    }
icculus@9382
  1272
    return status;
slouken@0
  1273
}
slouken@0
  1274
icculus@2049
  1275
slouken@3537
  1276
SDL_AudioStatus
icculus@2049
  1277
SDL_GetAudioStatus(void)
icculus@2049
  1278
{
icculus@2049
  1279
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1280
}
icculus@2049
  1281
icculus@2049
  1282
void
icculus@2049
  1283
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1284
{
icculus@2049
  1285
    SDL_AudioDevice *device = get_audio_device(devid);
gabomdq@9148
  1286
    if (device) {
gabomdq@9148
  1287
        current_audio.impl.LockDevice(device);
icculus@2049
  1288
        device->paused = pause_on;
gabomdq@9148
  1289
        current_audio.impl.UnlockDevice(device);
icculus@2049
  1290
    }
icculus@2049
  1291
}
icculus@2049
  1292
slouken@1895
  1293
void
slouken@1895
  1294
SDL_PauseAudio(int pause_on)
slouken@0
  1295
{
gabomdq@9148
  1296
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1297
}
icculus@2049
  1298
slouken@0
  1299
icculus@2049
  1300
void
icculus@2049
  1301
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1302
{
icculus@2049
  1303
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1304
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1305
    if (device) {
icculus@2049
  1306
        current_audio.impl.LockDevice(device);
slouken@1895
  1307
    }
slouken@0
  1308
}
slouken@0
  1309
slouken@1895
  1310
void
slouken@1895
  1311
SDL_LockAudio(void)
slouken@0
  1312
{
icculus@2049
  1313
    SDL_LockAudioDevice(1);
icculus@2049
  1314
}
slouken@0
  1315
icculus@2049
  1316
void
icculus@2049
  1317
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1318
{
slouken@1895
  1319
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1320
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1321
    if (device) {
icculus@2049
  1322
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1323
    }
slouken@0
  1324
}
slouken@0
  1325
slouken@1895
  1326
void
slouken@1895
  1327
SDL_UnlockAudio(void)
slouken@0
  1328
{
icculus@2049
  1329
    SDL_UnlockAudioDevice(1);
icculus@2049
  1330
}
slouken@0
  1331
icculus@2049
  1332
void
icculus@2049
  1333
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1334
{
icculus@2049
  1335
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1336
    if (device) {
icculus@2049
  1337
        close_audio_device(device);
slouken@2060
  1338
        open_devices[devid - 1] = NULL;
slouken@1895
  1339
    }
slouken@0
  1340
}
slouken@0
  1341
slouken@1895
  1342
void
slouken@1895
  1343
SDL_CloseAudio(void)
slouken@0
  1344
{
icculus@2049
  1345
    SDL_CloseAudioDevice(1);
slouken@0
  1346
}
slouken@0
  1347
slouken@1895
  1348
void
slouken@1895
  1349
SDL_AudioQuit(void)
slouken@0
  1350
{
icculus@7348
  1351
    SDL_AudioDeviceID i;
icculus@7348
  1352
icculus@7345
  1353
    if (!current_audio.name) {  /* not initialized?! */
icculus@7345
  1354
        return;
icculus@7345
  1355
    }
icculus@7345
  1356
icculus@2049
  1357
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@7342
  1358
        if (open_devices[i] != NULL) {
icculus@7446
  1359
            SDL_CloseAudioDevice(i+1);
icculus@7342
  1360
        }
icculus@2049
  1361
    }
slouken@0
  1362
icculus@9394
  1363
    free_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9394
  1364
    free_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9394
  1365
icculus@2049
  1366
    /* Free the driver data */
icculus@2049
  1367
    current_audio.impl.Deinitialize();
icculus@9393
  1368
icculus@9394
  1369
    SDL_DestroyMutex(current_audio.detectionLock);
icculus@9393
  1370
icculus@9393
  1371
    SDL_zero(current_audio);
icculus@9393
  1372
    SDL_zero(open_devices);
slouken@0
  1373
}
slouken@0
  1374
icculus@1982
  1375
#define NUM_FORMATS 10
slouken@0
  1376
static int format_idx;
slouken@0
  1377
static int format_idx_sub;
icculus@1982
  1378
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1379
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1380
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1381
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1382
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1383
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1384
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1385
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1386
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1387
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1388
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1389
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1390
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1391
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1392
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1393
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1394
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1395
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1396
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1397
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1398
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1399
};
slouken@0
  1400
icculus@1982
  1401
SDL_AudioFormat
icculus@1982
  1402
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1403
{
slouken@1895
  1404
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1405
        if (format_list[format_idx][0] == format) {
slouken@1895
  1406
            break;
slouken@1895
  1407
        }
slouken@1895
  1408
    }
slouken@1895
  1409
    format_idx_sub = 0;
icculus@9382
  1410
    return SDL_NextAudioFormat();
slouken@0
  1411
}
slouken@0
  1412
icculus@1982
  1413
SDL_AudioFormat
slouken@1895
  1414
SDL_NextAudioFormat(void)
slouken@0
  1415
{
slouken@1895
  1416
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
icculus@9382
  1417
        return 0;
slouken@1895
  1418
    }
icculus@9382
  1419
    return format_list[format_idx][format_idx_sub++];
slouken@0
  1420
}
slouken@0
  1421
slouken@1895
  1422
void
slouken@1895
  1423
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1424
{
slouken@1895
  1425
    switch (spec->format) {
slouken@1895
  1426
    case AUDIO_U8:
slouken@1895
  1427
        spec->silence = 0x80;
slouken@1895
  1428
        break;
slouken@1895
  1429
    default:
slouken@1895
  1430
        spec->silence = 0x00;
slouken@1895
  1431
        break;
slouken@1895
  1432
    }
icculus@2049
  1433
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1434
    spec->size *= spec->channels;
slouken@1895
  1435
    spec->size *= spec->samples;
slouken@0
  1436
}
slouken@1895
  1437
icculus@2049
  1438
icculus@2049
  1439
/*
icculus@2049
  1440
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1441
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1442
 */
icculus@2049
  1443
void
icculus@2049
  1444
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1445
{
icculus@2049
  1446
    /* Mix the user-level audio format */
icculus@2049
  1447
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1448
    if (device != NULL) {
icculus@2049
  1449
        SDL_AudioFormat format;
icculus@2049
  1450
        if (device->convert.needed) {
icculus@2049
  1451
            format = device->convert.src_format;
icculus@2049
  1452
        } else {
icculus@2049
  1453
            format = device->spec.format;
icculus@2049
  1454
        }
icculus@2049
  1455
        SDL_MixAudioFormat(dst, src, format, len, volume);
icculus@2049
  1456
    }
icculus@2049
  1457
}
icculus@2049
  1458
slouken@1895
  1459
/* vi: set ts=4 sw=4 expandtab: */