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