src/audio/SDL_audio.c
author Sam Lantinga
Fri, 06 Jan 2017 00:47:42 -0800
changeset 10772 ffb8f194db6e
parent 10770 5a868ac16876
child 10790 92726dd20acd
permissions -rw-r--r--
Fixed confusion between Ryan's new audio stream and the audio buffer we were calling stream in the callback
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);
slouken@10772
   550
    const int data_len = device->callbackspec.size;
slouken@10772
   551
    Uint8 *data;
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)) {
slouken@10772
   568
            SDL_assert(data_len == device->spec.size);
slouken@10772
   569
            data = current_audio.impl.GetDeviceBuf(device);
icculus@9398
   570
        } else {
icculus@9398
   571
            /* if the device isn't enabled, we still write to the
icculus@10765
   572
               work_buffer, so the app's callback will fire with
icculus@9398
   573
               a regular frequency, in case they depend on that
icculus@9398
   574
               for timing or progress. They can use hotplug
icculus@10757
   575
               now to know if the device failed.
icculus@10765
   576
               Streaming playback uses work_buffer, too. */
slouken@10772
   577
            data = NULL;
slouken@2716
   578
        }
slouken@3336
   579
slouken@10772
   580
        if (data == NULL) {
slouken@10772
   581
            data = device->work_buffer;
icculus@9398
   582
        }
slouken@2716
   583
slouken@10466
   584
        if ( SDL_AtomicGet(&device->enabled) ) {
icculus@10757
   585
            /* !!! FIXME: this should be LockDevice. */
slouken@10466
   586
            SDL_LockMutex(device->mixer_lock);
slouken@10466
   587
            if (SDL_AtomicGet(&device->paused)) {
slouken@10772
   588
                SDL_memset(data, silence, data_len);
slouken@10466
   589
            } else {
slouken@10772
   590
                callback(udata, data, data_len);
slouken@10466
   591
            }
slouken@10466
   592
            SDL_UnlockMutex(device->mixer_lock);
icculus@10757
   593
        } else {
slouken@10772
   594
            SDL_memset(data, silence, data_len);
icculus@9398
   595
        }
slouken@2716
   596
icculus@10757
   597
        if (device->stream) {
icculus@10757
   598
            /* Stream available audio to device, converting/resampling. */
icculus@10757
   599
            /* if this fails...oh well. We'll play silence here. */
slouken@10772
   600
            SDL_AudioStreamPut(device->stream, data, data_len);
icculus@10757
   601
icculus@10759
   602
            while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->spec.size)) {
slouken@10772
   603
                data = SDL_AtomicGet(&device->enabled) ? current_audio.impl.GetDeviceBuf(device) : NULL;
slouken@10772
   604
                if (data == NULL) {
icculus@10757
   605
                    SDL_AudioStreamClear(device->stream);
icculus@10757
   606
                    SDL_Delay(delay);
icculus@10757
   607
                    break;
icculus@10757
   608
                } else {
slouken@10772
   609
                    const int got = SDL_AudioStreamGet(device->stream, data, device->spec.size);
icculus@10757
   610
                    SDL_assert((got < 0) || (got == device->spec.size));
icculus@10757
   611
                    if (got != device->spec.size) {
slouken@10772
   612
                        SDL_memset(data, device->spec.silence, device->spec.size);
icculus@10757
   613
                    }
icculus@10757
   614
                    current_audio.impl.PlayDevice(device);
icculus@10757
   615
                    current_audio.impl.WaitDevice(device);
icculus@10757
   616
                }
slouken@1895
   617
            }
slouken@10772
   618
        } else if (data == device->work_buffer) {
icculus@10757
   619
            /* nothing to do; pause like we queued a buffer to play. */
icculus@9398
   620
            SDL_Delay(delay);
icculus@10757
   621
        } else {  /* writing directly to the device. */
icculus@10757
   622
            /* queue this buffer and wait for it to finish playing. */
icculus@9398
   623
            current_audio.impl.PlayDevice(device);
icculus@9398
   624
            current_audio.impl.WaitDevice(device);
slouken@1895
   625
        }
slouken@1895
   626
    }
slouken@1562
   627
icculus@10473
   628
    current_audio.impl.PrepareToClose(device);
icculus@10473
   629
icculus@9398
   630
    /* Wait for the audio to drain. */
icculus@10473
   631
    SDL_Delay(((device->spec.samples * 1000) / device->spec.freq) * 2);
slouken@21
   632
icculus@9382
   633
    return 0;
slouken@0
   634
}
slouken@0
   635
icculus@10239
   636
/* The general capture thread function */
icculus@10239
   637
static int SDLCALL
icculus@10239
   638
SDL_CaptureAudio(void *devicep)
icculus@10239
   639
{
icculus@10239
   640
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@10239
   641
    const int silence = (int) device->spec.silence;
icculus@10239
   642
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
slouken@10772
   643
    const int data_len = device->spec.size;
slouken@10772
   644
    Uint8 *data;
icculus@10239
   645
    void *udata = device->spec.userdata;
icculus@10757
   646
    SDL_AudioCallback callback = device->spec.callback;
icculus@10239
   647
icculus@10239
   648
    SDL_assert(device->iscapture);
icculus@10239
   649
icculus@10239
   650
    /* The audio mixing is always a high priority thread */
icculus@10239
   651
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
icculus@10239
   652
icculus@10239
   653
    /* Perform any thread setup */
icculus@10239
   654
    device->threadid = SDL_ThreadID();
icculus@10239
   655
    current_audio.impl.ThreadInit(device);
icculus@10239
   656
icculus@10239
   657
    /* Loop, filling the audio buffers */
icculus@10239
   658
    while (!SDL_AtomicGet(&device->shutdown)) {
icculus@10239
   659
        int still_need;
icculus@10239
   660
        Uint8 *ptr;
icculus@10239
   661
icculus@10239
   662
        if (!SDL_AtomicGet(&device->enabled) || SDL_AtomicGet(&device->paused)) {
icculus@10239
   663
            SDL_Delay(delay);  /* just so we don't cook the CPU. */
icculus@10757
   664
            if (device->stream) {
icculus@10757
   665
                SDL_AudioStreamClear(device->stream);
icculus@10757
   666
            }
icculus@10239
   667
            current_audio.impl.FlushCapture(device);  /* dump anything pending. */
icculus@10239
   668
            continue;
icculus@10239
   669
        }
icculus@10239
   670
icculus@10239
   671
        /* Fill the current buffer with sound */
slouken@10772
   672
        still_need = data_len;
icculus@10757
   673
icculus@10765
   674
        /* Use the work_buffer to hold data read from the device. */
slouken@10772
   675
        data = device->work_buffer;
slouken@10772
   676
        SDL_assert(data != NULL);
icculus@10757
   677
slouken@10772
   678
        ptr = data;
icculus@10239
   679
icculus@10239
   680
        /* We still read from the device when "paused" to keep the state sane,
icculus@10239
   681
           and block when there isn't data so this thread isn't eating CPU.
icculus@10239
   682
           But we don't process it further or call the app's callback. */
icculus@10239
   683
icculus@10239
   684
        while (still_need > 0) {
icculus@10239
   685
            const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need);
icculus@10239
   686
            SDL_assert(rc <= still_need);  /* device should not overflow buffer. :) */
icculus@10239
   687
            if (rc > 0) {
icculus@10239
   688
                still_need -= rc;
icculus@10239
   689
                ptr += rc;
icculus@10239
   690
            } else {  /* uhoh, device failed for some reason! */
icculus@10239
   691
                SDL_OpenedAudioDeviceDisconnected(device);
icculus@10239
   692
                break;
icculus@10239
   693
            }
icculus@10239
   694
        }
icculus@10239
   695
icculus@10239
   696
        if (still_need > 0) {
icculus@10239
   697
            /* Keep any data we already read, silence the rest. */
icculus@10239
   698
            SDL_memset(ptr, silence, still_need);
icculus@10239
   699
        }
icculus@10239
   700
icculus@10757
   701
        if (device->stream) {
icculus@10757
   702
            /* if this fails...oh well. */
slouken@10772
   703
            SDL_AudioStreamPut(device->stream, data, data_len);
icculus@10757
   704
icculus@10759
   705
            while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->callbackspec.size)) {
icculus@10765
   706
                const int got = SDL_AudioStreamGet(device->stream, device->work_buffer, device->callbackspec.size);
icculus@10757
   707
                SDL_assert((got < 0) || (got == device->callbackspec.size));
icculus@10757
   708
                if (got != device->callbackspec.size) {
icculus@10765
   709
                    SDL_memset(device->work_buffer, device->spec.silence, device->callbackspec.size);
icculus@10757
   710
                }
icculus@10239
   711
icculus@10757
   712
                /* !!! FIXME: this should be LockDevice. */
icculus@10757
   713
                SDL_LockMutex(device->mixer_lock);
icculus@10757
   714
                if (!SDL_AtomicGet(&device->paused)) {
icculus@10765
   715
                    callback(udata, device->work_buffer, device->callbackspec.size);
icculus@10757
   716
                }
icculus@10757
   717
                SDL_UnlockMutex(device->mixer_lock);
icculus@10757
   718
            }
icculus@10757
   719
        } else {  /* feeding user callback directly without streaming. */
icculus@10757
   720
            /* !!! FIXME: this should be LockDevice. */
icculus@10757
   721
            SDL_LockMutex(device->mixer_lock);
icculus@10757
   722
            if (!SDL_AtomicGet(&device->paused)) {
slouken@10772
   723
                callback(udata, data, device->callbackspec.size);
icculus@10757
   724
            }
icculus@10757
   725
            SDL_UnlockMutex(device->mixer_lock);
icculus@10239
   726
        }
icculus@10239
   727
    }
icculus@10239
   728
icculus@10239
   729
    current_audio.impl.FlushCapture(device);
icculus@10239
   730
icculus@10239
   731
    return 0;
icculus@10239
   732
}
icculus@10239
   733
slouken@322
   734
icculus@1982
   735
static SDL_AudioFormat
slouken@1895
   736
SDL_ParseAudioFormat(const char *string)
slouken@1794
   737
{
icculus@2076
   738
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   739
    CHECK_FMT_STRING(U8);
icculus@2049
   740
    CHECK_FMT_STRING(S8);
icculus@2049
   741
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   742
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   743
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   744
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   745
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   746
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   747
    CHECK_FMT_STRING(U16);
icculus@2049
   748
    CHECK_FMT_STRING(S16);
icculus@2049
   749
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   750
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   751
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   752
    CHECK_FMT_STRING(S32);
icculus@2049
   753
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   754
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   755
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   756
    CHECK_FMT_STRING(F32);
slouken@2060
   757
#undef CHECK_FMT_STRING
icculus@2049
   758
    return 0;
slouken@1895
   759
}
slouken@1895
   760
slouken@1895
   761
int
slouken@1895
   762
SDL_GetNumAudioDrivers(void)
slouken@1895
   763
{
icculus@9382
   764
    return SDL_arraysize(bootstrap) - 1;
slouken@1895
   765
}
slouken@1895
   766
slouken@1895
   767
const char *
slouken@1895
   768
SDL_GetAudioDriver(int index)
slouken@1895
   769
{
slouken@1895
   770
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
icculus@9382
   771
        return bootstrap[index]->name;
slouken@1895
   772
    }
icculus@9382
   773
    return NULL;
slouken@1794
   774
}
slouken@1794
   775
slouken@1895
   776
int
slouken@1895
   777
SDL_AudioInit(const char *driver_name)
slouken@0
   778
{
icculus@2049
   779
    int i = 0;
icculus@2049
   780
    int initialized = 0;
icculus@2049
   781
    int tried_to_init = 0;
slouken@0
   782
icculus@2049
   783
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   784
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   785
    }
slouken@0
   786
icculus@9392
   787
    SDL_zero(current_audio);
icculus@9392
   788
    SDL_zero(open_devices);
icculus@2049
   789
slouken@1895
   790
    /* Select the proper audio driver */
slouken@1909
   791
    if (driver_name == NULL) {
slouken@1909
   792
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   793
    }
icculus@2049
   794
icculus@2049
   795
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   796
        /* make sure we should even try this driver before doing so... */
icculus@2049
   797
        const AudioBootStrap *backend = bootstrap[i];
slouken@6900
   798
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
slouken@6900
   799
            (!driver_name && backend->demand_only)) {
icculus@2049
   800
            continue;
icculus@2049
   801
        }
slouken@0
   802
icculus@2049
   803
        tried_to_init = 1;
icculus@9392
   804
        SDL_zero(current_audio);
icculus@2049
   805
        current_audio.name = backend->name;
icculus@2049
   806
        current_audio.desc = backend->desc;
icculus@3699
   807
        initialized = backend->init(&current_audio.impl);
slouken@1895
   808
    }
icculus@2049
   809
icculus@2049
   810
    if (!initialized) {
icculus@2049
   811
        /* specific drivers will set the error message if they fail... */
icculus@2049
   812
        if (!tried_to_init) {
slouken@1895
   813
            if (driver_name) {
icculus@3699
   814
                SDL_SetError("Audio target '%s' not available", driver_name);
slouken@1895
   815
            } else {
slouken@1895
   816
                SDL_SetError("No available audio device");
slouken@1895
   817
            }
slouken@1895
   818
        }
icculus@2049
   819
icculus@9392
   820
        SDL_zero(current_audio);
icculus@9382
   821
        return -1;            /* No driver was available, so fail. */
slouken@1895
   822
    }
icculus@2049
   823
icculus@9394
   824
    current_audio.detectionLock = SDL_CreateMutex();
icculus@9393
   825
icculus@10471
   826
    finish_audio_entry_points_init();
icculus@2049
   827
icculus@9393
   828
    /* Make sure we have a list of devices available at startup. */
icculus@9394
   829
    current_audio.impl.DetectDevices();
icculus@9393
   830
icculus@9382
   831
    return 0;
slouken@0
   832
}
slouken@0
   833
slouken@1895
   834
/*
slouken@1895
   835
 * Get the current audio driver name
slouken@1895
   836
 */
slouken@1895
   837
const char *
slouken@1895
   838
SDL_GetCurrentAudioDriver()
slouken@0
   839
{
icculus@2049
   840
    return current_audio.name;
slouken@0
   841
}
slouken@0
   842
icculus@9394
   843
/* Clean out devices that we've removed but had to keep around for stability. */
icculus@9394
   844
static void
icculus@9394
   845
clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
icculus@9394
   846
{
icculus@9394
   847
    SDL_AudioDeviceItem *item = *devices;
icculus@9394
   848
    SDL_AudioDeviceItem *prev = NULL;
icculus@9394
   849
    int total = 0;
icculus@9394
   850
icculus@9394
   851
    while (item) {
icculus@9394
   852
        SDL_AudioDeviceItem *next = item->next;
icculus@9394
   853
        if (item->handle != NULL) {
icculus@9394
   854
            total++;
icculus@9394
   855
            prev = item;
icculus@9394
   856
        } else {
icculus@9394
   857
            if (prev) {
icculus@9394
   858
                prev->next = next;
icculus@9394
   859
            } else {
icculus@9394
   860
                *devices = next;
icculus@9394
   861
            }
icculus@9394
   862
            SDL_free(item);
icculus@9394
   863
        }
icculus@9394
   864
        item = next;
icculus@9394
   865
    }
icculus@9394
   866
icculus@9394
   867
    *devCount = total;
icculus@9394
   868
    *removedFlag = SDL_FALSE;
icculus@9394
   869
}
icculus@9394
   870
icculus@9394
   871
slouken@1895
   872
int
icculus@2049
   873
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   874
{
icculus@5593
   875
    int retval = 0;
icculus@5593
   876
icculus@2049
   877
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   878
        return -1;
icculus@2049
   879
    }
icculus@5593
   880
icculus@9394
   881
    SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   882
    if (iscapture && current_audio.captureDevicesRemoved) {
icculus@9394
   883
        clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
icculus@2049
   884
    }
icculus@2049
   885
icculus@9394
   886
    if (!iscapture && current_audio.outputDevicesRemoved) {
icculus@9394
   887
        clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
icculus@9394
   888
        current_audio.outputDevicesRemoved = SDL_FALSE;
icculus@5593
   889
    }
icculus@5593
   890
icculus@9393
   891
    retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   892
    SDL_UnlockMutex(current_audio.detectionLock);
icculus@9393
   893
icculus@5593
   894
    return retval;
icculus@2049
   895
}
icculus@2049
   896
slouken@0
   897
icculus@2049
   898
const char *
icculus@2049
   899
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   900
{
icculus@9393
   901
    const char *retval = NULL;
icculus@9393
   902
icculus@2049
   903
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   904
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   905
        return NULL;
icculus@2049
   906
    }
icculus@2049
   907
icculus@2049
   908
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   909
        SDL_SetError("No capture support");
icculus@2049
   910
        return NULL;
slouken@1895
   911
    }
icculus@2049
   912
icculus@9394
   913
    if (index >= 0) {
icculus@9394
   914
        SDL_AudioDeviceItem *item;
icculus@9394
   915
        int i;
slouken@0
   916
icculus@9394
   917
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
   918
        item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
icculus@9394
   919
        i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
icculus@9394
   920
        if (index < i) {
icculus@9394
   921
            for (i--; i > index; i--, item = item->next) {
icculus@9394
   922
                SDL_assert(item != NULL);
icculus@9394
   923
            }
icculus@9394
   924
            SDL_assert(item != NULL);
icculus@9394
   925
            retval = item->name;
slouken@7904
   926
        }
icculus@9394
   927
        SDL_UnlockMutex(current_audio.detectionLock);
slouken@1895
   928
    }
slouken@262
   929
icculus@9394
   930
    if (retval == NULL) {
icculus@9394
   931
        SDL_SetError("No such device");
icculus@5593
   932
    }
icculus@5593
   933
icculus@9394
   934
    return retval;
icculus@2049
   935
}
icculus@2049
   936
icculus@2049
   937
icculus@2049
   938
static void
slouken@2060
   939
close_audio_device(SDL_AudioDevice * device)
icculus@2049
   940
{
icculus@10471
   941
    if (!device) {
icculus@10471
   942
        return;
icculus@2049
   943
    }
icculus@10471
   944
icculus@10471
   945
    if (device->id > 0) {
icculus@10471
   946
        SDL_AudioDevice *opendev = open_devices[device->id - 1];
icculus@10471
   947
        SDL_assert((opendev == device) || (opendev == NULL));
icculus@10471
   948
        if (opendev == device) {
icculus@10471
   949
            open_devices[device->id - 1] = NULL;
icculus@10471
   950
        }
slouken@10468
   951
    }
icculus@9012
   952
icculus@10480
   953
    SDL_AtomicSet(&device->shutdown, 1);
icculus@10480
   954
    SDL_AtomicSet(&device->enabled, 0);
icculus@10480
   955
    if (device->thread != NULL) {
icculus@10480
   956
        SDL_WaitThread(device->thread, NULL);
icculus@10480
   957
    }
icculus@10480
   958
    if (device->mixer_lock != NULL) {
icculus@10480
   959
        SDL_DestroyMutex(device->mixer_lock);
icculus@10480
   960
    }
icculus@10757
   961
icculus@10765
   962
    SDL_free(device->work_buffer);
icculus@10757
   963
    SDL_FreeAudioStream(device->stream);
icculus@10757
   964
icculus@10480
   965
    if (device->hidden != NULL) {
icculus@10480
   966
        current_audio.impl.CloseDevice(device);
icculus@10480
   967
    }
icculus@9012
   968
icculus@10681
   969
    SDL_FreeDataQueue(device->buffer_queue);
icculus@10757
   970
icculus@10480
   971
    SDL_free(device);
icculus@2049
   972
}
icculus@2049
   973
icculus@2049
   974
icculus@2049
   975
/*
icculus@2049
   976
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   977
 *  Fills in a sanitized copy in (prepared).
icculus@2049
   978
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
   979
 */
icculus@2049
   980
static int
slouken@2060
   981
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
   982
{
slouken@2060
   983
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
   984
icculus@2049
   985
    if (orig->freq == 0) {
icculus@2049
   986
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
   987
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
   988
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
   989
        }
slouken@1895
   990
    }
icculus@2049
   991
icculus@2049
   992
    if (orig->format == 0) {
icculus@2049
   993
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
   994
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
   995
            prepared->format = AUDIO_S16;       /* a reasonable default */
slouken@1895
   996
        }
slouken@1895
   997
    }
icculus@2049
   998
icculus@2049
   999
    switch (orig->channels) {
slouken@2060
  1000
    case 0:{
slouken@2060
  1001
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
  1002
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
  1003
                prepared->channels = 2; /* a reasonable default */
slouken@2060
  1004
            }
slouken@2060
  1005
            break;
icculus@2049
  1006
        }
slouken@1895
  1007
    case 1:                    /* Mono */
slouken@1895
  1008
    case 2:                    /* Stereo */
slouken@1895
  1009
    case 4:                    /* surround */
slouken@1895
  1010
    case 6:                    /* surround with center and lfe */
slouken@1895
  1011
        break;
slouken@1895
  1012
    default:
icculus@2049
  1013
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
  1014
        return 0;
slouken@1895
  1015
    }
icculus@2049
  1016
icculus@2049
  1017
    if (orig->samples == 0) {
icculus@2049
  1018
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
  1019
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
  1020
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
  1021
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
  1022
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
  1023
            int power2 = 1;
icculus@2049
  1024
            while (power2 < samples) {
icculus@2049
  1025
                power2 *= 2;
icculus@2049
  1026
            }
icculus@2049
  1027
            prepared->samples = power2;
slouken@1895
  1028
        }
slouken@1895
  1029
    }
slouken@0
  1030
slouken@1895
  1031
    /* Calculate the silence and size of the audio specification */
icculus@2049
  1032
    SDL_CalculateAudioSpec(prepared);
slouken@21
  1033
icculus@2049
  1034
    return 1;
icculus@2049
  1035
}
slouken@1408
  1036
icculus@2049
  1037
static SDL_AudioDeviceID
icculus@2049
  1038
open_audio_device(const char *devname, int iscapture,
slouken@2866
  1039
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1040
                  int allowed_changes, int min_id)
icculus@2049
  1041
{
icculus@10270
  1042
    const SDL_bool is_internal_thread = (desired->callback != NULL);
icculus@2049
  1043
    SDL_AudioDeviceID id = 0;
slouken@2866
  1044
    SDL_AudioSpec _obtained;
icculus@2049
  1045
    SDL_AudioDevice *device;
icculus@10757
  1046
    SDL_bool build_stream;
icculus@9394
  1047
    void *handle = NULL;
icculus@2049
  1048
    int i = 0;
slouken@21
  1049
icculus@2049
  1050
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1051
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
  1052
        return 0;
icculus@2049
  1053
    }
icculus@2049
  1054
icculus@2049
  1055
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
  1056
        SDL_SetError("No capture support");
icculus@2049
  1057
        return 0;
icculus@2049
  1058
    }
icculus@2049
  1059
icculus@10471
  1060
    /* !!! FIXME: there is a race condition here if two devices open from two threads at once. */
icculus@9393
  1061
    /* Find an available device ID... */
icculus@9393
  1062
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@9393
  1063
        if (open_devices[id] == NULL) {
icculus@9393
  1064
            break;
icculus@9393
  1065
        }
icculus@9393
  1066
    }
icculus@9393
  1067
icculus@9393
  1068
    if (id == SDL_arraysize(open_devices)) {
icculus@9393
  1069
        SDL_SetError("Too many open audio devices");
icculus@9393
  1070
        return 0;
icculus@9393
  1071
    }
icculus@9393
  1072
slouken@2866
  1073
    if (!obtained) {
slouken@2866
  1074
        obtained = &_obtained;
slouken@2866
  1075
    }
slouken@2866
  1076
    if (!prepare_audiospec(desired, obtained)) {
icculus@2049
  1077
        return 0;
icculus@2049
  1078
    }
icculus@2049
  1079
icculus@2049
  1080
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
  1081
    if (devname == NULL) {
icculus@2049
  1082
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
  1083
    }
slouken@21
  1084
icculus@2049
  1085
    /*
icculus@2049
  1086
     * Catch device names at the high level for the simple case...
icculus@2049
  1087
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
  1088
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
  1089
     *  always NULL when it hits the low level.
icculus@2049
  1090
     *
icculus@2049
  1091
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
  1092
     *  opens of the default system device.
icculus@2049
  1093
     */
icculus@2049
  1094
icculus@10258
  1095
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultCaptureDevice)) {
icculus@2049
  1096
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
  1097
            SDL_SetError("No such device");
icculus@2049
  1098
            return 0;
icculus@2049
  1099
        }
icculus@2049
  1100
        devname = NULL;
icculus@2049
  1101
icculus@2049
  1102
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1103
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
  1104
                SDL_SetError("Audio device already open");
icculus@2049
  1105
                return 0;
icculus@2049
  1106
            }
icculus@2049
  1107
        }
icculus@9394
  1108
    } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
  1109
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
  1110
            SDL_SetError("No such device");
icculus@2049
  1111
            return 0;
icculus@2049
  1112
        }
icculus@2049
  1113
        devname = NULL;
icculus@2049
  1114
icculus@2049
  1115
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1116
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
  1117
                SDL_SetError("Audio device already open");
icculus@2049
  1118
                return 0;
icculus@2049
  1119
            }
icculus@2049
  1120
        }
icculus@9394
  1121
    } else if (devname != NULL) {
icculus@9394
  1122
        /* if the app specifies an exact string, we can pass the backend
icculus@9394
  1123
           an actual device handle thingey, which saves them the effort of
icculus@9394
  1124
           figuring out what device this was (such as, reenumerating
icculus@9394
  1125
           everything again to find the matching human-readable name).
icculus@9394
  1126
           It might still need to open a device based on the string for,
icculus@9394
  1127
           say, a network audio server, but this optimizes some cases. */
icculus@9394
  1128
        SDL_AudioDeviceItem *item;
icculus@9394
  1129
        SDL_LockMutex(current_audio.detectionLock);
icculus@9394
  1130
        for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) {
icculus@9394
  1131
            if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) {
icculus@9394
  1132
                handle = item->handle;
icculus@9394
  1133
                break;
icculus@9394
  1134
            }
icculus@9394
  1135
        }
icculus@9394
  1136
        SDL_UnlockMutex(current_audio.detectionLock);
icculus@9394
  1137
    }
icculus@9394
  1138
icculus@9394
  1139
    if (!current_audio.impl.AllowsArbitraryDeviceNames) {
icculus@9394
  1140
        /* has to be in our device list, or the default device. */
icculus@9394
  1141
        if ((handle == NULL) && (devname != NULL)) {
icculus@9394
  1142
            SDL_SetError("No such device.");
icculus@9394
  1143
            return 0;
icculus@9394
  1144
        }
icculus@2049
  1145
    }
icculus@2049
  1146
icculus@10256
  1147
    device = (SDL_AudioDevice *) SDL_calloc(1, sizeof (SDL_AudioDevice));
icculus@2049
  1148
    if (device == NULL) {
icculus@2049
  1149
        SDL_OutOfMemory();
icculus@2049
  1150
        return 0;
icculus@2049
  1151
    }
icculus@9393
  1152
    device->id = id + 1;
slouken@2866
  1153
    device->spec = *obtained;
icculus@10235
  1154
    device->iscapture = iscapture ? SDL_TRUE : SDL_FALSE;
slouken@10467
  1155
    device->handle = handle;
icculus@2049
  1156
icculus@10238
  1157
    SDL_AtomicSet(&device->shutdown, 0);  /* just in case. */
icculus@10238
  1158
    SDL_AtomicSet(&device->paused, 1);
icculus@10238
  1159
    SDL_AtomicSet(&device->enabled, 1);
icculus@10238
  1160
icculus@9391
  1161
    /* Create a mutex for locking the sound buffers */
icculus@2049
  1162
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
  1163
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
  1164
        if (device->mixer_lock == NULL) {
icculus@2049
  1165
            close_audio_device(device);
icculus@2049
  1166
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
  1167
            return 0;
icculus@2049
  1168
        }
icculus@2049
  1169
    }
icculus@2049
  1170
icculus@9394
  1171
    if (current_audio.impl.OpenDevice(device, handle, devname, iscapture) < 0) {
icculus@2049
  1172
        close_audio_device(device);
icculus@2049
  1173
        return 0;
icculus@2049
  1174
    }
icculus@10255
  1175
icculus@10255
  1176
    /* if your target really doesn't need it, set it to 0x1 or something. */
icculus@10255
  1177
    /* otherwise, close_audio_device() won't call impl.CloseDevice(). */
icculus@10255
  1178
    SDL_assert(device->hidden != NULL);
slouken@0
  1179
slouken@1895
  1180
    /* See if we need to do any conversion */
icculus@10757
  1181
    build_stream = SDL_FALSE;
slouken@2866
  1182
    if (obtained->freq != device->spec.freq) {
slouken@2866
  1183
        if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
slouken@2866
  1184
            obtained->freq = device->spec.freq;
slouken@2866
  1185
        } else {
icculus@10757
  1186
            build_stream = SDL_TRUE;
slouken@2866
  1187
        }
slouken@2866
  1188
    }
slouken@2866
  1189
    if (obtained->format != device->spec.format) {
slouken@2866
  1190
        if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
slouken@2866
  1191
            obtained->format = device->spec.format;
slouken@2866
  1192
        } else {
icculus@10757
  1193
            build_stream = SDL_TRUE;
slouken@2866
  1194
        }
slouken@2866
  1195
    }
slouken@2866
  1196
    if (obtained->channels != device->spec.channels) {
slouken@2866
  1197
        if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
slouken@2866
  1198
            obtained->channels = device->spec.channels;
slouken@2866
  1199
        } else {
icculus@10757
  1200
            build_stream = SDL_TRUE;
slouken@2866
  1201
        }
slouken@2866
  1202
    }
slouken@7518
  1203
icculus@10757
  1204
    /* !!! FIXME in 2.1: add SDL_AUDIO_ALLOW_SAMPLES_CHANGE flag?
icculus@10757
  1205
       As of 2.0.6, we will build a stream to buffer the difference between
icculus@10757
  1206
       what the app wants to feed and the device wants to eat, so everyone
icculus@10757
  1207
       gets their way. In prior releases, SDL would force the callback to
icculus@10757
  1208
       feed at the rate the device requested, adjusted for resampling.
slouken@7518
  1209
     */
slouken@7518
  1210
    if (device->spec.samples != obtained->samples) {
icculus@10757
  1211
        build_stream = SDL_TRUE;
slouken@7518
  1212
    }
slouken@7518
  1213
icculus@10757
  1214
    SDL_CalculateAudioSpec(obtained);  /* recalc after possible changes. */
icculus@10757
  1215
icculus@10757
  1216
    device->callbackspec = *obtained;
icculus@10757
  1217
icculus@10757
  1218
    if (build_stream) {
icculus@10757
  1219
        if (iscapture) {
icculus@10757
  1220
            device->stream = SDL_NewAudioStream(device->spec.format,
icculus@10757
  1221
                                  device->spec.channels, device->spec.freq,
icculus@10757
  1222
                                  obtained->format, obtained->channels, obtained->freq);
icculus@10757
  1223
        } else {
icculus@10757
  1224
            device->stream = SDL_NewAudioStream(obtained->format, obtained->channels,
icculus@10757
  1225
                                  obtained->freq, device->spec.format,
icculus@10757
  1226
                                  device->spec.channels, device->spec.freq);
icculus@10757
  1227
        }
icculus@10757
  1228
icculus@10757
  1229
        if (!device->stream) {
icculus@2049
  1230
            close_audio_device(device);
icculus@2049
  1231
            return 0;
slouken@1895
  1232
        }
slouken@1895
  1233
    }
icculus@2049
  1234
icculus@9012
  1235
    if (device->spec.callback == NULL) {  /* use buffer queueing? */
icculus@9012
  1236
        /* pool a few packets to start. Enough for two callbacks. */
icculus@10757
  1237
        device->buffer_queue = SDL_NewDataQueue(SDL_AUDIOBUFFERQUEUE_PACKETLEN, obtained->size * 2);
icculus@10681
  1238
        if (!device->buffer_queue) {
icculus@10681
  1239
            close_audio_device(device);
icculus@10681
  1240
            SDL_SetError("Couldn't create audio buffer queue");
icculus@10681
  1241
            return 0;
icculus@9012
  1242
        }
icculus@10262
  1243
        device->spec.callback = iscapture ? SDL_BufferQueueFillCallback : SDL_BufferQueueDrainCallback;
icculus@9012
  1244
        device->spec.userdata = device;
icculus@9012
  1245
    }
icculus@9012
  1246
icculus@10760
  1247
    /* Allocate a scratch audio buffer */
icculus@10765
  1248
    device->work_buffer_len = build_stream ? device->callbackspec.size : 0;
icculus@10765
  1249
    if (device->spec.size > device->work_buffer_len) {
icculus@10765
  1250
        device->work_buffer_len = device->spec.size;
icculus@10760
  1251
    }
icculus@10765
  1252
    SDL_assert(device->work_buffer_len > 0);
icculus@10760
  1253
icculus@10765
  1254
    device->work_buffer = (Uint8 *) SDL_malloc(device->work_buffer_len);
icculus@10765
  1255
    if (device->work_buffer == NULL) {
icculus@10760
  1256
        close_audio_device(device);
icculus@10760
  1257
        SDL_OutOfMemory();
icculus@10760
  1258
        return 0;
icculus@10760
  1259
    }
icculus@10760
  1260
icculus@10757
  1261
    open_devices[id] = device;  /* add it to our list of open devices. */
icculus@2049
  1262
slouken@1895
  1263
    /* Start the audio thread if necessary */
icculus@2049
  1264
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
  1265
        /* Start the audio thread */
icculus@10277
  1266
        /* !!! FIXME: we don't force the audio thread stack size here if it calls into user code, but maybe we should? */
icculus@10277
  1267
        /* buffer queueing callback only needs a few bytes, so make the stack tiny. */
icculus@10277
  1268
        const size_t stacksize = is_internal_thread ? 64 * 1024 : 0;
icculus@10277
  1269
        char threadname[64];
icculus@10146
  1270
icculus@10277
  1271
        SDL_snprintf(threadname, sizeof (threadname), "SDLAudioDev%d", (int) device->id);
icculus@10277
  1272
        device->thread = SDL_CreateThreadInternal(iscapture ? SDL_CaptureAudio : SDL_RunAudio, threadname, stacksize, device);
icculus@10146
  1273
icculus@2049
  1274
        if (device->thread == NULL) {
icculus@10471
  1275
            close_audio_device(device);
slouken@1895
  1276
            SDL_SetError("Couldn't create audio thread");
icculus@2049
  1277
            return 0;
icculus@2049
  1278
        }
icculus@2049
  1279
    }
icculus@2049
  1280
icculus@9393
  1281
    return device->id;
icculus@2049
  1282
}
icculus@2049
  1283
icculus@2049
  1284
icculus@2049
  1285
int
slouken@2866
  1286
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
  1287
{
icculus@2049
  1288
    SDL_AudioDeviceID id = 0;
icculus@2049
  1289
icculus@2049
  1290
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
  1291
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1292
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
icculus@9382
  1293
            return -1;
slouken@1895
  1294
        }
icculus@2049
  1295
    }
slouken@0
  1296
icculus@2049
  1297
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
  1298
    if (open_devices[0] != NULL) {
icculus@2049
  1299
        SDL_SetError("Audio device is already opened");
icculus@9382
  1300
        return -1;
slouken@1895
  1301
    }
icculus@2049
  1302
slouken@2866
  1303
    if (obtained) {
slouken@2866
  1304
        id = open_audio_device(NULL, 0, desired, obtained,
slouken@2866
  1305
                               SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
slouken@2866
  1306
    } else {
slouken@8919
  1307
        id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
slouken@2866
  1308
    }
slouken@21
  1309
icculus@5964
  1310
    SDL_assert((id == 0) || (id == 1));
icculus@9382
  1311
    return (id == 0) ? -1 : 0;
icculus@2049
  1312
}
slouken@21
  1313
icculus@2049
  1314
SDL_AudioDeviceID
icculus@2049
  1315
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2866
  1316
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1317
                    int allowed_changes)
icculus@2049
  1318
{
slouken@2866
  1319
    return open_audio_device(device, iscapture, desired, obtained,
slouken@2866
  1320
                             allowed_changes, 2);
slouken@1895
  1321
}
slouken@1895
  1322
slouken@3537
  1323
SDL_AudioStatus
icculus@2049
  1324
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
  1325
{
icculus@2049
  1326
    SDL_AudioDevice *device = get_audio_device(devid);
slouken@3537
  1327
    SDL_AudioStatus status = SDL_AUDIO_STOPPED;
icculus@10238
  1328
    if (device && SDL_AtomicGet(&device->enabled)) {
icculus@10238
  1329
        if (SDL_AtomicGet(&device->paused)) {
slouken@1895
  1330
            status = SDL_AUDIO_PAUSED;
slouken@1895
  1331
        } else {
slouken@1895
  1332
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1333
        }
slouken@1895
  1334
    }
icculus@9382
  1335
    return status;
slouken@0
  1336
}
slouken@0
  1337
icculus@2049
  1338
slouken@3537
  1339
SDL_AudioStatus
icculus@2049
  1340
SDL_GetAudioStatus(void)
icculus@2049
  1341
{
icculus@2049
  1342
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1343
}
icculus@2049
  1344
icculus@2049
  1345
void
icculus@2049
  1346
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1347
{
icculus@2049
  1348
    SDL_AudioDevice *device = get_audio_device(devid);
gabomdq@9148
  1349
    if (device) {
gabomdq@9148
  1350
        current_audio.impl.LockDevice(device);
icculus@10238
  1351
        SDL_AtomicSet(&device->paused, pause_on ? 1 : 0);
gabomdq@9148
  1352
        current_audio.impl.UnlockDevice(device);
icculus@2049
  1353
    }
icculus@2049
  1354
}
icculus@2049
  1355
slouken@1895
  1356
void
slouken@1895
  1357
SDL_PauseAudio(int pause_on)
slouken@0
  1358
{
gabomdq@9148
  1359
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1360
}
icculus@2049
  1361
slouken@0
  1362
icculus@2049
  1363
void
icculus@2049
  1364
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1365
{
icculus@2049
  1366
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1367
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1368
    if (device) {
icculus@2049
  1369
        current_audio.impl.LockDevice(device);
slouken@1895
  1370
    }
slouken@0
  1371
}
slouken@0
  1372
slouken@1895
  1373
void
slouken@1895
  1374
SDL_LockAudio(void)
slouken@0
  1375
{
icculus@2049
  1376
    SDL_LockAudioDevice(1);
icculus@2049
  1377
}
slouken@0
  1378
icculus@2049
  1379
void
icculus@2049
  1380
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1381
{
slouken@1895
  1382
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1383
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1384
    if (device) {
icculus@2049
  1385
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1386
    }
slouken@0
  1387
}
slouken@0
  1388
slouken@1895
  1389
void
slouken@1895
  1390
SDL_UnlockAudio(void)
slouken@0
  1391
{
icculus@2049
  1392
    SDL_UnlockAudioDevice(1);
icculus@2049
  1393
}
slouken@0
  1394
icculus@2049
  1395
void
icculus@2049
  1396
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1397
{
icculus@10471
  1398
    close_audio_device(get_audio_device(devid));
slouken@0
  1399
}
slouken@0
  1400
slouken@1895
  1401
void
slouken@1895
  1402
SDL_CloseAudio(void)
slouken@0
  1403
{
icculus@2049
  1404
    SDL_CloseAudioDevice(1);
slouken@0
  1405
}
slouken@0
  1406
slouken@1895
  1407
void
slouken@1895
  1408
SDL_AudioQuit(void)
slouken@0
  1409
{
icculus@7348
  1410
    SDL_AudioDeviceID i;
icculus@7348
  1411
icculus@7345
  1412
    if (!current_audio.name) {  /* not initialized?! */
icculus@7345
  1413
        return;
icculus@7345
  1414
    }
icculus@7345
  1415
icculus@2049
  1416
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@10471
  1417
        close_audio_device(open_devices[i]);
icculus@2049
  1418
    }
slouken@0
  1419
icculus@9394
  1420
    free_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount);
icculus@9394
  1421
    free_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount);
icculus@9394
  1422
icculus@2049
  1423
    /* Free the driver data */
icculus@2049
  1424
    current_audio.impl.Deinitialize();
icculus@9393
  1425
icculus@9394
  1426
    SDL_DestroyMutex(current_audio.detectionLock);
icculus@9393
  1427
icculus@9393
  1428
    SDL_zero(current_audio);
icculus@9393
  1429
    SDL_zero(open_devices);
slouken@0
  1430
}
slouken@0
  1431
icculus@1982
  1432
#define NUM_FORMATS 10
slouken@0
  1433
static int format_idx;
slouken@0
  1434
static int format_idx_sub;
icculus@1982
  1435
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1436
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1437
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1438
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1439
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1440
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1441
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1442
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1443
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1444
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1445
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1446
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1447
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1448
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1449
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1450
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1451
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1452
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1453
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1454
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1455
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1456
};
slouken@0
  1457
icculus@1982
  1458
SDL_AudioFormat
icculus@1982
  1459
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1460
{
slouken@1895
  1461
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1462
        if (format_list[format_idx][0] == format) {
slouken@1895
  1463
            break;
slouken@1895
  1464
        }
slouken@1895
  1465
    }
slouken@1895
  1466
    format_idx_sub = 0;
icculus@9382
  1467
    return SDL_NextAudioFormat();
slouken@0
  1468
}
slouken@0
  1469
icculus@1982
  1470
SDL_AudioFormat
slouken@1895
  1471
SDL_NextAudioFormat(void)
slouken@0
  1472
{
slouken@1895
  1473
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
icculus@9382
  1474
        return 0;
slouken@1895
  1475
    }
icculus@9382
  1476
    return format_list[format_idx][format_idx_sub++];
slouken@0
  1477
}
slouken@0
  1478
slouken@1895
  1479
void
slouken@1895
  1480
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1481
{
slouken@1895
  1482
    switch (spec->format) {
slouken@1895
  1483
    case AUDIO_U8:
slouken@1895
  1484
        spec->silence = 0x80;
slouken@1895
  1485
        break;
slouken@1895
  1486
    default:
slouken@1895
  1487
        spec->silence = 0x00;
slouken@1895
  1488
        break;
slouken@1895
  1489
    }
icculus@2049
  1490
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1491
    spec->size *= spec->channels;
slouken@1895
  1492
    spec->size *= spec->samples;
slouken@0
  1493
}
slouken@1895
  1494
icculus@2049
  1495
icculus@2049
  1496
/*
icculus@2049
  1497
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1498
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1499
 */
icculus@2049
  1500
void
icculus@2049
  1501
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1502
{
icculus@2049
  1503
    /* Mix the user-level audio format */
icculus@2049
  1504
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1505
    if (device != NULL) {
icculus@10757
  1506
        SDL_MixAudioFormat(dst, src, device->callbackspec.format, len, volume);
icculus@2049
  1507
    }
icculus@2049
  1508
}
icculus@2049
  1509
slouken@1895
  1510
/* vi: set ts=4 sw=4 expandtab: */