src/audio/SDL_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 May 2018 12:00:21 -0400
changeset 11994 8e094f91ddab
parent 11952 d82e6e63c632
child 12078 16bb2e8488b8
permissions -rw-r--r--
thread: fixed compiler warnings on non-Linux systems that use pthread.

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