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