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