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