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